Skip to content

Commit 298a165

Browse files
authoredMar 26, 2019
fix: make fallback script work with multiple variables per line (#252)
1 parent f81c117 commit 298a165

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed
 

‎packages/base/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"url-search-params-polyfill": "^5.0.0"
2626
},
2727
"devDependencies": {
28+
"array-uniq": "^2.0.0",
2829
"eslint": "^5.13.0",
2930
"eslint-config-airbnb-base": "^13.1.0"
3031
},

‎packages/main/lib/postcss-add-fallback/index.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const postcss = require('postcss');
22
const fs = require('fs');
3+
const arrayUniq = require('array-uniq');
34

45
const findCSSVars = styleString => {
56
const vars = new Map();
@@ -24,12 +25,16 @@ module.exports = postcss.plugin('add fallback plugin', function (opts) {
2425

2526
root.walkDecls(decl => {
2627
// extract var name
27-
const match = decl.value.match(/var\((.+)\)/);
28-
if (match) {
29-
const varName = match[1];
30-
if (params.has(varName)) {
31-
decl.value = decl.value.replace(varName, `${varName}, ${params.get(varName)}`)
32-
}
28+
let matches = decl.value.match(/var\(([^\,\(\)]+)\)/g);
29+
matches = arrayUniq(matches);
30+
if (matches) {
31+
matches.forEach(varMatch => {
32+
const varNameMatch = varMatch.match(/var\((.*)\)/);
33+
const varName = varNameMatch[1];
34+
if (params.has(varName)) {
35+
decl.value = decl.value.replace(new RegExp(varName, 'g'), `${varName}, ${params.get(varName)}`)
36+
}
37+
});
3338
}
3439
params.set(decl.prop, decl.value);
3540
});

‎yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,11 @@ array-uniq@^1.0.1, array-uniq@^1.0.3:
12331233
resolved "https://registry.yarnpkg.com/array-uniq/-/array-uniq-1.0.3.tgz#af6ac877a25cc7f74e058894753858dfdb24fdb6"
12341234
integrity sha1-r2rId6Jcx/dOBYiUdThY39sk/bY=
12351235

1236+
array-uniq@^2.0.0:
1237+
version "2.0.0"
1238+
resolved "https://registry.yarnpkg.com/array-uniq/-/array-uniq-2.0.0.tgz#0009e30306e37a6dd2e2e2480db5316fdade1583"
1239+
integrity sha512-O3QZEr+3wDj7otzF7PjNGs6CA3qmYMLvt5xGkjY/V0VxS+ovvqVo/5wKM/OVOAyuX4DTh9H31zE/yKtO66hTkg==
1240+
12361241
array-unique@^0.2.1:
12371242
version "0.2.1"
12381243
resolved "https://registry.yarnpkg.com/array-unique/-/array-unique-0.2.1.tgz#a1d97ccafcbc2625cc70fadceb36a50c58b01a53"

0 commit comments

Comments
 (0)
Please sign in to comment.