Support hashes in filenames
This commit is contained in:
parent
4fc5328411
commit
c0b730f25d
10
index.js
10
index.js
|
|
@ -48,13 +48,15 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) {
|
|||
.catch(function (err) {
|
||||
compilation.errors.push(prettyError(err, compiler.context).toString());
|
||||
return {
|
||||
content: self.options.showErrors ? prettyError(err, compiler.context).toJsonHtml() : 'ERROR'
|
||||
content: self.options.showErrors ? prettyError(err, compiler.context).toJsonHtml() : 'ERROR',
|
||||
outputName: self.options.filename
|
||||
};
|
||||
})
|
||||
.then(function (compilationResult) {
|
||||
// If the compilation change didnt change the cache is valid
|
||||
isCompilationCached = compilationResult.hash && self.childCompilerHash === compilationResult.hash;
|
||||
self.childCompilerHash = compilationResult.hash;
|
||||
self.childCompilationOutputName = compilationResult.outputName;
|
||||
callback();
|
||||
return compilationResult.content;
|
||||
});
|
||||
|
|
@ -150,7 +152,7 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) {
|
|||
})
|
||||
.then(function (html) {
|
||||
// Replace the compilation result with the evaluated html code
|
||||
compilation.assets[self.options.filename] = {
|
||||
compilation.assets[self.childCompilationOutputName] = {
|
||||
source: function () {
|
||||
return html;
|
||||
},
|
||||
|
|
@ -162,7 +164,7 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) {
|
|||
.then(function () {
|
||||
// Let other plugins know that we are done:
|
||||
return applyPluginsAsyncWaterfall('html-webpack-plugin-after-emit', {
|
||||
html: compilation.assets[self.options.filename],
|
||||
html: compilation.assets[self.childCompilationOutputName],
|
||||
plugin: self
|
||||
});
|
||||
})
|
||||
|
|
@ -349,7 +351,7 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chu
|
|||
// If a hard coded public path exists use it
|
||||
? compilation.mainTemplate.getPublicPath({hash: webpackStatsJson.hash})
|
||||
// If no public path was set get a relative url path
|
||||
: path.relative(path.resolve(compilation.options.output.path, path.dirname(self.options.filename)), compilation.options.output.path)
|
||||
: path.relative(path.resolve(compilation.options.output.path, path.dirname(self.childCompilationOutputName)), compilation.options.output.path)
|
||||
.split(path.sep).join('/');
|
||||
|
||||
if (publicPath.length && publicPath.substr(-1, 1) !== '/') {
|
||||
|
|
|
|||
|
|
@ -36,7 +36,8 @@ module.exports.compileTemplate = function compileTemplate (template, context, ou
|
|||
filename: outputFilename,
|
||||
publicPath: compilation.outputOptions.publicPath
|
||||
};
|
||||
var cachedAsset = compilation.assets[outputOptions.filename];
|
||||
// Store the result of the parent compilation before we start the child compilation
|
||||
var assetsBeforeCompilation = Object.assign({}, compilation.assets[outputOptions.filename]);
|
||||
// Create an additional child compiler which takes the template
|
||||
// and turns it into an Node.JS html factory.
|
||||
// This allows us to use loaders during the compilation
|
||||
|
|
@ -66,9 +67,17 @@ module.exports.compileTemplate = function compileTemplate (template, context, ou
|
|||
// Compile and return a promise
|
||||
return new Promise(function (resolve, reject) {
|
||||
childCompiler.runAsChild(function (err, entries, childCompilation) {
|
||||
compilation.assets[outputOptions.filename] = cachedAsset;
|
||||
if (cachedAsset === undefined) {
|
||||
delete compilation.assets[outputOptions.filename];
|
||||
// Replace [hash] placeholders in filename
|
||||
var outputName = compilation.mainTemplate.applyPluginsWaterfall('asset-path', outputOptions.filename, {
|
||||
hash: childCompilation.hash,
|
||||
chunk: entries[0]
|
||||
});
|
||||
// Restore the parent compilation to the state like it
|
||||
// was before the child compilation
|
||||
compilation.assets[outputName] = assetsBeforeCompilation[outputName];
|
||||
if (assetsBeforeCompilation[outputName] === undefined) {
|
||||
// If it wasn't there - delete it
|
||||
delete compilation.assets[outputName];
|
||||
}
|
||||
// Resolve / reject the promise
|
||||
if (childCompilation && childCompilation.errors && childCompilation.errors.length) {
|
||||
|
|
@ -82,8 +91,10 @@ module.exports.compileTemplate = function compileTemplate (template, context, ou
|
|||
resolve({
|
||||
// Hash of the template entry point
|
||||
hash: entries[0].hash,
|
||||
// Output name
|
||||
outputName: outputName,
|
||||
// Compiled code
|
||||
content: childCompilation.assets[outputOptions.filename].source()
|
||||
content: childCompilation.assets[outputName].source()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -39,7 +39,13 @@ function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expec
|
|||
expect(compilationWarnings).toBe('');
|
||||
}
|
||||
var outputFileExists = fs.existsSync(path.join(OUTPUT_DIR, outputFile));
|
||||
expect(outputFileExists).toBe(true);
|
||||
expect({
|
||||
exists: outputFileExists,
|
||||
filename: outputFile
|
||||
}).toEqual({
|
||||
exists: true,
|
||||
filename: outputFile
|
||||
});
|
||||
if (!outputFileExists) {
|
||||
return done();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue