Skip to content

Commit

Permalink
chore(release): 4.0.0-rc.4
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 11, 2020
1 parent dc6cd6a commit 401bf91
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 250 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"ecmaVersion": 2018
},
"env": {
"node": true,
"es6": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended"
"extends": ["eslint:recommended", "prettier"]
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [4.0.0-rc.4](https://github.com/postcss-modules-local-by-default/compare/v4.0.0-rc.3...v4.0.0-rc.4) - 2020-10-11

### Fixes

- compatibility with plugins other plugins

## [4.0.0-rc.3](https://github.com/postcss-modules-local-by-default/compare/v4.0.0-rc.2...v4.0.0-rc.3) - 2020-10-08

### Fixes
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-modules-local-by-default",
"version": "4.0.0-rc.3",
"version": "4.0.0-rc.4",
"description": "A CSS Modules transform to make local scope the default",
"main": "src/index.js",
"author": "Mark Dalgleish",
Expand Down Expand Up @@ -40,6 +40,7 @@
"devDependencies": {
"coveralls": "^3.1.0",
"eslint": "^7.10.0",
"eslint-config-prettier": "^6.12.0",
"husky": "^4.3.0",
"jest": "^26.5.2",
"lint-staged": "^10.4.0",
Expand Down
177 changes: 85 additions & 92 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const isSpacing = (node) => node.type === "combinator" && node.value === " ";
function normalizeNodeArray(nodes) {
const array = [];

nodes.forEach(function (x) {
nodes.forEach((x) => {
if (Array.isArray(x)) {
normalizeNodeArray(x).forEach(function (item) {
normalizeNodeArray(x).forEach((item) => {
array.push(item);
});
} else if (x) {
Expand Down Expand Up @@ -43,7 +43,7 @@ function localizeNode(rule, mode, localAliasMap) {

context.hasPureGlobals = false;

newNodes = node.nodes.map(function (n) {
newNodes = node.nodes.map((n) => {
const nContext = {
global: context.global,
lastWasSpacing: true,
Expand Down Expand Up @@ -301,8 +301,8 @@ function isWordAFunctionArgument(wordNode, functionNode) {
: false;
}

function localizeDeclValues(localize, decl, context) {
const valueNodes = valueParser(decl.value);
function localizeDeclarationValues(localize, declaration, context) {
const valueNodes = valueParser(declaration.value);

valueNodes.walk((node, index, nodes) => {
const subContext = {
Expand All @@ -313,11 +313,12 @@ function localizeDeclValues(localize, decl, context) {
};
nodes[index] = localizeDeclNode(node, subContext);
});
decl.value = valueNodes.toString();

declaration.value = valueNodes.toString();
}

function localizeDecl(decl, context) {
const isAnimation = /animation$/i.test(decl.prop);
function localizeDeclaration(declaration, context) {
const isAnimation = /animation$/i.test(declaration.prop);

if (isAnimation) {
const validIdent = /^-?[_a-z][_a-z0-9-]*$/i;
Expand Down Expand Up @@ -360,7 +361,7 @@ function localizeDecl(decl, context) {
const didParseAnimationName = false;
let parsedAnimationKeywords = {};
let stepsFunctionNode = null;
const valueNodes = valueParser(decl.value).walk((node) => {
const valueNodes = valueParser(declaration.value).walk((node) => {
/* If div-token appeared (represents as comma ','), a possibility of an animation-keywords should be reflesh. */
if (node.type === "div") {
parsedAnimationKeywords = {};
Expand Down Expand Up @@ -400,26 +401,24 @@ function localizeDecl(decl, context) {
return localizeDeclNode(node, subContext);
});

decl.value = valueNodes.toString();
declaration.value = valueNodes.toString();

return;
}

const isAnimationName = /animation(-name)?$/i.test(decl.prop);
const isAnimationName = /animation(-name)?$/i.test(declaration.prop);

if (isAnimationName) {
return localizeDeclValues(true, decl, context);
return localizeDeclarationValues(true, declaration, context);
}

const hasUrl = /url\(/i.test(decl.value);
const hasUrl = /url\(/i.test(declaration.value);

if (hasUrl) {
return localizeDeclValues(false, decl, context);
return localizeDeclarationValues(false, declaration, context);
}
}

const isVisited = Symbol("isVisited");

module.exports = (options = {}) => {
if (options && options.mode) {
if (
Expand All @@ -442,102 +441,96 @@ module.exports = (options = {}) => {
const localAliasMap = new Map();

return {
Root(root) {
OnceExit(root) {
const { icssImports } = extractICSS(root, false);

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

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;
root.walkAtRules((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
);

if (globalMatch) {
if (pureMode) {
throw atRule.error(
"@keyframes :global(...) is not allowed in pure mode"
);
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.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") {
localizeDecl(decl, {
atRule.walkDecls((declaration) => {
localizeDeclaration(declaration, {
localAliasMap,
options: options,
global: globalMode,
global: globalKeyframes,
});
}
});
}

atRule[isVisited] = true;
},
Rule(rule) {
if (rule[isVisited]) {
return;
}

if (
rule.parent &&
rule.parent.type === "atrule" &&
/keyframes$/i.test(rule.parent.name)
) {
// ignore keyframe rules
return;
}
});
} else if (atRule.nodes) {
atRule.nodes.forEach((declaration) => {
if (declaration.type === "decl") {
localizeDeclaration(declaration, {
localAliasMap,
options: options,
global: globalMode,
});
}
});
}
});

const context = localizeNode(rule, options.mode, localAliasMap);
root.walkRules((rule) => {
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;

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

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

rule[isVisited] = true;
// Less-syntax mixins parse as rules with no nodes
if (rule.nodes) {
rule.nodes.forEach((declaration) =>
localizeDeclaration(declaration, context)
);
}
});
},
};
},
Expand Down
Loading

0 comments on commit 401bf91

Please sign in to comment.