Skip to content

Commit 6ee4754

Browse files
chore(release): 4.0.0
1 parent 911bfc6 commit 6ee4754

11 files changed

+3328
-1762
lines changed

.travis.yml

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
1+
sudo: false
2+
3+
git:
4+
depth: 10
5+
6+
branches:
7+
only:
8+
- master
9+
110
language: node_js
2-
node_js:
3-
- "4"
4-
- "6"
5-
- "node"
11+
12+
# cache node modules
13+
cache:
14+
directories:
15+
- $HOME/.npm
16+
- node_modules
17+
18+
matrix:
19+
fast_finish: true
20+
include:
21+
- node_js: '10'
22+
script: npm run test -- --no-coverage
23+
- node_js: '8'
24+
script: npm run test -- --no-coverage
25+
- node_js: '6'
26+
script: npm run test -- --no-coverage
27+
28+
before_install:
29+
- npm install -g npm@latest
30+
- node --version
31+
- npm --version

README.md

+20-16
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,19 +31,19 @@ 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(`
37-
:import(colors) {
38+
:import("colors.css") {
3839
a: b;
3940
}
4041
:export {
4142
c: d;
4243
}
43-
`)
44+
`);
4445

45-
extractICSS(css)
46+
extractICSS(css);
4647
/*
4748
{
4849
icssImports: {
@@ -62,18 +63,21 @@ 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+
Glen Maddern, Bogdan Chadkin and Evilebottnawi 2015-present.

package.json

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "icss-utils",
3-
"version": "3.0.1",
3+
"version": "4.0.0",
44
"description": "ICSS utils for postcss ast",
55
"main": "lib/index.js",
66
"files": [
@@ -20,21 +20,23 @@
2020
]
2121
},
2222
"eslintConfig": {
23+
"parser": "babel-eslint",
2324
"parserOptions": {
2425
"sourceType": "module"
2526
},
2627
"env": {
27-
"es6": true
28+
"es6": true,
29+
"jest": true
2830
},
2931
"extends": "eslint:recommended"
3032
},
3133
"babel": {
3234
"presets": [
3335
[
34-
"env",
36+
"@babel/preset-env",
3537
{
3638
"targets": {
37-
"node": 4
39+
"node": 6
3840
}
3941
}
4042
]
@@ -57,16 +59,19 @@
5759
},
5860
"homepage": "https://github.com/css-modules/icss-utils#readme",
5961
"dependencies": {
60-
"postcss": "^6.0.2"
62+
"postcss": "^7.0.0"
6163
},
6264
"devDependencies": {
63-
"babel-cli": "^6.24.1",
64-
"babel-jest": "^20.0.3",
65-
"babel-preset-env": "^1.5.2",
66-
"eslint": "^4.0.0",
67-
"husky": "^0.13.4",
68-
"jest": "^20.0.4",
69-
"lint-staged": "^3.6.1",
65+
"@babel/cli": "^7.1.0",
66+
"@babel/core": "^7.1.0",
67+
"@babel/preset-env": "^7.1.0",
68+
"babel-eslint": "^9.0.0",
69+
"babel-core": "7.0.0-bridge.0",
70+
"babel-jest": "^23.6.0",
71+
"eslint": "^5.6.0",
72+
"husky": "^0.14.1",
73+
"jest": "^23.6.0",
74+
"lint-staged": "^7.3.0",
7075
"prettier": "^1.4.4"
7176
}
7277
}

src/createICSSRules.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import postcss from "postcss";
22

33
const createImports = imports => {
4-
return Object.keys(imports).map(path => {
5-
const aliases = imports[path];
4+
return Object.keys(imports).map(token => {
5+
const aliases = imports[token];
66
const declarations = Object.keys(aliases).map(key =>
77
postcss.decl({
88
prop: key,
@@ -12,7 +12,7 @@ const createImports = imports => {
1212
);
1313
return postcss
1414
.rule({
15-
selector: `:import('${path}')`,
15+
selector: `:import(${token})`,
1616
raws: { after: "\n" }
1717
})
1818
.append(declarations);

src/extractICSS.js

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

33
const getDeclsObject = rule => {
44
const object = {};
@@ -17,11 +17,17 @@ const extractICSS = (css, removeRules = true) => {
1717
const matches = importPattern.exec(node.selector);
1818
if (matches) {
1919
const path = matches[1].replace(/'|"/g, "");
20-
const aliases = Object.assign(
21-
icssImports[path] || {},
22-
getDeclsObject(node)
23-
);
24-
icssImports[path] = aliases;
20+
const extra = matches[2] ? matches[2] : "";
21+
const dep = `"${path}"${extra ? ` ${extra}` : ""}`;
22+
icssImports[dep] = Object.assign(icssImports[dep] || {}, {
23+
path,
24+
extra,
25+
tokens: Object.assign(
26+
{},
27+
icssImports[dep] ? icssImports[dep].tokens : {},
28+
getDeclsObject(node)
29+
)
30+
});
2531
if (removeRules) {
2632
node.remove();
2733
}

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
});

test/createICSSRules.test.js

+54-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
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

9-
test("create :import statement", () => {
11+
test("create :import statement with single quotes", () => {
1012
expect(
1113
run(
1214
{
13-
"path/file": {
15+
"'path/file'": {
1416
e: "f"
1517
}
1618
},
@@ -19,6 +21,52 @@ test("create :import statement", () => {
1921
).toEqual(":import('path/file') {\n e: f\n}");
2022
});
2123

24+
test("create :import statement with double quotes", () => {
25+
expect(
26+
run(
27+
{
28+
'"path/file"': {
29+
e: "f"
30+
}
31+
},
32+
{}
33+
)
34+
).toEqual(':import("path/file") {\n e: f\n}');
35+
});
36+
37+
test("create multiple :import statement", () => {
38+
expect(
39+
run(
40+
{
41+
"path/file": {
42+
e: "f"
43+
},
44+
"path/other": {
45+
a: "b"
46+
}
47+
},
48+
{}
49+
)
50+
).toEqual(
51+
":import(path/file) {\n e: f\n}\n:import(path/other) {\n a: b\n}"
52+
);
53+
});
54+
55+
test("create :import statement with extra", () => {
56+
expect(
57+
run(
58+
{
59+
"'path/file' screen and (orientation:landscape)": {
60+
e: "f"
61+
}
62+
},
63+
{}
64+
)
65+
).toEqual(
66+
":import('path/file' screen and (orientation:landscape)) {\n e: f\n}"
67+
);
68+
});
69+
2270
test("create :export statement", () => {
2371
expect(
2472
run(
@@ -35,13 +83,13 @@ test("create :import and :export", () => {
3583
expect(
3684
run(
3785
{
38-
colors: {
86+
'"colors"': {
3987
a: "b"
4088
}
4189
},
4290
{
4391
c: "d"
4492
}
4593
)
46-
).toEqual(":import('colors') {\n a: b\n}\n:export {\n c: d\n}");
94+
).toEqual(':import("colors") {\n a: b\n}\n:export {\n c: d\n}');
4795
});

0 commit comments

Comments
 (0)