Skip to content

Commit

Permalink
Handle loading .storybook/babel.config.js (#6633) (#6634)
Browse files Browse the repository at this point in the history
Handle loading `.storybook/babel.config.js` (#6633)
  • Loading branch information
ndelangen authored May 20, 2019
2 parents 070ed36 + 1b7e75f commit a08f44e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/cli/test/fixtures/react_babel_config_js/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports = {
presets: [
['@babel/preset-env', { shippedProposals: true, useBuiltIns: 'usage', corejs: '3' }],
'@babel/preset-react',
],
plugins: ['@babel/plugin-proposal-optional-chaining'],
};
2 changes: 2 additions & 0 deletions lib/cli/test/fixtures/react_babel_config_js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"devDependencies": {
"@babel/core": "^7.3.4",
"@babel/plugin-proposal-optional-chaining": "7.0.0",
"@babel/preset-env": "^7.4.1",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5"
}
}
14 changes: 10 additions & 4 deletions lib/core/src/server/utils/load-custom-babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ function removeReactHmre(presets) {
// Tries to load a .babelrc and returns the parsed object if successful
function loadFromPath(babelConfigPath) {
let config;

if (fs.existsSync(babelConfigPath)) {
const content = fs.readFileSync(babelConfigPath, 'utf-8');
try {
config = JSON5.parse(content);
config = /^module.exports/.test(content)

This comment has been minimized.

Copy link
@ndelangen

ndelangen May 22, 2019

Author Member

This detection seems to be a little too specific.

#6633 (comment)

? require(babelConfigPath) // eslint-disable-line
: JSON5.parse(content);
config.babelrc = false;
logger.info('=> Loading custom .babelrc');
logger.info('=> Loading custom babel config');
} catch (e) {
logger.error(`=> Error parsing .babelrc file: ${e.message}`);
logger.error(`=> Error parsing babel config file: ${e.message}`);
throw e;
}
}
Expand Down Expand Up @@ -59,7 +62,10 @@ function isBabelLoader8() {
}

export default async function(configDir, getDefaultConfig) {
const babelConfig = loadFromPath(path.resolve(configDir, '.babelrc'));
const projectRoot = process.cwd();
const babelConfig =
loadFromPath(path.resolve(configDir, '.babelrc')) ||
loadFromPath(path.resolve(projectRoot, 'babel.config.js'));

if (babelConfig) {
// If the custom config uses babel's `extends` clause, then replace it with
Expand Down

1 comment on commit a08f44e

@vercel
Copy link

@vercel vercel bot commented on a08f44e May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.