Skip to content

Commit 98d6b26

Browse files
chore(release): 4.1.0 (#61)
1 parent 43168ab commit 98d6b26

12 files changed

+3719
-2649
lines changed

.travis.yml

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
1+
sudo: false
2+
3+
git:
4+
depth: 10
5+
6+
branches:
7+
only:
8+
- master
9+
- next
10+
111
language: node_js
2-
node_js:
3-
- "6"
4-
- "node"
12+
13+
cache:
14+
yarn: true
15+
directories:
16+
- node_modules
17+
18+
matrix:
19+
include:
20+
- node_js: "11"
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: "11"
29+
env: JOB_PART=test
30+
31+
before_script:
32+
- node --version
33+
- yarn --version
34+
35+
script:
36+
- yarn $JOB_PART

README.md

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![Build Status](https://travis-ci.org/css-modules/icss-utils.svg)](https://travis-ci.org/css-modules/icss-utils)
22

3-
# ICSS Utils
3+
# ICSS Utils
44

55
## replaceSymbols
66

@@ -10,9 +10,10 @@ This is broken into its own module in case the behaviour needs to be replicated
1010
(i.e. [CSS Modules Values](https://github.com/css-modules/postcss-modules-values))
1111

1212
```js
13-
import { replaceSymbols, replaceValueSymbols } from "icss-utils"
14-
replaceSymbols(css, replacements)
15-
replaceValueSymbols(string, replacements)
13+
import { replaceSymbols, replaceValueSymbols } from "icss-utils";
14+
15+
replaceSymbols(css, replacements);
16+
replaceValueSymbols(string, replacements);
1617
```
1718

1819
Where:
@@ -30,8 +31,8 @@ A symbol is a string of alphanumeric, `-` or `_` characters. A replacement can b
3031
Extracts and remove (if removeRules is equal true) from PostCSS tree `:import` and `:export` statements.
3132

3233
```js
33-
import postcss from 'postcss';
34-
import { extractICSS } from 'icss-utils'
34+
import postcss from "postcss";
35+
import { extractICSS } from "icss-utils";
3536

3637
const css = postcss.parse(`
3738
:import(colors) {
@@ -40,9 +41,9 @@ const css = postcss.parse(`
4041
:export {
4142
c: d;
4243
}
43-
`)
44+
`);
4445

45-
extractICSS(css)
46+
extractICSS(css);
4647
/*
4748
{
4849
icssImports: {
@@ -62,18 +63,22 @@ extractICSS(css)
6263
Converts icss imports and exports definitions to postcss ast
6364

6465
```js
65-
createICSSRules({
66-
colors: {
67-
a: 'b'
66+
createICSSRules(
67+
{
68+
colors: {
69+
a: "b"
70+
}
71+
},
72+
{
73+
c: "d"
6874
}
69-
}, {
70-
c: 'd'
71-
})
75+
);
7276
```
7377

7478
## License
7579

7680
ISC
7781

7882
---
79-
Glen Maddern and Bogdan Chadkin, 2015.
83+
84+
Glen Maddern, Bogdan Chadkin and Evilebottnawi 2015-present.

package.json

+39-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "icss-utils",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"description": "ICSS utils for postcss ast",
55
"main": "lib/index.js",
66
"engines": {
@@ -10,39 +10,48 @@
1010
"lib"
1111
],
1212
"scripts": {
13+
"lint": "eslint . --ignore-path .gitignore",
1314
"build": "babel --out-dir lib src",
14-
"test": "jest --coverage",
15-
"precommit": "lint-staged",
15+
"pretest": "npm run lint",
16+
"test": "npm run test:only",
17+
"test:only": "jest",
1618
"prepublish": "yarn test && yarn run build"
1719
},
18-
"lint-staged": {
19-
"*.js": [
20-
"prettier --write",
21-
"eslint",
22-
"git add"
20+
"babel": {
21+
"presets": [
22+
[
23+
"@babel/preset-env",
24+
{
25+
"targets": {
26+
"node": 6
27+
}
28+
}
29+
]
2330
]
2431
},
2532
"eslintConfig": {
33+
"parser": "babel-eslint",
2634
"parserOptions": {
2735
"sourceType": "module"
2836
},
2937
"env": {
30-
"es6": true
38+
"es6": true,
39+
"jest": true
3140
},
3241
"extends": "eslint:recommended"
3342
},
34-
"babel": {
35-
"presets": [
36-
[
37-
"env",
38-
{
39-
"targets": {
40-
"node": 6
41-
}
42-
}
43-
]
43+
"lint-staged": {
44+
"*.js": [
45+
"prettier --write",
46+
"eslint",
47+
"git add"
4448
]
4549
},
50+
"husky": {
51+
"hooks": {
52+
"pre-commit": "lint-staged"
53+
}
54+
},
4655
"repository": {
4756
"type": "git",
4857
"url": "git+https://github.com/css-modules/icss-utils.git"
@@ -60,16 +69,18 @@
6069
},
6170
"homepage": "https://github.com/css-modules/icss-utils#readme",
6271
"dependencies": {
63-
"postcss": "^7.0.5"
72+
"postcss": "^7.0.14"
6473
},
6574
"devDependencies": {
66-
"babel-cli": "^6.24.1",
67-
"babel-jest": "^20.0.3",
68-
"babel-preset-env": "^1.5.2",
69-
"eslint": "^4.0.0",
70-
"husky": "^0.13.4",
71-
"jest": "^20.0.4",
72-
"lint-staged": "^3.6.1",
73-
"prettier": "^1.4.4"
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"
7485
}
7586
}

src/createICSSRules.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ const createImports = imports => {
1010
raws: { before: "\n " }
1111
})
1212
);
13-
return postcss
14-
.rule({
15-
selector: `:import('${path}')`,
16-
raws: { after: "\n" }
17-
})
18-
.append(declarations);
13+
14+
const hasDeclarations = declarations.length > 0;
15+
16+
const rule = postcss.rule({
17+
selector: `:import('${path}')`,
18+
raws: { after: hasDeclarations ? "\n" : "" }
19+
});
20+
21+
if (hasDeclarations) {
22+
rule.append(declarations);
23+
}
24+
25+
return rule;
1926
});
2027
};
2128

@@ -27,15 +34,18 @@ const createExports = exports => {
2734
raws: { before: "\n " }
2835
})
2936
);
37+
3038
if (declarations.length === 0) {
3139
return [];
3240
}
41+
3342
const rule = postcss
3443
.rule({
3544
selector: `:export`,
3645
raws: { after: "\n" }
3746
})
3847
.append(declarations);
48+
3949
return [rule];
4050
};
4151

src/extractICSS.js

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

33
const getDeclsObject = rule => {
44
const object = {};
5+
56
rule.walkDecls(decl => {
67
const before = decl.raws.before ? decl.raws.before.trim() : "";
8+
79
object[before + decl.prop] = decl.value;
810
});
11+
912
return object;
1013
};
1114

1215
const extractICSS = (css, removeRules = true) => {
1316
const icssImports = {};
1417
const icssExports = {};
18+
1519
css.each(node => {
1620
if (node.type === "rule") {
1721
if (node.selector.slice(0, 7) === ":import") {
1822
const matches = importPattern.exec(node.selector);
23+
1924
if (matches) {
2025
const path = matches[1].replace(/'|"/g, "");
21-
const aliases = Object.assign(
26+
27+
icssImports[path] = Object.assign(
2228
icssImports[path] || {},
2329
getDeclsObject(node)
2430
);
25-
icssImports[path] = aliases;
31+
2632
if (removeRules) {
2733
node.remove();
2834
}
2935
}
3036
}
37+
3138
if (node.selector === ":export") {
3239
Object.assign(icssExports, getDeclsObject(node));
40+
3341
if (removeRules) {
3442
node.remove();
3543
}
3644
}
3745
}
3846
});
47+
3948
return { icssImports, icssExports };
4049
};
4150

src/replaceSymbols.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const replaceSymbols = (css, replacements) => {
66
node.value = replaceValueSymbols(node.value, replacements);
77
} else if (node.type === "rule") {
88
node.selector = replaceValueSymbols(node.selector, replacements);
9-
} else if (node.type === "atrule" && node.name === "media") {
9+
} else if (
10+
node.type === "atrule" &&
11+
["media", "supports"].includes(node.name.toLowerCase())
12+
) {
1013
node.params = replaceValueSymbols(node.params, replacements);
1114
}
1215
});

src/replaceValueSymbols.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ const matchValueName = /[$]?[\w-]+/g;
22

33
const replaceValueSymbols = (value, replacements) => {
44
let matches;
5+
56
while ((matches = matchValueName.exec(value))) {
67
const replacement = replacements[matches[0]];
8+
79
if (replacement) {
810
value =
911
value.slice(0, matches.index) +
1012
replacement +
1113
value.slice(matchValueName.lastIndex);
14+
1215
matchValueName.lastIndex -= matches[0].length - replacement.length;
1316
}
1417
}
18+
1519
return value;
1620
};
1721

test/createICSSRules.test.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
/* eslint-env jest */
21
import postcss from "postcss";
32
import { createICSSRules } from "../src";
43

54
const run = (imports, exports) => {
6-
return postcss.root().append(createICSSRules(imports, exports)).toString();
5+
return postcss
6+
.root()
7+
.append(createICSSRules(imports, exports))
8+
.toString();
79
};
810

11+
test("create empty :import statement", () => {
12+
expect(
13+
run(
14+
{
15+
"path/file": {}
16+
},
17+
{}
18+
)
19+
).toEqual(":import('path/file') {}");
20+
});
21+
922
test("create :import statement", () => {
1023
expect(
1124
run(

test/extractICSS.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-env jest */
21
import postcss from "postcss";
32
import { extractICSS } from "../src";
43

0 commit comments

Comments
 (0)