Skip to content

Commit 930b459

Browse files
authored
feat: allow configuring scss options separately from sass (vuejs#4386)
closes vuejs#4116
1 parent c76d2e6 commit 930b459

File tree

7 files changed

+67
-8
lines changed

7 files changed

+67
-8
lines changed

Diff for: docs/config/README.md

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

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

309311
::: tip

Diff for: docs/guide/css.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,17 @@ module.exports = {
131131
css: {
132132
loaderOptions: {
133133
// pass options to sass-loader
134+
// @/ is an alias to src/
135+
// so this assumes you have a file named `src/variables.sass`
134136
sass: {
135-
// @/ is an alias to src/
136-
// so this assumes you have a file named `src/variables.scss`
137+
data: `@import "~@/variables.sass"`
138+
},
139+
// by default the `sass` option will apply to both syntaxes
140+
// because `scss` syntax is also processed by sass-loader underlyingly
141+
// but when configuring the `data` option
142+
// `scss` syntax requires an semicolon at the end of a statement, while `sass` syntax requires none
143+
// in that case, we can target the `scss` syntax separately using the `scss` option
144+
scss: {
137145
data: `@import "~@/variables.scss";`
138146
},
139147
// pass Less.js Options to less-loader

Diff for: 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 提示

Diff for: docs/zh/guide/css.md

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

111111
## 向预处理器 Loader 传递选项
112112

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

115115
``` js
116116
// vue.config.js
@@ -120,8 +120,24 @@ module.exports = {
120120
// 给 sass-loader 传递选项
121121
sass: {
122122
// @/ 是 src/ 的别名
123-
// 所以这里假设你有 `src/variables.scss` 这个文件
123+
// 所以这里假设你有 `src/variables.sass` 这个文件
124+
data: `@import "~@/variables.sass"`
125+
},
126+
// 默认情况下 `sass` 选项会同时对 `sass` 和 `scss` 语法同时生效
127+
// 因为 `scss` 语法在内部也是由 sass-loader 处理的
128+
// 但是在配置 `data` 选项的时候
129+
// `scss` 语法会要求语句结尾必须有分号,`sass` 则要求必须没有分号
130+
// 在这种情况下,我们可以使用 `scss` 选项,对 `scss` 语法进行单独配置
131+
scss: {
124132
data: `@import "~@/variables.scss";`
133+
},
134+
// 给 less-loader 传递 Less.js 相关选项
135+
less:{
136+
// http://lesscss.org/usage/#less-options-strict-units `Global Variables`
137+
// `primary` is global variables fields name
138+
globalVars: {
139+
primary: '#fff'
140+
}
125141
}
126142
}
127143
}

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

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

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

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,17 @@ module.exports = (api, rootOptions) => {
162162

163163
createCSSRule('css', /\.css$/)
164164
createCSSRule('postcss', /\.p(ost)?css$/)
165-
createCSSRule('scss', /\.scss$/, 'sass-loader', Object.assign(defaultSassLoaderOptions, loaderOptions.sass))
166-
createCSSRule('sass', /\.sass$/, 'sass-loader', Object.assign(defaultSassLoaderOptions, {
167-
indentedSyntax: true
168-
}, loaderOptions.sass))
165+
createCSSRule('scss', /\.scss$/, 'sass-loader', Object.assign(
166+
defaultSassLoaderOptions,
167+
loaderOptions.scss || loaderOptions.sass
168+
))
169+
createCSSRule('sass', /\.sass$/, 'sass-loader', Object.assign(
170+
defaultSassLoaderOptions,
171+
{
172+
indentedSyntax: true
173+
},
174+
loaderOptions.sass
175+
))
169176
createCSSRule('less', /\.less$/, 'less-loader', loaderOptions.less)
170177
createCSSRule('stylus', /\.styl(us)?$/, 'stylus-loader', Object.assign({
171178
preferPathResolver: 'webpack'

Diff for: packages/@vue/cli-service/lib/options.js

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

0 commit comments

Comments
 (0)