遇到了同样的问题;我正在制作适用于react-dom 和react-native 的组件(使用react-native-web)。我还使用 Storybook 单独测试/开发组件。
solution described here 为我工作。
据我了解,问题是由于插件@babel/plugin-proposal-class-properties 在编译过程中未包含在内。
我最终得到了以下.storybook/webpack.config.js;因为我链接的解决方案格式有点错误。
const path = require('path')
module.exports = async ({config, mode}) => {
config.module.rules.push({
test: /\.(js|jsx|ts|tsx)$/,
loader: 'babel-loader',
include: path.resolve(__dirname, '../node_modules/'),
options: {
presets: [
"@babel/env",
"@babel/preset-react",
"module:metro-react-native-babel-preset",
],
plugins: [
"react-native-web",
"@babel/plugin-proposal-class-properties"
]
}
})
config.module.rules.push({
test: /\.ttf$/,
loader: 'url-loader',
include: path.resolve(__dirname, '../node_modules/react-native-vector-icons/Fontisto.js')
})
config.resolve.alias = {
'react-native': 'react-native-web'
}
return config
}
P.S. 我使用 this person's issue 让 recommended installation for "web" 工作。我的.storybook/preview.js 看起来像这样:
... (export parameters, export globalTypes, etc..)
// ==
import Fontisto from '../node_modules/react-native-vector-icons/Fontisto'
import iconFont from '../node_modules/react-native-vector-icons/Fonts/Fontisto.ttf'
const iconFontStyle =`
@font-face {
src: url(${iconFont});
font-family: Fontisto;
}
`
const style = document.createElement('style')
style.type = 'text/css'
if (style.styleSheet) {
style.styleSheet.cssText = iconFontStyle
} else {
style.appendChild(document.createTextNode(iconFontStyle))
}
document.head.appendChild(style)
//
希望一切顺利!
P.P.S 抱歉,值得一提的是,我不确定这个 Babel 配置是否更好地放在 .storybook/main.js as recommended by Storybook.js 中。我可以不让这个方法起作用;并且找不到任何关于我将如何去做的例子;我上面提到的解决方案是第一位的。