1 | import { IPluginContext } from '@tarojs/service'
|
2 | import { getSassLoaderOption } from '@tarojs/runner-utils'
|
3 |
|
4 | export default (ctx: IPluginContext, opts) => {
|
5 | ctx.modifyWebpackChain(async ({ chain }) => {
|
6 | const { enableSourceMap = true } = opts
|
7 | const taroEnv = process.env.TARO_ENV
|
8 | if (taroEnv) {
|
9 | const sass = require('node-sass')
|
10 | const currentPlatform = ctx.platforms.get(taroEnv)
|
11 | if (!currentPlatform) return
|
12 | const { sass: sassOption } = ctx.initialConfig
|
13 | const platformConfig = ctx.initialConfig[currentPlatform.useConfigName]
|
14 |
|
15 | const defaultSassLoaderOption = {
|
16 | sourceMap: enableSourceMap,
|
17 | implementation: sass,
|
18 | sassOptions: {
|
19 | outputStyle: 'expanded'
|
20 | }
|
21 | }
|
22 | const newSassLoaderOption = await getSassLoaderOption({
|
23 | sass: sassOption,
|
24 | sassLoaderOption: platformConfig?.sassLoaderOption
|
25 | })
|
26 | chain.module
|
27 | .rule('addChainStyleSass')
|
28 | .test(ctx.helper.REG_SASS)
|
29 | .pre()
|
30 | .use('resolveUrl')
|
31 | .loader(require.resolve('resolve-url-loader'))
|
32 | .end()
|
33 | .use('sass')
|
34 | .loader(require.resolve('sass-loader'))
|
35 | .options(Object.assign({}, defaultSassLoaderOption, newSassLoaderOption))
|
36 | .end()
|
37 | }
|
38 | })
|
39 | }
|