UNPKG

4.72 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.printCompiling = exports.printBuildError = exports.getServeSpinner = exports.bindProdLogger = exports.bindDevLogger = void 0;
4const helper_1 = require("@tarojs/helper");
5const fp_1 = require("lodash/fp");
6const ora = require("ora");
7const formatMessages = require("webpack-format-messages");
8// const syntaxErrorLabel = 'Syntax error:';
9const getServeSpinner = (() => {
10 let spinner;
11 return () => {
12 if (!spinner)
13 spinner = ora('Starting development server, please wait~');
14 return spinner;
15 };
16})();
17exports.getServeSpinner = getServeSpinner;
18const printCompiling = () => {
19 getServeSpinner().text = 'Compiling...';
20 getServeSpinner().start();
21};
22exports.printCompiling = printCompiling;
23const printBuildError = (err) => {
24 const message = err.message;
25 const stack = err.stack;
26 if (stack && message.indexOf('from UglifyJs') !== -1) {
27 try {
28 const matched = /(.+)\[(.+):(.+),(.+)\]\[.+\]/.exec(stack);
29 if (!matched) {
30 throw new Error('Using errors for control flow is bad.');
31 }
32 const problemPath = matched[2];
33 const line = matched[3];
34 const column = matched[4];
35 console.log('Failed to minify the code from this file: \n\n', helper_1.chalk.yellow(`\t${problemPath}:${line}${column !== '0' ? ':' + column : ''}`), '\n');
36 }
37 catch (ignored) {
38 console.log('Failed to minify the bundle.', err);
39 }
40 }
41 else {
42 console.log((message || err) + '\n');
43 }
44 console.log();
45};
46exports.printBuildError = printBuildError;
47const printSuccess = () => {
48 getServeSpinner().stopAndPersist({
49 symbol: '✅ ',
50 text: helper_1.chalk.green('Compiled successfully!\n')
51 });
52};
53const printWarning = () => {
54 getServeSpinner().stopAndPersist({
55 symbol: '⚠️ ',
56 text: helper_1.chalk.yellow('Compiled with warnings.\n')
57 });
58};
59const printFailed = () => {
60 getServeSpinner().stopAndPersist({
61 symbol: '🙅 ',
62 text: helper_1.chalk.red('Failed to compile.\n')
63 });
64};
65const printWhenBeforeCompile = compiler => {
66 compiler.hooks.beforeCompile.tap('taroBeforeCompile', () => {
67 printCompiling();
68 });
69 return compiler;
70};
71const printWhenInvalid = compiler => {
72 compiler.hooks.invalid.tap('taroInvalid', () => {
73 printCompiling();
74 });
75 return compiler;
76};
77const printWhenFailed = compiler => {
78 compiler.hooks.failed.tap('taroFailed', error => {
79 printBuildError(error);
80 });
81 return compiler;
82};
83let isFirst = true;
84const printWhenFirstDone = (devUrl, compiler) => {
85 compiler.hooks.done.tap('taroDone', () => {
86 if (isFirst) {
87 isFirst = false;
88 getServeSpinner().clear();
89 console.log();
90 console.log(helper_1.chalk.cyan(`ℹ️ Listening at ${devUrl}`));
91 console.log(helper_1.chalk.gray('\n监听文件修改中...\n'));
92 }
93 });
94 return compiler;
95};
96const _printWhenDone = ({ verbose = false }, compiler) => {
97 compiler.hooks.done.tap('taroDone', stats => {
98 const { errors, warnings } = formatMessages(stats);
99 if (!stats.hasErrors() && !stats.hasWarnings()) {
100 printSuccess();
101 }
102 if (stats.hasErrors()) {
103 printFailed();
104 errors.forEach(e => console.log(e + '\n'));
105 verbose && process.exit(1);
106 return;
107 }
108 if (stats.hasWarnings()) {
109 printWarning();
110 warnings.forEach(w => console.log(w + '\n'));
111 }
112 verbose &&
113 console.log(stats.toString({
114 colors: true,
115 modules: false,
116 children: false,
117 chunks: false,
118 chunkModules: false,
119 warnings: verbose
120 }) + '\n');
121 });
122 return compiler;
123};
124const printWhenDone = (0, fp_1.partial)(_printWhenDone, [{ verbose: false }]);
125const printWhenDoneVerbosely = (0, fp_1.partial)(_printWhenDone, [{ verbose: true }]);
126const bindDevLogger = (compiler, devUrl = '') => {
127 console.log();
128 (0, fp_1.pipe)(printWhenBeforeCompile, (0, fp_1.partial)(printWhenFirstDone, [devUrl]), printWhenDone, printWhenFailed, printWhenInvalid)(compiler);
129 return compiler;
130};
131exports.bindDevLogger = bindDevLogger;
132const bindProdLogger = compiler => {
133 console.log();
134 (0, fp_1.pipe)(printWhenBeforeCompile, printWhenDoneVerbosely, printWhenFailed)(compiler);
135 return compiler;
136};
137exports.bindProdLogger = bindProdLogger;
138//# sourceMappingURL=logHelper.js.map
\No newline at end of file