UNPKG

react-torch

Version:

A lib to server-side render with react.

141 lines (140 loc) 7.12 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var chalk_1 = __importDefault(require("chalk")); var utils_1 = require("../../internal/utils"); var formatWebpackMessages_1 = __importDefault(require("./formatWebpackMessages")); var isInteractive = process.stdout.isTTY; function createCompiler(_a) { var _this = this; var appName = _a.appName, config = _a.config, urls = _a.urls, useYarn = _a.useYarn, webpack = _a.webpack; // "Compiler" is a low-level interface to webpack. // It lets us listen to some events and provide our own custom messages. var compiler; try { compiler = webpack(config); } catch (err) { console.log(chalk_1.default.red('Failed to compile.')); console.log(); console.log(err instanceof Error ? err.message : err); console.log(); process.exit(1); } // "invalid" event fires when you have changed a file, and webpack is // recompiling a bundle. WebpackDevServer takes care to pause serving the // bundle, so if you refresh, it'll wait instead of serving the old one. // "invalid" is short for "bundle invalidated", it doesn't imply any errors. compiler.hooks.invalid.tap('invalid', function () { if (isInteractive) { (0, utils_1.clear)(); } console.log('Compiling...'); }); var isFirstCompile = true; // "done" event fires when webpack has finished recompiling the bundle. // Whether or not you have warnings or errors, you will get this event. compiler.hooks.done.tap('done', function (stats) { return __awaiter(_this, void 0, void 0, function () { var statsData, messages, isSuccessful; return __generator(this, function (_a) { if (isInteractive) { (0, utils_1.clear)(); } statsData = stats.toJson({ all: false, warnings: true, errors: true, }); messages = (0, formatWebpackMessages_1.default)(statsData); isSuccessful = !messages.errors.length && !messages.warnings.length; if (isSuccessful) { console.log(chalk_1.default.green('Compiled successfully!')); } if (isSuccessful && (isInteractive || isFirstCompile)) { printInstructions(appName, urls, useYarn); } isFirstCompile = false; // If errors exist, only show errors. if (messages.errors.length) { // Only keep the first error. Others are often indicative // of the same problem, but confuse the reader with noise. if (messages.errors.length > 1) { messages.errors.length = 1; } console.log(chalk_1.default.red('Failed to compile.\n')); console.log(messages.errors.join('\n\n')); return [2 /*return*/]; } // Show warnings if no errors were found. if (messages.warnings.length) { console.log(chalk_1.default.yellow('Compiled with warnings.\n')); console.log(messages.warnings.join('\n\n')); // Teach some ESLint tricks. console.log('\nSearch for the ' + chalk_1.default.underline(chalk_1.default.yellow('keywords')) + ' to learn more about each warning.'); console.log('To ignore, add ' + chalk_1.default.cyan('// eslint-disable-next-line') + ' to the line before.\n'); } return [2 /*return*/]; }); }); }); return compiler; } exports.default = createCompiler; function printInstructions(appName, urls, useYarn) { console.log(); console.log("You can now view " + chalk_1.default.bold(appName) + " in the browser."); console.log(); if (urls.lanUrlForTerminal) { console.log(" " + chalk_1.default.bold('Local:') + " " + urls.localUrlForTerminal); console.log(" " + chalk_1.default.bold('On Your Network:') + " " + urls.lanUrlForTerminal); } else { console.log(" " + urls.localUrlForTerminal); } console.log(); console.log('Note that the development build is not optimized.'); console.log("To create a production build, use " + (chalk_1.default.cyan((useYarn ? 'yarn' : 'npm run') + " build") + ".")); console.log(); }