Skip to content

Commit 32e2c48

Browse files
committed
Purify icss utils
1 parent f8f6f79 commit 32e2c48

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

src/icss.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const postcss = require('postcss')
2+
3+
const genICSSImportsRules = imports => {
4+
return imports.map(({ path, aliases }) => {
5+
const declarations = Object.keys(aliases).map(key =>
6+
postcss.decl({
7+
prop: key,
8+
value: aliases[key],
9+
raws: { before: '\n ' }
10+
})
11+
)
12+
return postcss
13+
.rule({
14+
selector: `:import(${path})`,
15+
raws: { after: '\n' }
16+
})
17+
.append(declarations)
18+
})
19+
}
20+
21+
const genICSSExportsRule = exports => {
22+
const declarations = Object.keys(exports).map(key =>
23+
postcss.decl({
24+
prop: key,
25+
value: exports[key],
26+
raws: { before: '\n ' }
27+
})
28+
)
29+
return postcss
30+
.rule({
31+
selector: `:export`,
32+
raws: { after: '\n' }
33+
})
34+
.append(declarations)
35+
}
36+
37+
export const genICSSRules = (imports, exports) => [
38+
...genICSSImportsRules(imports),
39+
genICSSExportsRule(exports)
40+
]

src/index.js

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,11 @@
11
const postcss = require('postcss')
22
const { default: replaceSymbols, replaceAll } = require('icss-replace-symbols')
3+
const { genICSSRules } = require('./icss.js')
34

45
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
56
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g
67
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
78

8-
const addImportsRules = (css, imports) => {
9-
const rules = imports.map(({ path, aliases }) => {
10-
const declarations = Object.keys(aliases).map(key =>
11-
postcss.decl({
12-
prop: key,
13-
value: aliases[key],
14-
raws: { before: '\n ' }
15-
})
16-
)
17-
return postcss
18-
.rule({
19-
selector: `:import(${path})`,
20-
raws: { after: '\n' }
21-
})
22-
.append(declarations)
23-
})
24-
css.prepend(rules)
25-
}
26-
27-
const addExportsRule = (css, exports) => {
28-
const declarations = Object.keys(exports).map(key =>
29-
postcss.decl({
30-
prop: key,
31-
value: exports[key],
32-
raws: { before: '\n ' }
33-
})
34-
)
35-
const rule = postcss
36-
.rule({
37-
selector: `:export`,
38-
raws: { after: '\n' }
39-
})
40-
.append(declarations)
41-
css.prepend(rule)
42-
}
43-
449
let importIndex = 0
4510
const createImportedName = importName =>
4611
`i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`
@@ -74,7 +39,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
7439
.map(alias => {
7540
let tokens = matchImport.exec(alias)
7641
if (tokens) {
77-
let [, /*match*/ theirName, myName = theirName] = tokens
42+
let [, theirName, myName = theirName] = tokens
7843
let importedName = createImportedName(myName)
7944
definitions[myName] = importedName
8045
return { theirName, importedName }
@@ -110,7 +75,5 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
11075
/* Perform replacements */
11176
replaceSymbols(css, definitions)
11277

113-
addExportsRule(css, definitions)
114-
115-
addImportsRules(css, importAliases)
78+
css.prepend(genICSSRules(importAliases, definitions))
11679
})

0 commit comments

Comments
 (0)