我有一个简单的故事书项目,其结构如下:
├── .storybook
├── .babelrc
├── package.json
├── node_modules
├── stories
│ ├── index.js
我可以运行我的配置start-storybook -p 6006
// .storybook/config.js
import { configure } from '@storybook/react'
function loadStories() {
require('../stories/index.js')
}
configure(loadStories, module)
现在我想包含一些组件,它们是后面的目录。所以新的文件结构是:
├── storybook
│ ├── .storybook
│ ├── .babelrc
│ ├── package.json
│ ├── node_modules
├── stories
│ ├── index.js
我的配置现在从一个目录中调用故事:
// ./storybook/.storybook/config.js
import { configure } from '@storybook/react'
function loadStories() {
require('../../stories/index.js')
}
configure(loadStories, module)
但似乎故事书现在无法解析文件,即使唯一的变化是故事已被移回文件。我收到以下错误:
ERROR in ../admin-components/components/Button/Button.js 40:26
Module parse failed: Unexpected token (40:26)
You may need an appropriate loader to handle this file type.
| import React from "react"
|
> const Button = (props) => <button>Click Me!!!</button>
|
| export default Button
@ ../admin-components/components/Button/index.js 1:0-29 3:15-21
@ ../admin-components/components/index.js
@ ./stories/index.js
@ ./.storybook/config.js
@ multi ./node_modules/@storybook/core/dist/server/config/polyfills.js ./node_modules/@storybook/core/dist/server/config/globals.js ./.storybook/config.js ./node_modules/webpack-hot-middleware/client.js?reload=true
我是否需要一些自定义解析器配置,.babelrc
或者这会与默认的故事书配置发生冲突。也许故事书有一些设置可以处理这个目录结构?
编辑 已尝试向我的 webpack 配置添加更多配置以允许解析 JSX,但无济于事。
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = (storybookBaseConfig, configType) => {
storybookBaseConfig.resolve.alias = {
'prop-types$': path.join(__dirname, '../node_modules/axe-prop-types')
};
storybookBaseConfig.module.rules.push({
test: /\.(js|jsx)$/,
exclude: [/bower_components/, /node_modules/, /styles/],
loader: 'babel-loader',
include: path.resolve(__dirname, '../stories'),
query: {
presets: ['@babel/react']
}
});
storybookBaseConfig.plugins.push(new CopyWebpackPlugin([{ from: '.storybook/fonts', to: 'fonts' }]))
if (configType === 'PRODUCTION') {
config.optimization.minimize = false;
}
return storybookBaseConfig;
}
收到以下错误:
ERROR in ./stories/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Plugin/Preset files are not allowed to export objects, only functions.