1、根路径下新建build.js(注意安装必要的npm包)
const { run } = require('runjs')
const chalk = require('chalk')
const path = require('path');
const rawArgv = process.argv.slice(2)
const args = rawArgv.join(' ')
if (process.env.npm_config_preview || rawArgv.includes('--preview')) {
const report = rawArgv.includes('--report')
run(`vue-cli-service build ${args}`)
const port = 6760
const publicPath = '/wxmanage/'
var express = require('express')
const app = express()
app.use(publicPath, express.static(path.join(__dirname,'../dist'), {
index: ['index.html', '/']
}))
app.use('*', function(req, res, next){
res.sendFile(path.join(__dirname,'../dist/index.html'))
})
app.listen(port, function () {
console.log(chalk.green(`> Preview at http://localhost:${port}${publicPath}`))
if (report) {
console.log(chalk.green(`> Report at http://localhost:${port}${publicPath}report.html`))
}
})
} else {
run(`vue-cli-service build ${args}`)
}
2、package.json添加如下命令
"preview": "node build.js --preview --mode mode",
3、启动命令
npm run preview