BVB Source Codes

reactide Show build-config-chain.js Source code

Return Download reactide: download build-config-chain.js Source code - Download reactide Source code - Type:.js
  1. "use strict";
  2.  
  3. exports.__esModule = true;
  4.  
  5. var _assign = require("babel-runtime/core-js/object/assign");
  6.  
  7. var _assign2 = _interopRequireDefault(_assign);
  8.  
  9. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  10.  
  11. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  12.  
  13. exports.default = buildConfigChain;
  14.  
  15. var _resolve = require("../../../helpers/resolve");
  16.  
  17. var _resolve2 = _interopRequireDefault(_resolve);
  18.  
  19. var _json = require("json5");
  20.  
  21. var _json2 = _interopRequireDefault(_json);
  22.  
  23. var _pathIsAbsolute = require("path-is-absolute");
  24.  
  25. var _pathIsAbsolute2 = _interopRequireDefault(_pathIsAbsolute);
  26.  
  27. var _pathExists = require("path-exists");
  28.  
  29. var _pathExists2 = _interopRequireDefault(_pathExists);
  30.  
  31. var _path = require("path");
  32.  
  33. var _path2 = _interopRequireDefault(_path);
  34.  
  35. var _fs = require("fs");
  36.  
  37. var _fs2 = _interopRequireDefault(_fs);
  38.  
  39. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  40.  
  41. var existsCache = {};
  42. var jsonCache = {};
  43.  
  44. var BABELIGNORE_FILENAME = ".babelignore";
  45. var BABELRC_FILENAME = ".babelrc";
  46. var PACKAGE_FILENAME = "package.json";
  47.  
  48. function exists(filename) {
  49.   var cached = existsCache[filename];
  50.   if (cached == null) {
  51.     return existsCache[filename] = _pathExists2.default.sync(filename);
  52.   } else {
  53.     return cached;
  54.   }
  55. }
  56.  
  57. function buildConfigChain() {
  58.   var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  59.   var log = arguments[1];
  60.  
  61.   var filename = opts.filename;
  62.   var builder = new ConfigChainBuilder(log);
  63.  
  64.   // resolve all .babelrc files
  65.   if (opts.babelrc !== false) {
  66.     builder.findConfigs(filename);
  67.   }
  68.  
  69.   builder.mergeConfig({
  70.     options: opts,
  71.     alias: "base",
  72.     dirname: filename && _path2.default.dirname(filename)
  73.   });
  74.  
  75.   return builder.configs;
  76. }
  77.  
  78. var ConfigChainBuilder = function () {
  79.   function ConfigChainBuilder(log) {
  80.     (0, _classCallCheck3.default)(this, ConfigChainBuilder);
  81.  
  82.     this.resolvedConfigs = [];
  83.     this.configs = [];
  84.     this.log = log;
  85.   }
  86.  
  87.   ConfigChainBuilder.prototype.findConfigs = function findConfigs(loc) {
  88.     if (!loc) return;
  89.  
  90.     if (!(0, _pathIsAbsolute2.default)(loc)) {
  91.       loc = _path2.default.join(process.cwd(), loc);
  92.     }
  93.  
  94.     var foundConfig = false;
  95.     var foundIgnore = false;
  96.  
  97.     while (loc !== (loc = _path2.default.dirname(loc))) {
  98.       if (!foundConfig) {
  99.         var configLoc = _path2.default.join(loc, BABELRC_FILENAME);
  100.         if (exists(configLoc)) {
  101.           this.addConfig(configLoc);
  102.           foundConfig = true;
  103.         }
  104.  
  105.         var pkgLoc = _path2.default.join(loc, PACKAGE_FILENAME);
  106.         if (!foundConfig && exists(pkgLoc)) {
  107.           foundConfig = this.addConfig(pkgLoc, "babel", JSON);
  108.         }
  109.       }
  110.  
  111.       if (!foundIgnore) {
  112.         var ignoreLoc = _path2.default.join(loc, BABELIGNORE_FILENAME);
  113.         if (exists(ignoreLoc)) {
  114.           this.addIgnoreConfig(ignoreLoc);
  115.           foundIgnore = true;
  116.         }
  117.       }
  118.  
  119.       if (foundIgnore && foundConfig) return;
  120.     }
  121.   };
  122.  
  123.   ConfigChainBuilder.prototype.addIgnoreConfig = function addIgnoreConfig(loc) {
  124.     var file = _fs2.default.readFileSync(loc, "utf8");
  125.     var lines = file.split("\n");
  126.  
  127.     lines = lines.map(function (line) {
  128.       return line.replace(/#(.*?)$/, "").trim();
  129.     }).filter(function (line) {
  130.       return !!line;
  131.     });
  132.  
  133.     if (lines.length) {
  134.       this.mergeConfig({
  135.         options: { ignore: lines },
  136.         alias: loc,
  137.         dirname: _path2.default.dirname(loc)
  138.       });
  139.     }
  140.   };
  141.  
  142.   ConfigChainBuilder.prototype.addConfig = function addConfig(loc, key) {
  143.     var json = arguments.length <= 2 || arguments[2] === undefined ? _json2.default : arguments[2];
  144.  
  145.     if (this.resolvedConfigs.indexOf(loc) >= 0) {
  146.       return false;
  147.     }
  148.  
  149.     this.resolvedConfigs.push(loc);
  150.  
  151.     var content = _fs2.default.readFileSync(loc, "utf8");
  152.     var options = void 0;
  153.  
  154.     try {
  155.       options = jsonCache[content] = jsonCache[content] || json.parse(content);
  156.       if (key) options = options[key];
  157.     } catch (err) {
  158.       err.message = loc + ": Error while parsing JSON - " + err.message;
  159.       throw err;
  160.     }
  161.  
  162.     this.mergeConfig({
  163.       options: options,
  164.       alias: loc,
  165.       dirname: _path2.default.dirname(loc)
  166.     });
  167.  
  168.     return !!options;
  169.   };
  170.  
  171.   ConfigChainBuilder.prototype.mergeConfig = function mergeConfig(_ref) {
  172.     var options = _ref.options;
  173.     var alias = _ref.alias;
  174.     var loc = _ref.loc;
  175.     var dirname = _ref.dirname;
  176.  
  177.     if (!options) {
  178.       return false;
  179.     }
  180.  
  181.     options = (0, _assign2.default)({}, options);
  182.  
  183.     dirname = dirname || process.cwd();
  184.     loc = loc || alias;
  185.  
  186.     // add extends clause
  187.     if (options.extends) {
  188.       var extendsLoc = (0, _resolve2.default)(options.extends, dirname);
  189.       if (extendsLoc) {
  190.         this.addConfig(extendsLoc);
  191.       } else {
  192.         if (this.log) this.log.error("Couldn't resolve extends clause of " + options.extends + " in " + alias);
  193.       }
  194.       delete options.extends;
  195.     }
  196.  
  197.     this.configs.push({
  198.       options: options,
  199.       alias: alias,
  200.       loc: loc,
  201.       dirname: dirname
  202.     });
  203.  
  204.     // env
  205.     var envOpts = void 0;
  206.     var envKey = process.env.BABEL_ENV || process.env.NODE_ENV || "development";
  207.     if (options.env) {
  208.       envOpts = options.env[envKey];
  209.       delete options.env;
  210.     }
  211.  
  212.     this.mergeConfig({
  213.       options: envOpts,
  214.       alias: alias + ".env." + envKey,
  215.       dirname: dirname
  216.     });
  217.   };
  218.  
  219.   return ConfigChainBuilder;
  220. }();
  221.  
  222. module.exports = exports["default"];
downloadbuild-config-chain.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