Skip to content

Commit

Permalink
fix a bug
Browse files Browse the repository at this point in the history
# issues 724
jantimon#724
  • Loading branch information
daifee committed Oct 12, 2017
1 parent aedc623 commit 1836c38
Show file tree
Hide file tree
Showing 3 changed files with 6,151 additions and 2 deletions.
41 changes: 40 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ function HtmlWebpackPlugin (options) {
chunks: 'all',
excludeChunks: [],
title: 'Webpack App',
xhtml: false
xhtml: false,
// resolve multi html recompile slow
multihtmlCache: false
}, options);
}

HtmlWebpackPlugin.prototype.apply = function (compiler) {
var self = this;
var isCompilationCached = false;
var compilationPromise;
// cache childCompilation
var childCompilation = null;
// when done, set true;
// when childCompilation's fileDependencies had changed, set false
var isValidChildCompilation = false;

this.options.template = this.getFullTemplatePath(this.options.template, compiler.context);

Expand All @@ -42,7 +49,33 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) {
this.options.filename = path.relative(compiler.options.output.path, filename);
}

compiler.plugin('invalid', function (fileName) {
if (childCompilation &&
childCompilation.fileDependencies.indexOf(fileName) !== -1) {
isValidChildCompilation = false;
}
});

compiler.plugin('done', function (stats) {
var compilation = stats.compilation;

if (childCompilation) {
// webpack watch
childCompilation.fileDependencies.forEach(function (fileName) {
if (compilation.fileDependencies.indexOf(fileName) === -1) {
compilation.fileDependencies.push(fileName);
}
});
}

isValidChildCompilation = true;
});

compiler.plugin('make', function (compilation, callback) {
if (self.options.multihtmlCache && isValidChildCompilation) {
return callback();
}

// Compile the template (queued)
compilationPromise = childCompiler.compileTemplate(self.options.template, compiler.context, self.options.filename, compilation)
.catch(function (err) {
Expand All @@ -53,6 +86,8 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) {
};
})
.then(function (compilationResult) {
childCompilation = compilationResult.childCompilation;

// If the compilation change didnt change the cache is valid
isCompilationCached = compilationResult.hash && self.childCompilerHash === compilationResult.hash;
self.childCompilerHash = compilationResult.hash;
Expand All @@ -63,6 +98,10 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) {
});

compiler.plugin('emit', function (compilation, callback) {
if (self.options.multihtmlCache && isValidChildCompilation) {
return callback();
}

var applyPluginsAsyncWaterfall = self.applyPluginsAsyncWaterfall(compilation);
// Get all chunks
var allChunks = compilation.getStats().toJson().chunks;
Expand Down
4 changes: 3 additions & 1 deletion lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ module.exports.compileTemplate = function compileTemplate (template, context, ou
// Output name
outputName: outputName,
// Compiled code
content: childCompilation.assets[outputName].source()
content: childCompilation.assets[outputName].source(),
// childCompilation for multipageCache
childCompilation: childCompilation
});
}
});
Expand Down
Loading

0 comments on commit 1836c38

Please sign in to comment.