UNPKG

3.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const fs = require("fs-extra");
5const tapable_1 = require("tapable");
6const _ = require("lodash");
7const chalk_1 = require("chalk");
8const constants_1 = require("./util/constants");
9const util_1 = require("./util");
10const config_1 = require("./config");
11class 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" /* H5 */ && (type !== "quickapp" /* 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" /* H5 */:
50 this.buildForH5(this.appPath, { watch, port });
51 break;
52 case "weapp" /* WEAPP */:
53 case "swan" /* SWAN */:
54 case "alipay" /* ALIPAY */:
55 case "tt" /* TT */:
56 case "quickapp" /* QUICKAPP */:
57 case "qq" /* QQ */:
58 case "jd" /* JD */:
59 this.buildForMini(this.appPath, buildOptions);
60 break;
61 case "rn" /* RN */:
62 this.buildForRN(this.appPath, { watch, port });
63 break;
64 case "ui" /* UI */:
65 this.buildForUILibrary(this.appPath, { watch, uiIndex });
66 break;
67 case "plugin" /* 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" /* WEAPP */]: '微信',
92 ["alipay" /* ALIPAY */]: '支付宝'
93 };
94 if (platform !== "weapp" /* WEAPP */ && platform !== "alipay" /* 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}
102exports.default = Builder;