vue-loader.js 2.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
const {
  getPlatformCompiler
} = require('@dcloudio/uni-cli-shared')

const {
  isUnaryTag,
  getPartialIdentifier
} = require('./util')

module.exports = function modifyVueLoader (webpackConfig, compilerOptions, api) {
  // vue-loader options

  const cacheConfig = {
    cacheDirectory: false,
    cacheIdentifier: false
  }

  if (process.env.UNI_USING_CACHE) {
    Object.assign(cacheConfig, api.genCacheConfig(
      'vue-template-compiler/' + process.env.UNI_PLATFORM,
      getPartialIdentifier()
    ))
  }

  webpackConfig.module
    .rule('vue')
    .test([/\.vue$/, /\.nvue$/])
    .use('vue-loader')
fxy060608's avatar
fxy060608 已提交
29
    .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/vue-loader'))
fxy060608's avatar
fxy060608 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    .tap(options => Object.assign(options, {
      compiler: getPlatformCompiler(),
      compilerOptions: Object.assign({
        isUnaryTag,
        preserveWhitespace: false
      }, compilerOptions)
    }, cacheConfig))
    .end()
    .use('uniapp-custom-block-loader')
    .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/webpack-custom-block-loader'))
    .options({
      compiler: getPlatformCompiler()
    })

  // h5 框架需要使用 scoped 样式,其他平台编译时识别是否 nvue 文件且注入 flex 相关样式
  if (process.env.UNI_PLATFORM === 'h5') {
    webpackConfig.module
      .rule('vue')
      .use('uniapp-h5-style-scoped')
      .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/webpack-scoped-loader'))
  } else {
    webpackConfig.module
      .rule('vue')
      .use('uniapp-nvue-style-loader')
      .loader(require.resolve('@dcloudio/webpack-uni-mp-loader/lib/style.js'))
  }
  // 是否启用 cache
  if (process.env.UNI_USING_CACHE) {
    webpackConfig.module
      .rule('vue')
      .use('cache-loader')
      .tap(options => Object.assign(options, api.genCacheConfig(
        'vue-loader/' + process.env.UNI_PLATFORM,
63
        getPartialIdentifier()
fxy060608's avatar
fxy060608 已提交
64 65 66 67 68 69 70 71
      )))
  } else {
    webpackConfig.module
      .rule('vue')
      .uses
      .delete('cache-loader')
  }
}