Skip to content

Commit

Permalink
fix: respected style field from package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Jul 2, 2020
1 parent b8e7732 commit e2205af
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/index.js
Expand Up @@ -21,6 +21,7 @@ import {
getModulesPlugins,
normalizeSourceMap,
shouldUseModulesPlugins,
sortByName,
} from './utils';

export default function loader(content, map, meta) {
Expand Down Expand Up @@ -137,11 +138,24 @@ 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 { localsConvention } = options;
const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : false;

const importCode = getImportCode(this, exportType, imports, esModule);
const importCode = getImportCode(
this,
exportType,
sortedImports,
esModule
);
const moduleCode = getModuleCode(
result,
exportType,
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/postcss-url-parser.js
Expand Up @@ -65,6 +65,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {

let hasHelper = false;

let index = 0;

css.walkDecls((decl) => {
if (!needParseDecl.test(decl.value)) {
return;
Expand Down Expand Up @@ -99,6 +101,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
const importKey = normalizedUrl;
let importName = importsMap.get(importKey);

index += 1;

if (!importName) {
importName = `___CSS_LOADER_URL_IMPORT_${importsMap.size}___`;
importsMap.set(importKey, importName);
Expand All @@ -114,6 +118,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
url: options.urlHandler
? options.urlHandler(urlToHelper)
: urlToHelper,
index,
},
});

Expand All @@ -128,6 +133,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
url: options.urlHandler
? options.urlHandler(normalizedUrl)
: normalizedUrl,
index,
},
});
}
Expand All @@ -142,7 +148,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
result.messages.push({
pluginName,
type: 'url-replacement',
value: { replacementName, importName, hash, needQuotes },
value: { replacementName, importName, hash, needQuotes, index },
});
}

Expand Down
11 changes: 11 additions & 0 deletions src/utils.js
Expand Up @@ -442,6 +442,16 @@ 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;
}

export {
normalizeUrl,
getFilter,
Expand All @@ -453,4 +463,5 @@ export {
getExportCode,
shouldUseModulesPlugins,
resolveRequests,
sortByName,
};

0 comments on commit e2205af

Please sign in to comment.