UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2var __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};
10Object.defineProperty(exports, "__esModule", { value: true });
11const path = require("path");
12exports.default = (ctx) => {
13 ctx.registerPlatform({
14 name: 'plugin',
15 useConfigName: 'mini',
16 fn({ config }) {
17 return __awaiter(this, void 0, void 0, function* () {
18 const { plugin, isWatch } = ctx.runOpts;
19 const { sourcePath, outputPath } = ctx.paths;
20 const { chalk, fs, PLATFORMS } = ctx.helper;
21 const { WEAPP, ALIPAY } = PLATFORMS;
22 const PLUGIN_JSON = 'plugin.json';
23 const PLUGIN_MOCK_JSON = 'plugin-mock.json';
24 const typeMap = {
25 [WEAPP]: '微信',
26 [ALIPAY]: '支付宝'
27 };
28 if (plugin !== WEAPP && plugin !== ALIPAY) {
29 console.log(chalk.red('目前插件编译仅支持 微信/支付宝 小程序!'));
30 return;
31 }
32 console.log(chalk.green(`开始编译${typeMap[plugin]}小程序插件`));
33 function buildWxPlugin() {
34 return __awaiter(this, void 0, void 0, function* () {
35 yield ctx.applyPlugins({
36 name: 'build',
37 opts: {
38 platform: 'weapp',
39 isBuildPlugin: true,
40 isWatch,
41 outputRoot: `${config.outputRoot}/miniprogram`
42 }
43 });
44 yield ctx.applyPlugins({
45 name: 'build',
46 opts: {
47 platform: 'weapp',
48 isBuildPlugin: false,
49 isWatch,
50 outputRoot: `${config.outputRoot}/miniprogram`
51 }
52 });
53 });
54 }
55 function buildAlipayPlugin() {
56 return __awaiter(this, void 0, void 0, function* () {
57 yield ctx.applyPlugins({
58 name: 'build',
59 opts: {
60 platform: 'alipay',
61 isWatch
62 }
63 });
64 const pluginJson = path.join(sourcePath, PLUGIN_JSON);
65 const pluginMockJson = path.join(sourcePath, PLUGIN_MOCK_JSON);
66 if (fs.existsSync(pluginJson)) {
67 fs.copyFileSync(pluginJson, path.join(outputPath, PLUGIN_JSON));
68 }
69 if (fs.existsSync(pluginMockJson)) {
70 fs.copyFileSync(pluginMockJson, path.join(outputPath, PLUGIN_MOCK_JSON));
71 }
72 });
73 }
74 switch (plugin) {
75 case WEAPP:
76 yield buildWxPlugin();
77 break;
78 case ALIPAY:
79 yield buildAlipayPlugin();
80 break;
81 default:
82 console.log(chalk.red('输入插件类型错误,目前只支持 weapp/alipay 插件类型'));
83 break;
84 }
85 });
86 }
87 });
88};