Skip to content

Commit 23bc1e9

Browse files
refactor: code
1 parent 0016e49 commit 23bc1e9

10 files changed

+1055
-507
lines changed

src/utils.js

+62-53
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ function getImportCode(
221221
require.resolve('./runtime/api')
222222
)});`
223223
);
224-
codeItems.push(
225-
`exports = module.exports = ___CSS_LOADER_API_IMPORT___(${sourceMap});`
226-
);
224+
codeItems.push(`exports = ___CSS_LOADER_API_IMPORT___(${sourceMap});`);
227225
}
228226

229227
imports.forEach((item) => {
@@ -384,71 +382,82 @@ function getExportCode(
384382
replacers,
385383
localsConvention
386384
) {
387-
if (exports.length === 0) {
388-
return '';
389-
}
385+
const exportItems = [];
386+
let exportLocalsCode;
390387

391-
const items = [];
392-
const addExportedItem = (name, value) => {
393-
items.push(`\t${JSON.stringify(name)}: ${JSON.stringify(value)}`);
394-
};
388+
if (exports.length > 0) {
389+
const exportLocals = [];
390+
const addExportedLocal = (name, value) => {
391+
exportLocals.push(`\t${JSON.stringify(name)}: ${JSON.stringify(value)}`);
392+
};
395393

396-
exports.forEach((item) => {
397-
const { name, value } = item;
394+
exports.forEach((item) => {
395+
const { name, value } = item;
398396

399-
switch (localsConvention) {
400-
case 'camelCase': {
401-
addExportedItem(name, value);
397+
switch (localsConvention) {
398+
case 'camelCase': {
399+
addExportedLocal(name, value);
402400

403-
const modifiedName = camelCase(name);
401+
const modifiedName = camelCase(name);
404402

405-
if (modifiedName !== name) {
406-
addExportedItem(modifiedName, value);
403+
if (modifiedName !== name) {
404+
addExportedLocal(modifiedName, value);
405+
}
406+
break;
407407
}
408-
break;
409-
}
410-
case 'camelCaseOnly': {
411-
addExportedItem(camelCase(name), value);
412-
break;
413-
}
414-
case 'dashes': {
415-
addExportedItem(name, value);
408+
case 'camelCaseOnly': {
409+
addExportedLocal(camelCase(name), value);
410+
break;
411+
}
412+
case 'dashes': {
413+
addExportedLocal(name, value);
416414

417-
const modifiedName = dashesCamelCase(name);
415+
const modifiedName = dashesCamelCase(name);
418416

419-
if (modifiedName !== name) {
420-
addExportedItem(modifiedName, value);
417+
if (modifiedName !== name) {
418+
addExportedLocal(modifiedName, value);
419+
}
420+
break;
421421
}
422-
break;
423-
}
424-
case 'dashesOnly': {
425-
addExportedItem(dashesCamelCase(name), value);
426-
break;
422+
case 'dashesOnly': {
423+
addExportedLocal(dashesCamelCase(name), value);
424+
break;
425+
}
426+
case 'asIs':
427+
default:
428+
addExportedLocal(name, value);
429+
break;
427430
}
428-
case 'asIs':
429-
default:
430-
addExportedItem(name, value);
431-
break;
432-
}
433-
});
431+
});
434432

435-
let exportCode = `// Exports\n${
436-
exportType === 'locals' ? 'module.exports' : 'exports.locals'
437-
} = {\n${items.join(',\n')}\n};`;
433+
exportLocalsCode = exportLocals.join(',\n');
438434

439-
replacers.forEach((replacer) => {
440-
if (replacer.type === 'icss-import') {
441-
const { replacementName, importName, localName } = replacer;
435+
replacers.forEach((replacer) => {
436+
if (replacer.type === 'icss-import') {
437+
const { replacementName, importName, localName } = replacer;
442438

443-
exportCode = exportCode.replace(new RegExp(replacementName, 'g'), () =>
444-
exportType === 'locals'
445-
? `" + ${importName}[${JSON.stringify(localName)}] + "`
446-
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
447-
);
439+
exportLocalsCode = exportLocalsCode.replace(
440+
new RegExp(replacementName, 'g'),
441+
() =>
442+
exportType === 'locals'
443+
? `" + ${importName}[${JSON.stringify(localName)}] + "`
444+
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
445+
);
446+
}
447+
});
448+
}
449+
450+
if (exportType === 'locals') {
451+
exportItems.push(`module.exports = {\n${exportLocalsCode}\n};`);
452+
} else {
453+
if (exportLocalsCode) {
454+
exportItems.push(`exports.locals = {\n${exportLocalsCode}\n};`);
448455
}
449-
});
450456

451-
return exportCode;
457+
exportItems.push('module.exports = exports;');
458+
}
459+
460+
return `// Exports\n${exportItems.join('\n')}\n`;
452461
}
453462

454463
export {

test/__snapshots__/icss.test.js.snap

+38-18
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ exports[`ICSS show work with the case "duplicate-export": errors 1`] = `Array []
55
exports[`ICSS show work with the case "duplicate-export": module 1`] = `
66
"// Imports
77
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
8-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
8+
exports = ___CSS_LOADER_API_IMPORT___(false);
99
// Module
1010
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
1111
// Exports
1212
exports.locals = {
1313
\\"_test\\": \\"_right_value\\"
14-
};"
14+
};
15+
module.exports = exports;
16+
"
1517
`;
1618

1719
exports[`ICSS show work with the case "duplicate-export": result 1`] = `
@@ -32,13 +34,15 @@ exports[`ICSS show work with the case "duplicate-export-in-multiple-export": err
3234
exports[`ICSS show work with the case "duplicate-export-in-multiple-export": module 1`] = `
3335
"// Imports
3436
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
35-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
37+
exports = ___CSS_LOADER_API_IMPORT___(false);
3638
// Module
3739
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
3840
// Exports
3941
exports.locals = {
4042
\\"_test\\": \\"_right_value\\"
41-
};"
43+
};
44+
module.exports = exports;
45+
"
4246
`;
4347

4448
exports[`ICSS show work with the case "duplicate-export-in-multiple-export": result 1`] = `
@@ -59,9 +63,11 @@ exports[`ICSS show work with the case "empty-export": errors 1`] = `Array []`;
5963
exports[`ICSS show work with the case "empty-export": module 1`] = `
6064
"// Imports
6165
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
62-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
66+
exports = ___CSS_LOADER_API_IMPORT___(false);
6367
// Module
6468
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
69+
// Exports
70+
module.exports = exports;
6571
"
6672
`;
6773

@@ -83,9 +89,11 @@ exports[`ICSS show work with the case "empty-import": errors 1`] = `Array []`;
8389
exports[`ICSS show work with the case "empty-import": module 1`] = `
8490
"// Imports
8591
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
86-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
92+
exports = ___CSS_LOADER_API_IMPORT___(false);
8793
// Module
8894
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
95+
// Exports
96+
module.exports = exports;
8997
"
9098
`;
9199

@@ -107,13 +115,15 @@ exports[`ICSS show work with the case "export": errors 1`] = `Array []`;
107115
exports[`ICSS show work with the case "export": module 1`] = `
108116
"// Imports
109117
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
110-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
118+
exports = ___CSS_LOADER_API_IMPORT___(false);
111119
// Module
112120
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
113121
// Exports
114122
exports.locals = {
115123
\\"_test\\": \\"_test\\"
116-
};"
124+
};
125+
module.exports = exports;
126+
"
117127
`;
118128

119129
exports[`ICSS show work with the case "export": result 1`] = `
@@ -134,14 +144,16 @@ exports[`ICSS show work with the case "export-reserved-keywords": errors 1`] = `
134144
exports[`ICSS show work with the case "export-reserved-keywords": module 1`] = `
135145
"// Imports
136146
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
137-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
147+
exports = ___CSS_LOADER_API_IMPORT___(false);
138148
// Module
139149
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
140150
// Exports
141151
exports.locals = {
142152
\\"constructor\\": \\"constructor\\",
143153
\\"toString\\": \\"toString\\"
144-
};"
154+
};
155+
module.exports = exports;
156+
"
145157
`;
146158

147159
exports[`ICSS show work with the case "export-reserved-keywords": result 1`] = `
@@ -163,14 +175,16 @@ exports[`ICSS show work with the case "import": module 1`] = `
163175
"// Imports
164176
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
165177
var ___CSS_LOADER_ICSS_IMPORT_0___ = require(\\"-!../../../../../src/index.js??[ident]!./vars.css\\");
166-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
178+
exports = ___CSS_LOADER_API_IMPORT___(false);
167179
exports.i(___CSS_LOADER_ICSS_IMPORT_0___);
168180
// Module
169181
exports.push([module.id, \\".className {\\\\n color: \\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\";\\\\n}\\\\n\\", \\"\\"]);
170182
// Exports
171183
exports.locals = {
172184
\\"primary-color\\": \\"\\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\"\\"
173-
};"
185+
};
186+
module.exports = exports;
187+
"
174188
`;
175189

176190
exports[`ICSS show work with the case "import": result 1`] = `
@@ -200,15 +214,17 @@ exports[`ICSS show work with the case "import-reserved-keywords": module 1`] = `
200214
"// Imports
201215
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
202216
var ___CSS_LOADER_ICSS_IMPORT_0___ = require(\\"-!../../../../../src/index.js??[ident]!./vars.css\\");
203-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
217+
exports = ___CSS_LOADER_API_IMPORT___(false);
204218
exports.i(___CSS_LOADER_ICSS_IMPORT_0___);
205219
// Module
206220
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\\", \\"\\"]);
207221
// Exports
208222
exports.locals = {
209223
\\"primary-color\\": \\"\\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\"\\",
210224
\\"secondary-color\\": \\"\\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"secondary-color\\"] + \\"\\"
211-
};"
225+
};
226+
module.exports = exports;
227+
"
212228
`;
213229

214230
exports[`ICSS show work with the case "import-reserved-keywords": result 1`] = `
@@ -238,14 +254,16 @@ exports[`ICSS show work with the case "multiple-export": errors 1`] = `Array []`
238254
exports[`ICSS show work with the case "multiple-export": module 1`] = `
239255
"// Imports
240256
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
241-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
257+
exports = ___CSS_LOADER_API_IMPORT___(false);
242258
// Module
243259
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
244260
// Exports
245261
exports.locals = {
246262
\\"_test\\": \\"_test\\",
247263
\\"_foo\\": \\"_bar\\"
248-
};"
264+
};
265+
module.exports = exports;
266+
"
249267
`;
250268

251269
exports[`ICSS show work with the case "multiple-export": result 1`] = `
@@ -266,7 +284,7 @@ exports[`ICSS show work with the case "multiple-keys-values-in-export": errors 1
266284
exports[`ICSS show work with the case "multiple-keys-values-in-export": module 1`] = `
267285
"// Imports
268286
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
269-
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
287+
exports = ___CSS_LOADER_API_IMPORT___(false);
270288
// Module
271289
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
272290
// Exports
@@ -276,7 +294,9 @@ exports.locals = {
276294
\\"_test2\\": \\"'string'\\",
277295
\\"_test3\\": \\"1px 2px 3px\\",
278296
\\"_test4\\": \\"1px 2px 3px, 1px 2px 3px\\"
279-
};"
297+
};
298+
module.exports = exports;
299+
"
280300
`;
281301

282302
exports[`ICSS show work with the case "multiple-keys-values-in-export": result 1`] = `

0 commit comments

Comments
 (0)