Skip to content

Commit 31cdc86

Browse files
n-zeployyx990803
authored andcommitted
feat(css modules): Add CSS Module localIdentName option to vue config (#915)
Changed the vue config to take a user input option for CSS module localIdentName and default back to initial localIdentName ([name]_[local]__[hash:base64:5])
1 parent bbc931c commit 31cdc86

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

docs/css.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ As for standalone style files, any files ending with `.module.(css|sass|scss|les
1717

1818
If you wish to be able to use CSS modules without the `.module` postfix, you can set `css: { modules: true }` in `vue.config.js`. This option does not affect `*.vue` files.
1919

20+
If you wish to customize the CSS modules class name output you can set the `css: { localIdentName: [name]__[local]--[hash:base64:5]}` in `vue.config.js`.
21+
2022
### Pre-Processors
2123

2224
You can select pre-processors (Sass/Less/Stylus) when creating the project. If you did not do so, you can also just manually install the corresponding webpack loaders. The loaders are pre-configured and will automatically be picked up. For example, to add Sass to an existing project, simply run:

packages/@vue/cli-service/__tests__/css.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ test('css.sourceMap', () => {
140140
})
141141
})
142142

143+
test('css.localIdentName', () => {
144+
const localIdentName = '[name]__[local]--[hash:base64:5]'
145+
const config = genConfig({
146+
vue: {
147+
css: {
148+
modules: true,
149+
localIdentName: localIdentName
150+
}
151+
}
152+
})
153+
LANGS.forEach(lang => {
154+
expect(findOptions(config, lang, 'css').localIdentName).toBe(localIdentName)
155+
})
156+
})
157+
143158
test('css.loaderOptions', () => {
144159
const data = '$env: production;'
145160
const config = genConfig({

packages/@vue/cli-service/lib/config/css.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module.exports = (api, options) => {
1919
extract: true,
2020
modules: false,
2121
sourceMap: false,
22-
loaderOptions: {}
22+
loaderOptions: {},
23+
localIdentName: '[name]_[local]__[hash:base64:5]'
2324
}
2425
const userOptions = Object.assign(defaultOptions, options.css || {})
2526
const extract = isProd && userOptions.extract !== false
@@ -52,7 +53,7 @@ module.exports = (api, options) => {
5253
options.loaders = Object.assign(resolver.vue(), options.loaders)
5354
options.cssSourceMap = !!userOptions.cssSourceMap
5455
options.cssModules = Object.assign({
55-
localIdentName: '[name]_[local]__[hash:base64:5]'
56+
localIdentName: baseOptions.localIdentName
5657
}, options.cssModules)
5758
return options
5859
})

packages/@vue/cli-service/lib/options.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const schema = createSchema(joi => joi.object({
1717
css: joi.object({
1818
modules: joi.boolean(),
1919
extract: joi.boolean(),
20+
localIdentName: joi.string(),
2021
sourceMap: joi.boolean(),
2122
loaderOptions: joi.object({
2223
sass: joi.object(),
@@ -77,6 +78,7 @@ exports.defaults = () => ({
7778
css: {
7879
// extract: true,
7980
// modules: false,
81+
// localIdentName: '[name]_[local]_[hash:base64:5]',
8082
// sourceMap: false,
8183
// loaderOptions: {}
8284
},

packages/@vue/cli-service/lib/webpack/CSSLoaderResolver.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = class CSSLoaderResolver {
1313
* @param {Object} options
1414
* @param {boolean} [options.sourceMap=undefined] Enable sourcemaps.
1515
* @param {boolean} [options.modules=undefined] Enable CSS modules.
16+
* @param {string} [options.localIdentName='[name]_[local]__[hash:base64:5]'] Customizes CSS modules localIdentName.
1617
* @param {boolean} [options.extract=undefined] Extract CSS.
1718
* @param {boolean} [options.minimize=undefined] Minimize CSS.
1819
* @param {boolean} [options.postcss=undefined] Enable postcss-loader.
@@ -21,6 +22,7 @@ module.exports = class CSSLoaderResolver {
2122
constructor ({
2223
sourceMap,
2324
modules,
25+
localIdentName,
2426
extract,
2527
minimize,
2628
postcss,
@@ -32,6 +34,7 @@ module.exports = class CSSLoaderResolver {
3234
this.extract = extract && !process.env.VUE_CLI_CSS_SHADOW_MODE
3335
this.minimize = minimize
3436
this.modules = modules
37+
this.localIdentName = localIdentName
3538
this.postcss = postcss
3639
this.loaderOptions = loaderOptions || {}
3740
}
@@ -45,7 +48,7 @@ module.exports = class CSSLoaderResolver {
4548
if (this.modules) {
4649
cssLoaderOptions.modules = true
4750
cssLoaderOptions.importLoaders = 1
48-
cssLoaderOptions.localIdentName = '[name]_[local]__[hash:base64:5]'
51+
cssLoaderOptions.localIdentName = this.localIdentName
4952
}
5053

5154
if (loader === 'css') {

0 commit comments

Comments
 (0)