1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const helper_1 = require("@tarojs/helper");
|
13 | const lodash_1 = require("lodash");
|
14 | const webpack = require("webpack");
|
15 | const prerender_1 = require("./prerender/prerender");
|
16 | const component_1 = require("./template/component");
|
17 | const logHelper_1 = require("./utils/logHelper");
|
18 | const build_conf_1 = require("./webpack/build.conf");
|
19 | const chain_1 = require("./webpack/chain");
|
20 | const customizeChain = (chain, modifyWebpackChainFunc, customizeFunc) => __awaiter(void 0, void 0, void 0, function* () {
|
21 | if (modifyWebpackChainFunc instanceof Function) {
|
22 | yield modifyWebpackChainFunc(chain, webpack, {
|
23 | componentConfig: component_1.componentConfig
|
24 | });
|
25 | }
|
26 | if (customizeFunc instanceof Function) {
|
27 | customizeFunc(chain, webpack, helper_1.META_TYPE);
|
28 | }
|
29 | });
|
30 | function build(appPath, config) {
|
31 | return __awaiter(this, void 0, void 0, function* () {
|
32 | const mode = config.mode;
|
33 |
|
34 | const newConfig = yield (0, chain_1.makeConfig)(config);
|
35 |
|
36 | const webpackChain = (0, build_conf_1.default)(appPath, mode, newConfig);
|
37 |
|
38 | yield customizeChain(webpackChain, newConfig.modifyWebpackChain, newConfig.webpackChain);
|
39 | if (typeof newConfig.onWebpackChainReady === 'function') {
|
40 | newConfig.onWebpackChainReady(webpackChain);
|
41 | }
|
42 |
|
43 | const webpackConfig = webpackChain.toConfig();
|
44 | return new Promise((resolve, reject) => {
|
45 | const compiler = webpack(webpackConfig);
|
46 | const onBuildFinish = newConfig.onBuildFinish;
|
47 | let prerender;
|
48 | const onFinish = function (error, stats) {
|
49 | if (typeof onBuildFinish !== 'function')
|
50 | return;
|
51 | onBuildFinish({
|
52 | error,
|
53 | stats,
|
54 | isWatch: newConfig.isWatch
|
55 | });
|
56 | };
|
57 | const callback = (err, stats) => __awaiter(this, void 0, void 0, function* () {
|
58 | if (err || stats.hasErrors()) {
|
59 | const error = err !== null && err !== void 0 ? err : stats.toJson().errors;
|
60 | (0, logHelper_1.printBuildError)(error);
|
61 | onFinish(error, null);
|
62 | return reject(error);
|
63 | }
|
64 | if (!(0, lodash_1.isEmpty)(newConfig.prerender)) {
|
65 | prerender = prerender !== null && prerender !== void 0 ? prerender : new prerender_1.Prerender(newConfig, webpackConfig, stats, config.template.Adapter);
|
66 | yield prerender.render();
|
67 | }
|
68 | onFinish(null, stats);
|
69 | resolve(stats);
|
70 | });
|
71 | if (newConfig.isWatch) {
|
72 | (0, logHelper_1.bindDevLogger)(compiler);
|
73 | compiler.watch({
|
74 | aggregateTimeout: 300,
|
75 | poll: undefined
|
76 | }, callback);
|
77 | }
|
78 | else {
|
79 | (0, logHelper_1.bindProdLogger)(compiler);
|
80 | compiler.run(callback);
|
81 | }
|
82 | });
|
83 | });
|
84 | }
|
85 | exports.default = build;
|
86 |
|
\ | No newline at end of file |