index.js 3.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
const fs = require('fs')
const path = require('path')

fxy060608's avatar
fxy060608 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
const {
  manifestPlatformOptions
} = require('./lib/env')

const {
  assetsDir
} = require('./lib/copy-webpack-options')

require('./lib/check-update')()

const initBuildCommand = require('./commands/build')
const initServeCommand = require('./commands/serve')

module.exports = (api, options) => {
  initServeCommand(api, options)

  initBuildCommand(api, options)

fxy060608's avatar
fxy060608 已提交
22
  if (process.env.UNI_PLATFORM === 'quickapp-vue') {
fxy060608's avatar
fxy060608 已提交
23
    process.env.UNI_OUTPUT_DIR = path.resolve(process.env.UNI_OUTPUT_DIR, 'build')
fxy060608's avatar
fxy060608 已提交
24
    Object.assign(options, {
fxy060608's avatar
fxy060608 已提交
25
      assetsDir,
fxy060608's avatar
fxy060608 已提交
26
      parallel: false,
fxy060608's avatar
fxy060608 已提交
27
      outputDir: process.env.UNI_OUTPUT_DIR
fxy060608's avatar
fxy060608 已提交
28 29
    })
    require('./lib/options')(options)
fxy060608's avatar
fxy060608 已提交
30 31 32 33 34 35 36 37
    const platformOptions = {
      webpackConfig: {},
      chainWebpack () {}
    }
    const manifestPlatformOptions = {}
    api.configureWebpack(require('./lib/configure-webpack')(platformOptions, manifestPlatformOptions, options, api))
    api.chainWebpack(require('./lib/chain-webpack')(platformOptions, options, api))

fxy060608's avatar
fxy060608 已提交
38
    const vueConfig = require('@dcloudio/uni-quickapp-vue/lib/vue.config.js')
fxy060608's avatar
fxy060608 已提交
39 40 41 42 43
    api.configureWebpack(vueConfig.configureWebpack)
    api.chainWebpack(vueConfig.chainWebpack)
    return
  }

fxy060608's avatar
fxy060608 已提交
44 45 46 47 48
  const type = ['app-plus', 'h5'].includes(process.env.UNI_PLATFORM)
    ? process.env.UNI_PLATFORM
    : 'mp'

  const platformOptions = require('./lib/' + type)
fxy060608's avatar
fxy060608 已提交
49 50 51 52 53 54

  let vueConfig = platformOptions.vueConfig

  if (typeof vueConfig === 'function') {
    vueConfig = vueConfig(options, api)
  }
fxy060608's avatar
fxy060608 已提交
55

fxy060608's avatar
fxy060608 已提交
56
  Object.assign(options, { // TODO 考虑非 HBuilderX 运行时,可以支持自定义输出目录
fxy060608's avatar
fxy060608 已提交
57 58
    outputDir: process.env.UNI_OUTPUT_TMP_DIR || process.env.UNI_OUTPUT_DIR,
    assetsDir
59
  }, vueConfig) // 注意,此处目前是覆盖关系,后续考虑改为webpack merge逻辑
fxy060608's avatar
fxy060608 已提交
60 61 62

  require('./lib/options')(options)

fxy060608's avatar
fxy060608 已提交
63
  api.configureWebpack(require('./lib/configure-webpack')(platformOptions, manifestPlatformOptions, options, api))
fxy060608's avatar
fxy060608 已提交
64
  api.chainWebpack(require('./lib/chain-webpack')(platformOptions, options, api))
fxy060608's avatar
fxy060608 已提交
65

fxy060608's avatar
fxy060608 已提交
66
  global.uniPlugin.configureWebpack.forEach(configureWebpack => {
fxy060608's avatar
fxy060608 已提交
67 68 69
    api.configureWebpack(function (webpackConfig) {
      return configureWebpack(webpackConfig, options)
    })
fxy060608's avatar
fxy060608 已提交
70 71
  })
  global.uniPlugin.chainWebpack.forEach(chainWebpack => {
fxy060608's avatar
fxy060608 已提交
72 73 74
    api.chainWebpack(function (webpackConfig) {
      return chainWebpack(webpackConfig, options)
    })
fxy060608's avatar
fxy060608 已提交
75 76
  })

fxy060608's avatar
fxy060608 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  if (
    process.env.UNI_PLATFORM === 'h5' ||
    (
      process.env.UNI_PLATFORM === 'app-plus' &&
      process.env.UNI_USING_V3
    )
  ) {
    const migrate = require('@dcloudio/uni-migration')
    const wxcomponents = path.resolve(process.env.UNI_INPUT_DIR, 'wxcomponents')
    if (fs.existsSync(wxcomponents)) { // 转换 mp-weixin 小程序组件
      migrate(wxcomponents, false, {
        silent: true // 不输出日志
      })
    }
  }
fxy060608's avatar
fxy060608 已提交
92 93 94
}

module.exports.defaultModes = {
fxy060608's avatar
fxy060608 已提交
95 96
  'uni-serve': 'development',
  'uni-build': process.env.NODE_ENV
fxy060608's avatar
fxy060608 已提交
97
}