|
| 1 | +const ora = require('ora') |
| 2 | +const rm = require('rimraf') |
| 3 | +const chalk = require('chalk') |
| 4 | +const webpack = require('webpack') |
| 5 | +const program = require('commander') |
| 6 | +const { userConf, supportedModes } = require('../config/index') |
| 7 | +const getWebpackConf = require('./getWebpackConf') |
| 8 | +const { resolveDist } = require('./utils') |
| 9 | + |
| 10 | +program |
| 11 | + .option('-w, --watch', 'watch mode') |
| 12 | + .option('-p, --production', 'production release') |
| 13 | + .parse(process.argv) |
| 14 | + |
| 15 | +// 提供npm argv找到期望构建的平台,必须在上面支持的平台列表里 |
| 16 | +const npmConfigArgvOriginal = (process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original) || [] |
| 17 | +const modeArr = npmConfigArgvOriginal.filter(item => typeof item === 'string').map(item => item.replace('--', '')).filter(item => supportedModes.includes(item)) |
| 18 | + |
| 19 | +// 暂时兼容npm7的写法 |
| 20 | +if (!npmConfigArgvOriginal.length) { |
| 21 | + const env = process.env |
| 22 | + supportedCrossMode.forEach(key => { |
| 23 | + if (env[`npm_config_${key}`] === 'true') { |
| 24 | + modeArr.push(key) |
| 25 | + } |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +if (!modeArr.length) modeArr.push(userConf.srcMode) |
| 30 | + |
| 31 | +let webpackConfs = [] |
| 32 | + |
| 33 | +modeArr.forEach((mode) => { |
| 34 | + const options = Object.assign({}, userConf, { |
| 35 | + mode, |
| 36 | + production: program.production, |
| 37 | + watch: program.watch, |
| 38 | + report: process.env.npm_config_report, |
| 39 | + subDir: (userConf.isPlugin || userConf.cloudFunc) ? 'miniprogram' : '' |
| 40 | + }) |
| 41 | + webpackConfs.push(getWebpackConf(options)) |
| 42 | +}) |
| 43 | + |
| 44 | +if (userConf.isPlugin) { |
| 45 | + const options = Object.assign({}, userConf, { |
| 46 | + plugin: true, |
| 47 | + mode: 'wx', |
| 48 | + production: program.production, |
| 49 | + watch: program.watch, |
| 50 | + report: process.env.npm_config_report, |
| 51 | + subDir: 'plugin' |
| 52 | + }) |
| 53 | + webpackConfs.push(getWebpackConf(options)) |
| 54 | +} |
| 55 | + |
| 56 | +if (webpackConfs.length === 1) { |
| 57 | + webpackConfs = webpackConfs[0] |
| 58 | +} |
| 59 | + |
| 60 | +const spinner = ora('building...') |
| 61 | +spinner.start() |
| 62 | + |
| 63 | +try { |
| 64 | + modeArr.forEach(item => { |
| 65 | + rm.sync(resolveDist(item, '*')) |
| 66 | + }) |
| 67 | +} catch (e) { |
| 68 | + console.error(e) |
| 69 | + console.log('\n\n删除dist文件夹遇到了一些问题,如果遇到问题请手工删除dist重来\n\n') |
| 70 | +} |
| 71 | + |
| 72 | +if (program.watch) { |
| 73 | + webpack(webpackConfs).watch(undefined, callback) |
| 74 | +} else { |
| 75 | + webpack(webpackConfs, callback) |
| 76 | +} |
| 77 | + |
| 78 | +function callback (err, stats) { |
| 79 | + spinner.stop() |
| 80 | + if (err) { |
| 81 | + process.exitCode = 1 |
| 82 | + return console.error(err) |
| 83 | + } |
| 84 | + if (Array.isArray(stats.stats)) { |
| 85 | + stats.stats.forEach(item => { |
| 86 | + console.log(item.compilation.name + '打包结果:') |
| 87 | + process.stdout.write(item.toString({ |
| 88 | + colors: true, |
| 89 | + modules: false, |
| 90 | + children: false, |
| 91 | + chunks: false, |
| 92 | + chunkModules: false, |
| 93 | + entrypoints: false |
| 94 | + }) + '\n\n') |
| 95 | + }) |
| 96 | + } else { |
| 97 | + process.stdout.write(stats.toString({ |
| 98 | + colors: true, |
| 99 | + modules: false, |
| 100 | + children: false, |
| 101 | + chunks: false, |
| 102 | + chunkModules: false, |
| 103 | + entrypoints: false |
| 104 | + }) + '\n\n') |
| 105 | + } |
| 106 | + |
| 107 | + if (stats.hasErrors()) { |
| 108 | + console.log(chalk.red(' Build failed with errors.\n')) |
| 109 | + } else if (program.watch) { |
| 110 | + console.log(chalk.cyan(` Build complete at ${new Date()}.\n Still watching...\n`)) |
| 111 | + } else { |
| 112 | + console.log(chalk.cyan(' Build complete.\n')) |
| 113 | + } |
| 114 | +} |
0 commit comments