Skip to content

Commit e8ab768

Browse files
refactor: code
1 parent 11a8a79 commit e8ab768

File tree

6 files changed

+79
-98
lines changed

6 files changed

+79
-98
lines changed

src/index.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,9 @@ export default function loader(content, map, meta) {
3737
const callback = this.async();
3838
const sourceMap = options.sourceMap || false;
3939

40-
if (sourceMap && map) {
41-
// eslint-disable-next-line no-param-reassign
42-
map = normalizeSourceMap(map);
43-
} else {
44-
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
45-
// eslint-disable-next-line no-param-reassign
46-
map = null;
47-
}
40+
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
41+
// eslint-disable-next-line no-param-reassign
42+
map = sourceMap && map ? normalizeSourceMap(map) : null;
4843

4944
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
5045
if (meta) {
@@ -140,7 +135,7 @@ export default function loader(content, map, meta) {
140135
const importCode = getImportCode(importItems, onlyLocals);
141136
const moduleCode = getModuleCode(result, sourceMap, onlyLocals);
142137
const exportCode = getExportCode(exportItems, onlyLocals);
143-
const apiCode = getApiCode(this, sourceMap, importItems, moduleCode);
138+
const apiCode = getApiCode(this, sourceMap, onlyLocals);
144139

145140
return callback(
146141
null,

src/plugins/postcss-icss-parser.js

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import postcss from 'postcss';
22
import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
33
import loaderUtils from 'loader-utils';
4-
import cc from 'camelcase';
54

6-
import { getImportItemCode } from '../utils';
5+
import { getExportItemCode, getImportItemCode } from '../utils';
76

87
const pluginName = 'postcss-icss-parser';
98

@@ -17,56 +16,6 @@ function hasImportMessage(messages, url) {
1716
);
1817
}
1918

20-
function camelCase(str) {
21-
return cc(str);
22-
}
23-
24-
function dashesCamelCase(str) {
25-
return str.replace(/-+(\w)/g, (match, firstLetter) =>
26-
firstLetter.toUpperCase()
27-
);
28-
}
29-
30-
function getExportItem(key, value, localsStyle) {
31-
let targetKey;
32-
const items = [];
33-
34-
function addEntry(k) {
35-
items.push(`\t${JSON.stringify(k)}: ${JSON.stringify(value)}`);
36-
}
37-
38-
switch (localsStyle) {
39-
case 'camelCase':
40-
addEntry(key);
41-
targetKey = camelCase(key);
42-
43-
if (targetKey !== key) {
44-
addEntry(targetKey);
45-
}
46-
break;
47-
case 'camelCaseOnly':
48-
addEntry(camelCase(key));
49-
break;
50-
case 'dashes':
51-
addEntry(key);
52-
targetKey = dashesCamelCase(key);
53-
54-
if (targetKey !== key) {
55-
addEntry(targetKey);
56-
}
57-
break;
58-
case 'dashesOnly':
59-
addEntry(dashesCamelCase(key));
60-
break;
61-
case 'asIs':
62-
default:
63-
addEntry(key);
64-
break;
65-
}
66-
67-
return items;
68-
}
69-
7019
export default postcss.plugin(
7120
pluginName,
7221
(options = {}) =>
@@ -118,9 +67,7 @@ export default postcss.plugin(
11867

11968
result.messages.push({
12069
pluginName,
121-
export: getExportItem(name, value, options.exportLocalsStyle).join(
122-
',\n'
123-
),
70+
export: getExportItemCode(name, value, options.exportLocalsStyle),
12471
type: 'export',
12572
item: { name, value },
12673
});

src/plugins/postcss-import-parser.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import postcss from 'postcss';
22
import valueParser from 'postcss-value-parser';
33

4-
import { getImportItemCode } from '../utils';
4+
import { uniqWith, getImportItemCode } from '../utils';
55

66
const pluginName = 'postcss-import-parser';
77

@@ -86,22 +86,15 @@ function walkAtRules(css, result, filter) {
8686
return items;
8787
}
8888

89-
function uniq(array) {
90-
return array.reduce(
91-
(acc, d) =>
92-
!acc.find((el) => el.url === d.url && el.media === d.media)
93-
? [...acc, d]
94-
: acc,
95-
[]
96-
);
97-
}
98-
9989
export default postcss.plugin(
10090
pluginName,
10191
(options = {}) =>
10292
function process(css, result) {
10393
const traversed = walkAtRules(css, result, options.filter);
104-
const paths = uniq(traversed);
94+
const paths = uniqWith(
95+
traversed,
96+
(value, other) => value.url === other.url && value.media === other.media
97+
);
10598

10699
paths.forEach((item) => {
107100
result.messages.push({

src/plugins/postcss-url-parser.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import postcss from 'postcss';
22
import valueParser from 'postcss-value-parser';
33

4-
import { getUrlHelperCode, getUrlItemCode } from '../utils';
4+
import { uniqWith, flatten, getUrlHelperCode, getUrlItemCode } from '../utils';
55

66
const pluginName = 'postcss-url-parser';
77

@@ -104,29 +104,15 @@ function walkDeclsWithUrl(css, result, filter) {
104104
return items;
105105
}
106106

107-
function uniqWith(array, comparator) {
108-
return array.reduce(
109-
(acc, d) => (!acc.some((item) => comparator(d, item)) ? [...acc, d] : acc),
110-
[]
111-
);
112-
}
113-
114-
function flatten(array) {
115-
return array.reduce((a, b) => a.concat(b), []);
116-
}
117-
118-
function isEqual(value, other) {
119-
return value.url === other.url && value.needQuotes === other.needQuotes;
120-
}
121-
122107
export default postcss.plugin(
123108
pluginName,
124109
(options = {}) =>
125110
function process(css, result) {
126111
const traversed = walkDeclsWithUrl(css, result, options.filter);
127112
const paths = uniqWith(
128113
flatten(traversed.map((item) => item.urls)),
129-
isEqual
114+
(value, other) =>
115+
value.url === other.url && value.needQuotes === other.needQuotes
130116
);
131117

132118
if (paths.length === 0) {

src/utils.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ import modulesValues from 'postcss-modules-values';
1515
import localByDefault from 'postcss-modules-local-by-default';
1616
import extractImports from 'postcss-modules-extract-imports';
1717
import modulesScope from 'postcss-modules-scope';
18+
import camelCase from 'camelcase';
19+
20+
function uniqWith(array, comparator) {
21+
return array.reduce(
22+
(acc, d) => (!acc.some((item) => comparator(d, item)) ? [...acc, d] : acc),
23+
[]
24+
);
25+
}
26+
27+
function flatten(array) {
28+
return array.reduce((a, b) => a.concat(b), []);
29+
}
30+
31+
function dashesCamelCase(str) {
32+
return str.replace(/-+(\w)/g, (match, firstLetter) =>
33+
firstLetter.toUpperCase()
34+
);
35+
}
1836

1937
function getImportPrefix(loaderContext, importLoaders) {
2038
if (importLoaders === false) {
@@ -206,9 +224,8 @@ function getUrlItemCode(item, loaderContext) {
206224
)})${hash ? ` + ${hash}` : ''}${needQuotes ? ', true' : ''});`;
207225
}
208226

209-
function getApiCode(loaderContext, sourceMap, importItems, moduleCode) {
210-
// No imports and no module code
211-
if (importItems.length === 0 && moduleCode.length === 0) {
227+
function getApiCode(loaderContext, sourceMap, onlyLocals) {
228+
if (onlyLocals) {
212229
return '';
213230
}
214231

@@ -236,6 +253,46 @@ function getModuleCode(result, sourceMap, onlyLocals) {
236253
)}, ""${sourceMap && result.map ? `,${result.map}` : ''}]);\n`;
237254
}
238255

256+
function getExportItemCode(key, value, localsStyle) {
257+
let targetKey;
258+
const items = [];
259+
260+
function addEntry(k) {
261+
items.push(`\t${JSON.stringify(k)}: ${JSON.stringify(value)}`);
262+
}
263+
264+
switch (localsStyle) {
265+
case 'camelCase':
266+
addEntry(key);
267+
targetKey = camelCase(key);
268+
269+
if (targetKey !== key) {
270+
addEntry(targetKey);
271+
}
272+
break;
273+
case 'camelCaseOnly':
274+
addEntry(camelCase(key));
275+
break;
276+
case 'dashes':
277+
addEntry(key);
278+
targetKey = dashesCamelCase(key);
279+
280+
if (targetKey !== key) {
281+
addEntry(targetKey);
282+
}
283+
break;
284+
case 'dashesOnly':
285+
addEntry(dashesCamelCase(key));
286+
break;
287+
case 'asIs':
288+
default:
289+
addEntry(key);
290+
break;
291+
}
292+
293+
return items.join(',\n');
294+
}
295+
239296
function getExportCode(exportItems, onlyLocals) {
240297
if (exportItems.length === 0) {
241298
return '';
@@ -317,6 +374,9 @@ function prepareCode(file, messages, loaderContext, importPrefix, onlyLocals) {
317374
}
318375

319376
export {
377+
uniqWith,
378+
flatten,
379+
dashesCamelCase,
320380
getImportPrefix,
321381
getLocalIdent,
322382
getFilter,
@@ -328,6 +388,7 @@ export {
328388
getApiCode,
329389
getImportCode,
330390
getModuleCode,
391+
getExportItemCode,
331392
getExportCode,
332393
prepareCode,
333394
};

test/__snapshots__/exportOnlyLocals-option.test.js.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
exports[`modules true (mode: local): errors 1`] = `Array []`;
44

55
exports[`modules true (mode: local): module 1`] = `
6-
"exports = module.exports = require(\\"../../../src/runtime/api.js\\")(false);
7-
// Exports
6+
"// Exports
87
module.exports = {
98
\\"v-def\\": \\"\\" + require(\\"-!../../../src/index.js??ref--4-0!./values.css\\")[\\"v-def\\"] + \\"\\",
109
\\"v-other\\": \\"\\" + require(\\"-!../../../src/index.js??ref--4-0!./values.css\\")[\\"v-other\\"] + \\"\\",

0 commit comments

Comments
 (0)