action.ts 1.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { extend } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2
import { RollupWatcher } from 'rollup'
fxy060608's avatar
fxy060608 已提交
3
import { BuildOptions, ServerOptions } from 'vite'
fxy060608's avatar
fxy060608 已提交
4 5 6 7
import {
  TIPS_BUILD_COMPLETE,
  TIPS_WATCH_FOR_CHANGES,
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13
import { CliOptions } from '.'
import { build, buildSSR } from './build'
import { createServer, createSSRServer } from './server'
import { initEnv } from './utils'

export async function runDev(options: CliOptions & ServerOptions) {
fxy060608's avatar
fxy060608 已提交
14
  extend(options, { watch: true, minify: false })
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19
  initEnv('dev', options)
  try {
    if (options.platform === 'h5') {
      await (options.ssr ? createSSRServer(options) : createServer(options))
    } else {
fxy060608's avatar
fxy060608 已提交
20 21 22
      const watcher = (await build(options)) as RollupWatcher
      watcher.on('event', (event) => {
        if (event.code === 'BUNDLE_END') {
fxy060608's avatar
fxy060608 已提交
23
          event.result.close()
fxy060608's avatar
fxy060608 已提交
24
          console.log(TIPS_WATCH_FOR_CHANGES)
fxy060608's avatar
fxy060608 已提交
25 26
        }
      })
fxy060608's avatar
fxy060608 已提交
27 28
    }
  } catch (e) {
29 30 31 32 33
    if (options.platform === 'h5') {
      console.error(`error when starting dev server:\n${e.stack || e}`)
    } else {
      console.error(`error during build:\n${e.stack || e}`)
    }
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40 41 42 43
    process.exit(1)
  }
}

export async function runBuild(options: CliOptions & BuildOptions) {
  initEnv('build', options)
  try {
    await (options.ssr && options.platform === 'h5'
      ? buildSSR(options)
      : build(options))
fxy060608's avatar
fxy060608 已提交
44
    console.log(TIPS_BUILD_COMPLETE)
fxy060608's avatar
fxy060608 已提交
45
  } catch (e) {
fxy060608's avatar
fxy060608 已提交
46
    console.error(`error during build:\n${e.stack || e}`)
fxy060608's avatar
fxy060608 已提交
47 48 49
    process.exit(1)
  }
}