UNPKG

react-cosmos

Version:

CLI for running React Cosmos inside webpack-powered apps

139 lines (99 loc) 8.22 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.webpackExport = webpackExport; var _shared = require("./shared"); var _getExportWebpackConfig = require("./webpackConfig/getExportWebpackConfig"); function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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); }); }; } function webpackExport(_x) { return _webpackExport.apply(this, arguments); } function _webpackExport() { _webpackExport = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref) { var cosmosConfig, userWebpack, webpackConfig, webpackError; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: cosmosConfig = _ref.cosmosConfig; userWebpack = (0, _shared.getWebpack)(cosmosConfig.rootDir); if (userWebpack) { _context.next = 4; break; } return _context.abrupt("return"); case 4: _context.next = 6; return (0, _getExportWebpackConfig.getExportWebpackConfig)(cosmosConfig, userWebpack); case 6: webpackConfig = _context.sent; _context.prev = 7; _context.next = 10; return runWebpackCompiler(userWebpack, webpackConfig); case 10: _context.next = 17; break; case 12: _context.prev = 12; _context.t0 = _context["catch"](7); webpackError = _context.t0; if (webpackError.webpackErrors) { webpackError.webpackErrors.forEach(function (error) { console.error("".concat(error, "\n")); }); } throw webpackError; case 17: case "end": return _context.stop(); } } }, _callee, null, [[7, 12]]); })); return _webpackExport.apply(this, arguments); } function runWebpackCompiler(userWebpack, webpackConfig) { return new Promise(function (resolve, reject) { var compiler = userWebpack(webpackConfig); compiler.run(function (err, stats) { if (err) { reject(err); } else if (stats !== null && stats !== void 0 && stats.hasErrors()) { var error = new WebpackCompilationError(); error.webpackErrors = stats.toJson().errors; reject(error); } else { resolve(stats); } }); }); } var WebpackCompilationError = /*#__PURE__*/function (_Error) { _inherits(WebpackCompilationError, _Error); var _super = _createSuper(WebpackCompilationError); function WebpackCompilationError() { var _this; _classCallCheck(this, WebpackCompilationError); _this = _super.call(this, 'Webpack errors occurred'); _defineProperty(_assertThisInitialized(_this), "webpackErrors", void 0); return _this; } return _createClass(WebpackCompilationError); }( /*#__PURE__*/_wrapNativeSuper(Error));