From d5a1de21a65532f66b05a1f6c2146574ce385567 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Sat, 21 Oct 2017 23:16:33 +0900 Subject: [PATCH] fix: refer to the `entrypoint` instead of the first `module` (`module.identifier`) (#601) --- src/loader.js | 15 ++++++----- .../webpack-integration.test.js.snap | 6 +++++ .../a.js | 2 ++ .../b.js | 2 ++ .../expected/main.txt | 2 ++ .../loader.js | 3 +++ .../module.js | 1 + .../webpack.config.js | 26 +++++++++++++++++++ 8 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 test/cases/string-export-with-optimize-module-order/a.js create mode 100644 test/cases/string-export-with-optimize-module-order/b.js create mode 100644 test/cases/string-export-with-optimize-module-order/expected/main.txt create mode 100644 test/cases/string-export-with-optimize-module-order/loader.js create mode 100644 test/cases/string-export-with-optimize-module-order/module.js create mode 100644 test/cases/string-export-with-optimize-module-order/webpack.config.js diff --git a/src/loader.js b/src/loader.js index f552c6eb..c7214819 100644 --- a/src/loader.js +++ b/src/loader.js @@ -95,13 +95,16 @@ export function pitch(request) { } try { let text = this.exec(source, request); - if (typeof text === 'string') { text = [[0, text]]; } - text.forEach((item) => { - const id = item[0]; - compilation.modules.forEach((module) => { - if (module.id === id) { item[0] = module.identifier(); } + if (typeof text === 'string') { + text = [[compilation.entries[0].identifier(), text]]; + } else { + text.forEach((item) => { + const id = item[0]; + compilation.modules.forEach((module) => { + if (module.id === id) { item[0] = module.identifier(); } + }); }); - }); + } this[NS](text, query); if (text.locals && typeof resultSource !== 'undefined') { resultSource += `\nmodule.exports = ${JSON.stringify(text.locals)};`; diff --git a/test/__snapshots__/webpack-integration.test.js.snap b/test/__snapshots__/webpack-integration.test.js.snap index 964ebe26..4c818805 100644 --- a/test/__snapshots__/webpack-integration.test.js.snap +++ b/test/__snapshots__/webpack-integration.test.js.snap @@ -160,3 +160,9 @@ exports[`Webpack Integration Tests splitted-chunk 1`] = ` exports[`Webpack Integration Tests splitted-multiple-entries 1`] = `""`; exports[`Webpack Integration Tests splitted-multiple-entries 2`] = `""`; + +exports[`Webpack Integration Tests string-export-with-optimize-module-order 1`] = ` +"a +b +" +`; diff --git a/test/cases/string-export-with-optimize-module-order/a.js b/test/cases/string-export-with-optimize-module-order/a.js new file mode 100644 index 00000000..0acc1187 --- /dev/null +++ b/test/cases/string-export-with-optimize-module-order/a.js @@ -0,0 +1,2 @@ +require("./module")(); +module.exports = "a\n"; diff --git a/test/cases/string-export-with-optimize-module-order/b.js b/test/cases/string-export-with-optimize-module-order/b.js new file mode 100644 index 00000000..b728c1d5 --- /dev/null +++ b/test/cases/string-export-with-optimize-module-order/b.js @@ -0,0 +1,2 @@ +require("./module")(); +module.exports = "b\n"; diff --git a/test/cases/string-export-with-optimize-module-order/expected/main.txt b/test/cases/string-export-with-optimize-module-order/expected/main.txt new file mode 100644 index 00000000..422c2b7a --- /dev/null +++ b/test/cases/string-export-with-optimize-module-order/expected/main.txt @@ -0,0 +1,2 @@ +a +b diff --git a/test/cases/string-export-with-optimize-module-order/loader.js b/test/cases/string-export-with-optimize-module-order/loader.js new file mode 100644 index 00000000..d8b59045 --- /dev/null +++ b/test/cases/string-export-with-optimize-module-order/loader.js @@ -0,0 +1,3 @@ +module.exports = function(source) { + return source; +} diff --git a/test/cases/string-export-with-optimize-module-order/module.js b/test/cases/string-export-with-optimize-module-order/module.js new file mode 100644 index 00000000..cc40a464 --- /dev/null +++ b/test/cases/string-export-with-optimize-module-order/module.js @@ -0,0 +1 @@ +module.exports = () => {}; diff --git a/test/cases/string-export-with-optimize-module-order/webpack.config.js b/test/cases/string-export-with-optimize-module-order/webpack.config.js new file mode 100644 index 00000000..eb7e0381 --- /dev/null +++ b/test/cases/string-export-with-optimize-module-order/webpack.config.js @@ -0,0 +1,26 @@ +import ExtractTextPlugin from '../../../src/index'; + +function moduleOrderOptimizer() { + this.plugin("after-plugins", compiler => + compiler.plugin("compilation", compilation => + compilation.plugin("optimize-module-order", modules => { + const index = modules.findIndex(module => module.rawRequest == "./module"); + [modules[0], modules[index]] = [modules[index], modules[0]]; + }))); +} + +module.exports = { + entry: ['./a', './b'], + module: { + loaders: [ + { + test: /(a|b)\.js$/, + use: ExtractTextPlugin.extract('./loader') + }, + ], + }, + plugins: [ + moduleOrderOptimizer, + new ExtractTextPlugin('[name].txt'), + ], +};