Skip to content

Commit 96ab7ae

Browse files
committed
feat: allow configuring scss options separately from sass (#4386)
closes #4116 (cherry picked from commit 930b459)
1 parent 3c8a890 commit 96ab7ae

File tree

7 files changed

+67
-8
lines changed

7 files changed

+67
-8
lines changed

docs/config/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ See [the plugin's README](https://github.com/vuejs/vue-cli/blob/dev/packages/%40
306306
- [less-loader](https://github.com/webpack-contrib/less-loader)
307307
- [stylus-loader](https://github.com/shama/stylus-loader)
308308

309+
It's also possible to target `scss` syntax separately from `sass`, with the `scss` option.
310+
309311
See also: [Passing Options to Pre-Processor Loaders](../guide/css.md#passing-options-to-pre-processor-loaders)
310312

311313
::: tip

docs/guide/css.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,17 @@ module.exports = {
126126
css: {
127127
loaderOptions: {
128128
// pass options to sass-loader
129+
// @/ is an alias to src/
130+
// so this assumes you have a file named `src/variables.sass`
129131
sass: {
130-
// @/ is an alias to src/
131-
// so this assumes you have a file named `src/variables.scss`
132+
data: `@import "~@/variables.sass"`
133+
},
134+
// by default the `sass` option will apply to both syntaxes
135+
// because `scss` syntax is also processed by sass-loader underlyingly
136+
// but when configuring the `data` option
137+
// `scss` syntax requires an semicolon at the end of a statement, while `sass` syntax requires none
138+
// in that case, we can target the `scss` syntax separately using the `scss` option
139+
scss: {
132140
data: `@import "~@/variables.scss";`
133141
},
134142
// pass Less.js Options to less-loader

docs/zh/config/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ module.exports = {
292292
- [less-loader](https://github.com/webpack-contrib/less-loader)
293293
- [stylus-loader](https://github.com/shama/stylus-loader)
294294

295+
另外,也可以使用 `scss` 选项,针对 `scss` 语法进行单独配置(区别于 `sass` 语法)。
296+
295297
更多细节可查阅:[向预处理器 Loader 传递选项](../guide/css.html#向预处理器-loader-传递选项)
296298

297299
::: tip 提示

docs/zh/guide/css.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ module.exports = {
118118

119119
## 向预处理器 Loader 传递选项
120120

121-
有的时候你想要向 webpack 的预处理器 loader 传递选项。你可以使用 `vue.config.js` 中的 `css.loaderOptions` 选项。比如你可以这样向所有 Sass 样式传入共享的全局变量:
121+
有的时候你想要向 webpack 的预处理器 loader 传递选项。你可以使用 `vue.config.js` 中的 `css.loaderOptions` 选项。比如你可以这样向所有 Sass/Less 样式传入共享的全局变量:
122122

123123
``` js
124124
// vue.config.js
@@ -128,8 +128,24 @@ module.exports = {
128128
// 给 sass-loader 传递选项
129129
sass: {
130130
// @/ 是 src/ 的别名
131-
// 所以这里假设你有 `src/variables.scss` 这个文件
131+
// 所以这里假设你有 `src/variables.sass` 这个文件
132+
data: `@import "~@/variables.sass"`
133+
},
134+
// 默认情况下 `sass` 选项会同时对 `sass` 和 `scss` 语法同时生效
135+
// 因为 `scss` 语法在内部也是由 sass-loader 处理的
136+
// 但是在配置 `data` 选项的时候
137+
// `scss` 语法会要求语句结尾必须有分号,`sass` 则要求必须没有分号
138+
// 在这种情况下,我们可以使用 `scss` 选项,对 `scss` 语法进行单独配置
139+
scss: {
132140
data: `@import "~@/variables.scss";`
141+
},
142+
// 给 less-loader 传递 Less.js 相关选项
143+
less:{
144+
// http://lesscss.org/usage/#less-options-strict-units `Global Variables`
145+
// `primary` is global variables fields name
146+
globalVars: {
147+
primary: '#fff'
148+
}
133149
}
134150
}
135151
}

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

+23
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,29 @@ test('css.loaderOptions', () => {
197197
expect(findOptions(config, 'sass', 'sass')).toMatchObject({ data, indentedSyntax: true, sourceMap: false })
198198
})
199199

200+
test('scss loaderOptions', () => {
201+
const sassData = '$env: production'
202+
const scssData = '$env: production;'
203+
204+
const config = genConfig({
205+
vue: {
206+
css: {
207+
loaderOptions: {
208+
sass: {
209+
sassData
210+
},
211+
scss: {
212+
scssData
213+
}
214+
}
215+
}
216+
}
217+
})
218+
219+
expect(findOptions(config, 'scss', 'sass')).toMatchObject({ scssData, sourceMap: false })
220+
expect(findOptions(config, 'sass', 'sass')).toMatchObject({ sassData, indentedSyntax: true, sourceMap: false })
221+
})
222+
200223
test('should use dart sass implementation whenever possible', () => {
201224
const config = genConfig()
202225
expect(findOptions(config, 'scss', 'sass')).toMatchObject({ fiber: require('fibers'), implementation: require('sass') })

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,17 @@ module.exports = (api, options) => {
165165

166166
createCSSRule('css', /\.css$/)
167167
createCSSRule('postcss', /\.p(ost)?css$/)
168-
createCSSRule('scss', /\.scss$/, 'sass-loader', Object.assign(defaultSassLoaderOptions, loaderOptions.sass))
169-
createCSSRule('sass', /\.sass$/, 'sass-loader', Object.assign(defaultSassLoaderOptions, {
170-
indentedSyntax: true
171-
}, loaderOptions.sass))
168+
createCSSRule('scss', /\.scss$/, 'sass-loader', Object.assign(
169+
defaultSassLoaderOptions,
170+
loaderOptions.scss || loaderOptions.sass
171+
))
172+
createCSSRule('sass', /\.sass$/, 'sass-loader', Object.assign(
173+
defaultSassLoaderOptions,
174+
{
175+
indentedSyntax: true
176+
},
177+
loaderOptions.sass
178+
))
172179
createCSSRule('less', /\.less$/, 'less-loader', loaderOptions.less)
173180
createCSSRule('stylus', /\.styl(us)?$/, 'stylus-loader', Object.assign({
174181
preferPathResolver: 'webpack'

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

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const schema = createSchema(joi => joi.object({
4040
loaderOptions: joi.object({
4141
css: joi.object(),
4242
sass: joi.object(),
43+
scss: joi.object(),
4344
less: joi.object(),
4445
stylus: joi.object(),
4546
postcss: joi.object()

0 commit comments

Comments
 (0)