UNPKG

4.46 kBJavaScriptView Raw
1"use strict";
2var __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};
11Object.defineProperty(exports, "__esModule", { value: true });
12const webpack = require("webpack");
13const runner_utils_1 = require("@tarojs/runner-utils");
14const constants_1 = require("./utils/constants");
15const logHelper_1 = require("./utils/logHelper");
16const build_conf_1 = require("./webpack/build.conf");
17const customizeChain = (chain, modifyWebpackChainFunc, customizeFunc) => __awaiter(void 0, void 0, void 0, function* () {
18 if (modifyWebpackChainFunc instanceof Function) {
19 yield modifyWebpackChainFunc(chain, webpack);
20 }
21 if (customizeFunc instanceof Function) {
22 customizeFunc(chain, webpack, constants_1.PARSE_AST_TYPE);
23 }
24});
25const makeConfig = (buildConfig) => __awaiter(void 0, void 0, void 0, function* () {
26 const sassLoaderOption = yield runner_utils_1.getSassLoaderOption(buildConfig);
27 return Object.assign(Object.assign({}, buildConfig), { sassLoaderOption });
28});
29function build(appPath, config, mainBuilder) {
30 return __awaiter(this, void 0, void 0, function* () {
31 const mode = config.mode;
32 const newConfig = yield makeConfig(config);
33 // config.webpackChain 自定义 Webpack 配置,接受函数形式的配置。
34 const webpackChain = build_conf_1.default(appPath, mode, config);
35 yield customizeChain(webpackChain, newConfig.modifyWebpackChain, newConfig.webpackChain);
36 const webpackConfig = webpackChain.toConfig();
37 webpackConfig.stats = 'verbose';
38 webpackConfig.profile = true;
39 const onBuildFinish = config.onBuildFinish;
40 // console.log('webpackConfig', webpackConfig.output)
41 return new Promise((resolve, reject) => {
42 const compiler = webpack(webpackConfig);
43 if (config.isWatch) {
44 logHelper_1.bindDevLogger(compiler, config.buildAdapter);
45 compiler.watch({
46 aggregateTimeout: 300,
47 poll: true
48 }, (err, stats) => {
49 if (err) {
50 logHelper_1.printBuildError(err);
51 if (typeof onBuildFinish === 'function') {
52 onBuildFinish({
53 error: err,
54 stats: null,
55 isWatch: true
56 });
57 }
58 return reject(err);
59 }
60 if (typeof onBuildFinish === 'function') {
61 onBuildFinish({
62 error: null,
63 stats,
64 isWatch: true
65 });
66 }
67 resolve();
68 });
69 }
70 else { // prod
71 logHelper_1.bindProdLogger(compiler, config.buildAdapter);
72 compiler.run((err, stats) => {
73 if (err) {
74 logHelper_1.printBuildError(err);
75 if (typeof onBuildFinish === 'function') {
76 onBuildFinish({
77 error: err,
78 stats: null,
79 isWatch: false
80 });
81 }
82 return reject(err);
83 }
84 if (typeof onBuildFinish === 'function') {
85 onBuildFinish({
86 error: null,
87 stats,
88 isWatch: false
89 });
90 }
91 resolve();
92 });
93 }
94 });
95 });
96}
97exports.default = build;