Skip to content

Commit e72ecc8

Browse files
committed
scoped css: adjust id generation
1 parent f2af5db commit e72ecc8

File tree

5 files changed

+12
-21
lines changed

5 files changed

+12
-21
lines changed

lib/id-generator.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/style-rewriter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var postcss = require('postcss')
22
var selectorParser = require('postcss-selector-parser')
3-
var idGen = require('./id-generator')
3+
var hash = require('hash-sum')
44

55
var currentId
66
var addId = postcss.plugin('add-id', function () {
@@ -20,7 +20,7 @@ var addId = postcss.plugin('add-id', function () {
2020
module.exports = function (css) {
2121
this.cacheable()
2222
var cb = this.async()
23-
currentId = idGen.get(this.resourcePath)
23+
currentId = '_v-' + hash(this.resourcePath)
2424
postcss([addId])
2525
.process(css)
2626
.then(function (result) {

lib/template-rewriter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var parse5 = require('parse5')
22
var parser = new parse5.Parser()
33
var serializer = new parse5.Serializer()
4-
var idGen = require('./id-generator')
4+
var hash = require('hash-sum')
55

66
module.exports = function (html) {
77
this.cacheable()
8-
var id = idGen.get(this.resourcePath)
8+
var id = '_v-' + hash(this.resourcePath)
99
var tree = parser.parseFragment(html)
1010
walk(tree, function (node) {
1111
if (node.attrs) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"test": "eslint lib && mocha test/test.js --slow 5000"
2424
},
2525
"dependencies": {
26+
"hash-sum": "^1.0.2",
2627
"loader-utils": "^0.2.10",
2728
"parse5": "^1.5.0",
2829
"postcss": "^4.1.16",

test/test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ describe('vue-loader', function () {
9494
entry: './test/fixtures/scoped-css.js'
9595
}, function (window) {
9696
var module = window.testModule
97+
var id = '_v-' + hash(require.resolve('./fixtures/scoped-css.vue'))
9798
expect(module.template).to.contain(
98-
'<div _v-1=""><h1 _v-1="">hi</h1></div>\n' +
99-
'<p class="abc def" _v-1="">hi</p>'
99+
'<div ' + id + '=""><h1 ' + id + '="">hi</h1></div>\n' +
100+
'<p class="abc def" ' + id + '="">hi</p>'
100101
)
101102
var style = window.document.querySelector('style').textContent
102-
expect(style).to.contain('.test[_v-1] {\n color: yellow;\n}')
103-
expect(style).to.contain('h1[_v-1] {\n color: green;\n}')
103+
expect(style).to.contain('.test[' + id + '] {\n color: yellow;\n}')
104+
expect(style).to.contain('h1[' + id + '] {\n color: green;\n}')
104105
done()
105106
})
106107
})
@@ -112,7 +113,8 @@ describe('vue-loader', function () {
112113
var styles = window.document.querySelectorAll('style')
113114
expect(styles[0].textContent).to.contain('h1 { color: red; }')
114115
// import with scoped
115-
expect(styles[1].textContent).to.contain('h1[_v-2] { color: green; }')
116+
var id = '_v-' + hash(require.resolve('./fixtures/import-scoped.css'))
117+
expect(styles[1].textContent).to.contain('h1[' + id + '] { color: green; }')
116118
done()
117119
})
118120
})

0 commit comments

Comments
 (0)