diff --git a/lib/HotModuleReplacementPlugin.js b/lib/HotModuleReplacementPlugin.js index 06423d3fe73..58a1f1fe5df 100644 --- a/lib/HotModuleReplacementPlugin.js +++ b/lib/HotModuleReplacementPlugin.js @@ -150,6 +150,10 @@ module.exports = class HotModuleReplacementPlugin { compiler.hooks.compilation.tap( "HotModuleReplacementPlugin", (compilation, { normalModuleFactory }) => { + // This applies the HMR plugin only to the targeted compiler + // It should not affect child compilations + if (compilation.compiler !== compiler) return; + const hotUpdateChunkTemplate = compilation.hotUpdateChunkTemplate; if (!hotUpdateChunkTemplate) return; diff --git a/test/hotCases/child-compiler/issue-9706/file.js b/test/hotCases/child-compiler/issue-9706/file.js new file mode 100644 index 00000000000..4fd27070716 --- /dev/null +++ b/test/hotCases/child-compiler/issue-9706/file.js @@ -0,0 +1,3 @@ +export default 1; +--- +export default 2; diff --git a/test/hotCases/child-compiler/issue-9706/index.js b/test/hotCases/child-compiler/issue-9706/index.js new file mode 100644 index 00000000000..4c4092071bb --- /dev/null +++ b/test/hotCases/child-compiler/issue-9706/index.js @@ -0,0 +1,12 @@ +import value, { assets } from "./report-child-assets-loader!./file"; + +it("should not emit hot updates from child compilers", done => { + expect(value).toBe(1); + expect(assets).toEqual(["test.js"]); + module.hot.accept("./report-child-assets-loader!./file", () => { + expect(value).toBe(2); + expect(assets).toEqual(["test.js"]); + done(); + }); + NEXT(require("../../update")(done)); +}); diff --git a/test/hotCases/child-compiler/issue-9706/report-child-assets-loader.js b/test/hotCases/child-compiler/issue-9706/report-child-assets-loader.js new file mode 100644 index 00000000000..d7bfab6cd43 --- /dev/null +++ b/test/hotCases/child-compiler/issue-9706/report-child-assets-loader.js @@ -0,0 +1,32 @@ +const NodeTemplatePlugin = require("../../../../lib/node/NodeTemplatePlugin"); +const FunctionModulePlugin = require("../../../../lib/FunctionModulePlugin"); +const SingleEntryPlugin = require("../../../../lib/SingleEntryPlugin"); + +const compilerCache = new WeakMap(); + +module.exports = function(source) { + let childCompiler = compilerCache.get(this._compiler); + if (childCompiler === undefined) { + childCompiler = this._compilation.createChildCompiler( + "my-compiler|" + this.request, + { + filename: "test.js" + }, + [ + new NodeTemplatePlugin(), + new FunctionModulePlugin(), + new SingleEntryPlugin(this.context, this.resource) + ] + ); + compilerCache.set(this._compiler, childCompiler); + } + const callback = this.async(); + childCompiler.runAsChild((err, entries, compilation) => { + if (err) return callback(err); + + const result = `export const assets = ${JSON.stringify( + compilation.getAssets().map(a => a.name) + )};\n${source}`; + callback(null, result); + }); +};