UNPKG

4.66 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __importDefault = (this && this.__importDefault) || function (mod) {
11 return (mod && mod.__esModule) ? mod : { "default": mod };
12};
13var __importStar = (this && this.__importStar) || function (mod) {
14 if (mod && mod.__esModule) return mod;
15 var result = {};
16 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
17 result["default"] = mod;
18 return result;
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21const chalk_1 = __importDefault(require("chalk"));
22const path = __importStar(require("path"));
23const webpack_1 = __importDefault(require("webpack"));
24const clearConsole_1 = require("./clearConsole");
25const formatWebpackMessages_1 = require("./formatWebpackMessages");
26const processHandler_1 = require("./processHandler");
27const isInteractive = process.stdout.isTTY;
28const { argv } = process;
29const argvSeparatorIndex = argv.indexOf('--');
30const appArgs = ~argvSeparatorIndex
31 ? process.argv.slice(argvSeparatorIndex + 1, argv.length)
32 : [];
33// tslint:disable:cognitive-complexity
34function createCompiler(config, options) {
35 let compiler;
36 const controller = new processHandler_1.AppController(path.join(config.output.path, config.output.filename), appArgs);
37 try {
38 compiler = webpack_1.default(config);
39 }
40 catch (err) {
41 console.log(chalk_1.default.red('Failed to compile.'));
42 console.log();
43 console.log(err.message || err);
44 console.log();
45 process.exit(1);
46 throw err.message;
47 }
48 compiler.hooks.invalid.tap('compileInvalidate', () => __awaiter(this, void 0, void 0, function* () {
49 if (isInteractive) {
50 clearConsole_1.clearConsole();
51 }
52 console.log('Compiling...');
53 console.log();
54 if (!options.noAutoStart) {
55 yield controller.stopApp();
56 }
57 }));
58 compiler.hooks.done.tap('compileDone', stats => {
59 if (isInteractive) {
60 clearConsole_1.clearConsole();
61 }
62 const statsJson = stats.toJson({ errors: true, warnings: true });
63 const messages = formatWebpackMessages_1.formatWebpackMessages(statsJson);
64 const isSuccessful = !messages.errors.length && !messages.warnings.length;
65 if (isSuccessful) {
66 const successMessage = chalk_1.default.green('Compiled successfully!');
67 const time = chalk_1.default.dim(`(${statsJson.time}ms)`);
68 console.log(`${successMessage} ${time}`);
69 console.log();
70 }
71 if (messages.errors.length) {
72 const collapsedErrorLength = messages.errors.length - 1;
73 if (messages.errors.length > 1 && !options.noCollapse) {
74 messages.errors.length = 1;
75 }
76 console.log(chalk_1.default.red('Failed to compile.\n'));
77 console.log(messages.errors.join('\n\n'));
78 if (collapsedErrorLength && !options.noCollapse) {
79 console.log();
80 console.log(chalk_1.default.red(collapsedErrorLength +
81 (collapsedErrorLength > 1 ? ' errors have' : ' error has') +
82 ' been collapsed.'));
83 }
84 return;
85 }
86 // Show warnings if no errors were found.
87 if (messages.warnings.length) {
88 console.log(chalk_1.default.yellow('Compiled with warnings.\n'));
89 console.log(messages.warnings.join('\n\n'));
90 console.log('\nSearch for the ' +
91 chalk_1.default.cyan('keywords') +
92 ' to learn more about each warning.');
93 console.log('To ignore, add ' +
94 chalk_1.default.yellow('// tslint:disable-next-line') +
95 ' to the line before.\n');
96 }
97 // Start the app if there are no errors
98 if (!messages.errors.length && !options.noAutoStart) {
99 controller.runApp();
100 }
101 });
102 return compiler;
103}
104exports.createCompiler = createCompiler;
105//# sourceMappingURL=webpackCompiler.js.map
\No newline at end of file