-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
build-storybook doesn't respect tsconfig.json baseUrl config (Module not found: Error: Can't resolve 'components') #3291
Comments
hey @danielduan this is a react app fwiw but the question itself is more of a typescript + storybook interop question |
My bad, I automatically assumed TS was used in the angular one. In any case, |
ok that sounds extremely promising. is this "different webpack config and babel config" documented anywhere? I looked thru the docs and basically found nothing about the build process (kinda surprised!) |
Have you tried adding https://www.npmjs.com/package/tsconfig-paths-webpack-plugin to your project? |
@felipedeboni Thank you so much!! Perfectly solve the issue! |
@felipeleusin sorry for the delayed response, I only just saw this. I tried it but it doesnt seem to work. I have no idea if its my fault, I don't even know how to debug this thing! my const path = require('path');
const TSDocgenPlugin = require('react-docgen-typescript-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
module.exports = (baseConfig, env, defaultConfig) => {
defaultConfig.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('awesome-typescript-loader')
});
defaultConfig.plugins.push(new TSDocgenPlugin());
defaultConfig.plugins.push(new TsconfigPathsPlugin({})); // this is the plugin
defaultConfig.resolve.extensions.push('.ts', '.tsx');
return defaultConfig;
}; my baseUrl is src, so if I did this right I think I should be able to do absolute imports like this: Can anyone advise how to debug what is going wrong here? |
@tsiq-swyx This is my config, it may not be correct, but hope it helps:
BTW, as mentioned in the plugin's docs:
And I see you are requiring a |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
+1. it'd be great if it worked out of the box. |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
This is a big usecase and should stay open till proper fix |
by the way, even though label |
After digging through the source I have solved this issue. {
"extends": "../tsconfig.app.json"
} In its turn, |
@DmitryEfimenko, adding a custom |
also, I found out, it does not quite fix the issue. The moment you add some reference to some custom path alias (like |
Having custom path alias in TS project is always painful since you have to configure |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
this stale bot is very annoying. Issues should be closed just because no work was done and people didn't comment |
There is a lot of discussions here, but I can't understand the action item. |
Here is my use case. I have a mono-repo app generated by Angular CLI 6.0 which has two projects. Here is an approximate structure:
The
The
That won't work because storybook won't be able to interpret |
Ok, now reading this in the 5th time I see you talked before about angular as well. I am closing this one since the initial issue was about React, which doesn't have any angular-cli / TS integration out of the box. Regarding the paths issue, it was already discussed in #2718 and has workarounds (let's continue there if you still have any problems). Also, make sure you are working with the latest alpha (4 alpha 10 ATM) which has an integration with angular-cli v6. |
I'd think this is not an Angular, but rather TypeScript related issue. Your reference to the other issue is right on though. I'll check if workarounds specified there work. Thanks! |
The issue you're having is not with Storybook @tsiq-swyx. It is a webpack misconfiguration issue. Aliases also need to be added to the custom TS webpack config in the e.g. sample
needs to have the corresponding entries in webpack. e.g.
|
For some reason it looks like resolve plugins aren't extended. So if you're using
|
In my opinion there is an issue around this line. It should get the modules of the original config as well. My workaround, in const process = require("process");
module.exports = {
// ...
webpackFinal: (config) => {
config.resolve.modules.push(process.cwd() + "/node_modules");
config.resolve.modules.push(process.cwd() + "/src");
// this is needed for working w/ linked folders
config.resolve.symlinks = false;
return config;
}
}; |
The solution above from @cristian-spiescu worked for me. It's worth to mention that you must use |
@jwm0 your solution worked for me. Here's my
|
In my case, the issue was resolved by adding the commands below to
|
I'm using angular 11, and still facing the same issue, I tried @dwilhel1 solution but it seems not working for me here is my tsconfig.json and here is an example of imports in my main routing module main.js file: |
I have next folder structure:
In Button.stories.tsx import App/Helpers/Button works fine, but in DropdownButton.stories.tsx import "App/Helpers/DropdownButton/DropdownButton" does not work. More then that, if I run tsc and near to tsx files appear corresponding js files, then modules found. Solution of cristian-spiescu works. tsconfig.json:
|
found another solution. my tsconfig.json looks like this: {
"extends": "./tsconfig.paths.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": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true,
"baseUrl": "./src",
"paths": {
"@/*": [
"./*"
],
}
},
"include": [
"src/**/*"
],
"exclude": [
"scripts/templates/**/*",
"config/**/*",
"config-overrides.js",
"main/**/*"
]
} so if I were to import const rewireAliases = require('react-app-rewire-aliases');
const { paths } = require('react-app-rewired');
const path = require('path');
/* config-overrides.js */
module.exports = {
"stories": [
"../src/**/*.stories.mdx",
"../src/**/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/preset-create-react-app"
],
"webpackFinal": (config, env) => {
config = rewireAliases.aliasesOptions({
'@': path.resolve(__dirname, `${paths.appSrc}`)
})(config, env);
// config.target = "electron-renderer";
return config;
}
} I commented on the |
This worked for me. Spent a few days working on it, finally a solution. Thanks @cristian-spiescu. |
I recently upgraded to v6.4.0 and just discovered that I no longer need the work around (a variation of #3291 (comment)). Anyone else see the same? If not I'll go through my git history and report back on what changed that fixed it for me. |
this is fixed my problem also. thank you!! |
FWIW I'm new to Storybook, v6.4.5, and I still needed |
My workaround, in const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin')
module.exports = {
stories: [
'../stories/**/*.stories.mdx',
'../util/**/*.stories.@(js|jsx|ts|tsx)',
'../component/**/*.stories.@(js|jsx|ts|tsx)'
],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/addon-storysource',
'@storybook/addon-viewport'
],
typescript: {
reactDocgen: 'react-docgen-typescript',
reactDocgenTypescriptOptions: {
compilerOptions: {
allowSyntheticDefaultImports: false,
esModuleInterop: false
}
}
},
webpackFinal: async (config) => {
config.resolve.plugins = [
...(config.resolve.plugins || []),
new TsconfigPathsPlugin()
]
return config
}
} |
it solved my issue, thank you |
Bug or support request summary
I'm honestly not sure if this is a bug or a request for support.
Please provide issue details here - What did you expect to happen? What happened instead?
I know Typescript isn't super well supported with Storybook but I figure I'd just lay out what I have found here.
Basically I have a React component library built in Typescript and viewed with Storybook. The file structure looks something like
I want to use absolute importing instead of relative importing, aka if I am importing
MyComponent
inMyContainer
i want to goimport {MyComponent} from 'components'
instead ofimport {MyComponent} from '../../../components'
.This is supported in Typescript with the
tsconfig.json
setting:And this actually does build in
tsc
. However, it fails tobuild-storybook
.Project specfics and example error
in Tasks.tsx I have this line:
and this is the error I get in my project:
So I've done some digging and I think I need to do something with the storybook webpack config but I have no idea what it is. This is my current
.storybook/webpack.config.js
:I think all this is fine, however
ts-loader
should probably be resolving according to the rules laid out intsconfig.json
.things i have tried
I tried adding this line:
to the webpack config to take advantage of webpack's module resolution but I think that is probably the wrong way to do it because it just starts trying to resolve typescript all over again:
So I am stuck. Please help/give ideas on how to resolve this! I will put this in the Storybook Typescript documentation I am writing.
Please specify which version of Storybook and optionally any affected addons that you're running
The text was updated successfully, but these errors were encountered: