BVB Source Codes

storybook Show babel_config.js Source code

Return Download storybook: download babel_config.js Source code - Download storybook Source code - Type:.js
  1. import fs from 'fs';
  2. import path from 'path';
  3. import JSON5 from 'json5';
  4. import defaultConfig from './config/babel';
  5.  
  6. // avoid ESLint errors
  7. const logger = console;
  8.  
  9. function removeReactHmre(presets) {
  10.   const index = presets.indexOf('react-hmre');
  11.   if (index > -1) {
  12.     presets.splice(index, 1);
  13.   }
  14. }
  15.  
  16. // Tries to load a .babelrc and returns the parsed object if successful
  17. function loadFromPath(babelConfigPath) {
  18.   let config;
  19.   if (fs.existsSync(babelConfigPath)) {
  20.     const content = fs.readFileSync(babelConfigPath, 'utf-8');
  21.     try {
  22.       config = JSON5.parse(content);
  23.       config.babelrc = false;
  24.       logger.info('=> Loading custom .babelrc');
  25.     } catch (e) {
  26.       logger.error(`=> Error parsing .babelrc file: ${e.message}`);
  27.       throw e;
  28.     }
  29.   }
  30.  
  31.   if (!config) return null;
  32.  
  33.   // Remove react-hmre preset.
  34.   // It causes issues with react-storybook.
  35.   // We don't really need it.
  36.   // Earlier, we fix this by running storybook in the production mode.
  37.   // But, that hide some useful debug messages.
  38.   if (config.presets) {
  39.     removeReactHmre(config.presets);
  40.   }
  41.  
  42.   if (config.env && config.env.development && config.env.development.presets) {
  43.     removeReactHmre(config.env.development.presets);
  44.   }
  45.  
  46.   return config;
  47. }
  48.  
  49. export default function(configDir) {
  50.   let babelConfig = loadFromPath(path.resolve(configDir, '.babelrc'));
  51.   let inConfigDir = true;
  52.  
  53.   if (!babelConfig) {
  54.     babelConfig = loadFromPath('.babelrc');
  55.     inConfigDir = false;
  56.   }
  57.  
  58.   if (babelConfig) {
  59.     // If the custom config uses babel's `extends` clause, then replace it with
  60.     // an absolute path. `extends` will not work unless we do this.
  61.     if (babelConfig.extends) {
  62.       babelConfig.extends = inConfigDir
  63.         ? path.resolve(configDir, babelConfig.extends)
  64.         : path.resolve(babelConfig.extends);
  65.     }
  66.   }
  67.  
  68.   const finalConfig = babelConfig || defaultConfig;
  69.   // Ensure plugins are defined or fallback to an array to avoid empty values.
  70.   const babelConfigPlugins = finalConfig.plugins || [];
  71.   const extraPlugins = [
  72.     [
  73.       require.resolve('babel-plugin-react-docgen'),
  74.       {
  75.         DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
  76.       },
  77.     ],
  78.   ];
  79.   // If `babelConfigPlugins` is not an `Array`, calling `concat` will inject it
  80.   // as a single value, if it is an `Array` it will be spreaded.
  81.   finalConfig.plugins = [].concat(babelConfigPlugins, extraPlugins);
  82.  
  83.   return finalConfig;
  84. }
  85.  
downloadbabel_config.js Source code - Download storybook Source code
Related Source Codes/Software:
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
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
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top