Skip to content

Commit 1e98d96

Browse files
committed
feat: re-introduce css.modules option
BREAKING CHANGE: internal webpack rules for CSS have been changed.
1 parent 6132dbe commit 1e98d96

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

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

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

25-
const findRule = (config, lang, index = 2) => {
25+
const findRule = (config, lang, index = 3) => {
2626
const baseRule = config.module.rules.find(rule => {
2727
return rule.test.test(`.${lang}`)
2828
})
29-
// all CSS rules have oneOf with two child rules, one for <style lang="module">
30-
// and one for normal imports
29+
// all CSS rules have 4 oneOf rules:
30+
// - <style lang="module"> in Vue files
31+
// - <style> in Vue files
32+
// - *.modules.css imports from JS
33+
// - *.css imports from JS
3134
return baseRule.oneOf[index]
3235
}
3336

@@ -76,7 +79,13 @@ test('production defaults', () => {
7679
})
7780

7881
test('CSS Modules rules', () => {
79-
const config = genConfig()
82+
const config = genConfig({
83+
vue: {
84+
css: {
85+
modules: true
86+
}
87+
}
88+
})
8089
LANGS.forEach(lang => {
8190
const expected = {
8291
importLoaders: lang === 'css' ? 1 : 2, // no postcss-loader
@@ -85,10 +94,12 @@ test('CSS Modules rules', () => {
8594
sourceMap: false,
8695
modules: true
8796
}
88-
// module-query rules
97+
// vue-modules rules
8998
expect(findOptions(config, lang, 'css', 0)).toEqual(expected)
90-
// module-ext rules
91-
expect(findOptions(config, lang, 'css', 1)).toEqual(expected)
99+
// normal-modules rules
100+
expect(findOptions(config, lang, 'css', 2)).toEqual(expected)
101+
// normal rules
102+
expect(findOptions(config, lang, 'css', 3)).toEqual(expected)
92103
})
93104
})
94105

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = (api, options) => {
1414
const getAssetPath = require('../util/getAssetPath')
1515

1616
const {
17+
modules = false,
1718
extract = true,
1819
sourceMap = false,
1920
localIdentName = '[name]_[local]_[hash:base64:5]',
@@ -43,15 +44,20 @@ module.exports = (api, options) => {
4344
const baseRule = webpackConfig.module.rule(lang).test(test)
4445

4546
// rules for <style lang="module">
46-
const modulesRule = baseRule.oneOf('modules-query').resourceQuery(/module/)
47-
applyLoaders(modulesRule, true)
47+
const vueModulesRule = baseRule.oneOf('vue-modules').resourceQuery(/module/)
48+
applyLoaders(vueModulesRule, true)
49+
50+
// rules for <style>
51+
const vueNormalRule = baseRule.oneOf('vue').resourceQuery(/\?vue/)
52+
applyLoaders(vueNormalRule, false)
4853

4954
// rules for *.module.* files
50-
const modulesExtRule = baseRule.oneOf('modules-ext').test(/\.module\.\w+$/)
51-
applyLoaders(modulesExtRule, true)
55+
const extModulesRule = baseRule.oneOf('normal-modules').test(/\.module\.\w+$/)
56+
applyLoaders(extModulesRule, true)
5257

58+
// rules for normal CSS imports
5359
const normalRule = baseRule.oneOf('normal')
54-
applyLoaders(normalRule, false)
60+
applyLoaders(normalRule, modules)
5561

5662
function applyLoaders (rule, modules) {
5763
if (shouldExtract) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const schema = createSchema(joi => joi.object({
1212

1313
// css
1414
css: joi.object({
15+
modules: joi.boolean(),
1516
localIdentName: joi.string(),
1617
extract: joi.alternatives().try(joi.boolean(), joi.object()),
1718
sourceMap: joi.boolean(),
@@ -66,6 +67,7 @@ exports.defaults = () => ({
6667

6768
css: {
6869
// extract: true,
70+
// modules: false,
6971
// localIdentName: '[name]_[local]_[hash:base64:5]',
7072
// sourceMap: false,
7173
// loaderOptions: {}

0 commit comments

Comments
 (0)