Skip to content

Commit

Permalink
feat: added compatibility for new webpack 4 .hooks mode (#113)
Browse files Browse the repository at this point in the history
Added compatibility for new Webpack 4 .hooks mode
  • Loading branch information
chrispickford authored and jantimon committed Apr 3, 2018
1 parent c8fc0af commit 5ab0bb8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ FaviconsWebpackPlugin.prototype.apply = function (compiler) {
self.options.title = guessAppName(compiler.context);
}

// Generate the favicons
// Generate the favicons (webpack 4 compliant + back compat)
var compilationResult;
compiler.plugin('make', function (compilation, callback) {
(compiler.hooks
? compiler.hooks.make.tapAsync.bind(compiler.hooks.make, 'FaviconsWebpackPluginMake')
: compiler.plugin.bind(compiler, 'make')
)((compilation, callback) => {
childCompiler.compileTemplate(self.options, compiler.context, compilation)
.then(function (result) {
compilationResult = result;
Expand Down Expand Up @@ -81,9 +84,12 @@ FaviconsWebpackPlugin.prototype.apply = function (compiler) {
}
}

// Remove the stats from the output if they are not required
// Remove the stats from the output if they are not required (webpack 4 compliant + back compat)
if (!self.options.emitStats) {
compiler.plugin('emit', function (compilation, callback) {
(compiler.hooks
? compiler.hooks.emit.tapAsync.bind(compiler.hooks.emit, 'FaviconsWebpackPluginEmit')
: compiler.plugin.bind(compiler, 'emit')
)((compilation, callback) => {
delete compilation.assets[compilationResult.outputName];
callback();
});
Expand Down
32 changes: 20 additions & 12 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,36 @@ module.exports.compileTemplate = function compileTemplate (options, context, com
var compilerName = getCompilerName(context, outputOptions.filename);
var childCompiler = compilation.createChildCompiler(compilerName, outputOptions);
childCompiler.context = context;
childCompiler.apply(
new SingleEntryPlugin(context, '!!' + require.resolve('./favicons.js') + '?' +
JSON.stringify({
outputFilePrefix: options.prefix,
icons: options.icons,
background: options.background,
persistentCache: options.persistentCache,
appName: options.title
}) + '!' + options.logo)
);

var singleEntryPlugin = new SingleEntryPlugin(context, '!!' + require.resolve('./favicons.js') + '?' +
JSON.stringify({
outputFilePrefix: options.prefix,
icons: options.icons,
background: options.background,
persistentCache: options.persistentCache,
appName: options.title
}) + '!' + options.logo);

(compilation.hooks ? singleEntryPlugin.apply(childCompiler) : childCompiler.apply(singleEntryPlugin));

// Fix for "Uncaught TypeError: __webpack_require__(...) is not a function"
// Hot module replacement requires that every child compiler has its own
// cache. @see https://github.com/ampedandwired/html-webpack-plugin/pull/179
childCompiler.plugin('compilation', function (compilation) {
// (webpack 4 compliant + back compat)
(childCompiler.hooks
? childCompiler.hooks.compilation.tap.bind(childCompiler.hooks.compilation, 'FaviconsWebpackPluginCompilation')
: childCompiler.plugin.bind(childCompiler, 'compilation')
)((compilation) => {
if (compilation.cache) {
if (!compilation.cache[compilerName]) {
compilation.cache[compilerName] = {};
}
compilation.cache = compilation.cache[compilerName];
}
compilation.plugin('optimize-chunk-assets', function (chunks, callback) {
(compilation.hooks
? compilation.hooks.optimizeChunkAssets.tapAsync.bind(compilation.hooks.optimizeChunkAssets, 'FaviconsWebpackPluginOptimizeChunkAssets')
: compilation.plugin.bind(compilation, 'optimize-chunk-assets')
)((chunks, callback) => {
if (!chunks[0]) {
return callback(compilation.errors[0] || 'Favicons generation failed');
}
Expand Down

0 comments on commit 5ab0bb8

Please sign in to comment.