1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const fs = require("fs-extra");
|
5 | const tapable_1 = require("tapable");
|
6 | const _ = require("lodash");
|
7 | const chalk_1 = require("chalk");
|
8 | const constants_1 = require("./util/constants");
|
9 | const util_1 = require("./util");
|
10 | const config_1 = require("./config");
|
11 | class Builder {
|
12 | constructor(appPath) {
|
13 | this.hooks = {
|
14 | beforeBuild: new tapable_1.SyncHook(['config']),
|
15 | afterBuild: new tapable_1.SyncHook(['builder'])
|
16 | };
|
17 | this.appPath = appPath;
|
18 | this.init();
|
19 | }
|
20 | init() {
|
21 | this.resolveConfig();
|
22 | this.applyPlugins();
|
23 | }
|
24 | resolveConfig() {
|
25 | this.config = require(path.join(this.appPath, constants_1.PROJECT_CONFIG))(_.merge);
|
26 | }
|
27 | applyPlugins() {
|
28 | const plugins = this.config.plugins || [];
|
29 | if (plugins.length) {
|
30 | plugins.forEach((plugin) => {
|
31 | plugin.apply(this);
|
32 | });
|
33 | }
|
34 | }
|
35 | emptyFirst({ watch, type }) {
|
36 | const outputPath = path.join(this.appPath, `${this.config.outputRoot || config_1.default.OUTPUT_DIR}`);
|
37 | if (!fs.existsSync(outputPath)) {
|
38 | fs.ensureDirSync(outputPath);
|
39 | }
|
40 | else if (type !== "h5" && (type !== "quickapp" || !watch)) {
|
41 | util_1.emptyDirectory(outputPath);
|
42 | }
|
43 | }
|
44 | build(buildOptions) {
|
45 | this.hooks.beforeBuild.call(this.config);
|
46 | const { type, watch, platform, port, uiIndex } = buildOptions;
|
47 | this.emptyFirst({ type, watch });
|
48 | switch (type) {
|
49 | case "h5" :
|
50 | this.buildForH5(this.appPath, { watch, port });
|
51 | break;
|
52 | case "weapp" :
|
53 | case "swan" :
|
54 | case "alipay" :
|
55 | case "tt" :
|
56 | case "quickapp" :
|
57 | case "qq" :
|
58 | case "jd" :
|
59 | this.buildForMini(this.appPath, buildOptions);
|
60 | break;
|
61 | case "rn" :
|
62 | this.buildForRN(this.appPath, { watch, port });
|
63 | break;
|
64 | case "ui" :
|
65 | this.buildForUILibrary(this.appPath, { watch, uiIndex });
|
66 | break;
|
67 | case "plugin" :
|
68 | this.buildForPlugin(this.appPath, {
|
69 | watch,
|
70 | platform
|
71 | });
|
72 | break;
|
73 | default:
|
74 | console.log(chalk_1.default.red('输入类型错误,目前只支持 weapp/swan/alipay/tt/qq/h5/quickapp/rn 八端类型'));
|
75 | }
|
76 | }
|
77 | buildForH5(appPath, buildOptions) {
|
78 | require('./h5').build(appPath, buildOptions);
|
79 | }
|
80 | buildForMini(appPath, buildOptions) {
|
81 | require('./mini').build(appPath, buildOptions, null, this);
|
82 | }
|
83 | buildForRN(appPath, { watch, port }) {
|
84 | require('./rn').build(appPath, { watch, port });
|
85 | }
|
86 | buildForUILibrary(appPath, { watch, uiIndex }) {
|
87 | require('./ui/index').build(appPath, { watch, uiIndex });
|
88 | }
|
89 | buildForPlugin(appPath, { watch, platform }) {
|
90 | const typeMap = {
|
91 | ["weapp" ]: '微信',
|
92 | ["alipay" ]: '支付宝'
|
93 | };
|
94 | if (platform !== "weapp" && platform !== "alipay" ) {
|
95 | console.log(chalk_1.default.red('目前插件编译仅支持 微信/支付宝 小程序!'));
|
96 | return;
|
97 | }
|
98 | console.log(chalk_1.default.green(`开始编译${typeMap[platform]}小程序插件`));
|
99 | require('./plugin').build(appPath, { watch, platform }, this);
|
100 | }
|
101 | }
|
102 | exports.default = Builder;
|