index.js 2.5 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') {
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 26
      assetsDir,
      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 39 40 41 42 43
    const vueConfig = require('@dcloudio/uni-quickapp/lib/vue.config.js')
    api.configureWebpack(vueConfig.configureWebpack)
    api.chainWebpack(vueConfig.chainWebpack)
    return
  }

fxy060608's avatar
fxy060608 已提交
44
  const platformOptions = require('./lib/' + process.env.UNI_PLATFORM)
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50

  let vueConfig = platformOptions.vueConfig

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

fxy060608's avatar
fxy060608 已提交
52
  Object.assign(options, { // TODO 考虑非 HBuilderX 运行时,可以支持自定义输出目录
fxy060608's avatar
fxy060608 已提交
53 54
    outputDir: process.env.UNI_OUTPUT_TMP_DIR || process.env.UNI_OUTPUT_DIR,
    assetsDir
fxy060608's avatar
fxy060608 已提交
55
  }, vueConfig)
fxy060608's avatar
fxy060608 已提交
56 57 58

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

fxy060608's avatar
fxy060608 已提交
59
  api.configureWebpack(require('./lib/configure-webpack')(platformOptions, manifestPlatformOptions, options, api))
fxy060608's avatar
fxy060608 已提交
60
  api.chainWebpack(require('./lib/chain-webpack')(platformOptions, options, api))
fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

  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 已提交
77 78 79
}

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