Skip to content

Commit

Permalink
chore(release): 3.0.0-rc.2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 11, 2020
1 parent 07f62da commit 564c37c
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"node": true,
"jest": true
},
"extends": "eslint:recommended"
"extends": ["eslint:recommended", "prettier"]
}
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [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

### BREAKING CHANGE

- minimum supported `postcss` version is `^8.1.0`

### Fixes

- minimum supported `Node.js` version is `^10 || ^12 || >= 14`
- compatibility with other plugins
- compatibility with PostCSS 8

## [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

### 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-extract-imports",
"version": "3.0.0-rc.1",
"version": "3.0.0-rc.3",
"description": "A CSS Modules transform to extract local aliases for inline imports",
"main": "src/index.js",
"engines": {
Expand Down Expand Up @@ -38,6 +38,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
102 changes: 52 additions & 50 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
const topologicalSort = require("./topologicalSort");

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

const VISITED_MARKER = 1;

function createParentName(rule, root) {
return `__${root.index(rule.parent)}_${rule.selector}`;
}

function serializeImports(imports) {
return imports.map((importPath) => "`" + importPath + "`").join(", ");
}

/**
* :import('G') {}
*
Expand Down Expand Up @@ -54,53 +44,53 @@ function addImportToGraph(importId, parentId, graph, visited) {
}

visited[visitedId] = VISITED_MARKER;

siblings.push(importId);
}
}

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

return {
postcssPlugin: "postcss-modules-extract-imports",
prepare(result) {
prepare() {
const graph = {};
const visited = {};
const existingImports = {};
const importDecls = {};
const imports = {};

let importIndex = 0;

const createImportedName =
typeof options.createImportedName !== "function"
? (importName /*, path*/) =>
`i__imported_${importName.replace(/\W/g, "_")}_${importIndex++}`
: options.createImportedName;

return {
// Check the existing imports order and save refs
Rule(rule) {
const matches = icssImport.exec(rule.selector);
OnceExit(root, postcss) {
// Check the existing imports order and save refs
root.walkRules((rule) => {
const matches = icssImport.exec(rule.selector);

if (matches) {
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches;
const importPath = doubleQuotePath || singleQuotePath;
if (matches) {
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches;
const importPath = doubleQuotePath || singleQuotePath;

addImportToGraph(importPath, "root", graph, visited);
addImportToGraph(importPath, "root", graph, visited);

existingImports[importPath] = rule;
}
},
Declaration(decl) {
if (!declFilter.test(decl.prop)) {
return;
}
existingImports[importPath] = rule;
}
});

root.walkDecls(/^composes$/, (declaration) => {
const matches = declaration.value.match(matchImports);

let matches = decl.value.match(matchImports);
let tmpSymbols;
if (!matches) {
return;
}

if (matches) {
let tmpSymbols;
let [
,
/*match*/ symbols,
Expand All @@ -114,11 +104,22 @@ module.exports = (options = {}) => {
tmpSymbols = symbols.split(/\s+/).map((s) => `global(${s})`);
} else {
const importPath = doubleQuotePath || singleQuotePath;
const parentRule = createParentName(decl.parent, result.root);

let parent = declaration.parent;
let parentIndexes = "";

while (parent.type !== "root") {
parentIndexes =
parent.parent.index(parent) + "_" + parentIndexes;
parent = parent.parent;
}

const { selector } = declaration.parent;
const parentRule = `_${parentIndexes}${selector}`;

addImportToGraph(importPath, parentRule, graph, visited);

importDecls[importPath] = decl;
importDecls[importPath] = declaration;
imports[importPath] = imports[importPath] || {};

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

decl.value = tmpSymbols.join(" ");
}
},
OnceExit(root, postcss) {
declaration.value = tmpSymbols.join(" ");
});

const importsOrder = topologicalSort(graph, failOnWrongOrder);

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

const errMsg =
throw decl.error(
"Failed to resolve order of composed modules " +
serializeImports(importsOrder.nodes) +
".";

throw decl.error(errMsg, {
plugin: "postcss-modules-extract-imports",
word: "composes",
});
importsOrder.nodes
.map((importPath) => "`" + importPath + "`")
.join(", ") +
".",
{
plugin: "postcss-modules-extract-imports",
word: "composes",
}
);
}

let lastImportRule;
Expand Down
6 changes: 3 additions & 3 deletions src/topologicalSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ function walkGraph(node, graph, state, result, strict) {
const length = children.length;

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

if (er instanceof Error) {
return er;
if (error instanceof Error) {
return error;
}
}

Expand Down
Loading

0 comments on commit 564c37c

Please sign in to comment.