Skip to content

Commit

Permalink
Search for a .babelrc in the storybook config directory first, then t…
Browse files Browse the repository at this point in the history
…he project root (#149)

Closes #138, #139
  • Loading branch information
sethkinast authored and arunoda committed Apr 26, 2016
1 parent a2f6761 commit 75fbf63
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
33 changes: 22 additions & 11 deletions dist/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ var _extends3 = _interopRequireDefault(_extends2);
exports.default = function (baseConfig, configDir) {
var config = baseConfig;

// if user has a .babelrc file in current directory
// use that to extend webpack configurations
if (_fs2.default.existsSync('./.babelrc')) {
var content = _fs2.default.readFileSync('./.babelrc', 'utf-8');
try {
var babelrc = _cjson2.default.parse(content);
config.module.loaders[0].query = babelrc;
} catch (e) {
logger.error('=> Error parsing .babelrc file: ' + e.message);
throw e;
}
// search for a .babelrc in the config directory, then the module root directory
// if found, use that to extend webpack configurations
var babelConfig = loadBabelConfig(_path2.default.resolve(configDir, '.babelrc')) || loadBabelConfig('.babelrc');
if (babelConfig) {
config.module.loaders[0].query = babelConfig;
}

// Check whether a config.js file exists inside the storybook
Expand Down Expand Up @@ -73,6 +67,23 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
// avoid ESLint errors
var logger = console;

// Tries to load a .babelrc and returns the parsed object if successful
function loadBabelConfig(babelConfigPath) {
var config = void 0;
if (_fs2.default.existsSync(babelConfigPath)) {
var content = _fs2.default.readFileSync(babelConfigPath, 'utf-8');
try {
config = _cjson2.default.parse(content);
config.babelrc = false;
logger.info('=> Loading custom .babelrc');
} catch (e) {
logger.error('=> Error parsing .babelrc file: ' + e.message);
throw e;
}
}
return config;
}

// `baseConfig` is a webpack configuration bundled with storybook.
// React Storybook will look in the `configDir` directory
// (inside working directory) if a config path is not provided.
4 changes: 4 additions & 0 deletions docs/configure_storybook.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ module.exports = {

We allow you to use almost all [Webpack configurations](https://webpack.github.io/docs/configuration.html). So, you can customize as you wish.

## Custom Babel Config

Storybook will first search for a `.babelrc` inside the storybook config directory, and then at your project's root. If it doesn't find either of these files, it will use its default configuration instead.

## Load Custom HTML Head Content

Sometimes, we need to load custom DOM nodes inside the HTML `<head>` tag. For an example, this is how we can load TypeKit fonts with React Storybook.
Expand Down
35 changes: 24 additions & 11 deletions src/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,36 @@ import cjson from 'cjson';
// avoid ESLint errors
const logger = console;

// Tries to load a .babelrc and returns the parsed object if successful
function loadBabelConfig(babelConfigPath) {
let config;
if (fs.existsSync(babelConfigPath)) {
const content = fs.readFileSync(babelConfigPath, 'utf-8');
try {
config = cjson.parse(content);
config.babelrc = false;
logger.info('=> Loading custom .babelrc');
} catch (e) {
logger.error(`=> Error parsing .babelrc file: ${e.message}`);
throw e;
}
}
return config;
}

// `baseConfig` is a webpack configuration bundled with storybook.
// React Storybook will look in the `configDir` directory
// (inside working directory) if a config path is not provided.
export default function (baseConfig, configDir) {
const config = baseConfig;

// if user has a .babelrc file in current directory
// use that to extend webpack configurations
if (fs.existsSync('./.babelrc')) {
const content = fs.readFileSync('./.babelrc', 'utf-8');
try {
const babelrc = cjson.parse(content);
config.module.loaders[0].query = babelrc;
} catch (e) {
logger.error(`=> Error parsing .babelrc file: ${e.message}`);
throw e;
}
// search for a .babelrc in the config directory, then the module root directory
// if found, use that to extend webpack configurations
const babelConfig =
loadBabelConfig(path.resolve(configDir, '.babelrc')) ||
loadBabelConfig('.babelrc');
if (babelConfig) {
config.module.loaders[0].query = babelConfig;
}

// Check whether a config.js file exists inside the storybook
Expand Down

0 comments on commit 75fbf63

Please sign in to comment.