Skip to content

Commit f148bec

Browse files
committed
fix: use hash-sum to get variable id
1 parent 6b1d77d commit f148bec

File tree

4 files changed

+15
-60
lines changed

4 files changed

+15
-60
lines changed

example/App.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default {
3131
}
3232
ul {
3333
list-style: square;
34+
color: ${vm => vm.color};
3435
}
3536
`,
3637
data() {

lib/parseScript.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const parser = require('@babel/parser')
22
const traverse = require('@babel/traverse')
33
const generator = require('@babel/generator')
44
const t = require('@babel/types')
5+
const hashSum = require('hash-sum')
56
const { UNITS_RE } = require('./units')
67

78
const LANGS = ['css', 'stylus', 'less', 'sass', 'scss']
@@ -26,17 +27,17 @@ module.exports = script => {
2627
]
2728
})
2829

29-
const parseTaggedTemplate = (ref, isGlobal) => {
30+
const parseTaggedTemplate = ref => {
3031
const { quasi } = ref.node
3132
const quasis = []
3233
const vars = []
3334
const varDeclarations = []
34-
const variablePrefix = isGlobal ? 'gv' : `v`
3535

3636
for (const [i, q] of quasi.quasis.entries()) {
3737
quasis.push(q)
3838
if (quasi.expressions[i]) {
39-
const value = `var(--${variablePrefix}${i})`
39+
const variableId = `v${hashSum(quasi.expressions[i])}`
40+
const value = `var(--${variableId})`
4041
quasis.push({
4142
type: 'TemplateElement',
4243
value: { raw: value, cooked: value }
@@ -58,16 +59,13 @@ module.exports = script => {
5859
// var v0 = vm => vm.color
5960
varDeclarations.push(
6061
t.variableDeclaration('var', [
61-
t.variableDeclarator(
62-
t.identifier(`${variablePrefix}${i}`),
63-
quasi.expressions[i]
64-
)
62+
t.variableDeclarator(t.identifier(variableId), quasi.expressions[i])
6563
])
6664
)
6765

6866
// { '--v0': v0(vm) }
6967
// { '--v0': v0(vm) + unit }
70-
const id = t.identifier(`${variablePrefix}${i}`)
68+
const id = t.identifier(variableId)
7169
const expType = quasi.expressions[i].type
7270
const mustBeFunction = [
7371
'FunctionExpression',
@@ -89,7 +87,7 @@ module.exports = script => {
8987
}
9088
vars.push(
9189
t.objectProperty(
92-
t.stringLiteral(`--${variablePrefix}${i}`),
90+
t.stringLiteral(`--${variableId}`),
9391
mustBeFunction
9492
? getValue(true)
9593
: mustNotBeFunction
@@ -166,8 +164,7 @@ module.exports = script => {
166164

167165
const isGlobal = propertyPath.node.key.name === 'globalStyle'
168166
const { vars, varDeclarations, extractedStyle } = parseTaggedTemplate(
169-
ref,
170-
isGlobal
167+
ref
171168
)
172169
if (isGlobal) {
173170
globalStyle = extractedStyle

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@babel/parser": "^7.2.3",
2727
"@babel/traverse": "^7.2.3",
2828
"@babel/types": "^7.2.2",
29+
"hash-sum": "^2.0.0",
2930
"posthtml": "^0.11.3",
3031
"vue-template-compiler": "^2.6.11"
3132
},

yarn.lock

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5258,6 +5258,11 @@ hash-sum@^1.0.2:
52585258
resolved "https://registry.npmjs.org/hash-sum/-/hash-sum-1.0.2.tgz#33b40777754c6432573c120cc3808bbd10d47f04"
52595259
integrity sha1-M7QHd3VMZDJXPBIMw4CLvRDUfwQ=
52605260

5261+
hash-sum@^2.0.0:
5262+
version "2.0.0"
5263+
resolved "https://registry.yarnpkg.com/hash-sum/-/hash-sum-2.0.0.tgz#81d01bb5de8ea4a214ad5d6ead1b523460b0b45a"
5264+
integrity sha512-WdZTbAByD+pHfl/g9QSsBIIwy8IT+EsPiKDs0KNX+zSHhdDLFKdZu0BQHljvO+0QI/BasbMSUa8wYNCZTvhslg==
5265+
52615266
hash.js@^1.0.0, hash.js@^1.0.3:
52625267
version "1.1.7"
52635268
resolved "https://registry.npmjs.org/hash.js/-/hash.js-1.1.7.tgz#0babca538e8d4ee4a0f8988d68866537a003cf42"
@@ -7846,14 +7851,6 @@ minipass@^2.2.1, minipass@^2.3.3, minipass@^2.3.4:
78467851
safe-buffer "^5.1.2"
78477852
yallist "^3.0.0"
78487853

7849-
minipass@^2.8.6, minipass@^2.9.0:
7850-
version "2.9.0"
7851-
resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.9.0.tgz#e713762e7d3e32fed803115cf93e04bca9fcc9a6"
7852-
integrity sha512-wxfUjg9WebH+CUDX/CdbRlh5SmfZiy/hpkxaRI16Y9W56Pa75sWgd/rvFilSgrauD9NyFymP/+JFV3KwzIsJeg==
7853-
dependencies:
7854-
safe-buffer "^5.1.2"
7855-
yallist "^3.0.0"
7856-
78577854
minipass@^3.0.0, minipass@^3.1.1:
78587855
version "3.1.1"
78597856
resolved "https://registry.yarnpkg.com/minipass/-/minipass-3.1.1.tgz#7607ce778472a185ad6d89082aa2070f79cedcd5"
@@ -7868,13 +7865,6 @@ minizlib@^1.1.1:
78687865
dependencies:
78697866
minipass "^2.2.1"
78707867

7871-
minizlib@^1.2.1:
7872-
version "1.3.3"
7873-
resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-1.3.3.tgz#2290de96818a34c29551c8a8d301216bd65a861d"
7874-
integrity sha512-6ZYMOEnmVsdCeTJVE0W9ZD+pVnE8h9Hma/iOwwRDsdQoePpoX56/8B6z3P9VNwppJuBKNRuFDRNRqRWexT9G9Q==
7875-
dependencies:
7876-
minipass "^2.9.0"
7877-
78787868
mississippi@^2.0.0:
78797869
version "2.0.0"
78807870
resolved "https://registry.npmjs.org/mississippi/-/mississippi-2.0.0.tgz#3442a508fafc28500486feea99409676e4ee5a6f"
@@ -8151,22 +8141,6 @@ node-notifier@^5.2.1:
81518141
shellwords "^0.1.1"
81528142
which "^1.3.0"
81538143

8154-
node-pre-gyp@*:
8155-
version "0.14.0"
8156-
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz#9a0596533b877289bcad4e143982ca3d904ddc83"
8157-
integrity sha512-+CvDC7ZttU/sSt9rFjix/P05iS43qHCOOGzcr3Ry99bXG7VX953+vFyEuph/tfqoYu8dttBkE86JSKBO2OzcxA==
8158-
dependencies:
8159-
detect-libc "^1.0.2"
8160-
mkdirp "^0.5.1"
8161-
needle "^2.2.1"
8162-
nopt "^4.0.1"
8163-
npm-packlist "^1.1.6"
8164-
npmlog "^4.0.2"
8165-
rc "^1.2.7"
8166-
rimraf "^2.6.1"
8167-
semver "^5.3.0"
8168-
tar "^4.4.2"
8169-
81708144
node-pre-gyp@^0.10.0:
81718145
version "0.10.3"
81728146
resolved "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.10.3.tgz#3070040716afdc778747b61b6887bf78880b80fc"
@@ -11559,19 +11533,6 @@ tar@^4, tar@^4.4.3, tar@^4.4.8:
1155911533
safe-buffer "^5.1.2"
1156011534
yallist "^3.0.2"
1156111535

11562-
tar@^4.4.2:
11563-
version "4.4.13"
11564-
resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.13.tgz#43b364bc52888d555298637b10d60790254ab525"
11565-
integrity sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA==
11566-
dependencies:
11567-
chownr "^1.1.1"
11568-
fs-minipass "^1.2.5"
11569-
minipass "^2.8.6"
11570-
minizlib "^1.2.1"
11571-
mkdirp "^0.5.0"
11572-
safe-buffer "^5.1.2"
11573-
yallist "^3.0.3"
11574-
1157511536
term-size@^1.2.0:
1157611537
version "1.2.0"
1157711538
resolved "https://registry.npmjs.org/term-size/-/term-size-1.2.0.tgz#458b83887f288fc56d6fffbfad262e26638efa69"
@@ -12729,11 +12690,6 @@ yallist@^3.0.0, yallist@^3.0.2:
1272912690
resolved "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9"
1273012691
integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==
1273112692

12732-
yallist@^3.0.3:
12733-
version "3.1.1"
12734-
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
12735-
integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==
12736-
1273712693
yallist@^4.0.0:
1273812694
version "4.0.0"
1273912695
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"

0 commit comments

Comments
 (0)