diff --git a/lib/TemplatedPathPlugin.js b/lib/TemplatedPathPlugin.js index 5c9225d0e42..a6e29cc1151 100644 --- a/lib/TemplatedPathPlugin.js +++ b/lib/TemplatedPathPlugin.js @@ -105,9 +105,9 @@ class TemplatedPathPlugin { const outputOptions = this.outputOptions; const chunkFilename = outputOptions.chunkFilename || outputOptions.filename; if(REGEXP_CHUNKHASH_FOR_TEST.test(chunkFilename)) - hash.update(JSON.stringify(chunk.getChunkMaps(true, true).hash)); + hash.update(JSON.stringify(chunk.getChunkMaps(false, true).hash)); if(REGEXP_NAME_FOR_TEST.test(chunkFilename)) - hash.update(JSON.stringify(chunk.getChunkMaps(true, true).name)); + hash.update(JSON.stringify(chunk.getChunkMaps(false, true).name)); }); }); } diff --git a/test/WatchTestCases.test.js b/test/WatchTestCases.test.js index 611c56468da..23398805db2 100644 --- a/test/WatchTestCases.test.js +++ b/test/WatchTestCases.test.js @@ -155,6 +155,10 @@ describe("WatchTestCases", () => { return test; } + const globalContext = { + console: console + }; + function _require(currentDirectory, module) { if(Array.isArray(module) || /^\.\.?\//.test(module)) { let fn; @@ -170,11 +174,15 @@ describe("WatchTestCases", () => { p = path.join(currentDirectory, module); content = fs.readFileSync(p, "utf-8"); } - fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE) {" + content + "\n})", p); + if(options.target === "web" || options.target === "webworker") { + fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE, window) {" + content + "\n})", globalContext, p); + } else { + fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE) {" + content + "\n})", p); + } const m = { exports: {} }; - fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, run.name, jsonStats, state); + fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, run.name, jsonStats, state, globalContext); return module.exports; } else if(testConfig.modules && module in testConfig.modules) { return testConfig.modules[module]; @@ -188,7 +196,7 @@ describe("WatchTestCases", () => { } catch(e) {} if(testConfig.noTests) return process.nextTick(done); - _require(outputDirectory, "./bundle.js"); + _require(outputDirectory, testConfig.bundlePath || "./bundle.js"); if(exportedTests < 1) return done(new Error("No tests exported by test case")); runIdx++; diff --git a/test/watchCases/runtime/dynamic-import/0/dynamic.js b/test/watchCases/runtime/dynamic-import/0/dynamic.js new file mode 100644 index 00000000000..9ec2d94d947 --- /dev/null +++ b/test/watchCases/runtime/dynamic-import/0/dynamic.js @@ -0,0 +1 @@ +module.exports = "Normal"; \ No newline at end of file diff --git a/test/watchCases/runtime/dynamic-import/0/index.js b/test/watchCases/runtime/dynamic-import/0/index.js new file mode 100644 index 00000000000..da5d4c504fd --- /dev/null +++ b/test/watchCases/runtime/dynamic-import/0/index.js @@ -0,0 +1,28 @@ +it("should change chunkhash of main chunk", function () { + const mainChunk = STATS_JSON.chunks.find((chunk) => chunk.names.indexOf("main") !== -1); + (!mainChunk).should.be.false("Main chunk not found"); + switch (WATCH_STEP) { + case "0": + STATE.hash = mainChunk.hash; + break; + case "1": + mainChunk.hash.should.be.not.eql(STATE.hash); + break; + } +}); + +it("should load additional chunk", function (done) { + const step = WATCH_STEP; + import(/* webpackChunkName: "dynamic" */ './dynamic') + .then((dynamic) => { + switch (step) { + case "0": + dynamic.should.be.eql("Normal"); + break; + case "1": + dynamic.should.be.eql("Changed"); + break; + } + done(); + }); +}); diff --git a/test/watchCases/runtime/dynamic-import/1/dynamic.js b/test/watchCases/runtime/dynamic-import/1/dynamic.js new file mode 100644 index 00000000000..e89fb156534 --- /dev/null +++ b/test/watchCases/runtime/dynamic-import/1/dynamic.js @@ -0,0 +1 @@ +module.exports = "Changed"; \ No newline at end of file diff --git a/test/watchCases/runtime/dynamic-import/webpack.config.js b/test/watchCases/runtime/dynamic-import/webpack.config.js new file mode 100644 index 00000000000..72c46838831 --- /dev/null +++ b/test/watchCases/runtime/dynamic-import/webpack.config.js @@ -0,0 +1,5 @@ +module.exports = { + output: { + chunkFilename: "[name].[chunkhash].js" + } +}; diff --git a/test/watchCases/runtime/static-import/0/dynamic-and-static.js b/test/watchCases/runtime/static-import/0/dynamic-and-static.js new file mode 100644 index 00000000000..9ec2d94d947 --- /dev/null +++ b/test/watchCases/runtime/static-import/0/dynamic-and-static.js @@ -0,0 +1 @@ +module.exports = "Normal"; \ No newline at end of file diff --git a/test/watchCases/runtime/static-import/0/index.js b/test/watchCases/runtime/static-import/0/index.js new file mode 100644 index 00000000000..b951c06897c --- /dev/null +++ b/test/watchCases/runtime/static-import/0/index.js @@ -0,0 +1,44 @@ +require("should"); + +import * as both from './dynamic-and-static' +import * as staticModule from './static' + +it("should not change chunkhash of manifest chunk", function () { + const manifestChunk = STATS_JSON.chunks.find((chunk) => chunk.names.indexOf("manifest") !== -1); + (!manifestChunk).should.be.false("Main chunk not found"); + switch (WATCH_STEP) { + case "0": + STATE.hash = manifestChunk.hash; + staticModule.should.be.eql("Normal"); + both.should.be.eql("Normal"); + break; + case "1": + manifestChunk.hash.should.be.eql(STATE.hash); + staticModule.should.be.eql("Changed"); + both.should.be.eql("Normal"); + break; + case "2": + manifestChunk.hash.should.be.eql(STATE.hash); + staticModule.should.be.eql("Changed"); + both.should.be.eql("Changed"); + break; + } +}); + +it("should load additional chunk", function (done) { + const step = WATCH_STEP; + import(/* webpackChunkName: "dynamic-and-static" */ './dynamic-and-static') + .then((dynamic) => { + switch (step) { + case "0": + case "1": + dynamic.should.be.eql("Normal"); + break; + case "2": + dynamic.should.be.eql("Changed"); + break; + } + done(); + }) + .catch(done);; +}); diff --git a/test/watchCases/runtime/static-import/0/static.js b/test/watchCases/runtime/static-import/0/static.js new file mode 100644 index 00000000000..9ec2d94d947 --- /dev/null +++ b/test/watchCases/runtime/static-import/0/static.js @@ -0,0 +1 @@ +module.exports = "Normal"; \ No newline at end of file diff --git a/test/watchCases/runtime/static-import/1/static.js b/test/watchCases/runtime/static-import/1/static.js new file mode 100644 index 00000000000..e89fb156534 --- /dev/null +++ b/test/watchCases/runtime/static-import/1/static.js @@ -0,0 +1 @@ +module.exports = "Changed"; \ No newline at end of file diff --git a/test/watchCases/runtime/static-import/2/dynamic-and-static.js b/test/watchCases/runtime/static-import/2/dynamic-and-static.js new file mode 100644 index 00000000000..e89fb156534 --- /dev/null +++ b/test/watchCases/runtime/static-import/2/dynamic-and-static.js @@ -0,0 +1 @@ +module.exports = "Changed"; \ No newline at end of file diff --git a/test/watchCases/runtime/static-import/test.config.js b/test/watchCases/runtime/static-import/test.config.js new file mode 100644 index 00000000000..e693d485883 --- /dev/null +++ b/test/watchCases/runtime/static-import/test.config.js @@ -0,0 +1,6 @@ +module.exports = { + bundlePath: [ + "./manifest.bundle.js", + "./main.bundle.js" + ] +} \ No newline at end of file diff --git a/test/watchCases/runtime/static-import/webpack.config.js b/test/watchCases/runtime/static-import/webpack.config.js new file mode 100644 index 00000000000..4dc5f5cad0b --- /dev/null +++ b/test/watchCases/runtime/static-import/webpack.config.js @@ -0,0 +1,14 @@ +var CommonsChunkPlugin = require("../../../../lib/optimize/CommonsChunkPlugin"); +module.exports = { + output: { + filename: "[name].bundle.js", + chunkFilename: "[name].[chunkhash].js" + }, + target: "web", + plugins: [ + new CommonsChunkPlugin({ + name: ["manifest"], + minChunks: Infinity + }) + ] +};