diff --git a/lib/build/bundle.js b/lib/build/bundle.js index b9928e386..e592e2d2d 100644 --- a/lib/build/bundle.js +++ b/lib/build/bundle.js @@ -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); } } }; diff --git a/lib/build/bundler.js b/lib/build/bundler.js index 0ebec0567..cfaa63574 100644 --- a/lib/build/bundler.js +++ b/lib/build/bundler.js @@ -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() {