Skip to content

Commit 73000e3

Browse files
refactor: code (#72)
1 parent f3ae4d5 commit 73000e3

18 files changed

+2223
-2993
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.eslintrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 2018
4+
},
5+
"env": {
6+
"node": true,
7+
"es6": true,
8+
"jest": true
9+
},
10+
"extends": "eslint:recommended"
11+
}

.gitignore

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
node_modules
1+
.idea
2+
*.iml
3+
.nyc_output
24
coverage
3-
lib
5+
node_modules
6+

.travis.yml

+9-32
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
1-
sudo: false
2-
3-
git:
4-
depth: 10
5-
6-
branches:
7-
only:
8-
- master
9-
- next
10-
111
language: node_js
122

13-
cache:
14-
yarn: true
15-
directories:
16-
- node_modules
17-
18-
matrix:
19-
include:
20-
- node_js: "12"
21-
env: JOB_PART=pretest
22-
- node_js: "6"
23-
env: JOB_PART=test
24-
- node_js: "8"
25-
env: JOB_PART=test
26-
- node_js: "10"
27-
env: JOB_PART=test
28-
- node_js: "12"
29-
env: JOB_PART=test
3+
node_js:
4+
- "10"
5+
- "12"
6+
- "14"
307

31-
before_script:
32-
- node --version
33-
- yarn --version
8+
script: yarn ci
349

35-
script:
36-
- yarn $JOB_PART
10+
after_success:
11+
- cat ./coverage/lcov.info | node_modules/.bin/coveralls --verbose
12+
- cat ./coverage/coverage.json | node_modules/codecov.io/bin/codecov.io.js
13+
- rm -rf ./coverage

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ Converts icss imports and exports definitions to postcss ast
6666
createICSSRules(
6767
{
6868
colors: {
69-
a: "b"
70-
}
69+
a: "b",
70+
},
7171
},
7272
{
73-
c: "d"
74-
}
73+
c: "d",
74+
},
75+
// Need pass `rule` and `decl` from postcss
76+
// Please look at `Step 4` https://evilmartians.com/chronicles/postcss-8-plugin-migration
77+
postcss
7578
);
7679
```
7780

husky.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
hooks: {
3+
"pre-commit": "lint-staged",
4+
},
5+
};

lint-staged.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
"*.js": ["eslint --fix", "prettier --write"],
3+
"*.{json,md,yml,css,ts}": ["prettier --write"],
4+
};

package.json

+21-56
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,24 @@
22
"name": "icss-utils",
33
"version": "4.1.1",
44
"description": "ICSS utils for postcss ast",
5-
"main": "lib/index.js",
5+
"main": "src/index.js",
66
"engines": {
7-
"node": ">= 6"
7+
"node": ">= 10.13.0 || >= 12.13.0 || >= 14"
88
},
99
"files": [
10-
"lib"
10+
"src"
1111
],
1212
"scripts": {
13-
"lint": "eslint . --ignore-path .gitignore",
14-
"build": "babel --out-dir lib src",
15-
"pretest": "npm run lint",
16-
"test": "npm run test:only",
13+
"prettier": "prettier -l --ignore-path .gitignore . \"!test/test-cases\"",
14+
"eslint": "eslint --ignore-path .gitignore .",
15+
"lint": "yarn eslint && yarn prettier",
16+
"pretest": "yarn lint",
17+
"test": "jest --coverage",
1718
"test:only": "jest",
18-
"prepublish": "yarn test && yarn run build"
19-
},
20-
"babel": {
21-
"presets": [
22-
[
23-
"@babel/preset-env",
24-
{
25-
"targets": {
26-
"node": 6
27-
}
28-
}
29-
]
30-
]
31-
},
32-
"eslintConfig": {
33-
"parser": "babel-eslint",
34-
"parserOptions": {
35-
"sourceType": "module"
36-
},
37-
"env": {
38-
"es6": true,
39-
"jest": true
40-
},
41-
"extends": "eslint:recommended"
42-
},
43-
"lint-staged": {
44-
"*.js": [
45-
"prettier --write",
46-
"eslint",
47-
"git add"
48-
]
49-
},
50-
"husky": {
51-
"hooks": {
52-
"pre-commit": "lint-staged"
53-
}
19+
"autotest": "jest --coverage --watch",
20+
"cover": "jest --coverage",
21+
"ci": "yarn pretest && yarn cover",
22+
"prepublishOnly": "yarn test"
5423
},
5524
"repository": {
5625
"type": "git",
@@ -68,19 +37,15 @@
6837
"url": "https://github.com/css-modules/icss-utils/issues"
6938
},
7039
"homepage": "https://github.com/css-modules/icss-utils#readme",
71-
"dependencies": {
72-
"postcss": "^7.0.14"
73-
},
7440
"devDependencies": {
75-
"@babel/cli": "^7.1.0",
76-
"@babel/core": "^7.1.0",
77-
"@babel/preset-env": "^7.1.0",
78-
"babel-eslint": "^10.0.1",
79-
"babel-jest": "^24.1.0",
80-
"eslint": "^5.14.1",
81-
"husky": "^1.3.1",
82-
"jest": "^24.1.0",
83-
"lint-staged": "^8.1.4",
84-
"prettier": "^1.16.4"
41+
"eslint": "^7.9.0",
42+
"husky": "^4.3.0",
43+
"jest": "^26.4.2",
44+
"lint-staged": "^10.4.0",
45+
"postcss": "^8.0.6",
46+
"prettier": "^2.1.2"
47+
},
48+
"peerDependencies": {
49+
"postcss": "^8.0.0"
8550
}
8651
}

src/createICSSRules.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import postcss from "postcss";
2-
3-
const createImports = imports => {
4-
return Object.keys(imports).map(path => {
1+
const createImports = (imports, postcss) => {
2+
return Object.keys(imports).map((path) => {
53
const aliases = imports[path];
6-
const declarations = Object.keys(aliases).map(key =>
4+
const declarations = Object.keys(aliases).map((key) =>
75
postcss.decl({
86
prop: key,
97
value: aliases[key],
10-
raws: { before: "\n " }
8+
raws: { before: "\n " },
119
})
1210
);
1311

1412
const hasDeclarations = declarations.length > 0;
1513

1614
const rule = postcss.rule({
1715
selector: `:import('${path}')`,
18-
raws: { after: hasDeclarations ? "\n" : "" }
16+
raws: { after: hasDeclarations ? "\n" : "" },
1917
});
2018

2119
if (hasDeclarations) {
@@ -26,12 +24,12 @@ const createImports = imports => {
2624
});
2725
};
2826

29-
const createExports = exports => {
30-
const declarations = Object.keys(exports).map(key =>
27+
const createExports = (exports, postcss) => {
28+
const declarations = Object.keys(exports).map((key) =>
3129
postcss.decl({
3230
prop: key,
3331
value: exports[key],
34-
raws: { before: "\n " }
32+
raws: { before: "\n " },
3533
})
3634
);
3735

@@ -42,16 +40,16 @@ const createExports = exports => {
4240
const rule = postcss
4341
.rule({
4442
selector: `:export`,
45-
raws: { after: "\n" }
43+
raws: { after: "\n" },
4644
})
4745
.append(declarations);
4846

4947
return [rule];
5048
};
5149

52-
const createICSSRules = (imports, exports) => [
53-
...createImports(imports),
54-
...createExports(exports)
50+
const createICSSRules = (imports, exports, postcss) => [
51+
...createImports(imports, postcss),
52+
...createExports(exports, postcss),
5553
];
5654

57-
export default createICSSRules;
55+
module.exports = createICSSRules;

src/extractICSS.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const importPattern = /^:import\(("[^"]*"|'[^']*'|[^"']+)\)$/;
22

3-
const getDeclsObject = rule => {
3+
const getDeclsObject = (rule) => {
44
const object = {};
55

6-
rule.walkDecls(decl => {
6+
rule.walkDecls((decl) => {
77
const before = decl.raws.before ? decl.raws.before.trim() : "";
88

99
object[before + decl.prop] = decl.value;
@@ -16,7 +16,7 @@ const extractICSS = (css, removeRules = true) => {
1616
const icssImports = {};
1717
const icssExports = {};
1818

19-
css.each(node => {
19+
css.each((node) => {
2020
if (node.type === "rule") {
2121
if (node.selector.slice(0, 7) === ":import") {
2222
const matches = importPattern.exec(node.selector);
@@ -48,4 +48,4 @@ const extractICSS = (css, removeRules = true) => {
4848
return { icssImports, icssExports };
4949
};
5050

51-
export default extractICSS;
51+
module.exports = extractICSS;

src/index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
export { default as replaceValueSymbols } from "./replaceValueSymbols.js";
2-
export { default as replaceSymbols } from "./replaceSymbols.js";
3-
export { default as extractICSS } from "./extractICSS.js";
4-
export { default as createICSSRules } from "./createICSSRules.js";
1+
const replaceValueSymbols = require("./replaceValueSymbols.js");
2+
const replaceSymbols = require("./replaceSymbols.js");
3+
const extractICSS = require("./extractICSS.js");
4+
const createICSSRules = require("./createICSSRules.js");
5+
6+
module.exports = {
7+
replaceValueSymbols,
8+
replaceSymbols,
9+
extractICSS,
10+
createICSSRules,
11+
};

src/replaceSymbols.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import replaceValueSymbols from "./replaceValueSymbols.js";
1+
const replaceValueSymbols = require("./replaceValueSymbols.js");
22

33
const replaceSymbols = (css, replacements) => {
4-
css.walk(node => {
4+
css.walk((node) => {
55
if (node.type === "decl" && node.value) {
66
node.value = replaceValueSymbols(node.value.toString(), replacements);
77
} else if (node.type === "rule" && node.selector) {
@@ -15,4 +15,4 @@ const replaceSymbols = (css, replacements) => {
1515
});
1616
};
1717

18-
export default replaceSymbols;
18+
module.exports = replaceSymbols;

src/replaceValueSymbols.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ const replaceValueSymbols = (value, replacements) => {
1919
return value;
2020
};
2121

22-
export default replaceValueSymbols;
22+
module.exports = replaceValueSymbols;

test/createICSSRules.test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import postcss from "postcss";
2-
import { createICSSRules } from "../src";
1+
const postcss = require("postcss");
2+
const { createICSSRules } = require("../src");
33

44
const run = (imports, exports) => {
55
return postcss
66
.root()
7-
.append(createICSSRules(imports, exports))
7+
.append(createICSSRules(imports, exports, postcss))
88
.toString();
99
};
1010

1111
test("create empty :import statement", () => {
1212
expect(
1313
run(
1414
{
15-
"path/file": {}
15+
"path/file": {},
1616
},
1717
{}
1818
)
@@ -24,8 +24,8 @@ test("create :import statement", () => {
2424
run(
2525
{
2626
"path/file": {
27-
e: "f"
28-
}
27+
e: "f",
28+
},
2929
},
3030
{}
3131
)
@@ -38,7 +38,7 @@ test("create :export statement", () => {
3838
{},
3939
{
4040
a: "b",
41-
c: "d"
41+
c: "d",
4242
}
4343
)
4444
).toEqual(":export {\n a: b;\n c: d\n}");
@@ -49,11 +49,11 @@ test("create :import and :export", () => {
4949
run(
5050
{
5151
colors: {
52-
a: "b"
53-
}
52+
a: "b",
53+
},
5454
},
5555
{
56-
c: "d"
56+
c: "d",
5757
}
5858
)
5959
).toEqual(":import('colors') {\n a: b\n}\n:export {\n c: d\n}");

0 commit comments

Comments
 (0)