UNPKG

2.3 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs-extra')
4const path = require('path')
5const program = require('commander')
6// const chalk = require('chalk')
7
8const before = require('./_before')
9
10// const __ = require('../utils/translate')
11const validateConfig = require('../libs/validate-config')
12// const readBuildConfigFile = require('../utils/read-build-config-file')
13const setEnvFromCommand = require('../utils/set-env-from-command')
14const initNodeEnv = require('../utils/init-node-env')
15const getDirTemp = require('../libs/get-dir-tmp')
16
17const kootWebpackBuild = require('koot-webpack/build')
18
19program
20 .version(require('../package').version, '-v, --version')
21 .usage('[options]')
22 .option('-c, --client', 'Set STAGE to CLIENT')
23 .option('-s, --server', 'Set STAGE to SERVER')
24 .option('--stage <stage>', 'Set STAGE')
25 .option('--config <config-file-path>', 'Set config file pathname')
26 .option('--type <project-type>', 'Set project type')
27 .option('--koot-test', 'Koot test mode')
28 .parse(process.argv)
29
30const run = async () => {
31 // 清空 log
32 process.stdout.write('\x1B[2J\x1B[0f')
33
34 const {
35 client, server,
36 stage: _stage,
37 config,
38 type,
39 } = program
40
41 initNodeEnv()
42 // console.log(program)
43
44 setEnvFromCommand({
45 config,
46 type,
47 })
48
49 const stage = _stage ||
50 (client ? 'client' : undefined) ||
51 (server ? 'server' : undefined) ||
52 'client'
53
54 process.env.WEBPACK_BUILD_STAGE = stage || 'client'
55 process.env.WEBPACK_BUILD_ENV = 'prod'
56
57 await before(program)
58
59 // 处理目录
60 const dirAnalyzeBuild = require('../libs/get-dir-dev-tmp')(undefined, 'analyze')
61 await fs.ensureDir(dirAnalyzeBuild)
62 await fs.emptyDir(dirAnalyzeBuild)
63 await fs.ensureDir(path.resolve(dirAnalyzeBuild, 'public'))
64 await fs.ensureDir(path.resolve(dirAnalyzeBuild, 'server'))
65
66 // 读取构建配置
67 const kootConfig = {
68 ...await validateConfig(),
69
70 dist: dirAnalyzeBuild,
71 bundleVersionsKeep: false,
72 }
73
74 await kootWebpackBuild({
75 analyze: true,
76 ...kootConfig
77 })
78
79 // 清理临时目录
80 await fs.remove(getDirTemp())
81
82 // 清理结果目录
83 await fs.remove(dirAnalyzeBuild)
84
85 console.log(' ')
86}
87
88run()