Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
Refer to the entry point instead of the first module for default iden…
Browse files Browse the repository at this point in the history
…tifier

The order of modules can be reordered by optimize-module-order plugins.
The intention is to use the entry point as the default identifier, so
refer to entries property of the compilation.
  • Loading branch information
akihikodaki committed Oct 21, 2017
1 parent 5286ab2 commit f2c3056
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)};`;
Expand Down
6 changes: 6 additions & 0 deletions test/__snapshots__/webpack-integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
`;
2 changes: 2 additions & 0 deletions test/cases/string-export-with-optimize-module-order/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("./module")();
module.exports = "a\n";
2 changes: 2 additions & 0 deletions test/cases/string-export-with-optimize-module-order/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("./module")();
module.exports = "b\n";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a
b
3 changes: 3 additions & 0 deletions test/cases/string-export-with-optimize-module-order/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(source) {
return source;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => {};
Original file line number Diff line number Diff line change
@@ -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'),
],
};

0 comments on commit f2c3056

Please sign in to comment.