Skip to content

Commit

Permalink
fix(bundler): Revisions are inserted into platform.index for all bundles
Browse files Browse the repository at this point in the history
fragsalat committed Oct 11, 2018
1 parent 23be17f commit 435557c
Showing 2 changed files with 22 additions and 25 deletions.
40 changes: 16 additions & 24 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
@@ -310,12 +310,6 @@ exports.Bundle = class {
this.hash = generateHash(concat.content).slice(0, 10);
bundleFileName = generateHashedPath(this.config.name, this.hash);
}

//Again, in the config setup, we're at the last bundle, and we can modify the index.html correctly now
let outputDir = platform.baseUrl || platform.output; //If we have a baseUrl, then the files are served from there, else it's the output
if (platform.index) {
this.setIndexFileConfigTarget(platform, path.posix.join(outputDir, bundleFileName));
}
} else if (buildOptions.isApplicable('rev')) {
//Generate a unique hash based off of the bundle contents
//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 {
return 'function _aureliaConfigureModuleLoader(){' + config + '}';
}

setIndexFileConfigTarget(platform, location) {
//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);
const createSrcFileRegex = require('./utils').createSrcFileRegex;
let indexLocation = platform.index;
async writeRevToIndex(platform) {
try {
let indexFile = fs.readFileSync(indexLocation);
let outputDir = platform.baseUrl || platform.output; //If we have a baseUrl, then the files are served from there, else it's the output
let configMatcher = createSrcFileRegex(outputDir, this.moduleId);
if (configMatcher.test(indexFile)) {
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)
fs.writeFile(indexLocation, indexFile);
} else {
logger.error('Error: Unable to update ' + this.moduleId + ' path to ' + location + ', could not find existing reference to replace');
}
} catch (err) {
if (err.code === 'ENOENT') {
logger.error('Error: No index file found at "' + indexLocation + '"');
} else {
logger.error(err);
// Validate rev is enabled and a hash was generated
if (platform.index && this.hash) {
let indexFile = fs.readFileSync(platform.index);
const outputDir = platform.baseUrl || platform.output;
const configMatcher = Utils.createSrcFileRegex(outputDir, this.moduleId);
const bundleFileName = Utils.generateHashedPath(this.config.name, this.hash);
const bundleLocation = path.posix.join(outputDir, bundleFileName);
// Replace file name with hashed file name
if (configMatcher.test(indexFile)) {
indexFile = indexFile.replace(configMatcher, 'src="$2' + bundleLocation + '"');
await fs.writeFile(platform.index, indexFile);
logger.info(`INFO [Bundle] Updated file name for ${this.moduleId} in ${platform.index}`);
}
}
} catch (error) {
logger.error(`ERROR [Bundle] Couldn't update file name with revision in ${platform.index}`, error);
}
}
};
7 changes: 6 additions & 1 deletion lib/build/bundler.js
Original file line number Diff line number Diff line change
@@ -232,7 +232,12 @@ exports.Bundler = class {
}

write() {
return Promise.all(this.bundles.map(x => x.write(this.project.build.targets[0])));
return Promise.all(this.bundles.map(bundle => bundle.write(this.project.build.targets[0])))
.then(async() => {
for (let i = this.bundles.length; i--; ) {
await this.bundles[i].writeRevToIndex(this.project.build.targets[0]);
}
});
}

getDependencyInclusions() {

0 comments on commit 435557c

Please sign in to comment.