Skip to content

Commit bb6924a

Browse files
author
YiSiWang
committed
add localIdentName support
1 parent e388186 commit bb6924a

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
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: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -380,35 +380,49 @@ describe('vue-loader', function () {
380380
})
381381

382382
it.only('css-modules', function (done) {
383-
test({
384-
entry: './test/fixtures/css-modules.vue'
385-
}, function (window) {
386-
var module = window.vueModule
387-
388-
// get local class name
389-
var className = module.computed.style().red
390-
expect(className).to.match(/^_/)
391-
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 + ']')
410-
411-
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)
412426
})
413427
})
414428
})

0 commit comments

Comments
 (0)