Skip to content

Commit a852824

Browse files
committed
Remove path replacements
1 parent e84686a commit a852824

File tree

4 files changed

+659
-336
lines changed

4 files changed

+659
-336
lines changed

package.json

+19-8
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@
1414
},
1515
"lint-staged": {
1616
"*.js": [
17-
"prettier --single-quote --no-semi --write",
17+
"prettier --write",
18+
"eslint",
1819
"git add"
1920
]
2021
},
22+
"eslintConfig": {
23+
"parserOptions": {
24+
"sourceType": "module"
25+
},
26+
"env": {
27+
"es6": true
28+
},
29+
"extends": "eslint:recommended"
30+
},
2131
"babel": {
2232
"presets": [
2333
[
@@ -48,15 +58,16 @@
4858
"devDependencies": {
4959
"babel-cli": "^6.5.2",
5060
"babel-jest": "^20.0.3",
51-
"babel-preset-env": "^1.5.0",
52-
"husky": "^0.13.3",
53-
"jest": "^20.0.3",
54-
"lint-staged": "^3.4.2",
55-
"prettier": "^1.3.1",
61+
"babel-preset-env": "^1.5.2",
62+
"eslint": "^4.0.0",
63+
"husky": "^0.13.4",
64+
"jest": "^20.0.4",
65+
"lint-staged": "^3.6.1",
66+
"prettier": "^1.4.4",
5667
"strip-indent": "^2.0.0"
5768
},
5869
"dependencies": {
59-
"icss-utils": "^2.0.0",
60-
"postcss": "^6.0.1"
70+
"icss-utils": "^3.0.1",
71+
"postcss": "^6.0.2"
6172
}
6273
}

src/index.js

+49-51
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,88 @@
1-
import postcss from 'postcss'
1+
/* eslint-env node */
2+
import postcss from "postcss";
23
import {
34
replaceSymbols,
45
replaceValueSymbols,
56
extractICSS,
67
createICSSRules
7-
} from 'icss-utils'
8+
} from "icss-utils";
89

9-
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
10-
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g
11-
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
10+
const plugin = "postcss-modules-values";
11+
12+
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
13+
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g;
14+
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
1215

1316
// 'i' prefix to prevent postcss parsing "_" as css hook
1417
const getAliasName = (name, index) =>
15-
`i__value_${name.replace(/\W/g, '_')}_${index}`
18+
`__value__${name.replace(/\W/g, "_")}__${index}`;
1619

17-
module.exports = postcss.plugin('postcss-modules-values', () => (
18-
css,
19-
result
20-
) => {
21-
const { icssImports, icssExports } = extractICSS(css)
22-
let importIndex = 0
20+
module.exports = postcss.plugin(plugin, () => (css, result) => {
21+
const { icssImports, icssExports } = extractICSS(css);
22+
let importIndex = 0;
2323
const createImportedName = (path, name) => {
24-
const importedName = getAliasName(name, importIndex)
24+
const importedName = getAliasName(name, importIndex);
2525
if (icssImports[path] && icssImports[path][importedName]) {
26-
importIndex += 1
27-
return createImportedName(path, name)
26+
importIndex += 1;
27+
return createImportedName(path, name);
2828
}
29-
importIndex += 1
30-
return importedName
31-
}
29+
importIndex += 1;
30+
return importedName;
31+
};
3232

3333
const addDefinition = atRule => {
34-
let matches
34+
let matches;
3535
while ((matches = matchValueDefinition.exec(atRule.params))) {
36-
let [, key, value] = matches
36+
let [, key, value] = matches;
3737
// Add to the definitions, knowing that values can refer to each other
38-
icssExports[key] = replaceValueSymbols(value, icssExports)
39-
atRule.remove()
38+
icssExports[key] = replaceValueSymbols(value, icssExports);
39+
atRule.remove();
4040
}
41-
}
41+
};
4242

4343
const addImport = atRule => {
44-
let matches = matchImports.exec(atRule.params)
44+
let matches = matchImports.exec(atRule.params);
4545
if (matches) {
46-
let [, aliasesString, path] = matches
47-
// We can use constants for path names
48-
if (icssExports[path]) path = icssExports[path]
46+
const aliasesString = matches[1];
47+
let path = matches[2];
48+
path = path[0] === "'" || path[0] === '"' ? path.slice(1, -1) : path;
4949
let aliases = aliasesString
50-
.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1')
50+
.replace(/^\(\s*([\s\S]+)\s*\)$/, "$1")
5151
.split(/\s*,\s*/)
5252
.map(alias => {
53-
let tokens = matchImport.exec(alias)
53+
let tokens = matchImport.exec(alias);
5454
if (tokens) {
55-
let [, theirName, myName = theirName] = tokens
56-
let importedName = createImportedName(path, myName)
57-
icssExports[myName] = importedName
58-
return { theirName, importedName }
55+
let [, theirName, myName = theirName] = tokens;
56+
let importedName = createImportedName(path, myName);
57+
icssExports[myName] = importedName;
58+
return { theirName, importedName };
5959
} else {
60-
throw new Error(`@import statement "${alias}" is invalid!`)
60+
throw new Error(`@import statement "${alias}" is invalid!`);
6161
}
6262
})
6363
.reduce((acc, { theirName, importedName }) => {
64-
acc[importedName] = theirName
65-
return acc
66-
}, {})
67-
icssImports[path] = Object.assign({}, icssImports[path], aliases)
68-
atRule.remove()
64+
acc[importedName] = theirName;
65+
return acc;
66+
}, {});
67+
icssImports[path] = Object.assign({}, icssImports[path], aliases);
68+
atRule.remove();
6969
}
70-
}
70+
};
7171

7272
/* Look at all the @value statements and treat them as locals or as imports */
73-
css.walkAtRules('value', atRule => {
73+
css.walkAtRules("value", atRule => {
7474
if (matchImports.exec(atRule.params)) {
75-
addImport(atRule)
75+
addImport(atRule);
7676
} else {
77-
if (atRule.params.indexOf('@value') !== -1) {
78-
result.warn('Invalid value definition: ' + atRule.params)
77+
if (atRule.params.indexOf("@value") !== -1) {
78+
result.warn("Invalid value definition: " + atRule.params);
7979
}
8080

81-
addDefinition(atRule)
81+
addDefinition(atRule);
8282
}
83-
})
84-
85-
if (Object.keys(icssExports).length === 0) return
83+
});
8684

87-
replaceSymbols(css, icssExports)
85+
replaceSymbols(css, icssExports);
8886

89-
css.prepend(createICSSRules(icssImports, icssExports))
90-
})
87+
css.prepend(createICSSRules(icssImports, icssExports));
88+
});

0 commit comments

Comments
 (0)