"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.printInstructions = printInstructions;
exports.printPreviewNotice = printPreviewNotice;
exports.default = createWebpackCompiler;
exports.printSuccessMessages = printSuccessMessages;

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function () {
    return data;
  };

  return data;
}

function _clearConsole() {
  const data = _interopRequireDefault(require("react-dev-utils/clearConsole"));

  _clearConsole = function () {
    return data;
  };

  return data;
}

function _formatWebpackMessages() {
  const data = _interopRequireDefault(require("react-dev-utils/formatWebpackMessages"));

  _formatWebpackMessages = function () {
    return data;
  };

  return data;
}

function _boxen() {
  const data = _interopRequireDefault(require("boxen"));

  _boxen = function () {
    return data;
  };

  return data;
}

function ProjectUtils() {
  const data = _interopRequireWildcard(require("./project/ProjectUtils"));

  ProjectUtils = function () {
    return data;
  };

  return data;
}

function _Web() {
  const data = require("./Web");

  _Web = function () {
    return data;
  };

  return data;
}

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; 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, _Web().shouldWebpackClearLogs)();
const PLATFORM_TAG = ProjectUtils().getPlatformTag('web');

const withTag = (...messages) => [PLATFORM_TAG + ' ', ...messages].join('');

function log(projectRoot, message, showInDevtools = true) {
  if (showInDevtools) {
    ProjectUtils().logInfo(projectRoot, CONSOLE_TAG, message);
  } else {
    console.log(message);
  }
}

function logWarning(projectRoot, message) {
  ProjectUtils().logWarning(projectRoot, CONSOLE_TAG, withTag(message));
}

function logError(projectRoot, message) {
  ProjectUtils().logError(projectRoot, CONSOLE_TAG, withTag(message));
}

function printInstructions(projectRoot, {
  appName,
  urls,
  showInDevtools,
  showHelp
}) {
  printPreviewNotice(projectRoot, showInDevtools);
  let message = '\n';
  message += `${ProjectUtils().getPlatformTag('React')} You can now view ${_chalk().default.bold(appName)} in the browser.\n`;

  if (urls.lanUrlForTerminal) {
    message += `\n  ${_chalk().default.bold('Local:')}            ${urls.localUrlForTerminal}`;
    message += `\n  ${_chalk().default.bold('On Your Network:')}  ${urls.lanUrlForTerminal}`;
  } else {
    message += `\n  ${urls.localUrlForTerminal}`;
  }

  message += `\n\nNote that the development build is not optimized.\n`;
  message += `\n \u203A To create a production build, run ${_chalk().default.bold(`expo build:web`)}`;
  message += `\n \u203A Press ${_chalk().default.bold(`Ctrl+C`)} to exit.`;
  log(projectRoot, message, showInDevtools);

  if (showHelp) {
    const PLATFORM_TAG = ProjectUtils().getPlatformTag('Expo');
    log(projectRoot, `\n${PLATFORM_TAG} Press ${_chalk().default.bold('?')} to show a list of all available commands.`, showInDevtools);
  }
}

function printPreviewNotice(projectRoot, showInDevtools) {
  log(projectRoot, (0, _boxen().default)(_chalk().default.yellow('Web support in Expo is experimental and subject to breaking changes.\n' + 'Do not use this in production yet.'), {
    borderColor: 'yellow',
    padding: 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) {
      (0, _clearConsole().default)();
    } // 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, _Web().logEnvironmentInfo)(projectRoot, CONSOLE_TAG, config);
    }

    if (isSuccessful && !isFirstCompile && !nonInteractive) {
      printInstructions(projectRoot, {
        appName,
        urls,
        showInDevtools: isFirstCompile,
        showHelp: true
      });
    }

    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, _Web().logEnvironmentInfo)(projectRoot, CONSOLE_TAG, config);

  if (!nonInteractive || isFirstCompile) {
    printInstructions(projectRoot, {
      appName,
      urls,
      showInDevtools: isFirstCompile,
      showHelp: false
    });
  }
}
//# sourceMappingURL=__sourcemaps__/createWebpackCompiler.js.map