Skip to content

Commit 564c37c

Browse files
chore(release): 3.0.0-rc.2
1 parent 07f62da commit 564c37c

File tree

6 files changed

+226
-207
lines changed

6 files changed

+226
-207
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"node": true,
88
"jest": true
99
},
10-
"extends": "eslint:recommended"
10+
"extends": ["eslint:recommended", "prettier"]
1111
}

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [3.0.0-rc.2](https://github.com/postcss-modules-local-by-default/compare/v3.0.0-rc.1...v3.0.0-rc.2) - 2020-10-08
7+
8+
### BREAKING CHANGE
9+
10+
- minimum supported `postcss` version is `^8.1.0`
11+
12+
### Fixes
13+
14+
- minimum supported `Node.js` version is `^10 || ^12 || >= 14`
15+
- compatibility with other plugins
16+
- compatibility with PostCSS 8
17+
618
## [3.0.0-rc.1](https://github.com/postcss-modules-local-by-default/compare/v3.0.0-rc.0...v3.0.0-rc.1) - 2020-09-18
719

820
### Fixes

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-modules-extract-imports",
3-
"version": "3.0.0-rc.1",
3+
"version": "3.0.0-rc.3",
44
"description": "A CSS Modules transform to extract local aliases for inline imports",
55
"main": "src/index.js",
66
"engines": {
@@ -38,6 +38,7 @@
3838
"devDependencies": {
3939
"coveralls": "^3.1.0",
4040
"eslint": "^7.10.0",
41+
"eslint-config-prettier": "^6.12.0",
4142
"husky": "^4.3.0",
4243
"jest": "^26.5.2",
4344
"lint-staged": "^10.4.0",

src/index.js

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
const topologicalSort = require("./topologicalSort");
22

3-
const declWhitelist = ["composes"];
4-
const declFilter = new RegExp(`^(${declWhitelist.join("|")})$`);
53
const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)'|(global))$/;
64
const icssImport = /^:import\((?:"([^"]+)"|'([^']+)')\)/;
75

86
const VISITED_MARKER = 1;
97

10-
function createParentName(rule, root) {
11-
return `__${root.index(rule.parent)}_${rule.selector}`;
12-
}
13-
14-
function serializeImports(imports) {
15-
return imports.map((importPath) => "`" + importPath + "`").join(", ");
16-
}
17-
188
/**
199
* :import('G') {}
2010
*
@@ -54,53 +44,53 @@ function addImportToGraph(importId, parentId, graph, visited) {
5444
}
5545

5646
visited[visitedId] = VISITED_MARKER;
47+
5748
siblings.push(importId);
5849
}
5950
}
6051

6152
module.exports = (options = {}) => {
53+
let importIndex = 0;
54+
const createImportedName =
55+
typeof options.createImportedName !== "function"
56+
? (importName /*, path*/) =>
57+
`i__imported_${importName.replace(/\W/g, "_")}_${importIndex++}`
58+
: options.createImportedName;
6259
const failOnWrongOrder = options.failOnWrongOrder;
6360

6461
return {
6562
postcssPlugin: "postcss-modules-extract-imports",
66-
prepare(result) {
63+
prepare() {
6764
const graph = {};
6865
const visited = {};
6966
const existingImports = {};
7067
const importDecls = {};
7168
const imports = {};
7269

73-
let importIndex = 0;
74-
75-
const createImportedName =
76-
typeof options.createImportedName !== "function"
77-
? (importName /*, path*/) =>
78-
`i__imported_${importName.replace(/\W/g, "_")}_${importIndex++}`
79-
: options.createImportedName;
80-
8170
return {
82-
// Check the existing imports order and save refs
83-
Rule(rule) {
84-
const matches = icssImport.exec(rule.selector);
71+
OnceExit(root, postcss) {
72+
// Check the existing imports order and save refs
73+
root.walkRules((rule) => {
74+
const matches = icssImport.exec(rule.selector);
8575

86-
if (matches) {
87-
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches;
88-
const importPath = doubleQuotePath || singleQuotePath;
76+
if (matches) {
77+
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches;
78+
const importPath = doubleQuotePath || singleQuotePath;
8979

90-
addImportToGraph(importPath, "root", graph, visited);
80+
addImportToGraph(importPath, "root", graph, visited);
9181

92-
existingImports[importPath] = rule;
93-
}
94-
},
95-
Declaration(decl) {
96-
if (!declFilter.test(decl.prop)) {
97-
return;
98-
}
82+
existingImports[importPath] = rule;
83+
}
84+
});
85+
86+
root.walkDecls(/^composes$/, (declaration) => {
87+
const matches = declaration.value.match(matchImports);
9988

100-
let matches = decl.value.match(matchImports);
101-
let tmpSymbols;
89+
if (!matches) {
90+
return;
91+
}
10292

103-
if (matches) {
93+
let tmpSymbols;
10494
let [
10595
,
10696
/*match*/ symbols,
@@ -114,11 +104,22 @@ module.exports = (options = {}) => {
114104
tmpSymbols = symbols.split(/\s+/).map((s) => `global(${s})`);
115105
} else {
116106
const importPath = doubleQuotePath || singleQuotePath;
117-
const parentRule = createParentName(decl.parent, result.root);
107+
108+
let parent = declaration.parent;
109+
let parentIndexes = "";
110+
111+
while (parent.type !== "root") {
112+
parentIndexes =
113+
parent.parent.index(parent) + "_" + parentIndexes;
114+
parent = parent.parent;
115+
}
116+
117+
const { selector } = declaration.parent;
118+
const parentRule = `_${parentIndexes}${selector}`;
118119

119120
addImportToGraph(importPath, parentRule, graph, visited);
120121

121-
importDecls[importPath] = decl;
122+
importDecls[importPath] = declaration;
122123
imports[importPath] = imports[importPath] || {};
123124

124125
tmpSymbols = symbols.split(/\s+/).map((s) => {
@@ -130,10 +131,9 @@ module.exports = (options = {}) => {
130131
});
131132
}
132133

133-
decl.value = tmpSymbols.join(" ");
134-
}
135-
},
136-
OnceExit(root, postcss) {
134+
declaration.value = tmpSymbols.join(" ");
135+
});
136+
137137
const importsOrder = topologicalSort(graph, failOnWrongOrder);
138138

139139
if (importsOrder instanceof Error) {
@@ -143,15 +143,17 @@ module.exports = (options = {}) => {
143143
);
144144
const decl = importDecls[importPath];
145145

146-
const errMsg =
146+
throw decl.error(
147147
"Failed to resolve order of composed modules " +
148-
serializeImports(importsOrder.nodes) +
149-
".";
150-
151-
throw decl.error(errMsg, {
152-
plugin: "postcss-modules-extract-imports",
153-
word: "composes",
154-
});
148+
importsOrder.nodes
149+
.map((importPath) => "`" + importPath + "`")
150+
.join(", ") +
151+
".",
152+
{
153+
plugin: "postcss-modules-extract-imports",
154+
word: "composes",
155+
}
156+
);
155157
}
156158

157159
let lastImportRule;

src/topologicalSort.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ function walkGraph(node, graph, state, result, strict) {
3333
const length = children.length;
3434

3535
for (let i = 0; i < length; ++i) {
36-
const er = walkGraph(children[i], graph, state, result, strict);
36+
const error = walkGraph(children[i], graph, state, result, strict);
3737

38-
if (er instanceof Error) {
39-
return er;
38+
if (error instanceof Error) {
39+
return error;
4040
}
4141
}
4242

0 commit comments

Comments
 (0)