UNPKG

3.72 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createCompiler = void 0;
4const tslib_1 = require("tslib");
5const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
6const path = (0, tslib_1.__importStar)(require("path"));
7const webpack_1 = (0, tslib_1.__importDefault)(require("webpack"));
8const clearConsole_1 = require("./clearConsole");
9const formatWebpackMessages_1 = require("./formatWebpackMessages");
10const processHandler_1 = require("./processHandler");
11const env_1 = require("../util/env");
12const { argv } = process;
13const argvSeparatorIndex = argv.indexOf('--');
14const appArgs = ~argvSeparatorIndex
15 ? process.argv.slice(argvSeparatorIndex + 1, argv.length)
16 : [];
17function createCompiler(config, options) {
18 let compiler;
19 const controller = new processHandler_1.AppController(path.join(config.output.path, config.output.filename), appArgs);
20 try {
21 compiler = (0, webpack_1.default)(config);
22 }
23 catch (err) {
24 console.log(chalk_1.default.red('Failed to compile.'));
25 console.log();
26 console.log(err.message || err);
27 console.log();
28 process.exit(1);
29 }
30 compiler.hooks.invalid.tap('compileInvalidate', () => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
31 if (env_1.IS_INTERACTIVE) {
32 (0, 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 (0, clearConsole_1.clearConsole)();
43 }
44 const statsJson = stats.toJson({ errors: true, warnings: true });
45 const messages = (0, 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 // Show warnings if no errors were found.
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 // Start the app if there are no errors
80 if (!messages.errors.length && !options.noAutoStart) {
81 controller.runApp();
82 }
83 });
84 return compiler;
85}
86exports.createCompiler = createCompiler;
87//# sourceMappingURL=webpackCompiler.js.map
\No newline at end of file