Skip to content

refactor: postcss-parser #821

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
Nov 29, 2018
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
4 changes: 2 additions & 2 deletions lib/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const importParser = require('./postcss-import-parser');
const parser = require('./postcss-parser');
const icssParser = require('./postcss-icss-parser');
const urlParser = require('./postcss-url-parser');

module.exports = {
importParser,
parser,
icssParser,
urlParser,
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const postcss = require('postcss');
const valueParser = require('postcss-value-parser');
const icssUtils = require('icss-utils');
const Tokenizer = require('css-selector-tokenizer');
const loaderUtils = require('loader-utils');

const pluginName = 'postcss-icss-parser';

module.exports = postcss.plugin(
'postcss-parser',
pluginName,
(options) =>
function process(css) {
const importItems = options.importItems || [];
const urlItems = options.urlItems || [];
const icss = icssUtils.extractICSS(css);

const imports = {};
const icss = icssUtils.extractICSS(css);
const exports = icss.icssExports;

Object.keys(icss.icssImports).forEach((key) => {
Expand Down Expand Up @@ -50,46 +50,21 @@ module.exports = postcss.plugin(
return str;
}

Object.keys(exports).forEach((exportName) => {
exports[exportName] = replaceImportsInString(exports[exportName]);
});

function processNode(item) {
switch (item.type) {
case 'value':
item.nodes.forEach(processNode);
break;
case 'nested-item':
item.nodes.forEach(processNode);
break;
case 'item': {
const importIndex = imports[`$${item.name}`];
if (typeof importIndex === 'number') {
// eslint-disable-next-line no-param-reassign
item.name = `___CSS_LOADER_IMPORT___${importIndex}___`;
}
break;
}
// no default
}
}

// Replace tokens in declarations
css.walkDecls((decl) => {
const values = Tokenizer.parseValues(decl.value);

values.nodes.forEach((value) => {
value.nodes.forEach(processNode);
});

// eslint-disable-next-line no-param-reassign
decl.value = Tokenizer.stringifyValues(values);
decl.value = replaceImportsInString(decl.value.toString());
});

// Replace tokens in at-rules
css.walkAtRules((atrule) => {
if (typeof atrule.params === 'string') {
// eslint-disable-next-line no-param-reassign
atrule.params = replaceImportsInString(atrule.params);
}
// eslint-disable-next-line no-param-reassign
atrule.params = replaceImportsInString(atrule.params.toString());
});

// Replace tokens in export
Object.keys(exports).forEach((exportName) => {
exports[exportName] = replaceImportsInString(exports[exportName]);
});

/* eslint-disable no-param-reassign */
Expand Down
4 changes: 2 additions & 2 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const extractImports = require('postcss-modules-extract-imports');
const modulesScope = require('postcss-modules-scope');
const modulesValues = require('postcss-modules-values');

const { importParser, parser, urlParser } = require('./plugins');
const { importParser, icssParser, urlParser } = require('./plugins');

const Warning = require('./Warning');
const CssSyntaxError = require('./CssSyntaxError');
Expand Down Expand Up @@ -73,7 +73,7 @@ module.exports = function processCss(content, map, options, callback) {
plugins.push(urlParser(parserOptions));
}

plugins.push(parser(parserOptions));
plugins.push(icssParser(parserOptions));

postcss(plugins)
.process(content, {
Expand Down
28 changes: 21 additions & 7 deletions package-lock.json

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

Loading