-
Notifications
You must be signed in to change notification settings - Fork 102
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
Error occured when eject the cra-ts example #72
Comments
Hello @Mu-xue! I've encountered the same issue after ejecting my CRA app that used @storybook/preset-create-react-app, and based my thoughts on the following answer storybookjs/storybook#6690 (typescript config advice from that issue didn't solve anything):
So, I'm entirely new to Storybook infrastructure, but my understanding of things is this: Storybook has its own webpack config, which is configurable through presets specifically @storybook/preset-create-react-app. After ejecting, as per above issue, this preset no longer valid so actual storybook launch degrades to its default webpack config, which is actually reported in console output: Default webpack config is lacking support of typescript (your case, you've got a syntax error due to non-js So, what you have to do now is restore JSX and typescript support with separate config by adressing Typescript Config and Storybook for React sections of documentantion. I was able to restore my expected behaviour (jsx, typescript and ts props exctraction) from @storybook/preset-create-react-app with following manual configuration: // .storybook/main.js
const path = require("path")
module.exports = {
stories: ['../src/**/*.stories.(tsx|mdx)'],
webpackFinal: async config => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
include: path.resolve(__dirname, "../src"),
use: [
// There is also a second option in documentation to use babel instead of ts-loader which should also work
{
loader: require.resolve('ts-loader'),
},
{
loader: require.resolve('react-docgen-typescript-loader'),
options: {
tsconfigPath: path.resolve(__dirname, "../tsconfig.json")
}
}
],
})
config.resolve.extensions.push('.ts', '.tsx');
return config
},
addons: [
'@storybook/addon-docs'
]
} and tsconfig.json: {
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"jsx": "react"
},
"include": [
"src"
]
} This correspond to my original config which I had before ejecting my CRA: // .storybook/main.js
module.exports = {
stories: ['../src/**/*.stories.tsx'],
addons: [
{
name: '@storybook/preset-create-react-app',
options: {
tsDocgenLoaderOptions: {},
},
},
'@storybook/addon-docs'
]
} |
I was able to make it work with these changes:
I was able to restore expected behaviour as well, but with a bit different webpack 4 config for storybook: const path = require('path')
module.exports = {
stories: [
'../src/**/*.stories.mdx',
'../src/**/*.stories.@(js|jsx|ts|tsx)',
], webpackFinal: async config => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
include: path.resolve(__dirname, '../src'),
use: [// There is also a second option in documentation to use babel instead of ts-loader which should also work
{
loader: require.resolve('babel-loader'), options: {
customize: require.resolve('babel-preset-react-app/webpack-overrides'),
presets: [
[require.resolve('babel-preset-react-app'), { runtime: 'automatic' }],
[require.resolve('@babel/preset-env'), { targets: 'defaults' }],
],
plugins: [
[require.resolve('babel-plugin-named-asset-import'),
{ loaderMap: { svg: { ReactComponent: '@svgr/webpack?-svgo,+titleProp,+ref![path]' } } },
],
],
},
}],
}, {
test: /\.ts$/,
loader: 'babel-loader',
exclude: /node_modules/,
}, // Process any JS outside of the app with Babel.
// Unlike the application JS, we only compile the standard ES features.
{
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
presets: [
[require.resolve('babel-preset-react-app/dependencies'), { helpers: true }],
],
cacheDirectory: true, // See #6846 for context on why cacheCompression is disabled
cacheCompression: false, // Babel sourcemaps are needed for debugging into node_modules
// code. Without the options below, debuggers like VSCode
// show incorrect code and set breakpoints on the wrong lines.
sourceMaps: true,
inputSourceMap: true,
},
}, {
test: /\.scss$/,
use: ['style-loader', 'css-loader', {
loader: 'postcss-loader',
options: { plugins: () => [require('autoprefixer')()] },
}, 'sass-loader'],
})
config.resolve.extensions.push('.ts', '.tsx')
return config
}, addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
],
} |
I download the cra-ts example and run :
And the
Please help to fix it.
The text was updated successfully, but these errors were encountered: