action.ts 1.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import { extend } from '@vue/shared'
import { BuildOptions, ServerOptions } from 'vite'
import { CliOptions } from '.'
import { build, buildSSR } from './build'
fxy060608's avatar
fxy060608 已提交
5
import { runNVue } from './nvue'
fxy060608's avatar
fxy060608 已提交
6 7 8 9
import { createServer, createSSRServer } from './server'
import { initEnv } from './utils'

export async function runDev(options: CliOptions & ServerOptions) {
fxy060608's avatar
fxy060608 已提交
10
  extend(options, { watch: true, minify: false })
fxy060608's avatar
fxy060608 已提交
11 12 13 14 15
  initEnv('dev', options)
  try {
    if (options.platform === 'h5') {
      await (options.ssr ? createSSRServer(options) : createServer(options))
    } else {
fxy060608's avatar
fxy060608 已提交
16
      await build(options)
fxy060608's avatar
fxy060608 已提交
17
    }
fxy060608's avatar
fxy060608 已提交
18
    if (options.platform === 'app') {
fxy060608's avatar
fxy060608 已提交
19
      await runNVue('dev')
fxy060608's avatar
fxy060608 已提交
20
    }
fxy060608's avatar
fxy060608 已提交
21
  } catch (e) {
fxy060608's avatar
fxy060608 已提交
22
    console.error(`error when starting dev server:\n${e.stack || e}`)
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30 31 32
    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 已提交
33
    if (options.platform === 'app') {
fxy060608's avatar
fxy060608 已提交
34
      await runNVue('prod')
fxy060608's avatar
fxy060608 已提交
35
    }
fxy060608's avatar
fxy060608 已提交
36 37
    console.log(` DONE  Build complete.`)
  } catch (e) {
fxy060608's avatar
fxy060608 已提交
38
    console.error(`error during build:\n${e.stack || e}`)
fxy060608's avatar
fxy060608 已提交
39 40 41
    process.exit(1)
  }
}