build/build.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const ora = require('ora')
const rm = require('rimraf') // 删除
const chalk = require('chalk') // 颜色
const webpack = require('webpack')
const webpackConfig = require('./webpack.config')
const { resolve } = require('./utils')
const APP_ENV = process.env.APP_ENV ? process.env.APP_ENV : 'prod'

const spinner = ora(chalk.green(`${process.env.NODE_ENV}: ${APP_ENV}环境编译打包开始...`))
.start()


rm(resolve('dist'), err => {
if (err) throw err
webpack(webpackConfig, (err, stats) => {
spinner.stop()
if (err) throw err

process.stdout.write(stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
})+ '\n\n')

if (stats.hasErrors()) {
console.log(chalk.red.bold('编译打包失败.\n'))
process.exit(1)
}

console.log(chalk.blue('打包完成.\n'))
})
})

package.json

1
2
3
4
5
6
7
8
9
10
11
...
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --color --config ./build/webpack.config.js",
"start": "npm run dev",
"build": "cross-env NODE_ENV=production node ./build/build.js",
"build:prod": "cross-env APP_ENV=prod npm run build",
"build:qa": "cross-env APP_ENV=qa npm run build",
"demo": "node ./build/utils.js",
"test": "jest"
},
...