Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 8, 2020
2 parents dc58f6e + ff2721e commit 23651dd
Show file tree
Hide file tree
Showing 3 changed files with 445 additions and 424 deletions.
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

0 comments on commit 23651dd

Please sign in to comment.