index.js 2.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 23 24 25 26 27 28 29 30 31 32 33
  if (process.env.UNI_PLATFORM === 'quickapp') {
    Object.assign(options, {
      assetsDir,
      outputDir: path.resolve(process.env.UNI_OUTPUT_DIR, 'build')
    })
    require('./lib/options')(options)
    const vueConfig = require('@dcloudio/uni-quickapp/lib/vue.config.js')
    api.configureWebpack(vueConfig.configureWebpack)
    api.chainWebpack(vueConfig.chainWebpack)
    return
  }

fxy060608's avatar
fxy060608 已提交
34
  const platformOptions = require('./lib/' + process.env.UNI_PLATFORM)
fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40

  let vueConfig = platformOptions.vueConfig

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

fxy060608's avatar
fxy060608 已提交
42
  Object.assign(options, { // TODO 考虑非 HBuilderX 运行时,可以支持自定义输出目录
fxy060608's avatar
fxy060608 已提交
43 44
    outputDir: process.env.UNI_OUTPUT_TMP_DIR || process.env.UNI_OUTPUT_DIR,
    assetsDir
fxy060608's avatar
fxy060608 已提交
45
  }, vueConfig)
fxy060608's avatar
fxy060608 已提交
46 47 48

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

fxy060608's avatar
fxy060608 已提交
49
  api.configureWebpack(require('./lib/configure-webpack')(platformOptions, manifestPlatformOptions, options, api))
fxy060608's avatar
fxy060608 已提交
50
  api.chainWebpack(require('./lib/chain-webpack')(platformOptions, options, api))
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

  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 已提交
67 68 69
}

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