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

fix(index): handle module extraction correctly (require.ensure) #687

Merged
merged 3 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ExtractTextPlugin {
});
async.forEach(chunks, (chunk, callback) => { // eslint-disable-line no-shadow
const extractedChunk = extractedChunks[chunks.indexOf(chunk)];
const shouldExtract = !!(options.allChunks || isInitialOrHasNoParents(chunk));
const shouldExtract = !!options.allChunks;
chunk.sortModules();
async.forEach(chunk.mapModules(c => c), (module, callback) => { // eslint-disable-line no-shadow
let meta = module[NS];
Expand All @@ -152,8 +152,16 @@ class ExtractTextPlugin {
// A stricter `shouldExtract !== wasExtracted` check to guard against cases where a previously extracted
// module would be extracted twice. Happens when a module is a dependency of an initial and a non-initial
// chunk. See issue #604
if (shouldExtract && !wasExtracted) {
module[`${NS}/extract`] = shouldExtract; // eslint-disable-line no-path-concat

// check every module's chunks.parents() to decide extract or not
for (let i = 0; i < module.chunks.length; i++) {
if (!isInitialOrHasNoParents(module.chunks[i]) && !module.extracted) {
module.extracted = true;
break;
}
}
if (shouldExtract || (!module.extracted && !wasExtracted)) {
module[`${NS}/extract`] = true; // eslint-disable-line no-path-concat
compilation.rebuildModule(module, (err) => {
if (err) {
compilation.errors.push(err);
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/webpack-integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ exports[`Webpack Integration Tests splitted-chunk 1`] = `
"
`;

exports[`Webpack Integration Tests splitted-mix-multiple-entries 1`] = `"a"`;

exports[`Webpack Integration Tests splitted-mix-multiple-entries 2`] = `"b"`;

exports[`Webpack Integration Tests splitted-multiple-entries 1`] = `""`;

exports[`Webpack Integration Tests splitted-multiple-entries 2`] = `""`;
Expand Down
2 changes: 2 additions & 0 deletions test/cases/splitted-mix-multiple-entries/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./a.txt');
require('./c.js');
1 change: 1 addition & 0 deletions test/cases/splitted-mix-multiple-entries/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
5 changes: 5 additions & 0 deletions test/cases/splitted-mix-multiple-entries/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

require('./b.txt');
require.ensure([], (require) => {
require('./c.js');
});
1 change: 1 addition & 0 deletions test/cases/splitted-mix-multiple-entries/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions test/cases/splitted-mix-multiple-entries/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./c.txt');
1 change: 1 addition & 0 deletions test/cases/splitted-mix-multiple-entries/c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c
1 change: 1 addition & 0 deletions test/cases/splitted-mix-multiple-entries/expected/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions test/cases/splitted-mix-multiple-entries/expected/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
14 changes: 14 additions & 0 deletions test/cases/splitted-mix-multiple-entries/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ExtractTextPlugin from '../../../src/index';

module.exports = {
entry: {
a: './a',
b: './b',
},
plugins: [
new ExtractTextPlugin({
filename: '[name].txt',
allChunks: false,
}),
],
};