Skip to content

Refactor code #29

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 5 commits into from
Oct 8, 2020
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
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"url": "https://github.com/css-modules/postcss-modules-local-by-default.git"
},
"engines": {
"node": ">= 10.13.0 || >= 12.13.0 || >= 14"
"node": "^10 || ^12 || >= 14"
},
"keywords": [
"css-modules",
Expand All @@ -39,14 +39,14 @@
},
"devDependencies": {
"coveralls": "^3.1.0",
"eslint": "^7.9.0",
"eslint": "^7.10.0",
"husky": "^4.3.0",
"jest": "^26.4.2",
"jest": "^26.5.2",
"lint-staged": "^10.4.0",
"postcss": "^8.0.7",
"postcss": "^8.1.0",
"prettier": "^2.1.2"
},
"peerDependencies": {
"postcss": "^8.0.0"
"postcss": "^8.1.0"
}
}
165 changes: 89 additions & 76 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ const { extractICSS } = require("icss-utils");

const isSpacing = (node) => node.type === "combinator" && node.value === " ";

function getImportLocalAliases(icssImports) {
const localAliases = new Map();

Object.keys(icssImports).forEach((key) => {
Object.keys(icssImports[key]).forEach((prop) => {
localAliases.set(prop, icssImports[key][prop]);
});
});

return localAliases;
}

function normalizeNodeArray(nodes) {
const array = [];

Expand Down Expand Up @@ -432,6 +420,8 @@ function localizeDecl(decl, context) {
}
}

const isVisited = Symbol("isVisited");

module.exports = (options = {}) => {
if (options && options.mode) {
if (
Expand All @@ -450,85 +440,108 @@ module.exports = (options = {}) => {

return {
postcssPlugin: "postcss-modules-local-by-default",
RootExit(root) {
const { icssImports } = extractICSS(root, false);
const localAliasMap = getImportLocalAliases(icssImports);
prepare() {
const localAliasMap = new Map();

root.walkAtRules(function (atrule) {
if (/keyframes$/i.test(atrule.name)) {
const globalMatch = /^\s*:global\s*\((.+)\)\s*$/.exec(atrule.params);
const localMatch = /^\s*:local\s*\((.+)\)\s*$/.exec(atrule.params);
return {
Once(root) {
const { icssImports } = extractICSS(root, false);

let globalKeyframes = globalMode;
Object.keys(icssImports).forEach((key) => {
Object.keys(icssImports[key]).forEach((prop) => {
localAliasMap.set(prop, icssImports[key][prop]);
});
});
},
AtRule(atRule) {
if (atRule[isVisited]) {
return;
}

if (globalMatch) {
if (pureMode) {
throw atrule.error(
"@keyframes :global(...) is not allowed in pure mode"
);
}
atrule.params = globalMatch[1];
globalKeyframes = true;
} else if (localMatch) {
atrule.params = localMatch[0];
globalKeyframes = false;
} else if (!globalMode) {
if (atrule.params && !localAliasMap.has(atrule.params)) {
atrule.params = ":local(" + atrule.params + ")";
if (/keyframes$/i.test(atRule.name)) {
const globalMatch = /^\s*:global\s*\((.+)\)\s*$/.exec(
atRule.params
);
const localMatch = /^\s*:local\s*\((.+)\)\s*$/.exec(atRule.params);

let globalKeyframes = globalMode;

if (globalMatch) {
if (pureMode) {
throw atRule.error(
"@keyframes :global(...) is not allowed in pure mode"
);
}
atRule.params = globalMatch[1];
globalKeyframes = true;
} else if (localMatch) {
atRule.params = localMatch[0];
globalKeyframes = false;
} else if (!globalMode) {
if (atRule.params && !localAliasMap.has(atRule.params)) {
atRule.params = ":local(" + atRule.params + ")";
}
}
}

atrule.walkDecls(function (decl) {
localizeDecl(decl, {
localAliasMap,
options: options,
global: globalKeyframes,
});
});
} else if (atrule.nodes) {
atrule.nodes.forEach(function (decl) {
if (decl.type === "decl") {
atRule.walkDecls(function (decl) {
localizeDecl(decl, {
localAliasMap,
options: options,
global: globalMode,
global: globalKeyframes,
});
}
});
}
});
});
} else if (atRule.nodes) {
atRule.nodes.forEach(function (decl) {
if (decl.type === "decl") {
localizeDecl(decl, {
localAliasMap,
options: options,
global: globalMode,
});
}
});
}

root.walkRules(function (rule) {
if (
rule.parent &&
rule.parent.type === "atrule" &&
/keyframes$/i.test(rule.parent.name)
) {
// ignore keyframe rules
return;
}
atRule[isVisited] = true;
},
Rule(rule) {
if (rule[isVisited]) {
return;
}

const context = localizeNode(rule, options.mode, localAliasMap);
if (
rule.parent &&
rule.parent.type === "atrule" &&
/keyframes$/i.test(rule.parent.name)
) {
// ignore keyframe rules
return;
}

context.options = options;
context.localAliasMap = localAliasMap;
const context = localizeNode(rule, options.mode, localAliasMap);

if (pureMode && context.hasPureGlobals) {
throw rule.error(
'Selector "' +
rule.selector +
'" is not pure ' +
"(pure selectors must contain at least one local class or id)"
);
}
context.options = options;
context.localAliasMap = localAliasMap;

if (pureMode && context.hasPureGlobals) {
throw rule.error(
'Selector "' +
rule.selector +
'" is not pure ' +
"(pure selectors must contain at least one local class or id)"
);
}

rule.selector = context.selector;
rule.selector = context.selector;

// Less-syntax mixins parse as rules with no nodes
if (rule.nodes) {
rule.nodes.forEach((decl) => localizeDecl(decl, context));
}
});
// Less-syntax mixins parse as rules with no nodes
if (rule.nodes) {
rule.nodes.forEach((decl) => localizeDecl(decl, context));
}

rule[isVisited] = true;
},
};
},
};
};
Expand Down
Loading