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

Commit 348b46b

Browse files
Diogo Francomichael-ciniawsky
authored andcommitted
fix(index): don't crash with dynamic import() (#728)
1 parent 8ba60e5 commit 348b46b

File tree

16 files changed

+87
-5
lines changed

16 files changed

+87
-5
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,8 @@
8383
"eslint --fix",
8484
"git add"
8585
]
86+
},
87+
"jest": {
88+
"testEnvironment": "node"
8689
}
8790
}

src/index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,21 @@ class ExtractTextPlugin {
8787
}
8888
}
8989

90-
static renderExtractedChunk(chunk) {
90+
static renderExtractedChunk(compilation, chunk) {
9191
const source = new ConcatSource();
9292

9393
for (const chunkModule of chunk.modulesIterable) {
94-
let moduleSource = chunkModule.source();
94+
let moduleSource = chunkModule.source(
95+
compilation.dependencyTemplates,
96+
compilation.runtimeTemplate
97+
);
98+
99+
// This module was concatenated by the ModuleConcatenationPlugin; because the pitching loader
100+
// only produces commonjs results, at least for now things we want to extract can't be in them.
101+
// NOTE: if ESM support is added, _this workaround will break_.
102+
if (moduleSource instanceof ConcatSource) {
103+
moduleSource = null;
104+
}
95105

96106
// Async imports (require.ensure(), import().then) are CachedSource module
97107
// instances caching a ReplaceSource instance, which breaks the plugin
@@ -102,7 +112,13 @@ class ExtractTextPlugin {
102112
// it's "__webpack_require__();" statements. Skip it.
103113
if (moduleSource instanceof CachedSource) {
104114
if (chunkModule[NS] && chunkModule[NS].content) {
105-
moduleSource = new RawSource(chunkModule[NS].content[0][1]);
115+
moduleSource = new ConcatSource();
116+
if (chunkModule[NS].content.length > 1) {
117+
console.error(chunkModule[NS].content);
118+
}
119+
for (const content of chunkModule[NS].content) {
120+
moduleSource.add(new RawSource(content[1]));
121+
}
106122
} else {
107123
moduleSource = null;
108124
}
@@ -215,10 +231,12 @@ class ExtractTextPlugin {
215231
const shouldExtract = !!(
216232
options.allChunks || isInitialOrHasNoParents(chunk)
217233
);
218-
// chunk.sortModules();
219234

220235
async.forEach(
221-
Array.from(chunk.modulesIterable),
236+
Array.from(chunk.modulesIterable).sort(
237+
// NOTE: .index should be .index2 once ESM support is added
238+
(a, b) => a.index - b.index
239+
),
222240
(module, moduleCallback) => {
223241
// eslint-disable-line no-shadow
224242
let meta = module[NS];
@@ -231,6 +249,7 @@ class ExtractTextPlugin {
231249
// chunk. See issue #604
232250
if (shouldExtract && !wasExtracted) {
233251
module[`${NS}/extract`] = shouldExtract; // eslint-disable-line no-path-concat
252+
234253
return compilation.rebuildModule(module, (err) => {
235254
if (err) {
236255
compilation.errors.push(err);
@@ -328,6 +347,7 @@ class ExtractTextPlugin {
328347

329348
const chunk = extractedChunk.originalChunk;
330349
const source = ExtractTextPlugin.renderExtractedChunk(
350+
compilation,
331351
extractedChunk
332352
);
333353

src/loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export function pitch(request) {
140140

141141
this[NS](text, query);
142142

143+
// NOTE: converting this to ESM will require changes to renderExtractedChunk
143144
if (text.locals && typeof resultSource !== 'undefined') {
144145
resultSource += `\nmodule.exports = ${JSON.stringify(text.locals)};`;
145146
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Webpack Integration Tests chunk-modules-css-wrong-order 1`] = `
4+
".block {
5+
color: tomato;
6+
}
7+
.App {
8+
color: black;
9+
}
10+
"
11+
`;
12+
313
exports[`Webpack Integration Tests chunk-modules-nested-ordered-by-id 1`] = `
414
"body {
515
margin: 0;
@@ -147,6 +157,17 @@ exports[`Webpack Integration Tests splitted-chunk 1`] = `
147157
"
148158
`;
149159

160+
exports[`Webpack Integration Tests splitted-chunk-import 1`] = `
161+
"a
162+
"
163+
`;
164+
165+
exports[`Webpack Integration Tests splitted-chunk-import-allchunks 1`] = `
166+
"a
167+
b
168+
"
169+
`;
170+
150171
exports[`Webpack Integration Tests splitted-multiple-entries 1`] = `""`;
151172

152173
exports[`Webpack Integration Tests splitted-multiple-entries 2`] = `""`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './a.txt'
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+
require('./a.txt');
2+
import('./b.txt');
3+
import('./c.js');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import ExtractTextPlugin from '../../../src/index';
2+
3+
module.exports = {
4+
entry: './index',
5+
plugins: [
6+
new ExtractTextPlugin({
7+
filename: 'file.css',
8+
allChunks: true,
9+
}),
10+
],
11+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b

test/cases/splitted-chunk-import/c.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './a.txt'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('./a.txt');
2+
import('./b.txt');
3+
import('./c.js');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import ExtractTextPlugin from '../../../src/index';
2+
3+
module.exports = {
4+
entry: './index',
5+
plugins: [
6+
new ExtractTextPlugin({
7+
filename: 'file.css',
8+
allChunks: false,
9+
}),
10+
],
11+
};

0 commit comments

Comments
 (0)