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 00a90e1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 25 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -47,6 +47,7 @@
"cssesc": "^3.0.0",
"icss-utils": "^4.1.1",
"loader-utils": "^1.2.3",
"lodash.sortby": "^4.7.0",
"normalize-path": "^3.0.0",
"postcss": "^7.0.32",
"postcss-modules-extract-imports": "^2.0.0",
Expand Down
20 changes: 11 additions & 9 deletions src/index.js
Expand Up @@ -7,6 +7,7 @@ import postcss from 'postcss';
import postcssPkg from 'postcss/package.json';
import validateOptions from 'schema-utils';
import { satisfies } from 'semver';
import sortBy from 'lodash.sortby';

import CssSyntaxError from './CssSyntaxError';
import Warning from './Warning';
Expand All @@ -21,7 +22,6 @@ import {
getModulesPlugins,
normalizeSourceMap,
shouldUseModulesPlugins,
sortByName,
} from './utils';

export default function loader(content, map, meta) {
Expand Down Expand Up @@ -137,16 +137,18 @@ 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',
]);
/*
* Order
* CSS_LOADER_ICSS_IMPORT: [],
* CSS_LOADER_AT_RULE_IMPORT: [],
* CSS_LOADER_GET_URL_IMPORT: [],
* CSS_LOADER_URL_IMPORT: [],
* CSS_LOADER_URL_REPLACEMENT: [],
* */

const sortedImports = sortBy(imports, ['order', 'index']);

const { localsConvention } = options;
const esModule =
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/postcss-icss-parser.js
Expand Up @@ -42,6 +42,8 @@ export default postcss.plugin(
{
type: 'import',
value: {
// 'CSS_LOADER_ICSS_IMPORT'
order: 0,
importName,
url: options.urlHandler ? options.urlHandler(url) : url,
},
Expand Down
5 changes: 3 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,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
result.messages.push({
type: 'import',
value: {
// 'CSS_LOADER_AT_RULE_IMPORT'
order: 1,
importName,
url: options.urlHandler
? options.urlHandler(resolvedUrl)
Expand Down
14 changes: 13 additions & 1 deletion src/plugins/postcss-url-parser.js
Expand Up @@ -114,6 +114,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
pluginName,
type: 'import',
value: {
// 'CSS_LOADER_GET_URL_IMPORT'
order: 2,
importName: '___CSS_LOADER_GET_URL_IMPORT___',
url: options.urlHandler
? options.urlHandler(urlToHelper)
Expand All @@ -129,6 +131,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
pluginName,
type: 'import',
value: {
// 'CSS_LOADER_URL_IMPORT'
order: 3,
importName,
url: options.urlHandler
? options.urlHandler(normalizedUrl)
Expand All @@ -148,7 +152,15 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
result.messages.push({
pluginName,
type: 'url-replacement',
value: { replacementName, importName, hash, needQuotes, index },
value: {
// 'CSS_LOADER_URL_REPLACEMENT'
order: 4,
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 00a90e1

Please sign in to comment.