Skip to content

Commit 8226b65

Browse files
refactor: code
1 parent 671b8d5 commit 8226b65

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

src/index.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
getModulesPlugins,
2222
normalizeSourceMap,
2323
shouldUseModulesPlugins,
24-
sortByName,
2524
} from './utils';
2625

2726
export default function loader(content, map, meta) {
@@ -110,7 +109,13 @@ export default function loader(content, map, meta) {
110109
this.emitWarning(new Warning(warning));
111110
}
112111

113-
const imports = [];
112+
const imports = {
113+
CSS_LOADER_ICSS_IMPORT: [],
114+
CSS_LOADER_AT_RULE_IMPORT: [],
115+
CSS_LOADER_GET_URL_IMPORT: [],
116+
CSS_LOADER_URL_IMPORT: [],
117+
CSS_LOADER_URL_REPLACEMENT: [],
118+
};
114119
const apiImports = [];
115120
const urlReplacements = [];
116121
const icssReplacements = [];
@@ -120,7 +125,7 @@ export default function loader(content, map, meta) {
120125
// eslint-disable-next-line default-case
121126
switch (message.type) {
122127
case 'import':
123-
imports.push(message.value);
128+
imports[message.value.key].push(message.value);
124129
break;
125130
case 'api-import':
126131
apiImports.push(message.value);
@@ -137,16 +142,13 @@ export default function loader(content, map, meta) {
137142
}
138143
}
139144

140-
imports.sort((a, b) => a.index - b.index);
141145
apiImports.sort((a, b) => a.index - b.index);
142146

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-
]);
147+
const sortedImports = Object.keys(imports).reduce((accumulator, key) => {
148+
return accumulator.concat(
149+
imports[key].sort((a, b) => a.index - b.index)
150+
);
151+
}, []);
150152

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

src/plugins/postcss-icss-parser.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default postcss.plugin(
4242
{
4343
type: 'import',
4444
value: {
45+
key: 'CSS_LOADER_ICSS_IMPORT',
4546
importName,
4647
url: options.urlHandler ? options.urlHandler(url) : url,
4748
},

src/plugins/postcss-import-parser.js

+2-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,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
135134
result.messages.push({
136135
type: 'import',
137136
value: {
137+
key: 'CSS_LOADER_AT_RULE_IMPORT',
138138
importName,
139139
url: options.urlHandler
140140
? options.urlHandler(resolvedUrl)

src/plugins/postcss-url-parser.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
114114
pluginName,
115115
type: 'import',
116116
value: {
117+
key: 'CSS_LOADER_GET_URL_IMPORT',
117118
importName: '___CSS_LOADER_GET_URL_IMPORT___',
118119
url: options.urlHandler
119120
? options.urlHandler(urlToHelper)
@@ -129,6 +130,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
129130
pluginName,
130131
type: 'import',
131132
value: {
133+
key: 'CSS_LOADER_URL_IMPORT',
132134
importName,
133135
url: options.urlHandler
134136
? options.urlHandler(normalizedUrl)
@@ -148,7 +150,14 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
148150
result.messages.push({
149151
pluginName,
150152
type: 'url-replacement',
151-
value: { replacementName, importName, hash, needQuotes, index },
153+
value: {
154+
key: 'CSS_LOADER_URL_REPLACEMENT',
155+
replacementName,
156+
importName,
157+
hash,
158+
needQuotes,
159+
index,
160+
},
152161
});
153162
}
154163

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)