index.js 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 *
 * @format
 */
"use strict";

function _objectSpread(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = arguments[i] != null ? arguments[i] : {};
    var ownKeys = Object.keys(source);
    if (typeof Object.getOwnPropertySymbols === "function") {
      ownKeys = ownKeys.concat(
        Object.getOwnPropertySymbols(source).filter(function(sym) {
          return Object.getOwnPropertyDescriptor(source, sym).enumerable;
        })
      );
    }
    ownKeys.forEach(function(key) {
      _defineProperty(target, key, source[key]);
    });
  }
  return target;
}

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
}

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  try {
    var info = gen[key](arg);
    var value = info.value;
  } catch (error) {
    reject(error);
    return;
  }
  if (info.done) {
    resolve(value);
  } else {
    Promise.resolve(value).then(_next, _throw);
  }
}

function _asyncToGenerator(fn) {
  return function() {
    var self = this,
      args = arguments;
    return new Promise(function(resolve, reject) {
      var gen = fn.apply(self, args);
      function _next(value) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
      }
      function _throw(err) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
      }
      _next(undefined);
    });
  };
}

const IncrementalBundler = require("./IncrementalBundler");

const MetroHmrServer = require("./HmrServer");

const MetroServer = require("./Server");

const attachWebsocketServer = require("./lib/attachWebsocketServer");

const fs = require("fs");

const http = require("http");

const https = require("https");

const makeBuildCommand = require("./commands/build");

const makeDependenciesCommand = require("./commands/dependencies");

const makeServeCommand = require("./commands/serve");

const outputBundle = require("./shared/output/bundle");

const _require = require("metro-config"),
  loadConfig = _require.loadConfig,
  mergeConfig = _require.mergeConfig,
  getDefaultConfig = _require.getDefaultConfig;

const _require2 = require("metro-inspector-proxy"),
  InspectorProxy = _require2.InspectorProxy;

function getConfig(_x) {
  return _getConfig.apply(this, arguments);
}

function _getConfig() {
  _getConfig = _asyncToGenerator(function*(config) {
    const defaultConfig = yield getDefaultConfig(config.projectRoot);
    return mergeConfig(defaultConfig, config);
  });
  return _getConfig.apply(this, arguments);
}

function runMetro(_x2, _x3) {
  return _runMetro.apply(this, arguments);
}

function _runMetro() {
  _runMetro = _asyncToGenerator(function*(config, options) {
    const mergedConfig = yield getConfig(config);
    mergedConfig.reporter.update({
      type: "initialize_started",
      port: mergedConfig.server.port,
      // FIXME: We need to change that to watchFolders. It will be a
      // breaking since it affects custom reporter API.
      projectRoots: mergedConfig.watchFolders
    });
    return new MetroServer(mergedConfig, options);
  });
  return _runMetro.apply(this, arguments);
}

exports.runMetro = runMetro;
exports.loadConfig = loadConfig;

exports.createConnectMiddleware =
  /*#__PURE__*/
  (function() {
    var _ref = _asyncToGenerator(function*(config) {
      const metroServer = yield runMetro(config);
      let enhancedMiddleware = metroServer.processRequest; // Enhance the resulting middleware using the config options

      if (config.server.enhanceMiddleware) {
        enhancedMiddleware = config.server.enhanceMiddleware(
          enhancedMiddleware,
          metroServer
        );
      }

      return {
        attachHmrServer(httpServer) {
          attachWebsocketServer({
            httpServer,
            path: "/hot",
            websocketServer: new MetroHmrServer(
              metroServer.getBundler(),
              metroServer.getCreateModuleId(),
              config
            )
          });
        },

        metroServer,
        middleware: enhancedMiddleware,

        end() {
          metroServer.end();
        }
      };
    });

    return function(_x4) {
      return _ref.apply(this, arguments);
    };
  })();

exports.runServer =
  /*#__PURE__*/
  (function() {
    var _ref2 = _asyncToGenerator(function*(config, _ref3) {
      let host = _ref3.host,
        onReady = _ref3.onReady,
        onError = _ref3.onError,
        _ref3$secure = _ref3.secure,
        secure = _ref3$secure === void 0 ? false : _ref3$secure,
        secureKey = _ref3.secureKey,
        secureCert = _ref3.secureCert,
        _ref3$hmrEnabled = _ref3.hmrEnabled,
        hmrEnabled = _ref3$hmrEnabled === void 0 ? false : _ref3$hmrEnabled;

      // Lazy require
      const connect = require("connect");

      const serverApp = connect();

      const _ref4 = yield exports.createConnectMiddleware(config),
        attachHmrServer = _ref4.attachHmrServer,
        middleware = _ref4.middleware,
        end = _ref4.end;

      serverApp.use(middleware);
      let inspectorProxy = null;

      if (config.server.runInspectorProxy) {
        inspectorProxy = new InspectorProxy();
      }

      let httpServer;

      if (
        secure &&
        typeof secureKey === "string" &&
        typeof secureCert === "string"
      ) {
        httpServer = https.createServer(
          {
            key: fs.readFileSync(secureKey),
            cert: fs.readFileSync(secureCert)
          },
          serverApp
        );
      } else {
        httpServer = http.createServer(serverApp);
      }

      httpServer.on("error", error => {
        onError && onError(error);
        end();
      });
      return new Promise((resolve, reject) => {
        httpServer.listen(config.server.port, host, () => {
          onReady && onReady(httpServer);

          if (hmrEnabled) {
            attachHmrServer(httpServer);
          }

          if (inspectorProxy) {
            inspectorProxy.addWebSocketListener(httpServer); // TODO(hypuk): Refactor inspectorProxy.processRequest into separate request handlers
            // so that we could provide routes (/json/list and /json/version) here.
            // Currently this causes Metro to give warning about T31407894.

            serverApp.use(inspectorProxy.processRequest.bind(inspectorProxy));
          }

          resolve(httpServer);
        }); // Disable any kind of automatic timeout behavior for incoming
        // requests in case it takes the packager more than the default
        // timeout of 120 seconds to respond to a request.

        httpServer.timeout = 0;
        httpServer.on("error", error => {
          end();
          reject(error);
        });
        httpServer.on("close", () => {
          end();
        });
      });
    });

    return function(_x5, _x6) {
      return _ref2.apply(this, arguments);
    };
  })();

exports.runBuild =
  /*#__PURE__*/
  (function() {
    var _ref5 = _asyncToGenerator(function*(config, _ref6) {
      let _ref6$dev = _ref6.dev,
        dev = _ref6$dev === void 0 ? false : _ref6$dev,
        entry = _ref6.entry,
        onBegin = _ref6.onBegin,
        onComplete = _ref6.onComplete,
        onProgress = _ref6.onProgress,
        _ref6$minify = _ref6.minify,
        minify = _ref6$minify === void 0 ? true : _ref6$minify,
        _ref6$output = _ref6.output,
        output = _ref6$output === void 0 ? outputBundle : _ref6$output,
        out = _ref6.out,
        _ref6$platform = _ref6.platform,
        platform = _ref6$platform === void 0 ? "web" : _ref6$platform,
        _ref6$sourceMap = _ref6.sourceMap,
        sourceMap = _ref6$sourceMap === void 0 ? false : _ref6$sourceMap,
        sourceMapUrl = _ref6.sourceMapUrl;
      const metroServer = yield runMetro(config, {
        watch: false
      });

      try {
        const requestOptions = {
          dev,
          entryFile: entry,
          inlineSourceMap: sourceMap && !sourceMapUrl,
          minify,
          platform,
          sourceMapUrl: sourceMap === false ? undefined : sourceMapUrl,
          createModuleIdFactory: config.serializer.createModuleIdFactory,
          onProgress
        };

        if (onBegin) {
          onBegin();
        }

        const metroBundle = yield output.build(metroServer, requestOptions);

        if (onComplete) {
          onComplete();
        }

        if (out) {
          const bundleOutput = out.replace(/(\.js)?$/, ".js");
          const sourcemapOutput =
            sourceMap === false ? undefined : out.replace(/(\.js)?$/, ".map");
          const outputOptions = {
            bundleOutput,
            sourcemapOutput,
            dev,
            platform
          }; // eslint-disable-next-line no-console

          yield output.save(metroBundle, outputOptions, console.log);
        }

        return metroBundle;
      } finally {
        yield metroServer.end();
      }
    });

    return function(_x7, _x8) {
      return _ref5.apply(this, arguments);
    };
  })();

exports.buildGraph =
  /*#__PURE__*/
  (function() {
    var _ref7 = _asyncToGenerator(function*(config, _ref8) {
      let _ref8$customTransform = _ref8.customTransformOptions,
        customTransformOptions =
          _ref8$customTransform === void 0
            ? Object.create(null)
            : _ref8$customTransform,
        _ref8$dev = _ref8.dev,
        dev = _ref8$dev === void 0 ? false : _ref8$dev,
        entries = _ref8.entries,
        _ref8$minify = _ref8.minify,
        minify = _ref8$minify === void 0 ? false : _ref8$minify,
        onProgress = _ref8.onProgress,
        _ref8$platform = _ref8.platform,
        platform = _ref8$platform === void 0 ? "web" : _ref8$platform,
        _ref8$type = _ref8.type,
        type = _ref8$type === void 0 ? "module" : _ref8$type;
      const mergedConfig = yield getConfig(config);
      const bundler = new IncrementalBundler(mergedConfig);

      try {
        return yield bundler.buildGraphForEntries(
          entries,
          _objectSpread({}, MetroServer.DEFAULT_GRAPH_OPTIONS, {
            customTransformOptions,
            dev,
            minify,
            platform,
            type
          })
        );
      } finally {
        bundler.end();
      }
    });

    return function(_x9, _x10) {
      return _ref7.apply(this, arguments);
    };
  })();

exports.attachMetroCli = function(yargs) {
  let _ref9 =
      arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
    _ref9$build = _ref9.build,
    build = _ref9$build === void 0 ? {} : _ref9$build,
    _ref9$serve = _ref9.serve,
    serve = _ref9$serve === void 0 ? {} : _ref9$serve,
    _ref9$dependencies = _ref9.dependencies,
    dependencies = _ref9$dependencies === void 0 ? {} : _ref9$dependencies;

  if (build) {
    const _makeBuildCommand = makeBuildCommand(),
      command = _makeBuildCommand.command,
      description = _makeBuildCommand.description,
      builder = _makeBuildCommand.builder,
      handler = _makeBuildCommand.handler;

    yargs.command(command, description, builder, handler);
  }

  if (serve) {
    const _makeServeCommand = makeServeCommand(),
      command = _makeServeCommand.command,
      description = _makeServeCommand.description,
      builder = _makeServeCommand.builder,
      handler = _makeServeCommand.handler;

    yargs.command(command, description, builder, handler);
  }

  if (dependencies) {
    const _makeDependenciesComm = makeDependenciesCommand(),
      command = _makeDependenciesComm.command,
      description = _makeDependenciesComm.description,
      builder = _makeDependenciesComm.builder,
      handler = _makeDependenciesComm.handler;

    yargs.command(command, description, builder, handler);
  }

  return yargs;
};