Skip to content

Commit

Permalink
feat(css modules): Add CSS Module localIdentName option to vue config (
Browse files Browse the repository at this point in the history
…#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])
  • Loading branch information
n-zeplo authored and yyx990803 committed Mar 1, 2018
1 parent bbc931c commit 31cdc86
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/css.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ As for standalone style files, any files ending with `.module.(css|sass|scss|les

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.

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`.

### Pre-Processors

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:
Expand Down
15 changes: 15 additions & 0 deletions packages/@vue/cli-service/__tests__/css.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ test('css.sourceMap', () => {
})
})

test('css.localIdentName', () => {
const localIdentName = '[name]__[local]--[hash:base64:5]'
const config = genConfig({
vue: {
css: {
modules: true,
localIdentName: localIdentName
}
}
})
LANGS.forEach(lang => {
expect(findOptions(config, lang, 'css').localIdentName).toBe(localIdentName)
})
})

test('css.loaderOptions', () => {
const data = '$env: production;'
const config = genConfig({
Expand Down
5 changes: 3 additions & 2 deletions packages/@vue/cli-service/lib/config/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = (api, options) => {
extract: true,
modules: false,
sourceMap: false,
loaderOptions: {}
loaderOptions: {},
localIdentName: '[name]_[local]__[hash:base64:5]'
}
const userOptions = Object.assign(defaultOptions, options.css || {})
const extract = isProd && userOptions.extract !== false
Expand Down Expand Up @@ -52,7 +53,7 @@ module.exports = (api, options) => {
options.loaders = Object.assign(resolver.vue(), options.loaders)
options.cssSourceMap = !!userOptions.cssSourceMap
options.cssModules = Object.assign({
localIdentName: '[name]_[local]__[hash:base64:5]'
localIdentName: baseOptions.localIdentName
}, options.cssModules)
return options
})
Expand Down
2 changes: 2 additions & 0 deletions packages/@vue/cli-service/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const schema = createSchema(joi => joi.object({
css: joi.object({
modules: joi.boolean(),
extract: joi.boolean(),
localIdentName: joi.string(),
sourceMap: joi.boolean(),
loaderOptions: joi.object({
sass: joi.object(),
Expand Down Expand Up @@ -77,6 +78,7 @@ exports.defaults = () => ({
css: {
// extract: true,
// modules: false,
// localIdentName: '[name]_[local]_[hash:base64:5]',
// sourceMap: false,
// loaderOptions: {}
},
Expand Down
5 changes: 4 additions & 1 deletion packages/@vue/cli-service/lib/webpack/CSSLoaderResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = class CSSLoaderResolver {
* @param {Object} options
* @param {boolean} [options.sourceMap=undefined] Enable sourcemaps.
* @param {boolean} [options.modules=undefined] Enable CSS modules.
* @param {string} [options.localIdentName='[name]_[local]__[hash:base64:5]'] Customizes CSS modules localIdentName.
* @param {boolean} [options.extract=undefined] Extract CSS.
* @param {boolean} [options.minimize=undefined] Minimize CSS.
* @param {boolean} [options.postcss=undefined] Enable postcss-loader.
Expand All @@ -21,6 +22,7 @@ module.exports = class CSSLoaderResolver {
constructor ({
sourceMap,
modules,
localIdentName,
extract,
minimize,
postcss,
Expand All @@ -32,6 +34,7 @@ module.exports = class CSSLoaderResolver {
this.extract = extract && !process.env.VUE_CLI_CSS_SHADOW_MODE
this.minimize = minimize
this.modules = modules
this.localIdentName = localIdentName
this.postcss = postcss
this.loaderOptions = loaderOptions || {}
}
Expand All @@ -45,7 +48,7 @@ module.exports = class CSSLoaderResolver {
if (this.modules) {
cssLoaderOptions.modules = true
cssLoaderOptions.importLoaders = 1
cssLoaderOptions.localIdentName = '[name]_[local]__[hash:base64:5]'
cssLoaderOptions.localIdentName = this.localIdentName
}

if (loader === 'css') {
Expand Down

0 comments on commit 31cdc86

Please sign in to comment.