Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Jul 4, 2020
1 parent 671b8d5 commit 8226b65
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
24 changes: 13 additions & 11 deletions src/index.js
Expand Up @@ -21,7 +21,6 @@ import {
getModulesPlugins,
normalizeSourceMap,
shouldUseModulesPlugins,
sortByName,
} from './utils';

export default function loader(content, map, meta) {
Expand Down Expand Up @@ -110,7 +109,13 @@ export default function loader(content, map, meta) {
this.emitWarning(new Warning(warning));
}

const imports = [];
const imports = {
CSS_LOADER_ICSS_IMPORT: [],
CSS_LOADER_AT_RULE_IMPORT: [],
CSS_LOADER_GET_URL_IMPORT: [],
CSS_LOADER_URL_IMPORT: [],
CSS_LOADER_URL_REPLACEMENT: [],
};
const apiImports = [];
const urlReplacements = [];
const icssReplacements = [];
Expand All @@ -120,7 +125,7 @@ export default function loader(content, map, meta) {
// eslint-disable-next-line default-case
switch (message.type) {
case 'import':
imports.push(message.value);
imports[message.value.key].push(message.value);
break;
case 'api-import':
apiImports.push(message.value);
Expand All @@ -137,16 +142,13 @@ export default function loader(content, map, meta) {
}
}

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

const sortedImports = sortByName(imports, [
'CSS_LOADER_ICSS_IMPORT',
'CSS_LOADER_AT_RULE_IMPORT',
'CSS_LOADER_GET_URL_IMPORT',
'CSS_LOADER_URL_IMPORT',
'CSS_LOADER_URL_REPLACEMENT',
]);
const sortedImports = Object.keys(imports).reduce((accumulator, key) => {
return accumulator.concat(
imports[key].sort((a, b) => a.index - b.index)
);
}, []);

const { localsConvention } = options;
const esModule =
Expand Down
1 change: 1 addition & 0 deletions src/plugins/postcss-icss-parser.js
Expand Up @@ -42,6 +42,7 @@ export default postcss.plugin(
{
type: 'import',
value: {
key: 'CSS_LOADER_ICSS_IMPORT',
importName,
url: options.urlHandler ? options.urlHandler(url) : url,
},
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/postcss-import-parser.js
Expand Up @@ -125,8 +125,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {

try {
resolvedUrl = await resolveRequests(resolver, context, [
normalizedUrl,
url,
...new Set([normalizedUrl, url]),
]);
} catch (error) {
throw error;
Expand All @@ -135,6 +134,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
result.messages.push({
type: 'import',
value: {
key: 'CSS_LOADER_AT_RULE_IMPORT',
importName,
url: options.urlHandler
? options.urlHandler(resolvedUrl)
Expand Down
11 changes: 10 additions & 1 deletion src/plugins/postcss-url-parser.js
Expand Up @@ -114,6 +114,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
pluginName,
type: 'import',
value: {
key: 'CSS_LOADER_GET_URL_IMPORT',
importName: '___CSS_LOADER_GET_URL_IMPORT___',
url: options.urlHandler
? options.urlHandler(urlToHelper)
Expand All @@ -129,6 +130,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
pluginName,
type: 'import',
value: {
key: 'CSS_LOADER_URL_IMPORT',
importName,
url: options.urlHandler
? options.urlHandler(normalizedUrl)
Expand All @@ -148,7 +150,14 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
result.messages.push({
pluginName,
type: 'url-replacement',
value: { replacementName, importName, hash, needQuotes, index },
value: {
key: 'CSS_LOADER_URL_REPLACEMENT',
replacementName,
importName,
hash,
needQuotes,
index,
},
});
}

Expand Down
11 changes: 0 additions & 11 deletions src/utils.js
Expand Up @@ -450,16 +450,6 @@ async function resolveRequests(resolve, context, possibleRequests) {
});
}

function sortByName(array, orderNames) {
const result = [];

for (const name of orderNames) {
result.push(...array.filter((i) => i.importName.includes(name)));
}

return result;
}

/*
* May be url is server-relative url, but not //example.com
* */
Expand Down Expand Up @@ -490,6 +480,5 @@ export {
getExportCode,
shouldUseModulesPlugins,
resolveRequests,
sortByName,
isUrlRequestable,
};

0 comments on commit 8226b65

Please sign in to comment.