index.ts 1.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import webpack from 'webpack'
fxy060608's avatar
fxy060608 已提交
2 3
import { once } from '@dcloudio/uni-shared'

fxy060608's avatar
fxy060608 已提交
4
import { createConfig } from './config'
fxy060608's avatar
fxy060608 已提交
5 6 7
import { initModuleAlias } from './alias'

const initModuleAliasOnce = once(initModuleAlias)
fxy060608's avatar
fxy060608 已提交
8

fxy060608's avatar
fxy060608 已提交
9 10 11 12
function runWebpack(
  mode: 'production' | 'development',
  options: NVueCompilerOptions
) {
fxy060608's avatar
fxy060608 已提交
13
  initModuleAliasOnce()
fxy060608's avatar
fxy060608 已提交
14 15
  return new Promise<webpack.Compiler>((resolve, reject) => {
    const compiler = webpack(createConfig(mode, options), (err, stats) => {
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22
      if (err) {
        return reject(err.stack || err)
      }

      if (stats!.hasErrors()) {
        return reject(stats!.toString())
      }
fxy060608's avatar
fxy060608 已提交
23

fxy060608's avatar
fxy060608 已提交
24
      if (stats!.hasWarnings()) {
fxy060608's avatar
fxy060608 已提交
25
        const info = stats!.toJson({ all: false, warnings: true })
fxy060608's avatar
fxy060608 已提交
26 27
        console.warn(info.warnings)
      }
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34 35 36 37 38
      if (process.env.DEBUG) {
        console.log(
          stats!.toString({
            all: false,
            assets: true,
            colors: true, // 在控制台展示颜色
            // timings: true,
          })
        )
      }
      resolve(compiler)
fxy060608's avatar
fxy060608 已提交
39 40 41 42
    })
  })
}

fxy060608's avatar
fxy060608 已提交
43
export function runWebpackBuild(options: NVueCompilerOptions = {}) {
fxy060608's avatar
fxy060608 已提交
44
  return runWebpack('production', options)
fxy060608's avatar
fxy060608 已提交
45 46
}

fxy060608's avatar
fxy060608 已提交
47
export function runWebpackDev(options: NVueCompilerOptions = {}) {
fxy060608's avatar
fxy060608 已提交
48
  return runWebpack('development', options)
fxy060608's avatar
fxy060608 已提交
49
}