Skip to content

Commit 50570f6

Browse files
committed
correctly replace the template for async chunks
1 parent 860ee49 commit 50570f6

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

index.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ ExtractTextPlugin.prototype.loader = function(options) {
186186
return ExtractTextPlugin.loader(mergeOptions({id: this.id}, options));
187187
};
188188

189+
189190
ExtractTextPlugin.prototype.extract = function(options) {
190191
if(arguments.length > 1) {
191192
throw new Error("Breaking change: extract now only takes a single argument. Either an options " +
@@ -265,6 +266,11 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
265266
filename(getPath(module.source(), chunk)) :
266267
getPath(module.source(), chunk)(filename);
267268

269+
var replaceTemplate = (module, chunk, toModifyModule) => {
270+
var file = getFile(module, chunk);
271+
toModifyModule._source._value = toModifyModule._source._value.replace("%%extracted-file%%", file);
272+
};
273+
268274
compilation.plugin("optimize-tree", function(chunks, modules, callback) {
269275
extractedChunks = chunks.map(function() {
270276
return new Chunk();
@@ -360,6 +366,19 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
360366
}.bind(this));
361367
}.bind(this));
362368
compilation.plugin("optimize-module-ids", function(modules){
369+
370+
// HMR: inject file name into corresponding javascript modules in order to trigger
371+
// appropriate hot module reloading of CSS
372+
extractedChunks.forEach(function(extractedChunk) {
373+
extractedChunk.modules.forEach(function (module) {
374+
if(module.__fileInjected) {
375+
return;
376+
}
377+
module.__fileInjected = true;
378+
var originalModule = module.getOriginalModule();
379+
replaceTemplate(module, extractedChunk, originalModule);
380+
});
381+
});
363382
modules.forEach(function (module) {
364383
var data = toRemoveModules[module.identifier()];
365384
if (data) {
@@ -379,24 +398,12 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
379398
data.moduleToRemove.dependencies.forEach(d => {
380399
chunk.removeModule(d.module);
381400
});
401+
replaceTemplate(newModule, chunk, newModule);
382402
chunk.addModule(newModule);
383403
});
384404
}
385405
});
386406

387-
// HMR: inject file name into corresponding javascript modules in order to trigger
388-
// appropriate hot module reloading of CSS
389-
extractedChunks.forEach(function(extractedChunk) {
390-
extractedChunk.modules.forEach(function (module) {
391-
if(module.__fileInjected) {
392-
return;
393-
}
394-
module.__fileInjected = true;
395-
var file = getFile(module, extractedChunk);
396-
var originalModule = module.getOriginalModule();
397-
originalModule._source._value = originalModule._source._value.replace("%%extracted-file%%", file);
398-
});
399-
})
400407
});
401408

402409
compilation.plugin("additional-assets", function(callback) {

0 commit comments

Comments
 (0)