BVB Source Codes

reactide Show option-manager.js Source code

Return Download reactide: download option-manager.js Source code - Download reactide Source code - Type:.js
  1. "use strict";
  2.  
  3. exports.__esModule = true;
  4.  
  5. var _stringify = require("babel-runtime/core-js/json/stringify");
  6.  
  7. var _stringify2 = _interopRequireDefault(_stringify);
  8.  
  9. var _assign = require("babel-runtime/core-js/object/assign");
  10.  
  11. var _assign2 = _interopRequireDefault(_assign);
  12.  
  13. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  14.  
  15. var _getIterator3 = _interopRequireDefault(_getIterator2);
  16.  
  17. var _typeof2 = require("babel-runtime/helpers/typeof");
  18.  
  19. var _typeof3 = _interopRequireDefault(_typeof2);
  20.  
  21. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  22.  
  23. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  24.  
  25. var _node = require("../../../api/node");
  26.  
  27. var context = _interopRequireWildcard(_node);
  28.  
  29. var _plugin2 = require("../../plugin");
  30.  
  31. var _plugin3 = _interopRequireDefault(_plugin2);
  32.  
  33. var _babelMessages = require("babel-messages");
  34.  
  35. var messages = _interopRequireWildcard(_babelMessages);
  36.  
  37. var _index = require("./index");
  38.  
  39. var _resolve = require("../../../helpers/resolve");
  40.  
  41. var _resolve2 = _interopRequireDefault(_resolve);
  42.  
  43. var _cloneDeepWith = require("lodash/cloneDeepWith");
  44.  
  45. var _cloneDeepWith2 = _interopRequireDefault(_cloneDeepWith);
  46.  
  47. var _clone = require("lodash/clone");
  48.  
  49. var _clone2 = _interopRequireDefault(_clone);
  50.  
  51. var _merge = require("../../../helpers/merge");
  52.  
  53. var _merge2 = _interopRequireDefault(_merge);
  54.  
  55. var _config2 = require("./config");
  56.  
  57. var _config3 = _interopRequireDefault(_config2);
  58.  
  59. var _removed = require("./removed");
  60.  
  61. var _removed2 = _interopRequireDefault(_removed);
  62.  
  63. var _buildConfigChain = require("./build-config-chain");
  64.  
  65. var _buildConfigChain2 = _interopRequireDefault(_buildConfigChain);
  66.  
  67. var _path = require("path");
  68.  
  69. var _path2 = _interopRequireDefault(_path);
  70.  
  71. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  72.  
  73. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  74.  
  75. /* eslint max-len: 0 */
  76.  
  77. var OptionManager = function () {
  78.   function OptionManager(log) {
  79.     (0, _classCallCheck3.default)(this, OptionManager);
  80.  
  81.     this.resolvedConfigs = [];
  82.     this.options = OptionManager.createBareOptions();
  83.     this.log = log;
  84.   }
  85.  
  86.   OptionManager.memoisePluginContainer = function memoisePluginContainer(fn, loc, i, alias) {
  87.     for (var _iterator = OptionManager.memoisedPlugins, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  88.       var _ref;
  89.  
  90.       if (_isArray) {
  91.         if (_i >= _iterator.length) break;
  92.         _ref = _iterator[_i++];
  93.       } else {
  94.         _i = _iterator.next();
  95.         if (_i.done) break;
  96.         _ref = _i.value;
  97.       }
  98.  
  99.       var cache = _ref;
  100.  
  101.       if (cache.container === fn) return cache.plugin;
  102.     }
  103.  
  104.     var obj = void 0;
  105.  
  106.     if (typeof fn === "function") {
  107.       obj = fn(context);
  108.     } else {
  109.       obj = fn;
  110.     }
  111.  
  112.     if ((typeof obj === "undefined" ? "undefined" : (0, _typeof3.default)(obj)) === "object") {
  113.       var _plugin = new _plugin3.default(obj, alias);
  114.       OptionManager.memoisedPlugins.push({
  115.         container: fn,
  116.         plugin: _plugin
  117.       });
  118.       return _plugin;
  119.     } else {
  120.       throw new TypeError(messages.get("pluginNotObject", loc, i, typeof obj === "undefined" ? "undefined" : (0, _typeof3.default)(obj)) + loc + i);
  121.     }
  122.   };
  123.  
  124.   OptionManager.createBareOptions = function createBareOptions() {
  125.     var opts = {};
  126.  
  127.     for (var _key in _config3.default) {
  128.       var opt = _config3.default[_key];
  129.       opts[_key] = (0, _clone2.default)(opt.default);
  130.     }
  131.  
  132.     return opts;
  133.   };
  134.  
  135.   OptionManager.normalisePlugin = function normalisePlugin(plugin, loc, i, alias) {
  136.     plugin = plugin.__esModule ? plugin.default : plugin;
  137.  
  138.     if (!(plugin instanceof _plugin3.default)) {
  139.       // allow plugin containers to be specified so they don't have to manually require
  140.       if (typeof plugin === "function" || (typeof plugin === "undefined" ? "undefined" : (0, _typeof3.default)(plugin)) === "object") {
  141.         plugin = OptionManager.memoisePluginContainer(plugin, loc, i, alias);
  142.       } else {
  143.         throw new TypeError(messages.get("pluginNotFunction", loc, i, typeof plugin === "undefined" ? "undefined" : (0, _typeof3.default)(plugin)));
  144.       }
  145.     }
  146.  
  147.     plugin.init(loc, i);
  148.  
  149.     return plugin;
  150.   };
  151.  
  152.   OptionManager.normalisePlugins = function normalisePlugins(loc, dirname, plugins) {
  153.     return plugins.map(function (val, i) {
  154.       var plugin = void 0,
  155.           options = void 0;
  156.  
  157.       if (!val) {
  158.         throw new TypeError("Falsy value found in plugins");
  159.       }
  160.  
  161.       // destructure plugins
  162.       if (Array.isArray(val)) {
  163.         plugin = val[0];
  164.         options = val[1];
  165.       } else {
  166.         plugin = val;
  167.       }
  168.  
  169.       var alias = typeof plugin === "string" ? plugin : loc + "$" + i;
  170.  
  171.       // allow plugins to be specified as strings
  172.       if (typeof plugin === "string") {
  173.         var pluginLoc = (0, _resolve2.default)("babel-plugin-" + plugin, dirname) || (0, _resolve2.default)(plugin, dirname);
  174.         if (pluginLoc) {
  175.           plugin = require(pluginLoc);
  176.         } else {
  177.           throw new ReferenceError(messages.get("pluginUnknown", plugin, loc, i, dirname));
  178.         }
  179.       }
  180.  
  181.       plugin = OptionManager.normalisePlugin(plugin, loc, i, alias);
  182.  
  183.       return [plugin, options];
  184.     });
  185.   };
  186.  
  187.   /**
  188.    * This is called when we want to merge the input `opts` into the
  189.    * base options (passed as the `extendingOpts`: at top-level it's the
  190.    * main options, at presets level it's presets options).
  191.    *
  192.    *  - `alias` is used to output pretty traces back to the original source.
  193.    *  - `loc` is used to point to the original config.
  194.    *  - `dirname` is used to resolve plugins relative to it.
  195.    */
  196.  
  197.   OptionManager.prototype.mergeOptions = function mergeOptions(_ref2) {
  198.     var _this = this;
  199.  
  200.     var rawOpts = _ref2.options;
  201.     var extendingOpts = _ref2.extending;
  202.     var alias = _ref2.alias;
  203.     var loc = _ref2.loc;
  204.     var dirname = _ref2.dirname;
  205.  
  206.     alias = alias || "foreign";
  207.     if (!rawOpts) return;
  208.  
  209.     //
  210.     if ((typeof rawOpts === "undefined" ? "undefined" : (0, _typeof3.default)(rawOpts)) !== "object" || Array.isArray(rawOpts)) {
  211.       this.log.error("Invalid options type for " + alias, TypeError);
  212.     }
  213.  
  214.     //
  215.     var opts = (0, _cloneDeepWith2.default)(rawOpts, function (val) {
  216.       if (val instanceof _plugin3.default) {
  217.         return val;
  218.       }
  219.     });
  220.  
  221.     //
  222.     dirname = dirname || process.cwd();
  223.     loc = loc || alias;
  224.  
  225.     for (var _key2 in opts) {
  226.       var option = _config3.default[_key2];
  227.  
  228.       // check for an unknown option
  229.       if (!option && this.log) {
  230.         var pluginOptsInfo = "Check out http://babeljs.io/docs/usage/options/ for more info";
  231.  
  232.         if (_removed2.default[_key2]) {
  233.           this.log.error("Using removed Babel 5 option: " + alias + "." + _key2 + " - " + _removed2.default[_key2].message, ReferenceError);
  234.         } else {
  235.           this.log.error("Unknown option: " + alias + "." + _key2 + ". " + pluginOptsInfo, ReferenceError);
  236.         }
  237.       }
  238.     }
  239.  
  240.     // normalise options
  241.     (0, _index.normaliseOptions)(opts);
  242.  
  243.     // resolve plugins
  244.     if (opts.plugins) {
  245.       opts.plugins = OptionManager.normalisePlugins(loc, dirname, opts.plugins);
  246.     }
  247.  
  248.     // resolve presets
  249.     if (opts.presets) {
  250.       // If we're in the "pass per preset" mode, we resolve the presets
  251.       // and keep them for further execution to calculate the options.
  252.       if (opts.passPerPreset) {
  253.         opts.presets = this.resolvePresets(opts.presets, dirname, function (preset, presetLoc) {
  254.           _this.mergeOptions({
  255.             options: preset,
  256.             extending: preset,
  257.             alias: presetLoc,
  258.             loc: presetLoc,
  259.             dirname: dirname
  260.           });
  261.         });
  262.       } else {
  263.         // Otherwise, just merge presets options into the main options.
  264.         this.mergePresets(opts.presets, dirname);
  265.         delete opts.presets;
  266.       }
  267.     }
  268.  
  269.     // Merge them into current extending options in case of top-level
  270.     // options. In case of presets, just re-assign options which are got
  271.     // normalized during the `mergeOptions`.
  272.     if (rawOpts === extendingOpts) {
  273.       (0, _assign2.default)(extendingOpts, opts);
  274.     } else {
  275.       (0, _merge2.default)(extendingOpts || this.options, opts);
  276.     }
  277.   };
  278.  
  279.   /**
  280.    * Merges all presets into the main options in case we are not in the
  281.    * "pass per preset" mode. Otherwise, options are calculated per preset.
  282.    */
  283.  
  284.  
  285.   OptionManager.prototype.mergePresets = function mergePresets(presets, dirname) {
  286.     var _this2 = this;
  287.  
  288.     this.resolvePresets(presets, dirname, function (presetOpts, presetLoc) {
  289.       _this2.mergeOptions({
  290.         options: presetOpts,
  291.         alias: presetLoc,
  292.         loc: presetLoc,
  293.         dirname: _path2.default.dirname(presetLoc || "")
  294.       });
  295.     });
  296.   };
  297.  
  298.   /**
  299.    * Resolves presets options which can be either direct object data,
  300.    * or a module name to require.
  301.    */
  302.  
  303.  
  304.   OptionManager.prototype.resolvePresets = function resolvePresets(presets, dirname, onResolve) {
  305.     return presets.map(function (val) {
  306.       var options = void 0;
  307.       if (Array.isArray(val)) {
  308.         if (val.length > 2) {
  309.           throw new Error("Unexpected extra options " + (0, _stringify2.default)(val.slice(2)) + " passed to preset.");
  310.         }
  311.  
  312.         var _val = val;
  313.         val = _val[0];
  314.         options = _val[1];
  315.       }
  316.  
  317.       var presetLoc = void 0;
  318.       if (typeof val === "string") {
  319.         presetLoc = (0, _resolve2.default)("babel-preset-" + val, dirname) || (0, _resolve2.default)(val, dirname);
  320.         if (!presetLoc) {
  321.           throw new Error("Couldn't find preset " + (0, _stringify2.default)(val) + " relative to directory " + (0, _stringify2.default)(dirname));
  322.         }
  323.  
  324.         val = require(presetLoc);
  325.       }
  326.  
  327.       // For compatibility with babel-core < 6.13.x, allow presets to export an object with a
  328.       // a 'buildPreset' function that will return the preset itself, while still exporting a
  329.       // simple object (rather than a function), for supporting old Babel versions.
  330.       if ((typeof val === "undefined" ? "undefined" : (0, _typeof3.default)(val)) === "object" && val.buildPreset) val = val.buildPreset;
  331.  
  332.       if (typeof val !== "function" && options !== undefined) {
  333.         throw new Error("Options " + (0, _stringify2.default)(options) + " passed to " + (presetLoc || "a preset") + " which does not accept options.");
  334.       }
  335.  
  336.       if (typeof val === "function") val = val(context, options);
  337.  
  338.       if ((typeof val === "undefined" ? "undefined" : (0, _typeof3.default)(val)) !== "object") {
  339.         throw new Error("Unsupported preset format: " + val + ".");
  340.       }
  341.  
  342.       onResolve && onResolve(val);
  343.       return val;
  344.     });
  345.   };
  346.  
  347.   OptionManager.prototype.normaliseOptions = function normaliseOptions() {
  348.     var opts = this.options;
  349.  
  350.     for (var _key3 in _config3.default) {
  351.       var option = _config3.default[_key3];
  352.       var val = opts[_key3];
  353.  
  354.       // optional
  355.       if (!val && option.optional) continue;
  356.  
  357.       // aliases
  358.       if (option.alias) {
  359.         opts[option.alias] = opts[option.alias] || val;
  360.       } else {
  361.         opts[_key3] = val;
  362.       }
  363.     }
  364.   };
  365.  
  366.   OptionManager.prototype.init = function init() {
  367.     var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  368.  
  369.     for (var _iterator2 = (0, _buildConfigChain2.default)(opts, this.log), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  370.       var _ref3;
  371.  
  372.       if (_isArray2) {
  373.         if (_i2 >= _iterator2.length) break;
  374.         _ref3 = _iterator2[_i2++];
  375.       } else {
  376.         _i2 = _iterator2.next();
  377.         if (_i2.done) break;
  378.         _ref3 = _i2.value;
  379.       }
  380.  
  381.       var _config = _ref3;
  382.  
  383.       this.mergeOptions(_config);
  384.     }
  385.  
  386.     // normalise
  387.     this.normaliseOptions(opts);
  388.  
  389.     return this.options;
  390.   };
  391.  
  392.   return OptionManager;
  393. }();
  394.  
  395. exports.default = OptionManager;
  396.  
  397.  
  398. OptionManager.memoisedPlugins = [];
  399. module.exports = exports["default"];
downloadoption-manager.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top