Skip to content

Commit

Permalink
Merge pull request #9724 from webpack/bugfix/serve-regression
Browse files Browse the repository at this point in the history
HMR plugin should only affect the root compilation
  • Loading branch information
sokra committed Sep 23, 2019
2 parents b151069 + 1c4138d commit adfa484
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/HotModuleReplacementPlugin.js
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions test/hotCases/child-compiler/issue-9706/file.js
@@ -0,0 +1,3 @@
export default 1;
---
export default 2;
12 changes: 12 additions & 0 deletions 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));
});
@@ -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);
});
};

0 comments on commit adfa484

Please sign in to comment.