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

Commit 426e758

Browse files
committed
Refer to the entry point instead of the first module for default identifier
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.
1 parent 6a660f3 commit 426e758

File tree

8 files changed

+51
-6
lines changed

8 files changed

+51
-6
lines changed

src/loader.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,16 @@ export function pitch(request) {
9494
}
9595
try {
9696
let text = this.exec(source, request);
97-
if (typeof text === 'string') { text = [[0, text]]; }
98-
text.forEach((item) => {
99-
const id = item[0];
100-
compilation.modules.forEach((module) => {
101-
if (module.id === id) { item[0] = module.identifier(); }
97+
if (typeof text === 'string') {
98+
text = [[compilation.entries[0].identifier(), text]];
99+
} else {
100+
text.forEach((item) => {
101+
const id = item[0];
102+
compilation.modules.forEach((module) => {
103+
if (module.id === id) { item[0] = module.identifier(); }
104+
});
102105
});
103-
});
106+
}
104107
this[NS](text, query);
105108
if (text.locals && typeof resultSource !== 'undefined') {
106109
resultSource += `\nmodule.exports = ${JSON.stringify(text.locals)};`;

test/__snapshots__/webpack-integration.test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,9 @@ exports[`Webpack Integration Tests splitted-chunk 1`] = `
160160
exports[`Webpack Integration Tests splitted-multiple-entries 1`] = `""`;
161161

162162
exports[`Webpack Integration Tests splitted-multiple-entries 2`] = `""`;
163+
164+
exports[`Webpack Integration Tests string-export-with-optimize-module-order 1`] = `
165+
"a
166+
b
167+
"
168+
`;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("./module")();
2+
module.exports = "a\n";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("./module")();
2+
module.exports = "b\n";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a
2+
b
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function(source) {
2+
return source;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = () => {};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import ExtractTextPlugin from '../../../src/index';
2+
3+
function moduleOrderOptimizer() {
4+
this.plugin("after-plugins", compiler =>
5+
compiler.plugin("compilation", compilation =>
6+
compilation.plugin("optimize-module-order", modules => {
7+
const index = modules.findIndex(module => module.rawRequest == "./module");
8+
[modules[0], modules[index]] = [modules[index], modules[0]];
9+
})));
10+
}
11+
12+
module.exports = {
13+
entry: ['./a', './b'],
14+
module: {
15+
loaders: [
16+
{
17+
test: /(a|b)\.js$/,
18+
use: ExtractTextPlugin.extract('./loader')
19+
},
20+
],
21+
},
22+
plugins: [
23+
moduleOrderOptimizer,
24+
new ExtractTextPlugin('[name].txt'),
25+
],
26+
};

0 commit comments

Comments
 (0)