UNPKG

@expo/xdl

Version:
235 lines (190 loc) 7.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.printInstructions = printInstructions; exports.printPreviewNotice = printPreviewNotice; exports.default = createWebpackCompiler; exports.printSuccessMessages = printSuccessMessages; function _boxen() { const data = _interopRequireDefault(require("boxen")); _boxen = function () { return data; }; return data; } function _chalk() { const data = _interopRequireDefault(require("chalk")); _chalk = function () { return data; }; return data; } function _formatWebpackMessages() { const data = _interopRequireDefault(require("react-dev-utils/formatWebpackMessages")); _formatWebpackMessages = function () { return data; }; return data; } function ProjectUtils() { const data = _interopRequireWildcard(require("../project/ProjectUtils")); ProjectUtils = function () { return data; }; return data; } function _WebpackEnvironment() { const data = require("./WebpackEnvironment"); _WebpackEnvironment = function () { return data; }; return data; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Copyright (c) 2015-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ const CONSOLE_TAG = 'expo'; const SHOULD_CLEAR_CONSOLE = (0, _WebpackEnvironment().shouldWebpackClearLogs)(); function log(projectRoot, message, showInDevtools = true) { if (showInDevtools) { ProjectUtils().logInfo(projectRoot, CONSOLE_TAG, message); } else { console.log(message); } } function clearLogs() { process.stdout.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'); } function logWarning(projectRoot, message) { ProjectUtils().logWarning(projectRoot, CONSOLE_TAG, message); } function logError(projectRoot, message) { ProjectUtils().logError(projectRoot, CONSOLE_TAG, message); } function printInstructions(projectRoot, { appName, urls, shouldPrintHelp, showInDevtools }) { printPreviewNotice(projectRoot, showInDevtools); let message = '\n'; message += `You can now view ${_chalk().default.bold(appName)} in the browser\n`; const divider = _chalk().default.dim`│`; if (urls.lanUrlForTerminal) { message += `\n \u203A ${_chalk().default.reset('Local')} ${divider} ${urls.localUrlForTerminal}`; message += `\n \u203A ${_chalk().default.reset('LAN')} ${divider} ${urls.lanUrlForTerminal}`; } else { message += `\n \u203A ${urls.localUrlForTerminal}`; } message += '\n'; message += `\n \u203A Run ${_chalk().default.bold(`expo build:web`)} to optimize and build for production`; message += '\n'; message += `\n \u203A Press ${_chalk().default.bold(`w`)} ${divider} open in the browser`; if (shouldPrintHelp) { message += `\n \u203A Press ${_chalk().default.bold(`?`)} ${divider} show all commands`; } log(projectRoot, message, showInDevtools); } function printPreviewNotice(projectRoot, showInDevtools) { log(projectRoot, (0, _boxen().default)(_chalk().default.magenta.dim('Expo web is in late beta, please report any bugs or missing features on the Expo repo.\n' + 'You can follow the V1 release for more info: https://github.com/expo/expo/issues/6782'), { dimBorder: true, borderColor: 'magenta', padding: { top: 0, left: 1, bottom: 0, right: 1 } }), showInDevtools); } function createWebpackCompiler({ projectRoot, appName, config, urls, nonInteractive, webpackFactory, onFinished }) { // "Compiler" is a low-level interface to Webpack. // It lets us listen to some events and provide our own custom messages. const compiler = webpackFactory(config); // "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', () => { log(projectRoot, '\nCompiling...'); }); let 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', async stats => { if (SHOULD_CLEAR_CONSOLE && !nonInteractive) { clearLogs(); } // We have switched off the default Webpack output in WebpackDevServer // options so we are going to "massage" the warnings and errors and present // them in a readable focused way. // We only construct the warnings and errors for speed: // https://github.com/facebook/create-react-app/issues/4492#issuecomment-421959548 const statsData = stats.toJson({ all: false, warnings: true, errors: true }); const messages = (0, _formatWebpackMessages().default)(statsData); const isSuccessful = !messages.errors.length && !messages.warnings.length; if (isSuccessful) { (0, _WebpackEnvironment().logEnvironmentInfo)(projectRoot, CONSOLE_TAG, config); } if (isSuccessful && !isFirstCompile && !nonInteractive) { printInstructions(projectRoot, { appName, urls, shouldPrintHelp: true, showInDevtools: isFirstCompile }); } onFinished(); 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; } logError(projectRoot, _chalk().default.red('Failed to compile.\n') + messages.errors.join('\n\n')); return; } // Show warnings if no errors were found. if (messages.warnings.length) { logWarning(projectRoot, _chalk().default.yellow('Compiled with warnings.\n') + messages.warnings.join('\n\n')); } }); return compiler; } function printSuccessMessages({ projectRoot, appName, urls, config, isFirstCompile, nonInteractive }) { log(projectRoot, _chalk().default.bold.cyan(`Compiled successfully!`)); printPreviewNotice(projectRoot, isFirstCompile); (0, _WebpackEnvironment().logEnvironmentInfo)(projectRoot, CONSOLE_TAG, config); if (!nonInteractive || isFirstCompile) { printInstructions(projectRoot, { appName, urls, showInDevtools: isFirstCompile }); } } //# sourceMappingURL=../__sourcemaps__/webpack-utils/createWebpackCompiler.js.map