uni.config.js 2.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
const fs = require('fs')
const path = require('path')

const COMPONENTS_DIR_NAME = 'wxcomponents'

function getComponentsCopyOption () {
  if (process.env.UNI_OUTPUT_TMP_DIR) { // TODO v3不需要,即将废弃
    const componentsDir = path.resolve(process.env.UNI_INPUT_DIR, COMPONENTS_DIR_NAME)
9
    const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
fxy060608's avatar
fxy060608 已提交
10
    if (fs.existsSync(componentsDir)) {
11 12
      const ignore = ['**/*.vue', '**/*.css']
      return Object.assign({
fxy060608's avatar
fxy060608 已提交
13
        from: componentsDir,
14 15 16 17 18 19
        to: COMPONENTS_DIR_NAME
      }, CopyWebpackPluginVersion > 5 ? {
        globOptions: { ignore }
      } : {
        ignore
      })
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27 28 29 30
    }
  }
}

module.exports = {
  options: {
    extnames: { // TODO v3不需要此配置
      style: '.wxss',
      template: '.wxml',
      filter: '.wxs'
    },
fxy060608's avatar
fxy060608 已提交
31 32
    filterTag: 'wxs',
    subPackages: true
fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38 39 40
  },
  copyWebpackOptions (platformOptions, vueOptions) {
    const copyOptions = []
    const componentsCopyOption = getComponentsCopyOption()
    if (componentsCopyOption) {
      copyOptions.push(componentsCopyOption)
    }
    copyOptions.push('hybrid/html')
fxy060608's avatar
fxy060608 已提交
41 42 43
    global.uniModules.forEach(module => {
      copyOptions.push('uni_modules/' + module + '/hybrid/html')
    })
fxy060608's avatar
fxy060608 已提交
44 45 46 47
    if (process.env.UNI_USING_V3) { // TODO 将仅保留v3逻辑
      copyOptions.push(path.resolve(__dirname, '../dist/view.css'))
      copyOptions.push(path.resolve(__dirname, '../dist/view.umd.min.js'))
      // TODO 后续common与v3目录应该合并
48 49
      copyOptions.push(path.resolve(__dirname, process.env.UNI_USING_NVUE_COMPILER ? '../template/common'
        : '../template/weex'))
fxy060608's avatar
fxy060608 已提交
50 51 52
      copyOptions.push(path.resolve(__dirname, '../template/v3'))
    }
    return copyOptions
53
  },
fxy060608's avatar
fxy060608 已提交
54
  chainWebpack (config, vueOptions) {
fxy060608's avatar
fxy060608 已提交
55
    const isAppService = vueOptions.pluginOptions && !!vueOptions.pluginOptions['uni-app-plus'].service
fxy060608's avatar
fxy060608 已提交
56 57 58 59 60 61 62 63
    if (isAppService) {
      const subPackages = Object.keys(process.UNI_SUBPACKAGES)
      if (process.env.UNI_OPT_SUBPACKAGES && subPackages.length) {
        config
          .plugin('uni-app-plus-subpackages')
          .use(require('./plugin/sub-packages-plugin'))
      }
    }
64 65 66 67 68 69 70 71 72
    if (process.env.NODE_ENV === 'production') {
      config.optimization.minimizer('terser').tap((args) => {
        if (!args[0].terserOptions.output) {
          args[0].terserOptions.output = {}
        }
        args[0].terserOptions.output.ascii_only = true
        return args
      })
    }
fxy060608's avatar
fxy060608 已提交
73
  }
74
}