Skip to content

Commit 00a90e1

Browse files
refactor: code
1 parent 671b8d5 commit 00a90e1

7 files changed

+31
-25
lines changed

package-lock.json

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"cssesc": "^3.0.0",
4848
"icss-utils": "^4.1.1",
4949
"loader-utils": "^1.2.3",
50+
"lodash.sortby": "^4.7.0",
5051
"normalize-path": "^3.0.0",
5152
"postcss": "^7.0.32",
5253
"postcss-modules-extract-imports": "^2.0.0",

src/index.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import postcss from 'postcss';
77
import postcssPkg from 'postcss/package.json';
88
import validateOptions from 'schema-utils';
99
import { satisfies } from 'semver';
10+
import sortBy from 'lodash.sortby';
1011

1112
import CssSyntaxError from './CssSyntaxError';
1213
import Warning from './Warning';
@@ -21,7 +22,6 @@ import {
2122
getModulesPlugins,
2223
normalizeSourceMap,
2324
shouldUseModulesPlugins,
24-
sortByName,
2525
} from './utils';
2626

2727
export default function loader(content, map, meta) {
@@ -137,16 +137,18 @@ export default function loader(content, map, meta) {
137137
}
138138
}
139139

140-
imports.sort((a, b) => a.index - b.index);
141140
apiImports.sort((a, b) => a.index - b.index);
142141

143-
const sortedImports = sortByName(imports, [
144-
'CSS_LOADER_ICSS_IMPORT',
145-
'CSS_LOADER_AT_RULE_IMPORT',
146-
'CSS_LOADER_GET_URL_IMPORT',
147-
'CSS_LOADER_URL_IMPORT',
148-
'CSS_LOADER_URL_REPLACEMENT',
149-
]);
142+
/*
143+
* Order
144+
* CSS_LOADER_ICSS_IMPORT: [],
145+
* CSS_LOADER_AT_RULE_IMPORT: [],
146+
* CSS_LOADER_GET_URL_IMPORT: [],
147+
* CSS_LOADER_URL_IMPORT: [],
148+
* CSS_LOADER_URL_REPLACEMENT: [],
149+
* */
150+
151+
const sortedImports = sortBy(imports, ['order', 'index']);
150152

151153
const { localsConvention } = options;
152154
const esModule =

src/plugins/postcss-icss-parser.js

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export default postcss.plugin(
4242
{
4343
type: 'import',
4444
value: {
45+
// 'CSS_LOADER_ICSS_IMPORT'
46+
order: 0,
4547
importName,
4648
url: options.urlHandler ? options.urlHandler(url) : url,
4749
},

src/plugins/postcss-import-parser.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
125125

126126
try {
127127
resolvedUrl = await resolveRequests(resolver, context, [
128-
normalizedUrl,
129-
url,
128+
...new Set([normalizedUrl, url]),
130129
]);
131130
} catch (error) {
132131
throw error;
@@ -135,6 +134,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
135134
result.messages.push({
136135
type: 'import',
137136
value: {
137+
// 'CSS_LOADER_AT_RULE_IMPORT'
138+
order: 1,
138139
importName,
139140
url: options.urlHandler
140141
? options.urlHandler(resolvedUrl)

src/plugins/postcss-url-parser.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
114114
pluginName,
115115
type: 'import',
116116
value: {
117+
// 'CSS_LOADER_GET_URL_IMPORT'
118+
order: 2,
117119
importName: '___CSS_LOADER_GET_URL_IMPORT___',
118120
url: options.urlHandler
119121
? options.urlHandler(urlToHelper)
@@ -129,6 +131,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
129131
pluginName,
130132
type: 'import',
131133
value: {
134+
// 'CSS_LOADER_URL_IMPORT'
135+
order: 3,
132136
importName,
133137
url: options.urlHandler
134138
? options.urlHandler(normalizedUrl)
@@ -148,7 +152,15 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
148152
result.messages.push({
149153
pluginName,
150154
type: 'url-replacement',
151-
value: { replacementName, importName, hash, needQuotes, index },
155+
value: {
156+
// 'CSS_LOADER_URL_REPLACEMENT'
157+
order: 4,
158+
replacementName,
159+
importName,
160+
hash,
161+
needQuotes,
162+
index,
163+
},
152164
});
153165
}
154166

src/utils.js

-11
Original file line numberDiff line numberDiff line change
@@ -450,16 +450,6 @@ async function resolveRequests(resolve, context, possibleRequests) {
450450
});
451451
}
452452

453-
function sortByName(array, orderNames) {
454-
const result = [];
455-
456-
for (const name of orderNames) {
457-
result.push(...array.filter((i) => i.importName.includes(name)));
458-
}
459-
460-
return result;
461-
}
462-
463453
/*
464454
* May be url is server-relative url, but not //example.com
465455
* */
@@ -490,6 +480,5 @@ export {
490480
getExportCode,
491481
shouldUseModulesPlugins,
492482
resolveRequests,
493-
sortByName,
494483
isUrlRequestable,
495484
};

0 commit comments

Comments
 (0)