From 6898aa4ee0d51493097991414354e38520a2b09f Mon Sep 17 00:00:00 2001 From: David Heidrich Date: Sun, 18 Jun 2017 14:26:34 +0200 Subject: [PATCH] respect all chunks --- index.js | 27 +++++++++++++++++-- .../multiple-entries-async/expected/0.js | 26 +++++++++--------- .../multiple-entries-async/expected/1.js | 8 +++--- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 877c7f3e..5e29c6b3 100644 --- a/index.js +++ b/index.js @@ -7,10 +7,12 @@ var ConcatSource = require("webpack-sources").ConcatSource; var async = require("async"); var ExtractedModule = require("./ExtractedModule"); var Chunk = require("webpack/lib/Chunk"); +var NormalModule = require("webpack/lib/NormalModule"); var OrderUndefinedError = require("./OrderUndefinedError"); var loaderUtils = require("loader-utils"); var validateOptions = require('schema-utils'); var path = require('path'); +var OriginalSource = require("webpack-sources").OriginalSource; var NS = fs.realpathSync(__dirname); @@ -233,6 +235,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) { var options = this.options; compiler.plugin("this-compilation", function(compilation) { var extractCompilation = new ExtractTextPluginCompilation(); + var toRemoveModules = {}; compilation.plugin("normal-module-loader", function(loaderContext, module) { loaderContext[NS] = function(content, opt) { if(options.disable) @@ -290,9 +293,14 @@ ExtractTextPlugin.prototype.apply = function(compiler) { return callback(); } if(meta.content) { - extractCompilation.addResultToChunk(module.identifier(), meta.content, module, extractedChunk); + var ident = module.identifier(); + extractCompilation.addResultToChunk(ident, meta.content, module, extractedChunk); // remove generated result from chunk - chunk.removeModule(module); + if(toRemoveModules[ident]) { + toRemoveModules[ident].chunks.push(chunk) + } else { + toRemoveModules[ident] = { module: module, chunks: [chunk] }; + } } callback(); }); @@ -323,6 +331,21 @@ ExtractTextPlugin.prototype.apply = function(compiler) { callback(); }.bind(this)); }.bind(this)); + compilation.plugin("optimize-module-ids", function(modules){ + modules.forEach(function(module){ + var data = toRemoveModules[module.identifier()]; + if(data) { + var id = module.id; + var newModule = new NormalModule(module.request, module.userRequest, module.rawRequest, [], module.resource, module.parser); + newModule.id = id; + newModule._source = new OriginalSource('// removed by extract-text-webpack-plugin'); + data.chunks.forEach(function (chunk) { + chunk.removeModule(module); + chunk.addModule(newModule); + }); + } + }); + }); compilation.plugin("additional-assets", function(callback) { extractedChunks.forEach(function(extractedChunk) { if(extractedChunk.modules.length) { diff --git a/test/cases/multiple-entries-async/expected/0.js b/test/cases/multiple-entries-async/expected/0.js index b86d407c..7d0220d6 100644 --- a/test/cases/multiple-entries-async/expected/0.js +++ b/test/cases/multiple-entries-async/expected/0.js @@ -1,28 +1,28 @@ webpackJsonp([0],{ -/***/ 11: +/***/ 3: /***/ (function(module, exports, __webpack_require__) { -exports = module.exports = __webpack_require__(5)(false); -// imports - - -// module -exports.push([module.i, ".homepage {\n\tcolor: black;\n}\n", ""]); +__webpack_require__(8); -// exports +modules.export = function() { + return 'Route Homepage'; +}; /***/ }), -/***/ 2: +/***/ 8: /***/ (function(module, exports, __webpack_require__) { -__webpack_require__(11); +exports = module.exports = __webpack_require__(0)(false); +// imports -modules.export = function() { - return 'Route Homepage'; -}; + +// module +exports.push([module.i, ".homepage {\n\tcolor: black;\n}\n", ""]); + +// exports /***/ }) diff --git a/test/cases/multiple-entries-async/expected/1.js b/test/cases/multiple-entries-async/expected/1.js index 796d36f8..9700d7cc 100644 --- a/test/cases/multiple-entries-async/expected/1.js +++ b/test/cases/multiple-entries-async/expected/1.js @@ -1,9 +1,9 @@ webpackJsonp([1],{ -/***/ 1: +/***/ 2: /***/ (function(module, exports, __webpack_require__) { -__webpack_require__(10); +__webpack_require__(7); modules.export = function() { return 'Route Contact'; @@ -12,10 +12,10 @@ modules.export = function() { /***/ }), -/***/ 10: +/***/ 7: /***/ (function(module, exports, __webpack_require__) { -exports = module.exports = __webpack_require__(5)(false); +exports = module.exports = __webpack_require__(0)(false); // imports