chain-webpack.js 3.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const path = require('path')

3 4 5 6
const {
  sassLoaderVersion
} = require('@dcloudio/uni-cli-shared/lib/scss')

fxy060608's avatar
fxy060608 已提交
7 8 9 10
const {
  getPartialIdentifier
} = require('./util')

fxy060608's avatar
fxy060608 已提交
11 12 13 14
function resolve (dir) {
  return path.resolve(__dirname, '..', dir)
}

fxy060608's avatar
fxy060608 已提交
15
module.exports = function chainWebpack (platformOptions, vueOptions, api) {
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20
  const {
    runByHBuilderX, // 使用 HBuilderX 运行
    cssPreprocessOptions
  } = require('@dcloudio/uni-cli-shared')

21
  return function (webpackConfig) {
fxy060608's avatar
fxy060608 已提交
22 23
    // 处理静态资源 limit
    const staticTypes = ['images', 'media', 'fonts']
fxy060608's avatar
fxy060608 已提交
24 25 26 27 28 29 30 31 32
    staticTypes.forEach(staticType => {
      webpackConfig.module
        .rule(staticType)
        .use('url-loader')
        .loader('url-loader')
        .tap(options => Object.assign(options, {
          limit: 40960
        }))
    })
fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    // 条件编译 vue 文件统一直接过滤html,js,css三种类型,单独资源文件引用各自过滤

    const loaders = {
      'scss': 'sass-loader',
      'sass': 'sass-loader',
      'less': 'less-loader',
      'stylus': 'stylus-loader'
    }
    // 独立css,postcss,scss,sass,less,stylus
    const cssLang = ['css', 'postcss', 'scss', 'sass', 'less', 'stylus']

    const cssTypes = ['vue-modules', 'vue', 'normal-modules', 'normal']

    cssLang.forEach(lang => {
      const langRule = webpackConfig.module.rule(lang)
      const loader = loaders[lang]
      cssTypes.forEach(type => {
fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55 56 57 58 59
        if (process.env.UNI_USING_CACHE) {
          langRule.oneOf(type)
            .use(`uniapp-cache-css`)
            .loader('cache-loader')
            .options(api.genCacheConfig(
              'css-loader/' + process.env.UNI_PLATFORM,
              getPartialIdentifier()
            ))
            .before('css-loader')
        }
fxy060608's avatar
fxy060608 已提交
60 61 62 63 64
        langRule.oneOf(type)
          .use(`uniapp-preprocss`)
          .loader(resolve('packages/webpack-preprocess-loader'))
          .options(cssPreprocessOptions)
          .before('css-loader') // 在 css-loader 之后条件编译一次,避免 import 进来的 css 没有走条件编译
fxy060608's avatar
fxy060608 已提交
65

fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75
        if (loader) { // 在 scss,less,stylus 之前先条件编译一次
          langRule.oneOf(type)
            .use(`uniapp-preprocss-` + lang)
            .loader(resolve('packages/webpack-preprocess-loader'))
            .options(cssPreprocessOptions)
            .after(loader)
        }
      })
    })

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    if (sassLoaderVersion >= 8) { // check indentedSyntax
      // vue cli 3 and sass-loader 8
      cssTypes.forEach(type => {
        webpackConfig.module.rule('sass').oneOf(type).use('sass-loader').tap(options => {
          if (options.indentedSyntax) {
            if (!options.sassOptions) {
              options.sassOptions = {}
            }
            options.sassOptions.indentedSyntax = true
            delete options.indentedSyntax
          }
          return options
        })
      })
    }

fxy060608's avatar
fxy060608 已提交
92
    platformOptions.chainWebpack(webpackConfig, vueOptions, api)
fxy060608's avatar
fxy060608 已提交
93 94 95 96
    // define
    webpackConfig
      .plugin('uni-define')
      .use(require.resolve('webpack/lib/DefinePlugin'), [{
fxy060608's avatar
fxy060608 已提交
97 98
        'process.env.UNI_ENV': JSON.stringify(process.env.UNI_PLATFORM),
        'process.env.UNI_CLOUD_PROVIDER': process.env.UNI_CLOUD_PROVIDER
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103
      }])

    if (runByHBuilderX) { // 由 HBuilderX 运行时,移除进度,错误
      webpackConfig.plugins.delete('progress')
      webpackConfig.plugins.delete('friendly-errors')
104
    }
fxy060608's avatar
fxy060608 已提交
105 106 107
    if (process.env.BUILD_ENV === 'ali-ide') {
      webpackConfig.plugins.delete('progress')
    }
fxy060608's avatar
fxy060608 已提交
108 109
  }
}