Skip to content

Commit 1b5bdde

Browse files
committed
fix: css imports from js
BREAKING CHANGE: `css.modules` option has been removed. To import css files (or any other supported pre-processor files) as CSS Modules, append the request with a `?module` resourceQuery.
1 parent 76196ba commit 1b5bdde

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

docs/css.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ Vue CLI uses PostCSS internally, and enables [autoprefixer](https://github.com/p
1313

1414
You can [use CSS Modules in `*.vue` files](https://vue-loader.vuejs.org/en/features/css-modules.html) out of the box with `<style module>`.
1515

16-
As for standalone style files, any files ending with `.module.(css|sass|scss|less|styl|stylus)` will be processed as CSS modules.
16+
If you wish to import style files as CSS Modules in JavaScript, you can import a file with a `?module` resourceQuery:
1717

18-
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.
18+
``` js
19+
import styles from './foo.css?module'
20+
// works for all supported pre-processors as well
21+
import sassStyles from './foo.scss?module'
22+
```
1923

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`.
24+
If you wish to customize the CSS modules class name output you can set the `css.localIdentName` option in `vue.config.js`.
2125

2226
### Pre-Processors
2327

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

+16-18
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,25 @@ const genConfig = (pkg = {}, env) => {
2222
return config
2323
}
2424

25-
const findRule = (config, lang) => config.module.rules.find(rule => {
26-
return rule.test.test(`.${lang}`)
27-
})
25+
const findRule = (config, lang, index = 1) => {
26+
const baseRule = config.module.rules.find(rule => {
27+
return rule.test.test(`.${lang}`)
28+
})
29+
// all CSS rules have oneOf with two child rules, one for <style lang="module">
30+
// and one for normal imports
31+
return baseRule.oneOf[index]
32+
}
2833

29-
const findLoaders = (config, lang) => {
30-
const rule = findRule(config, lang)
34+
const findLoaders = (config, lang, index) => {
35+
const rule = findRule(config, lang, index)
3136
if (!rule) {
3237
throw new Error(`rule not found for ${lang}`)
3338
}
3439
return rule.use.map(({ loader }) => loader.replace(/-loader$/, ''))
3540
}
3641

37-
const findOptions = (config, lang, _loader) => {
38-
const rule = findRule(config, lang)
42+
const findOptions = (config, lang, _loader, index) => {
43+
const rule = findRule(config, lang, index)
3944
const use = rule.use.find(({ loader }) => loader.includes(`${_loader}-loader`))
4045
return use.options || {}
4146
}
@@ -70,16 +75,10 @@ test('production defaults', () => {
7075
})
7176
})
7277

73-
test('css.modules', () => {
74-
const config = genConfig({
75-
vue: {
76-
css: {
77-
modules: true
78-
}
79-
}
80-
})
78+
test('CSS Modules rules', () => {
79+
const config = genConfig()
8180
LANGS.forEach(lang => {
82-
expect(findOptions(config, lang, 'css')).toEqual({
81+
expect(findOptions(config, lang, 'css', 0)).toEqual({
8382
importLoaders: lang === 'css' ? 0 : 1, // no postcss-loader
8483
localIdentName: `[name]_[local]_[hash:base64:5]`,
8584
minimize: false,
@@ -123,13 +122,12 @@ test('css.localIdentName', () => {
123122
const config = genConfig({
124123
vue: {
125124
css: {
126-
modules: true,
127125
localIdentName: localIdentName
128126
}
129127
}
130128
})
131129
LANGS.forEach(lang => {
132-
expect(findOptions(config, lang, 'css').localIdentName).toBe(localIdentName)
130+
expect(findOptions(config, lang, 'css', 0).localIdentName).toBe(localIdentName)
133131
})
134132
})
135133

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module.exports = (api, options) => {
1313
api.chainWebpack(webpackConfig => {
1414
const {
1515
extract = true,
16-
modules = false,
1716
sourceMap = false,
1817
localIdentName = '[name]_[local]_[hash:base64:5]',
1918
loaderOptions = {}
@@ -39,13 +38,15 @@ module.exports = (api, options) => {
3938
]))
4039

4140
function createCSSRule (lang, test, loader, options) {
42-
const normalRule = webpackConfig.module.rule(lang).test(test).resourceQuery(q => !/module/.test(q))
43-
applyLoaders(normalRule, modules)
41+
const baseRule = webpackConfig.module.rule(lang).test(test)
4442

4543
// rules for <style lang="module">
46-
const modulesRule = webpackConfig.module.rule(lang + '-modules').test(test).resourceQuery(/module/)
44+
const modulesRule = baseRule.oneOf('modules').resourceQuery(/module/)
4745
applyLoaders(modulesRule, true)
4846

47+
const normalRule = baseRule.oneOf('normal')
48+
applyLoaders(normalRule, false)
49+
4950
function applyLoaders (rule, modules) {
5051
if (shouldExtract) {
5152
rule

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ const schema = createSchema(joi => joi.object({
1111

1212
// css
1313
css: joi.object({
14-
modules: joi.boolean(),
15-
extract: joi.alternatives().try(joi.boolean(), joi.object()),
1614
localIdentName: joi.string(),
15+
extract: joi.alternatives().try(joi.boolean(), joi.object()),
1716
sourceMap: joi.boolean(),
1817
loaderOptions: joi.object({
1918
sass: joi.object(),
@@ -63,7 +62,6 @@ exports.defaults = () => ({
6362

6463
css: {
6564
// extract: true,
66-
// modules: false,
6765
// localIdentName: '[name]_[local]_[hash:base64:5]',
6866
// sourceMap: false,
6967
// loaderOptions: {}

0 commit comments

Comments
 (0)