Ran into the same issue; I'm making components that'll work with both react-dom
and react-native
(using react-native-web
). I'm also using Storybook to test/develop the components in isolation.
The solution described here worked for me.
To my understanding, the issue is due to the plugin @babel/plugin-proposal-class-properties
not being included during compilation.
I ended up with the following .storybook/webpack.config.js
; as the solution I linked was a bit mal-formatted.
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. I used this person's issue to get the recommended installation for "web" working. My .storybook/preview.js
looks similar to this:
... (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)
//
Hope it all works out!
P.P.S Sorry, feel it's worth mentioning that I'm not sure if this Babel configuration is better put within .storybook/main.js
as recommended by Storybook.js. I could not get this method to work; and couldn't find any examples on how I'd go about doing it; my above mentioned solution came first.