Skip to content

Commit 435557c

Browse files
committed
fix(bundler): Revisions are inserted into platform.index for all bundles
1 parent 23be17f commit 435557c

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

lib/build/bundle.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,6 @@ exports.Bundle = class {
310310
this.hash = generateHash(concat.content).slice(0, 10);
311311
bundleFileName = generateHashedPath(this.config.name, this.hash);
312312
}
313-
314-
//Again, in the config setup, we're at the last bundle, and we can modify the index.html correctly now
315-
let outputDir = platform.baseUrl || platform.output; //If we have a baseUrl, then the files are served from there, else it's the output
316-
if (platform.index) {
317-
this.setIndexFileConfigTarget(platform, path.posix.join(outputDir, bundleFileName));
318-
}
319313
} else if (buildOptions.isApplicable('rev')) {
320314
//Generate a unique hash based off of the bundle contents
321315
//Must generate hash after we write the loader config so that any other bundle changes (hash changes) can cause a new hash for the vendor file
@@ -437,26 +431,24 @@ exports.Bundle = class {
437431
return 'function _aureliaConfigureModuleLoader(){' + config + '}';
438432
}
439433

440-
setIndexFileConfigTarget(platform, location) {
441-
//Replace the reference to the vendor bundle in the "index.html" file to use the correct build revision file (or remove the build revision hash);
442-
const createSrcFileRegex = require('./utils').createSrcFileRegex;
443-
let indexLocation = platform.index;
434+
async writeRevToIndex(platform) {
444435
try {
445-
let indexFile = fs.readFileSync(indexLocation);
446-
let outputDir = platform.baseUrl || platform.output; //If we have a baseUrl, then the files are served from there, else it's the output
447-
let configMatcher = createSrcFileRegex(outputDir, this.moduleId);
448-
if (configMatcher.test(indexFile)) {
449-
indexFile = indexFile.replace(configMatcher, 'src="$2' + location + '"');//Replace the old reference to the file with whatever the new one is (preserving any unknown path parts before)
450-
fs.writeFile(indexLocation, indexFile);
451-
} else {
452-
logger.error('Error: Unable to update ' + this.moduleId + ' path to ' + location + ', could not find existing reference to replace');
453-
}
454-
} catch (err) {
455-
if (err.code === 'ENOENT') {
456-
logger.error('Error: No index file found at "' + indexLocation + '"');
457-
} else {
458-
logger.error(err);
436+
// Validate rev is enabled and a hash was generated
437+
if (platform.index && this.hash) {
438+
let indexFile = fs.readFileSync(platform.index);
439+
const outputDir = platform.baseUrl || platform.output;
440+
const configMatcher = Utils.createSrcFileRegex(outputDir, this.moduleId);
441+
const bundleFileName = Utils.generateHashedPath(this.config.name, this.hash);
442+
const bundleLocation = path.posix.join(outputDir, bundleFileName);
443+
// Replace file name with hashed file name
444+
if (configMatcher.test(indexFile)) {
445+
indexFile = indexFile.replace(configMatcher, 'src="$2' + bundleLocation + '"');
446+
await fs.writeFile(platform.index, indexFile);
447+
logger.info(`INFO [Bundle] Updated file name for ${this.moduleId} in ${platform.index}`);
448+
}
459449
}
450+
} catch (error) {
451+
logger.error(`ERROR [Bundle] Couldn't update file name with revision in ${platform.index}`, error);
460452
}
461453
}
462454
};

lib/build/bundler.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ exports.Bundler = class {
232232
}
233233

234234
write() {
235-
return Promise.all(this.bundles.map(x => x.write(this.project.build.targets[0])));
235+
return Promise.all(this.bundles.map(bundle => bundle.write(this.project.build.targets[0])))
236+
.then(async() => {
237+
for (let i = this.bundles.length; i--; ) {
238+
await this.bundles[i].writeRevToIndex(this.project.build.targets[0]);
239+
}
240+
});
236241
}
237242

238243
getDependencyInclusions() {

0 commit comments

Comments
 (0)