Skip to content

Commit

Permalink
fix: hashed bundle file name rewrite should touch all instances
Browse files Browse the repository at this point in the history
closes #456
  • Loading branch information
3cp committed Jun 1, 2022
1 parent fa83749 commit 47d0b0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
6 changes: 2 additions & 4 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,11 @@ exports.Bundle = class {
}

const indexFile = fs.readFileSync(platform.index);
const outputDir = platform.baseUrl || platform.output;
const configMatcher = Utils.createSrcFileRegex(outputDir, this.moduleId);
const configMatcher = Utils.createBundleFileRegex(this.moduleId);
const bundleFileName = this.hash ? Utils.generateHashedPath(this.config.name, this.hash) : this.config.name;
const bundleLocation = path.posix.join(outputDir, bundleFileName);
// Replace file name with hashed file name
if (configMatcher.test(indexFile)) {
const newIndexFile = indexFile.replace(configMatcher, 'src="$2' + bundleLocation + '"');
const newIndexFile = indexFile.replace(configMatcher, bundleFileName);
if (newIndexFile !== indexFile) {
await fs.writeFile(platform.index, newIndexFile);
logger.info(`Updated file name for ${bundleFileName} in ${platform.index}`);
Expand Down
10 changes: 2 additions & 8 deletions lib/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,8 @@ exports.escapeForRegex = function(str) {
return str.replace(matchers, '\\$&');
};

exports.createSrcFileRegex = function() {
let parts = Array.prototype.slice.call(arguments);
let regexString = "\\b(?:src=(\"|')?(.*))(";
for (let i = 0; i < parts.length; i ++) {
regexString = regexString + exports.escapeForRegex(parts[i]) + (i < (parts.length - 1) ? '(/|\\\\)' : '');
}
regexString = regexString + "(.*?).js)(?:(\"|')?)";
return new RegExp(regexString);
exports.createBundleFileRegex = function(bundleName) {
return new RegExp(exports.escapeForRegex(bundleName) + '[^"\']*?\\.js', 'g');
};

function modifyFilename(pth, modifier) {
Expand Down
18 changes: 13 additions & 5 deletions spec/lib/build/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,26 @@ describe('the Utils.runSequentially function', () => {
});
});

describe('the Utils.createSrcFileRegex function', () => {
describe('the Utils.createBundleFileRegex function', () => {
it('matches script tag with double quotes', () => {
expect('<script src="scripts/vendor-bundle.js"></script>'.match(Utils.createSrcFileRegex('scripts', 'vendor-bundle'))).not.toBeFalsy();
expect('<script src="scripts/vendor-bundle.js"></script>'.match(Utils.createBundleFileRegex('vendor-bundle'))).not.toBeFalsy();

expect('<script src="scripts/vendor-bundle.js"></script>'.replace(Utils.createBundleFileRegex('vendor-bundle'), 'vendor-bundle-123.js')).toBe('<script src="scripts/vendor-bundle-123.js"></script>');

// dev to prod
expect('<link rel="preload" href="/scripts/app-bundle.js"><script src="scripts/app-bundle.js"></script><script src="scripts/vendor-bundle.js"></script>'.replace(Utils.createBundleFileRegex('app-bundle'), 'app-bundle-123.js').replace(Utils.createBundleFileRegex('vendor-bundle'), 'vendor-bundle-abc.js')).toBe('<link rel="preload" href="/scripts/app-bundle-123.js"><script src="scripts/app-bundle-123.js"></script><script src="scripts/vendor-bundle-abc.js"></script>');

// prod to dev
expect('<link rel="preload" href="/scripts/app-bundle-123.js"><script src="scripts/app-bundle-123.js"></script><script src="scripts/vendor-bundle-abc.js"></script>'.replace(Utils.createBundleFileRegex('app-bundle'), 'app-bundle.js').replace(Utils.createBundleFileRegex('vendor-bundle'), 'vendor-bundle.js')).toBe('<link rel="preload" href="/scripts/app-bundle.js"><script src="scripts/app-bundle.js"></script><script src="scripts/vendor-bundle.js"></script>');
});
it('matches script tag with single quotes', () => {
expect('<script src=\'scripts/vendor-bundle.js\'></script>'.match(Utils.createSrcFileRegex('scripts', 'vendor-bundle'))).not.toBeFalsy();
expect('<script src=\'scripts/vendor-bundle.js\'></script>'.match(Utils.createBundleFileRegex('vendor-bundle'))).not.toBeFalsy();
});
it('matches script tag without quotes', () => {
expect('<script src=scripts/vendor-bundle.js></script>'.match(Utils.createSrcFileRegex('scripts', 'vendor-bundle'))).not.toBeFalsy();
expect('<script src=scripts/vendor-bundle.js></script>'.match(Utils.createBundleFileRegex('vendor-bundle'))).not.toBeFalsy();
});
it('does not match other bundles', () => {
expect('<script src=scripts/app-bundle.js></script>'.match(Utils.createSrcFileRegex('scripts', 'vendor-bundle'))).toBeFalsy();
expect('<script src=scripts/app-bundle.js></script>'.match(Utils.createBundleFileRegex('vendor-bundle'))).toBeFalsy();
});
});

Expand Down

0 comments on commit 47d0b0f

Please sign in to comment.