Vue-cli-service 服务构建完成回调?

Vue-cli-service serve build completion callback?

我想启动一个 vue-cli 开发服务器,然后 运行 此构建完成后来自节点的命令。

有没有办法从节点脚本 运行 vue-cli-service serve 以便我可以在开发服务器启动并 运行ning 后收到回调?

vue-cli-service.js 没有 return 服务承诺(这将允许您设置完成回调),但您可以复制脚本(25 行)来执行此操作:

my-vue-cli-service.js:

#!/usr/bin/env node
const semver = require('semver')
const { error } = require('@vue/cli-shared-utils')
const requiredVersion = require('@vue/cli-service/package.json').engines.node

if (!semver.satisfies(process.version, requiredVersion)) {
  error(
    `You are using Node ${process.version}, but vue-cli-service ` +
    `requires Node ${requiredVersion}.\nPlease upgrade your Node version.`
  )
  process.exit(1)
}

const Service = require('@vue/cli-service')
const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd())

const rawArgv = process.argv.slice(2)
const args = require('minimist')(rawArgv)
const command = args._[0]

module.exports = () =>  // <--- add this line to return the service promise
  service.run(command, args, rawArgv).catch(err => {
    error(err)
    process.exit(1)
  })

然后,您可以创建另一个使用此副本的脚本,添加一个 then 回调以在服务器成功启动时调用:

我的-service.js:

#!/usr/bin/env node
const service = require('./my-vue-cli-service')

service().then(result => {
  if (result && result.server) {  // <--- server started successfully if result.server exists
    const {server, url} = result;
    console.log('ready');
  }
})

务必更新 package.json 以使用新脚本:

package.json:

{
  "scripts": {
    "serve": "./my-service.js serve"
  }
}