Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect babel config file reading #1156

Merged
merged 4 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions app/react/src/server/babel_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,19 @@ export default function(configDir) {
}

const finalConfig = babelConfig || defaultConfig;
finalConfig.plugins = finalConfig.plugins || [];
finalConfig.plugins.push([
require.resolve('babel-plugin-react-docgen'),
{ DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES' },
]);
// Ensure plugins are defined or fallback to an array to avoid empty values.
const babelConfigPlugins = finalConfig.plugins || [];
const extraPlugins = [
[
require.resolve('babel-plugin-react-docgen'),
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
];
// If `babelConfigPlugins` is not an `Array`, calling `concat` will inject it
// as a single value, if it is an `Array` it will be spreaded.
finalConfig.plugins = [].concat(babelConfigPlugins, extraPlugins);

return finalConfig;
}
90 changes: 90 additions & 0 deletions app/react/src/server/babel_config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import mock from 'mock-fs';
import loadBabelConfig from './babel_config';

describe('babel_config', () => {
// As the 'fs' is going to be mocked, let's call require.resolve
// so the require.cache has the correct route to the file.
// In fact let's use it in the tests :)
const babelPluginReactDocgenPath = require.resolve('babel-plugin-react-docgen');

it('should return the config with the extra plugins when `plugins` is an array.', () => {
// Mock a simple `.babelrc` config file.
mock({
'.babelrc': `{
"presets": [
"es2015",
"foo-preset"
],
"plugins": [
"foo-plugin"
]
}`,
});

const config = loadBabelConfig('.foo');

expect(config.plugins).toEqual([
'foo-plugin',
[
babelPluginReactDocgenPath,
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
]);

mock.restore();
});

it('should return the config with the extra plugins when `plugins` is not an array.', () => {
// Mock a `.babelrc` config file with plugins key not being an array.
mock({
'.babelrc': `{
"presets": [
"es2015",
"foo-preset"
],
"plugins": "bar-plugin"
}`,
});

const config = loadBabelConfig('.bar');

expect(config.plugins).toEqual([
'bar-plugin',
[
babelPluginReactDocgenPath,
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
]);

mock.restore();
});

it('should return the config only with the extra plugins when `plugins` is not present.', () => {
// Mock a `.babelrc` config file with no plugins key.
mock({
'.babelrc': `{
"presets": [
"es2015",
"foo-preset"
]
}`,
});

const config = loadBabelConfig('.biz');

expect(config.plugins).toEqual([
[
babelPluginReactDocgenPath,
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
]);

mock.restore();
});
});