Skip to content

Commit 6a3f57f

Browse files
fix: do not duplicate css on composes
1 parent 7c9f47b commit 6a3f57f

File tree

6 files changed

+189
-84
lines changed

6 files changed

+189
-84
lines changed

src/runtime/api.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,33 @@ module.exports = function(useSourceMap) {
2222

2323
// import a list of modules into the list
2424
// eslint-disable-next-line func-names
25-
list.i = function(modules, mediaQuery) {
25+
list.i = function(modules, mediaQuery, dedupe) {
2626
if (typeof modules === 'string') {
2727
// eslint-disable-next-line no-param-reassign
2828
modules = [[null, modules, '']];
2929
}
3030

31+
const alreadyImportedModules = {};
32+
33+
if (dedupe) {
34+
for (let i = 0; i < this.length; i++) {
35+
// eslint-disable-next-line prefer-destructuring
36+
const id = this[i][0];
37+
38+
if (id != null) {
39+
alreadyImportedModules[id] = true;
40+
}
41+
}
42+
}
43+
3144
for (let i = 0; i < modules.length; i++) {
3245
const item = [].concat(modules[i]);
3346

47+
if (dedupe && alreadyImportedModules[item[0]]) {
48+
// eslint-disable-next-line no-continue
49+
continue;
50+
}
51+
3452
if (mediaQuery) {
3553
if (!item[2]) {
3654
item[2] = mediaQuery;

src/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ function getImportCode(
329329
case 'icss-import':
330330
{
331331
const { importName, url, media } = item;
332-
const preparedMedia = media ? `, ${JSON.stringify(media)}` : '';
332+
const preparedMedia = media ? `, ${JSON.stringify(media)}` : ', ""';
333333

334334
if (!importPrefix) {
335335
importPrefix = getImportPrefix(loaderContext, importLoaders);
@@ -348,7 +348,7 @@ function getImportCode(
348348
);
349349

350350
if (exportType === 'full') {
351-
codeItems.push(`exports.i(${importName}${preparedMedia});`);
351+
codeItems.push(`exports.i(${importName}${preparedMedia}, true);`);
352352
}
353353
}
354354
break;

test/__snapshots__/icss.test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ exports[`ICSS show work with the case "import": module 1`] = `
176176
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
177177
var ___CSS_LOADER_ICSS_IMPORT_0___ = require(\\"-!../../../../../src/index.js??[ident]!./vars.css\\");
178178
exports = ___CSS_LOADER_API_IMPORT___(false);
179-
exports.i(___CSS_LOADER_ICSS_IMPORT_0___);
179+
exports.i(___CSS_LOADER_ICSS_IMPORT_0___, \\"\\", true);
180180
// Module
181181
exports.push([module.id, \\".className {\\\\n color: \\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\";\\\\n}\\\\n\\", \\"\\"]);
182182
// Exports
@@ -215,7 +215,7 @@ exports[`ICSS show work with the case "import-reserved-keywords": module 1`] = `
215215
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
216216
var ___CSS_LOADER_ICSS_IMPORT_0___ = require(\\"-!../../../../../src/index.js??[ident]!./vars.css\\");
217217
exports = ___CSS_LOADER_API_IMPORT___(false);
218-
exports.i(___CSS_LOADER_ICSS_IMPORT_0___);
218+
exports.i(___CSS_LOADER_ICSS_IMPORT_0___, \\"\\", true);
219219
// Module
220220
exports.push([module.id, \\".className {\\\\n color: \\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\";\\\\n display: \\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"secondary-color\\"] + \\";\\\\n}\\\\n\\", \\"\\"]);
221221
// Exports

0 commit comments

Comments
 (0)