UNPKG

3.18 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3// const path = require('path')
4const program = require('commander')
5const chalk = require('chalk')
6
7const __ = require('../utils/translate')
8const readBuildConfigFile = require('../utils/read-build-config-file')
9const sleep = require('../utils/sleep')
10const setEnvFromCommand = require('../utils/set-env-from-command')
11
12const superBuild = require('../core/webpack/enter')
13
14program
15 .version(require('../package').version, '-v, --version')
16 .usage('[options]')
17 .option('-c, --client', 'Set STAGE to CLIENT')
18 .option('-s, --server', 'Set STAGE to SERVER')
19 .option('--stage <stage>', 'Set STAGE')
20 .option('--env <env>', 'Set ENV')
21 .option('--config <config-file-path>', 'Set config file')
22 .option('--type <project-type>', 'Set project type')
23 .parse(process.argv)
24
25/**
26 * 执行打包
27 */
28const run = async () => {
29 // 清空 log
30 process.stdout.write('\x1B[2J\x1B[0f')
31
32 const {
33 client, server,
34 stage: _stage,
35 env = 'prod',
36 config,
37 type,
38 } = program
39
40 setEnvFromCommand({
41 config,
42 type,
43 })
44
45 const stage = _stage ? _stage : (client ? 'client' : (server ? 'server' : false))
46
47 // console.log(stage, env)
48
49 // if (!stage) {
50 // console.log(
51 // chalk.redBright('× ')
52 // + __('build.missing_option', {
53 // option: chalk.yellowBright('stage'),
54 // example: 'super-build ' + chalk.green('--stage client') + ' --env prod',
55 // indent: ' '
56 // })
57 // )
58 // return
59 // }
60
61 // if (!env) {
62 // console.log(
63 // chalk.redBright('× ')
64 // + __('build.missing_option', {
65 // option: chalk.yellowBright('env'),
66 // example: 'super-build ' + chalk.green('--env prod'),
67 // indent: ' '
68 // })
69 // )
70 // return
71 // }
72
73 // 在所有操作执行之前定义环境变量
74 process.env.WEBPACK_BUILD_STAGE = stage || 'client'
75 process.env.WEBPACK_BUILD_ENV = env
76
77 // 读取构建配置
78 const buildConfig = await readBuildConfigFile()
79 // const {
80 // server: hasServer
81 // } = buildConfig
82
83 // 如果提供了 stage,仅针对 stage 执行打包
84 if (stage) {
85 // if (stage === 'server' && !hasServer) {
86 // console.log(chalk.redBright('× '))
87 // }
88 return await superBuild(buildConfig)
89 }
90
91 // 如过没有提供 stage,自动相继打包 client 和 server
92 await superBuild({ ...buildConfig })
93 await sleep(100)
94
95 // if (!hasServer) return
96
97 console.log('\n' + ''.padEnd(60, '=') + '\n')
98 process.env.WEBPACK_BUILD_STAGE = 'server'
99 await superBuild({ ...buildConfig })
100 await sleep(100)
101
102 console.log('\n' + ''.padEnd(60, '=') + '\n')
103 console.log(
104 chalk.green('√ ')
105 + chalk.yellowBright('[super/build] ')
106 + __('build.complete', {
107 time: (new Date()).toLocaleString()
108 })
109 )
110}
111
112run()