1
- import postcss from 'postcss';
1
+ import postcss from 'postcss'
2
+ import replaceSymbols from './replace-symbols'
2
3
3
- const matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*')$/
4
+ const matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*'|[\w-]+ )$/
4
5
const matchLet = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|[^,]+)\s?/g
5
- const matchConstName = /[\w-]+/g
6
6
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
7
7
let options = {}
8
8
let importIndex = 0
9
9
let createImportedName = options && options.createImportedName || ((importName/*, path*/) => `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`)
10
10
11
- const replace = (declarations, object, propName) => {
12
- let matches
13
- while (matches = matchConstName.exec(object[propName])) {
14
- let replacement = declarations[matches[0]]
15
- if (replacement) {
16
- object[propName] = object[propName].slice(0, matches.index) + replacement + object[propName].slice(matchConstName.lastIndex)
17
- matchConstName.lastIndex -= matches[0].length - replacement.length
18
- }
19
- }
20
- }
21
-
22
11
export default css => {
23
12
/* Find any local let rules and store them*/
24
- let declarations = {}
13
+ let translations = {}
25
14
css.eachAtRule(/^-?let$/, atRule => {
26
15
let matches
27
16
while (matches = matchLet.exec(atRule.params)) {
28
17
let [/*match*/, key, value] = matches
29
- declarations[key] = value
18
+ translations[key] = value
19
+ atRule.removeSelf()
30
20
}
31
21
})
32
22
33
- console.log(declarations)
34
23
/* We want to export anything defined by now, but don't add it to the CSS yet or
35
24
it well get picked up by the replacement stuff */
36
- let exportDeclarations = Object.keys(declarations ).map(key => postcss.decl({
37
- value: declarations [key],
25
+ let exportDeclarations = Object.keys(translations ).map(key => postcss.decl({
26
+ value: translations [key],
38
27
prop: key,
39
28
before: "\n ",
40
29
_autoprefixerDisabled: true
@@ -46,23 +35,27 @@ export default css => {
46
35
let matches = matchImports.exec(atRule.params)
47
36
if (matches) {
48
37
let [/*match*/, aliases, path] = matches
38
+ // We can use constants for path names
39
+ if (translations[path]) path = translations[path]
49
40
let imports = aliases.split(/\s*,\s*/).map(alias => {
50
41
let tokens = matchImport.exec(alias)
51
42
if (tokens) {
52
43
let [/*match*/, theirName, myName = theirName] = tokens
53
44
let importedName = createImportedName(myName)
54
- declarations [myName] = importedName
45
+ translations [myName] = importedName
55
46
return {theirName, importedName}
56
47
} else {
57
48
throw new Error(`@import statement "${alias}" is invalid!`)
58
49
}
59
50
})
60
51
importAliases.push({path, imports})
52
+ atRule.removeSelf()
61
53
}
62
54
})
63
55
56
+ console.log(translations)
64
57
/* Perform replacements */
65
- css.eachDecl(decl => replace(declarations, decl, 'value') )
58
+ replaceSymbols(css, translations )
66
59
67
60
/* Add import rules */
68
61
importAliases.forEach(({path, imports}) => {
0 commit comments