Skip to content

Commit 63d347d

Browse files
committed
feat(interop): extract-text-webpack-plugin with allChunks: true interoperability
1 parent a3cb487 commit 63d347d

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

lib/utils.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const webpackPkg = require('webpack/package.json');
33
// eslint-disable-next-line import/no-extraneous-dependencies
44
const ruleMatcher = require('webpack/lib/ModuleFilenameHelpers').matchObject;
55
const { parseQuery } = require('loader-utils');
6-
6+
const escapeRegExpSpecialChars = require('escape-string-regexp');
77
const defaults = require('./config');
88

99
const loaderDefaults = defaults.loader;
@@ -51,10 +51,19 @@ module.exports.stringifySymbol = stringifySymbol;
5151
* @return {string}
5252
*/
5353
function replaceSpritePlaceholder(content, replacements) {
54-
const regexp = defaults.SPRITE_PLACEHOLDER_PATTERN;
55-
return content.replace(regexp, (match, p1) => {
56-
return p1 ? replacements[p1] : match;
57-
});
54+
let result = content;
55+
Object.keys(replacements)
56+
.map((subj) => {
57+
return {
58+
subj,
59+
re: new RegExp(escapeRegExpSpecialChars(subj), 'g')
60+
};
61+
})
62+
.forEach(({ subj, re }) => {
63+
result = result.replace(re, replacements[subj]);
64+
});
65+
66+
return result;
5867
}
5968

6069
module.exports.replaceSpritePlaceholder = replaceSpritePlaceholder;
@@ -79,11 +88,12 @@ function replaceInModuleSource(module, replacements) {
7988
module.exports.replaceInModuleSource = replaceInModuleSource;
8089

8190
/**
91+
* Because of extract-text-webpack-plugin interop returns just absolute path to filepath
8292
* @param {string} filepath
8393
* @return {string}
8494
*/
8595
function generateSpritePlaceholder(filepath) {
86-
return `{{sprite-filename|${filepath}}}`;
96+
return filepath;
8797
}
8898

8999
module.exports.generateSpritePlaceholder = generateSpritePlaceholder;
@@ -252,14 +262,35 @@ function getModuleChunk(module, modules) {
252262
if (Array.isArray(chunks) && chunks.length > 0) {
253263
return chunks[chunks.length - 1];
254264
} else if (issuer) {
255-
return getModuleChunk(issuer);
265+
return getModuleChunk(issuer, modules);
256266
}
257267

258268
return null;
259269
}
260270

261271
module.exports.getModuleChunk = getModuleChunk;
262272

273+
/**
274+
* extract-text-webpack-plugin compatible
275+
* @param {Compilation} compilation
276+
* @return {NormalModule[]}
277+
*/
278+
function getAllModules(compilation) {
279+
const allModules = compilation.modules;
280+
let modules = allModules.filter(isModuleShouldBeExtracted);
281+
282+
// Search in child compilations
283+
if (compilation.children.length > 0) {
284+
modules = compilation.children.reduce((acc, childCompilation) => {
285+
return acc.concat(childCompilation.modules);
286+
}, modules);
287+
}
288+
289+
return modules;
290+
}
291+
292+
module.exports.getAllModules = getAllModules;
293+
263294
/**
264295
* // TODO implement [chunkhash]
265296
* @param {string} filename
@@ -282,7 +313,7 @@ module.exports.interpolateSpriteFilename = interpolateSpriteFilename;
282313
*/
283314
function aggregate(symbols, compilation) {
284315
const { compiler } = compilation;
285-
const allModules = compilation.modules;
316+
const allModules = getAllModules(compilation);
286317
const modules = allModules.filter(isModuleShouldBeExtracted);
287318
const publicPath = compiler.options.output.publicPath || '';
288319
const rules = getLoadersRules(compiler);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"bluebird": "^3.5.0",
3131
"deepmerge": "1.3.2",
3232
"domready": "1.0.8",
33+
"escape-string-regexp": "1.0.5",
3334
"loader-utils": "^1.1.0",
3435
"svg-baker": "^1.2.6",
3536
"svg-baker-runtime": "^1.2.1",

0 commit comments

Comments
 (0)