Skip to content

Commit f8f6f79

Browse files
authored
Merge pull request #104 from css-modules/refactor
Refactor
2 parents e2e171a + d9f46c2 commit f8f6f79

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

src/index.js

+49-44
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,46 @@ const { default: replaceSymbols, replaceAll } = require('icss-replace-symbols')
44
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
55
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g
66
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
7-
let options = {}
7+
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+
844
let importIndex = 0
9-
let createImportedName =
10-
(options && options.createImportedName) ||
11-
((importName /*, path*/) =>
12-
`i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`)
45+
const createImportedName = importName =>
46+
`i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`
1347

1448
module.exports = postcss.plugin('postcss-modules-values', () => (
1549
css,
@@ -31,10 +65,10 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
3165
const addImport = atRule => {
3266
let matches = matchImports.exec(atRule.params)
3367
if (matches) {
34-
let [, aliases, path] = matches
68+
let [, aliasesString, path] = matches
3569
// We can use constants for path names
3670
if (definitions[path]) path = definitions[path]
37-
let imports = aliases
71+
let aliases = aliasesString
3872
.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1')
3973
.split(/\s*,\s*/)
4074
.map(alias => {
@@ -48,7 +82,11 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
4882
throw new Error(`@import statement "${alias}" is invalid!`)
4983
}
5084
})
51-
importAliases.push({ path, imports })
85+
.reduce((acc, { theirName, importedName }) => {
86+
acc[importedName] = theirName
87+
return acc
88+
}, {})
89+
importAliases.push({ path, aliases })
5290
atRule.remove()
5391
}
5492
}
@@ -66,46 +104,13 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
66104
}
67105
})
68106

69-
/* We want to export anything defined by now, but don't add it to the CSS yet or
70-
it well get picked up by the replacement stuff */
71-
let exportDeclarations = Object.keys(definitions).map(key =>
72-
postcss.decl({
73-
value: definitions[key],
74-
prop: key,
75-
raws: { before: '\n ' }
76-
})
77-
)
78-
79107
/* If we have no definitions, don't continue */
80-
if (!Object.keys(definitions).length) return
108+
if (Object.keys(definitions).length === 0) return
81109

82110
/* Perform replacements */
83111
replaceSymbols(css, definitions)
84112

85-
/* Add export rules if any */
86-
if (exportDeclarations.length > 0) {
87-
let exportRule = postcss.rule({
88-
selector: `:export`,
89-
raws: { after: '\n' }
90-
})
91-
exportRule.append(exportDeclarations)
92-
css.prepend(exportRule)
93-
}
94-
95-
/* Add import rules */
96-
importAliases.reverse().forEach(({ path, imports }) => {
97-
let importRule = postcss.rule({
98-
selector: `:import(${path})`,
99-
raws: { after: '\n' }
100-
})
101-
imports.forEach(({ theirName, importedName }) => {
102-
importRule.append({
103-
value: theirName,
104-
prop: importedName,
105-
raws: { before: '\n ' }
106-
})
107-
})
113+
addExportsRule(css, definitions)
108114

109-
css.prepend(importRule)
110-
})
115+
addImportsRules(css, importAliases)
111116
})

0 commit comments

Comments
 (0)