1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
5 | const path = tslib_1.__importStar(require("path"));
|
6 | const webpack_1 = tslib_1.__importDefault(require("webpack"));
|
7 | const clearConsole_1 = require("./clearConsole");
|
8 | const formatWebpackMessages_1 = require("./formatWebpackMessages");
|
9 | const processHandler_1 = require("./processHandler");
|
10 | const env_1 = require("../util/env");
|
11 | const { argv } = process;
|
12 | const argvSeparatorIndex = argv.indexOf('--');
|
13 | const appArgs = ~argvSeparatorIndex
|
14 | ? process.argv.slice(argvSeparatorIndex + 1, argv.length)
|
15 | : [];
|
16 | function createCompiler(config, options) {
|
17 | let compiler;
|
18 | const controller = new processHandler_1.AppController(path.join(config.output.path, config.output.filename), appArgs);
|
19 | try {
|
20 | compiler = webpack_1.default(config);
|
21 | }
|
22 | catch (err) {
|
23 | console.log(chalk_1.default.red('Failed to compile.'));
|
24 | console.log();
|
25 | console.log(err.message || err);
|
26 | console.log();
|
27 | process.exit(1);
|
28 | throw err.message;
|
29 | }
|
30 | compiler.hooks.invalid.tap('compileInvalidate', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
31 | if (env_1.IS_INTERACTIVE) {
|
32 | clearConsole_1.clearConsole();
|
33 | }
|
34 | console.log('Compiling...');
|
35 | console.log();
|
36 | if (!options.noAutoStart) {
|
37 | yield controller.stopApp();
|
38 | }
|
39 | }));
|
40 | compiler.hooks.done.tap('compileDone', stats => {
|
41 | if (env_1.IS_INTERACTIVE) {
|
42 | clearConsole_1.clearConsole();
|
43 | }
|
44 | const statsJson = stats.toJson({ errors: true, warnings: true });
|
45 | const messages = formatWebpackMessages_1.formatWebpackMessages(statsJson);
|
46 | const isSuccessful = !messages.errors.length && !messages.warnings.length;
|
47 | if (isSuccessful) {
|
48 | const successMessage = chalk_1.default.green('Compiled successfully!');
|
49 | const time = chalk_1.default.dim(`(${statsJson.time}ms)`);
|
50 | console.log(`${successMessage} ${time}`);
|
51 | console.log();
|
52 | }
|
53 | if (messages.errors.length) {
|
54 | const collapsedErrorLength = messages.errors.length - 1;
|
55 | if (messages.errors.length > 1 && !options.noCollapse) {
|
56 | messages.errors.length = 1;
|
57 | }
|
58 | console.log(chalk_1.default.red('Failed to compile.\n'));
|
59 | console.log(messages.errors.join('\n\n'));
|
60 | if (collapsedErrorLength && !options.noCollapse) {
|
61 | console.log();
|
62 | console.log(chalk_1.default.red(collapsedErrorLength +
|
63 | (collapsedErrorLength > 1 ? ' errors have' : ' error has') +
|
64 | ' been collapsed.'));
|
65 | }
|
66 | return;
|
67 | }
|
68 |
|
69 | if (messages.warnings.length) {
|
70 | console.log(chalk_1.default.yellow('Compiled with warnings.\n'));
|
71 | console.log(messages.warnings.join('\n\n'));
|
72 | console.log('\nSearch for the ' +
|
73 | chalk_1.default.cyan('keywords') +
|
74 | ' to learn more about each warning.');
|
75 | console.log('To ignore, add ' +
|
76 | chalk_1.default.yellow('// eslint-disable-next-line') +
|
77 | ' to the line before.\n');
|
78 | }
|
79 |
|
80 | if (!messages.errors.length && !options.noAutoStart) {
|
81 | controller.runApp();
|
82 | }
|
83 | });
|
84 | return compiler;
|
85 | }
|
86 | exports.createCompiler = createCompiler;
|
87 |
|
\ | No newline at end of file |