Skip to content

refactor: postcss-icss-parser #943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"dependencies": {
"camelcase": "^5.2.0",
"cssesc": "^3.0.0",
"icss-utils": "^4.1.0",
"icss-utils": "^4.1.1",
"loader-utils": "^1.2.3",
"normalize-path": "^3.0.0",
"postcss": "^7.0.14",
Expand Down
96 changes: 30 additions & 66 deletions src/plugins/postcss-icss-parser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';
import { extractICSS, replaceValueSymbols } from 'icss-utils';
import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
import loaderUtils from 'loader-utils';

const pluginName = 'postcss-icss-parser';

function hasImportMessage(messages, url) {
return messages.find(
(message) =>
message.pluginName === pluginName &&
message.type === 'import' &&
message.item.url === url &&
message.item.media === ''
);
}

export default postcss.plugin(
pluginName,
() =>
Expand All @@ -14,88 +23,43 @@ export default postcss.plugin(

let index = 0;

Object.keys(icssImports).forEach((key) => {
const url = loaderUtils.parseString(key);
for (const importUrl of Object.keys(icssImports)) {
const url = loaderUtils.parseString(importUrl);

Object.keys(icssImports[key]).forEach((prop) => {
for (const token of Object.keys(icssImports[importUrl])) {
index += 1;

importReplacements[prop] = `___CSS_LOADER_IMPORT___${index}___`;
importReplacements[token] = `___CSS_LOADER_IMPORT___${index}___`;

result.messages.push({
pluginName,
type: 'icss-import',
item: { url, export: icssImports[key][prop], index },
item: { url, export: icssImports[importUrl][token], index },
});

const alreadyIncluded = result.messages.find(
(message) =>
message.pluginName === pluginName &&
message.type === 'import' &&
message.item.url === url &&
message.item.media === ''
);

if (alreadyIncluded) {
return;
if (!hasImportMessage(result.messages, url)) {
result.messages.push({
pluginName,
type: 'import',
item: { url, media: '' },
});
}

result.messages.push({
pluginName,
type: 'import',
item: { url, media: '' },
});
});
});

function replaceImportsInString(str) {
const tokens = valueParser(str);

tokens.walk((node) => {
if (node.type !== 'word') {
return;
}

const token = node.value;
const replacement = importReplacements[token];

if (replacement) {
// eslint-disable-next-line no-param-reassign
node.value = replacement;
}
});

return tokens.toString();
}
}

// Replace tokens
css.walk((node) => {
// Due reusing `ast` from `postcss-loader` some plugins may remove `value`, `selector` or `params` properties
if (node.type === 'decl' && node.value) {
// eslint-disable-next-line no-param-reassign
node.value = replaceImportsInString(node.value.toString());
} else if (node.type === 'rule' && node.selector) {
// eslint-disable-next-line no-param-reassign
node.selector = replaceValueSymbols(
node.selector.toString(),
importReplacements
);
} else if (node.type === 'atrule' && node.params) {
// eslint-disable-next-line no-param-reassign
node.params = replaceImportsInString(node.params.toString());
}
});
replaceSymbols(css, importReplacements);

// Replace tokens in export
Object.keys(icssExports).forEach((exportName) => {
for (const exportName of Object.keys(icssExports)) {
result.messages.push({
pluginName,
type: 'export',
item: {
key: exportName,
value: replaceImportsInString(icssExports[exportName]),
value: replaceValueSymbols(
icssExports[exportName],
importReplacements
),
},
});
});
}
}
);
Loading