Skip to content

Commit decacd3

Browse files
refactor: reduce count of require
1 parent e662b61 commit decacd3

File tree

11 files changed

+318
-210
lines changed

11 files changed

+318
-210
lines changed

src/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,22 @@ export default function loader(content, map, meta) {
5454
plugins.push(...getModulesPlugins(options, this));
5555
}
5656

57+
const exportType = options.onlyLocals ? 'locals' : 'full';
58+
5759
// Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
5860
const importPrefix = getImportPrefix(this, options.importLoaders);
5961

6062
plugins.push(icssParser());
6163

62-
if (options.import !== false) {
64+
if (options.import !== false && exportType === 'full') {
6365
plugins.push(
6466
importParser({
6567
filter: getFilter(options.import, this.resourcePath),
6668
})
6769
);
6870
}
6971

70-
if (options.url !== false) {
72+
if (options.url !== false && exportType === 'full') {
7173
plugins.push(
7274
urlParser({
7375
filter: getFilter(options.url, this.resourcePath, (value) =>
@@ -109,22 +111,20 @@ export default function loader(content, map, meta) {
109111
}
110112
}
111113

112-
const isNormalMode = !options.onlyLocals;
113-
114-
const apiCode = isNormalMode ? getApiCode(this, sourceMap) : '';
114+
const apiCode = exportType === 'full' ? getApiCode(this, sourceMap) : '';
115115
const importCode =
116-
isNormalMode && imports.length > 0
117-
? getImportCode(this, imports, { importPrefix })
116+
imports.length > 0
117+
? getImportCode(this, imports, { importPrefix, exportType })
118+
: '';
119+
const moduleCode =
120+
exportType === 'full'
121+
? getModuleCode(this, result, replacers, sourceMap)
118122
: '';
119-
const moduleCode = isNormalMode
120-
? getModuleCode(this, result, replacers, { sourceMap, importPrefix })
121-
: '';
122123
const exportCode =
123124
exports.length > 0
124125
? getExportCode(this, exports, replacers, {
125-
importPrefix,
126126
localsConvention: options.localsConvention,
127-
onlyLocals: options.onlyLocals,
127+
exportType,
128128
})
129129
: '';
130130

src/plugins/postcss-icss-parser.js

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,47 @@
11
import postcss from 'postcss';
22
import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
3-
import loaderUtils from 'loader-utils';
43

54
const pluginName = 'postcss-icss-parser';
65

7-
function hasImportMessage(messages, url) {
8-
return messages.find(
9-
(message) =>
10-
message.pluginName === pluginName &&
11-
message.type === 'import' &&
12-
message.value &&
13-
message.value.url === url &&
14-
message.value.media === ''
15-
);
16-
}
17-
186
export default postcss.plugin(
197
pluginName,
208
() =>
219
function process(css, result) {
2210
const importReplacements = Object.create(null);
2311
const { icssImports, icssExports } = extractICSS(css);
2412

25-
let index = 0;
13+
Object.keys(icssImports).forEach((url, importIndex) => {
14+
const tokens = Object.keys(icssImports[url]);
15+
16+
if (tokens.length === 0) {
17+
return;
18+
}
2619

27-
for (const importUrl of Object.keys(icssImports)) {
28-
const url = loaderUtils.parseString(importUrl);
20+
const importName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}___`;
21+
22+
result.messages.push({
23+
pluginName,
24+
type: 'import',
25+
value: { type: 'icss-import', name: importName, url, media: '' },
26+
});
2927

30-
for (const token of Object.keys(icssImports[importUrl])) {
31-
const name = `___CSS_LOADER_IMPORT___${index}___`;
28+
tokens.forEach((token, replacementIndex) => {
29+
const name = `___CSS_LOADER_ICSS_IMPORT_${importIndex}_REPLACEMENT_${replacementIndex}___`;
30+
const localName = icssImports[url][token];
3231

33-
index += 1;
3432
importReplacements[token] = name;
3533

3634
result.messages.push({
3735
pluginName,
3836
type: 'replacer',
39-
value: {
40-
type: 'icss-import',
41-
name,
42-
url,
43-
export: icssImports[importUrl][token],
44-
},
37+
value: { type: 'icss-import', name, importName, localName },
4538
});
46-
47-
if (!hasImportMessage(result.messages, url)) {
48-
result.messages.push({
49-
pluginName,
50-
type: 'import',
51-
value: { type: 'icss-import', url, media: '', name },
52-
});
53-
}
54-
}
55-
}
39+
});
40+
});
5641

5742
replaceSymbols(css, importReplacements);
5843

59-
for (const exportName of Object.keys(icssExports)) {
60-
const name = exportName;
44+
Object.keys(icssExports).forEach((name) => {
6145
const value = replaceValueSymbols(
6246
icssExports[name],
6347
importReplacements
@@ -68,6 +52,6 @@ export default postcss.plugin(
6852
type: 'export',
6953
value: { name, value },
7054
});
71-
}
55+
});
7256
}
7357
);

src/plugins/postcss-import-parser.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,14 @@ export default postcss.plugin(
9696
(value, other) => value.url === other.url && value.media === other.media
9797
);
9898

99-
paths.forEach((item) => {
99+
paths.forEach((item, index) => {
100+
const { url, media } = item;
101+
const name = `___CSS_LOADER_IMPORT___${index}___`;
102+
100103
result.messages.push({
101104
pluginName,
102105
type: 'import',
103-
value: { type: '@import', url: item.url, media: item.media },
106+
value: { type: '@import', name, url, media },
104107
});
105108
});
106109
}

src/utils.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ function getImportCode(loaderContext, imports, options) {
228228
return;
229229
}
230230

231-
items.push(`exports.i(require(${url}), ${media});`);
231+
items.push(`var ${item.name} = require(${url});`);
232+
233+
if (options.exportType === 'full') {
234+
items.push(`exports.i(${item.name}, ${media});`);
235+
}
232236
}
233237

234238
if (item.type === 'url') {
@@ -274,9 +278,9 @@ function getImportCode(loaderContext, imports, options) {
274278
return `// Imports\n${items.join('\n')}\n`;
275279
}
276280

277-
function getModuleCode(loaderContext, result, replacers, options) {
281+
function getModuleCode(loaderContext, result, replacers, sourceMap) {
278282
const { css, map } = result;
279-
const sourceMapValue = options.sourceMap && map ? `,${map}` : '';
283+
const sourceMapValue = sourceMap && map ? `,${map}` : '';
280284
let cssCode = JSON.stringify(css);
281285

282286
replacers.forEach((replacer) => {
@@ -287,17 +291,11 @@ function getModuleCode(loaderContext, result, replacers, options) {
287291
}
288292

289293
if (type === 'icss-import') {
290-
const url = stringifyRequest(
291-
loaderContext,
292-
options.importPrefix + urlToRequest(replacer.url)
293-
);
294-
const exportName = JSON.stringify(replacer.export);
294+
cssCode = cssCode.replace(new RegExp(name, 'g'), () => {
295+
const { importName, localName } = replacer;
295296

296-
// eslint-disable-next-line no-param-reassign
297-
cssCode = cssCode.replace(
298-
new RegExp(replacer.name, 'g'),
299-
`" + require(${url}).locals[${exportName}] + "`
300-
);
297+
return `" + ${importName}.locals[${JSON.stringify(localName)}] + "`;
298+
});
301299
}
302300
});
303301

@@ -356,22 +354,20 @@ function getExportCode(loaderContext, exports, replacers, options) {
356354
}
357355
});
358356

359-
const exportType = options.onlyLocals ? 'module.exports' : 'exports.locals';
360-
let exportCode = `// Exports\n${exportType} = {\n${items.join(',\n')}\n};`;
357+
let exportCode = `// Exports\n${
358+
options.exportType === 'locals' ? 'module.exports' : 'exports.locals'
359+
} = {\n${items.join(',\n')}\n};`;
361360

362361
replacers.forEach((replacer) => {
363-
const { type } = replacer;
364-
365-
if (type === 'icss-import') {
366-
const importUrl = options.importPrefix + urlToRequest(replacer.url);
362+
if (replacer.type === 'icss-import') {
363+
const { name, importName } = replacer;
367364

368-
exportCode = exportCode.replace(new RegExp(replacer.name, 'g'), () => {
369-
const url = stringifyRequest(loaderContext, importUrl);
370-
const importName = JSON.stringify(replacer.export);
365+
exportCode = exportCode.replace(new RegExp(name, 'g'), () => {
366+
const localName = JSON.stringify(replacer.localName);
371367

372-
return options.onlyLocals
373-
? `" + require(${url})[${importName}] + "`
374-
: `" + require(${url}).locals[${importName}] + "`;
368+
return options.exportType === 'locals'
369+
? `" + ${importName}[${localName}] + "`
370+
: `" + ${importName}.locals[${localName}] + "`;
375371
});
376372
}
377373
});

0 commit comments

Comments
 (0)