UNPKG

4.16 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 helper_1 = require("@tarojs/helper");
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, helper_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) {
30 return __awaiter(this, void 0, void 0, function* () {
31 const mode = config.mode;
32 const newConfig = yield makeConfig(config);
33 const webpackChain = build_conf_1.default(appPath, mode, config);
34 yield customizeChain(webpackChain, newConfig.modifyWebpackChain, newConfig.webpackChain);
35 const webpackConfig = webpackChain.toConfig();
36 const onBuildFinish = config.onBuildFinish;
37 const compiler = webpack(webpackConfig);
38 return new Promise((resolve, reject) => {
39 if (newConfig.isWatch) {
40 logHelper_1.bindDevLogger(compiler);
41 compiler.watch({
42 aggregateTimeout: 300,
43 poll: true
44 }, (err, stats) => {
45 if (err) {
46 logHelper_1.printBuildError(err);
47 if (typeof onBuildFinish === 'function') {
48 onBuildFinish({
49 error: err,
50 stats: null,
51 isWatch: true
52 });
53 }
54 return reject(err);
55 }
56 if (typeof onBuildFinish === 'function') {
57 onBuildFinish({
58 error: null,
59 stats,
60 isWatch: true
61 });
62 }
63 resolve();
64 });
65 }
66 else {
67 logHelper_1.bindProdLogger(compiler);
68 compiler.run((err, stats) => {
69 if (err) {
70 logHelper_1.printBuildError(err);
71 if (typeof onBuildFinish === 'function') {
72 onBuildFinish({
73 error: err,
74 stats: null,
75 isWatch: false
76 });
77 }
78 return reject(err);
79 }
80 if (typeof onBuildFinish === 'function') {
81 onBuildFinish({
82 error: null,
83 stats,
84 isWatch: false
85 });
86 }
87 resolve();
88 });
89 }
90 });
91 });
92}
93exports.default = build;