UNPKG

react-cosmos

Version:

CLI for running React Cosmos inside webpack-powered apps

80 lines (55 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ensureHtmlWebackPlugin = ensureHtmlWebackPlugin; exports.getHtmlWebpackPlugin = getHtmlWebpackPlugin; var _importFrom = _interopRequireDefault(require("import-from")); var _lodash = require("lodash"); var _playgroundHtml = require("../../../shared/playgroundHtml"); var _plugins = require("./plugins"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function ensureHtmlWebackPlugin(_ref, plugins) { var rootDir = _ref.rootDir; if ((0, _plugins.hasPlugin)(plugins, 'HtmlWebpackPlugin')) { return plugins.map(function (plugin) { return isHtmlWebpackPlugin(plugin) ? changeHtmlPluginFilename(plugin) : plugin; }); } var htmlWebpackPlugin = getHtmlWebpackPlugin(rootDir); if (!htmlWebpackPlugin) { return plugins; } return [].concat(_toConsumableArray(plugins), [new htmlWebpackPlugin({ title: 'React Cosmos', filename: _playgroundHtml.RENDERER_FILENAME })]); } function getHtmlWebpackPlugin(rootDir) { return _importFrom.default.silent(rootDir, 'html-webpack-plugin'); } function isHtmlWebpackPlugin(plugin) { return (0, _plugins.isInstanceOfWebpackPlugin)(plugin, 'HtmlWebpackPlugin'); } function changeHtmlPluginFilename(htmlPlugin) { if (!isIndexHtmlWebpackPlugin(htmlPlugin)) return htmlPlugin; var options = htmlPlugin.userOptions || htmlPlugin.options; var safeOptions = (0, _lodash.omit)(options, 'chunks'); return new htmlPlugin.constructor(_objectSpread(_objectSpread({}, safeOptions), {}, { filename: _playgroundHtml.RENDERER_FILENAME })); } function isIndexHtmlWebpackPlugin(htmlPlugin) { var _ref2 = htmlPlugin.userOptions || htmlPlugin.options, filename = _ref2.filename; return filename === 'index.html' || typeof filename !== 'string' || filename.endsWith('/index.html'); }