Skip to content

Commit e98edb2

Browse files
YiSiWangYiSiWang
authored andcommitted
add localIdentName support
1 parent e388186 commit e98edb2

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

lib/loader.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,22 @@ module.exports = function (content) {
101101

102102
function addCssModulesToLoader (loader, part, index) {
103103
if (!part.module) return loader
104+
var option = options.cssModules || {}
104105
return loader.replace(/((?:^|!)css(?:-loader)?)(\?[^!]*)?/, function (m, $1, $2) {
105106
// $1: !css-loader
106107
// $2: ?a=b
107-
var option = loaderUtils.parseQuery($2)
108-
option.modules = true
109-
option.importLoaders = true
110-
option.localIdentName = '[hash:base64]'
108+
var query = loaderUtils.parseQuery($2)
109+
query.modules = true
110+
query.importLoaders = true
111+
query.localIdentName = option.localIdentName || '[hash:base64]'
111112
if (index !== -1) {
112113
// Note:
113114
// Class name is generated according to its filename.
114115
// Different <style> tags in the same .vue file may generate same names.
115116
// Append `_[index]` to class name to avoid this.
116-
option.localIdentName += '_' + index
117+
query.localIdentName += '_' + index
117118
}
118-
return $1 + '?' + JSON.stringify(option)
119+
return $1 + '?' + JSON.stringify(query)
119120
})
120121
}
121122

test/test.js

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -379,34 +379,68 @@ describe('vue-loader', function () {
379379
})
380380
})
381381

382-
it.only('css-modules', function (done) {
382+
it('css-modules', function (done) {
383+
function testWithIdent (localIdentName, regexToMatch, cb) {
384+
test({
385+
entry: './test/fixtures/css-modules.vue',
386+
vue: {
387+
cssModules: {
388+
localIdentName: localIdentName
389+
}
390+
}
391+
}, function (window) {
392+
var module = window.vueModule
393+
394+
// get local class name
395+
var className = module.computed.style().red
396+
expect(className).to.match(regexToMatch)
397+
398+
// class name in style
399+
var style = [].slice.call(window.document.querySelectorAll('style')).map(function (style) {
400+
return style.textContent
401+
}).join('\n')
402+
expect(style).to.contain('.' + className + ' {\n color: red;\n}')
403+
404+
// animation name
405+
var match = style.match(/@keyframes\s+(\S+)\s+{/)
406+
expect(match).to.have.length(2)
407+
var animationName = match[1]
408+
expect(animationName).to.not.equal('fade')
409+
expect(style).to.contain('animation: ' + animationName + ' 1s;')
410+
411+
// default module + pre-processor + scoped
412+
var anotherClassName = module.computed.$style().red
413+
expect(anotherClassName).to.match(regexToMatch).and.not.equal(className)
414+
var id = 'data-v-' + genId(require.resolve('./fixtures/css-modules.vue'))
415+
expect(style).to.contain('.' + anotherClassName + '[' + id + ']')
416+
417+
cb()
418+
})
419+
}
420+
// default localIdentName
421+
testWithIdent(undefined, /^_\w{22}/, function () {
422+
// specified localIdentName
423+
var ident = '[path][name]---[local]---[hash:base64:5]'
424+
var regex = /^test-fixtures-css-modules---red---\w{5}/
425+
testWithIdent(ident, regex, done)
426+
})
427+
})
428+
429+
it.only('css-modules in SSR', function (done) {
383430
test({
384-
entry: './test/fixtures/css-modules.vue'
431+
entry: './test/fixtures/css-modules.vue',
432+
vue: {
433+
target: 'node'
434+
}
385435
}, function (window) {
386436
var module = window.vueModule
387437

388-
// get local class name
438+
// class name
389439
var className = module.computed.style().red
390440
expect(className).to.match(/^_/)
391441

392-
// class name in style
393-
var style = [].slice.call(window.document.querySelectorAll('style')).map(function (style) {
394-
return style.textContent
395-
}).join('\n')
396-
expect(style).to.contain('.' + className + ' {\n color: red;\n}')
397-
398-
// animation name
399-
var match = style.match(/@keyframes\s+(\S+)\s+{/)
400-
expect(match).to.have.length(2)
401-
var animationName = match[1]
402-
expect(animationName).to.not.equal('fade')
403-
expect(style).to.contain('animation: ' + animationName + ' 1s;')
404-
405-
// default module + pre-processor + scoped
406-
var anotherClassName = module.computed.$style().red
407-
expect(anotherClassName).to.match(/^_/).and.not.equal(className)
408-
var id = 'data-v-' + genId(require.resolve('./fixtures/css-modules.vue'))
409-
expect(style).to.contain('.' + anotherClassName + '[' + id + ']')
442+
// no css
443+
expect(window.document.querySelector('style')).to.not.exist()
410444

411445
done()
412446
})

0 commit comments

Comments
 (0)