-
-
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
Storybook can't resolve aliases #14087
Comments
Try changing your aliases by adding an @ as a prefix. Eg: This happened to me when I used aliases without a prefix. The webpack confuse aliases with aliases from third-party libraries. |
@adhenrique So I've just tried to update my webpack + ts config with "@" before all my aliases and carefully updated all my files. It doesn't work. I still get the error:
|
Ok I found the solution. Just use "tsconfig-paths-webpack-plugin". The config now looks like this: const { TsconfigPathsPlugin } = require("tsconfig-paths-webpack-plugin");
module.exports = {
stories: ["../**/stories.tsx"],
webpackFinal: async (config) => {
[].push.apply(config.resolve.plugins, [
new TsconfigPathsPlugin({ extensions: config.resolve.extensions }),
]);
return {
...config,
module: {
...config.module,
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: { loader: "babel-loader" },
},
{ test: /\.css$/, use: ["style-loader", "css-loader"] },
{ test: /\.(png|jpg|gif)$/, use: ["file-loader"] },
{
test: /\.svg$/,
use: [
{
loader: "babel-loader",
},
{
loader: "react-svg-loader",
options: {
jsx: true,
},
},
],
},
],
},
};
},
}; I would recommend to officially support this plugin for typescript users, it saves so much time. |
Two methods for handling I've seen now to address this issue here and in #13184 are: [].push.apply(config.resolve.plugins, [
new TsconfigPathsPlugin({ extensions: config.resolve.extensions }),
]); config.resolve.plugins = [new TsconfigPathsPlugin({ extensions: config.resolve.extensions })] The first seems more robust because it's pushing the plugin onto the existing array whereas the second doesn't. I was able to use the second version without issue. But I'm wondering if it's prone to breakage due to not being as robust. @DoneDeal0 any thoughts? |
@balibebas Definitely prefer the most robust one, maybe:
Or the equivalent |
const path = require('path');
module.exports = {
core: {
builder: 'webpack5',
},
stories: ['../src/**/*.stories.tsx'],
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
webpackFinal: async (config) => {
config.resolve.alias = {
'@': path.resolve(__dirname, '..', 'src'),
};
return config;
},
}; allows for aliased imports such |
We're using Vite and ts project references and something like; viteFinal: async (config) => {
config.resolve.alias = {
...config.resolve.alias,
'@some-alias': path.resolve(__dirname, '../path/to/external/dir'),
};
return config;
}, worked for us! |
I added .storybook/main.js const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin')
module.exports = {
...,
webpackFinal: async (config, {configType}) => {
...
config.resolve.plugins = [
...config.resolve.plugins,
new TsconfigPathsPlugin(),
]
return config
}, |
Describe the bug
Storybook can't resolve aliases. The build crashes.
To Reproduce
Expected behavior
Storybook should build files with aliases imports.
Code snippets
My storybook config:
My webpack config:
My babel config:
System
System:
OS: macOS High Sierra 10.13.4
CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
Binaries:
Node: 12.7.0 - /usr/local/bin/node
Yarn: 1.16.0 - /usr/local/bin/yarn
npm: 6.13.6 - /usr/local/bin/npm
Browsers:
Chrome: 88.0.4324.192
Firefox: 84.0.2
Safari: 11.1
npmPackages:
@storybook/react: ^6.1.20 => 6.1.20
The text was updated successfully, but these errors were encountered: