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

Commit 0d7c94a

Browse files
committed
fixed async loading (keep original style loader in async chunks)
1 parent 950d7ef commit 0d7c94a

File tree

5 files changed

+107
-29
lines changed

5 files changed

+107
-29
lines changed

index.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,24 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
278278
var wasExtracted = Array.isArray(meta.content);
279279
if(shouldExtract !== wasExtracted) {
280280
// don't remove
281-
module.loaders[0].options.remove = !shouldExtract;
282-
module[NS + "/extract"] = shouldExtract; // eslint-disable-line no-path-concat
283-
compilation.rebuildModule(module, function(err) {
281+
var newModule = new NormalModule(
282+
module.request,
283+
module.userRequest,
284+
module.rawRequest,
285+
module.loaders,
286+
module.resource,
287+
module.parser
288+
);
289+
newModule[NS + "/extract"] = shouldExtract; // eslint-disable-line no-path-concat
290+
compilation.buildModule(newModule, false, newModule, null, function(err) {
284291
if(err) {
285292
compilation.errors.push(err);
286293
return callback();
287294
}
288-
meta = module[NS];
295+
meta = newModule[NS];
289296
// Error out if content is not an array and is not null
290297
if(!Array.isArray(meta.content) && meta.content != null) {
291-
err = new Error(module.identifier() + " doesn't export content");
298+
err = new Error(newModule.identifier() + " doesn't export content");
292299
compilation.errors.push(err);
293300
return callback();
294301
}
@@ -299,8 +306,13 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
299306
if(toRemoveModules[ident]) {
300307
toRemoveModules[ident].chunks.push(chunk)
301308
} else {
302-
toRemoveModules[ident] = { module: module, chunks: [chunk] };
309+
toRemoveModules[ident] = {
310+
module: newModule,
311+
moduleToRemove: module,
312+
chunks: [chunk]
313+
};
303314
}
315+
304316
}
305317
callback();
306318
});
@@ -345,11 +357,13 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
345357
module.parser
346358
);
347359
newModule.id = id;
348-
var localsMatch = module._source._value.match(/exports\.locals([\s\S]*};)/);
349-
localsMatch = localsMatch ? 'module.exports = '+localsMatch[0] : null;
350-
newModule._source = new OriginalSource('// removed by extract-text-webpack-plugin\n'+(localsMatch||''));
360+
newModule._source = data.module._source;
351361
data.chunks.forEach(function (chunk) {
352-
chunk.removeModule(module);
362+
chunk.removeModule(data.moduleToRemove);
363+
var deps = data.moduleToRemove.dependencies.slice();
364+
deps.forEach(d => {
365+
chunk.removeModule(d.module);
366+
});
353367
chunk.addModule(newModule);
354368
});
355369
}
Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,62 @@
11
webpackJsonp([0],{
22

3-
/***/ 3:
3+
/***/ 11:
44
/***/ (function(module, exports, __webpack_require__) {
55

6-
__webpack_require__(8);
7-
8-
modules.export = function() {
9-
return 'Route Homepage';
10-
};
11-
6+
// style-loader: Adds some css to the DOM by adding a <style> tag
7+
8+
// load the styles
9+
var content = __webpack_require__(13);
10+
if(typeof content === 'string') content = [[module.i, content, '']];
11+
// Prepare cssTransformation
12+
var transform;
13+
14+
var options = {}
15+
options.transform = transform
16+
// add the styles to the DOM
17+
var update = __webpack_require__(1)(content, options);
18+
if(content.locals) module.exports = content.locals;
19+
// Hot Module Replacement
20+
if(false) {
21+
// When the styles change, update the <style> tags
22+
if(!content.locals) {
23+
module.hot.accept("!!../../../../../node_modules/css-loader/index.js??ref--0-2!./styles.css", function() {
24+
var newContent = require("!!../../../../../node_modules/css-loader/index.js??ref--0-2!./styles.css");
25+
if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];
26+
update(newContent);
27+
});
28+
}
29+
// When the module is disposed, remove the <style> tags
30+
module.hot.dispose(function() { update(); });
31+
}
1232

1333
/***/ }),
1434

15-
/***/ 8:
35+
/***/ 13:
1636
/***/ (function(module, exports, __webpack_require__) {
1737

1838
exports = module.exports = __webpack_require__(0)(false);
1939
// imports
2040

2141

2242
// module
23-
exports.push([module.i, ".homepage {\n\tcolor: black;\n}\n", ""]);
43+
exports.push([module.i, ".homepage {\n\tcolor: yellow;\n}\n", ""]);
2444

2545
// exports
2646

2747

48+
/***/ }),
49+
50+
/***/ 4:
51+
/***/ (function(module, exports, __webpack_require__) {
52+
53+
__webpack_require__(11);
54+
55+
modules.export = function() {
56+
return 'Route Homepage';
57+
};
58+
59+
2860
/***/ })
2961

3062
});
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
webpackJsonp([1],{
22

3-
/***/ 2:
3+
/***/ 10:
44
/***/ (function(module, exports, __webpack_require__) {
55

6-
__webpack_require__(7);
7-
8-
modules.export = function() {
9-
return 'Route Contact';
10-
};
11-
6+
// style-loader: Adds some css to the DOM by adding a <style> tag
7+
8+
// load the styles
9+
var content = __webpack_require__(12);
10+
if(typeof content === 'string') content = [[module.i, content, '']];
11+
// Prepare cssTransformation
12+
var transform;
13+
14+
var options = {}
15+
options.transform = transform
16+
// add the styles to the DOM
17+
var update = __webpack_require__(1)(content, options);
18+
if(content.locals) module.exports = content.locals;
19+
// Hot Module Replacement
20+
if(false) {
21+
// When the styles change, update the <style> tags
22+
if(!content.locals) {
23+
module.hot.accept("!!../../../../../node_modules/css-loader/index.js??ref--0-2!./styles.css", function() {
24+
var newContent = require("!!../../../../../node_modules/css-loader/index.js??ref--0-2!./styles.css");
25+
if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];
26+
update(newContent);
27+
});
28+
}
29+
// When the module is disposed, remove the <style> tags
30+
module.hot.dispose(function() { update(); });
31+
}
1232

1333
/***/ }),
1434

15-
/***/ 7:
35+
/***/ 12:
1636
/***/ (function(module, exports, __webpack_require__) {
1737

1838
exports = module.exports = __webpack_require__(0)(false);
@@ -25,6 +45,18 @@ exports.push([module.i, ".contact {\n\tcolor: black;\n}\n", ""]);
2545
// exports
2646

2747

48+
/***/ }),
49+
50+
/***/ 3:
51+
/***/ (function(module, exports, __webpack_require__) {
52+
53+
__webpack_require__(10);
54+
55+
modules.export = function() {
56+
return 'Route Contact';
57+
};
58+
59+
2860
/***/ })
2961

3062
});

test/cases/multiple-entries-async/expected/homepage.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ body {
22
background: red;
33
}
44
.homepage {
5-
color: black;
5+
color: yellow;
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.homepage {
2-
color: black;
2+
color: yellow;
33
}

0 commit comments

Comments
 (0)