Describe the bug
Storybook can't resolve components path that are imported from a directory with the default index.js
βββ .storybook
β βββ config.js
β βββ addons.js
β βββ webpack.config.js
βββ app
β βββ javascript
β β βββ src
β β β βββ form
β β β β βββ checkbox.jsx
β β β β βββ radio.jsx
β β β β βββ form.jsx
β β β β βββ form.stories.jsx
β β β β βββ index.js
β β β βββ alert
β β β β βββ alertstuff.jsx
β β β β βββ alert.jsx
β β β β βββ index.jsx
β β β βββutils
β β β β βββ utilsstuff.jsx
β β β β βββ utilsotherstuff.jsx
β β β β βββ index.jsx
form/index.js
contains:
import Form from './form.jsx';
import Radio from './radio.jsx';
import Checkbox from './checkbox.jsx';
export { Checkbox, Radio, Form };
alert/index.js
is the same:
import AlertStuff from './alertstuff.jsx';
import Alert from './alert.jsx';
export { AlertStuff, Alert };
utils/index.js
contains:
import UtilsStuff from './utilsstuff.jsx';
import UtilsOtherStuff './utilsotherstuff.jsx';
export { UtilsStuff, UtilsOtherStuff };
form.jsx
contiains:
import { AlertStuff } from 'alert';
export class Form extends Component {
....
}
form/form.stories.jsx
looks like this:
import { storiesOf } from '@storybook/react';
import React from 'react';
import Form from './form';
storiesOf('Form', module)
.addWithJSX('simple', () => (
<Form
name={text('Name', 'Form1')}
/>
));
.storybook/webpack.config.js
looks like this:
const path = require("path");
// Export a function. Accept the base config as the only param.
module.exports = (storybookBaseConfig, configType) => {
storybookBaseConfig.module.rules.push({
test: /\.sass$/,
loaders: ["style-loader", "css-loader", "sass-loader"],
include: path.resolve(__dirname, "../")
});
return storybookBaseConfig;
};
.storybook/config.js
:
import { withKnobs } from '@storybook/addon-knobs/react';
import { addDecorator, configure, setAddon } from '@storybook/react';
import JSXAddon from 'storybook-addon-jsx';
addDecorator(withKnobs);
window.regeneratorRuntime = require('babel-runtime/regenerator');
setAddon(JSXAddon);
const req = require.context('../app/javascript/src', true, /.stories.jsx$/);
function loadStories() {
require('./welcomeStory');
req.keys().forEach(file => req(file));
}
configure(loadStories, module);
When running npm run storybook
I get the following error:
ERROR in ./app/javascript/src/form/form.jsx
Module not found: Error: Can't resolve 'alert' in '/Users/xxx/Code/xxx/xxx/app/javascript/src/form'
These components load and work fine
Storybook should be able to resolve the path when Storybook uses the component
System:
Solved it with the following in the webpack.config.js file:
storybookBaseConfig.resolve.modules = [
...(storybookBaseConfig.resolve.modules || []),
path.resolve(__dirname, "../app/javascript/src"),
];
Resolved the issue with npm i @storybook/react
Most helpful comment
Solved it with the following in the webpack.config.js file: