1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const configValidator_1 = require("../../doctor/configValidator");
|
12 | exports.default = (ctx) => {
|
13 | registerBuildHooks(ctx);
|
14 | ctx.registerCommand({
|
15 | name: 'build',
|
16 | optionsMap: {
|
17 | '--type [typeName]': 'Build type, weapp/swan/alipay/tt/h5/quickapp/rn/qq/jd',
|
18 | '--watch': 'Watch mode',
|
19 | '--page [pagePath]': 'Build one page',
|
20 | '--component [pagePath]': 'Build one component',
|
21 | '--env [env]': 'Env type',
|
22 | '--ui': 'Build Taro UI library',
|
23 | '--ui-index [uiIndexPath]': 'Index file for build Taro UI library',
|
24 | '--plugin [typeName]': 'Build Taro plugin project, weapp',
|
25 | '--port [port]': 'Specified port',
|
26 | '--release': 'Release quickapp'
|
27 | },
|
28 | fn(opts) {
|
29 | return __awaiter(this, void 0, void 0, function* () {
|
30 | const { platform, config } = opts;
|
31 | const { fs, chalk, PROJECT_CONFIG } = ctx.helper;
|
32 | const { outputPath, configPath } = ctx.paths;
|
33 | const { isWatch, envHasBeenSet } = ctx.runOpts;
|
34 | if (!fs.existsSync(configPath)) {
|
35 | console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`));
|
36 | process.exit(1);
|
37 | }
|
38 | const checkResult = yield checkConfig({
|
39 | configPath,
|
40 | projectConfig: ctx.initialConfig
|
41 | });
|
42 | if (checkResult.lines.length) {
|
43 | const NOTE_VALID = chalk.yellow('[!] ');
|
44 | const NOTE_INVALID = chalk.red('[✗] ');
|
45 | const lineChalk = chalk.hex('#fff');
|
46 | const errorChalk = chalk.hex('#f00');
|
47 | console.log(errorChalk(`Taro 配置有误,请检查! (${configPath})`));
|
48 | checkResult.lines.forEach(line => {
|
49 | console.log(' ' +
|
50 | (line.valid ? NOTE_VALID : NOTE_INVALID) +
|
51 | lineChalk(line.desc));
|
52 | });
|
53 | }
|
54 | if (typeof platform !== 'string') {
|
55 | console.log(chalk.red('请传入正确的编译类型!'));
|
56 | process.exit(0);
|
57 | }
|
58 | process.env.TARO_ENV = platform;
|
59 | fs.ensureDirSync(outputPath);
|
60 | let isProduction = false;
|
61 | if (!envHasBeenSet) {
|
62 | isProduction = process.env.NODE_ENV === 'production' || !isWatch;
|
63 | }
|
64 | yield ctx.applyPlugins('onBuildStart');
|
65 | yield ctx.applyPlugins({
|
66 | name: platform,
|
67 | opts: {
|
68 | config: Object.assign({}, config, { isWatch, mode: isProduction ? 'production' : 'development', modifyWebpackChain(chain, webpack) {
|
69 | return __awaiter(this, void 0, void 0, function* () {
|
70 | yield ctx.applyPlugins({
|
71 | name: 'modifyWebpackChain',
|
72 | initialVal: chain,
|
73 | opts: {
|
74 | chain,
|
75 | webpack
|
76 | }
|
77 | });
|
78 | });
|
79 | },
|
80 | modifyBuildAssets(assets) {
|
81 | return __awaiter(this, void 0, void 0, function* () {
|
82 | yield ctx.applyPlugins({
|
83 | name: 'modifyBuildAssets',
|
84 | initialVal: assets,
|
85 | opts: {
|
86 | assets
|
87 | }
|
88 | });
|
89 | });
|
90 | },
|
91 | modifyBuildTempFileContent(tempFiles) {
|
92 | return __awaiter(this, void 0, void 0, function* () {
|
93 | yield ctx.applyPlugins({
|
94 | name: 'modifyBuildTempFileContent',
|
95 | initialVal: tempFiles,
|
96 | opts: {
|
97 | tempFiles
|
98 | }
|
99 | });
|
100 | });
|
101 | },
|
102 | onBuildFinish({ error, stats, isWatch }) {
|
103 | return __awaiter(this, void 0, void 0, function* () {
|
104 | yield ctx.applyPlugins({
|
105 | name: 'onBuildFinish',
|
106 | opts: {
|
107 | error,
|
108 | stats,
|
109 | isWatch
|
110 | }
|
111 | });
|
112 | });
|
113 | } })
|
114 | }
|
115 | });
|
116 | });
|
117 | }
|
118 | });
|
119 | };
|
120 | function registerBuildHooks(ctx) {
|
121 | [
|
122 | 'modifyWebpackChain',
|
123 | 'modifyBuildAssets',
|
124 | 'modifyBuildTempFileContent',
|
125 | 'onBuildStart',
|
126 | 'onBuildFinish'
|
127 | ].forEach(methodName => {
|
128 | ctx.registerMethod(methodName);
|
129 | });
|
130 | }
|
131 | function checkConfig({ projectConfig, configPath }) {
|
132 | return __awaiter(this, void 0, void 0, function* () {
|
133 | const result = yield configValidator_1.default({
|
134 | configPath,
|
135 | projectConfig
|
136 | });
|
137 | return result;
|
138 | });
|
139 | }
|