-
Notifications
You must be signed in to change notification settings - Fork 106
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
alias not working #85
Comments
In the function // main.js module.exports = {
async viteFinal(config, { configType }) {
return {
...config,
resolve: {
alias: [
{
find: "@",
replacement: path.resolve(__dirname, "./src"),
},
{
find: "@assets",
replacement: path.resolve(__dirname, "./src/assets"),
},
{
find: "@components",
replacement: path.resolve(__dirname, "./src/components"),
},
],
},
};
},
}; If you want to reuse the project's vite.config.js, you can use the API const { loadConfigFromFile, mergeConfig } = require("vite");
module.exports = {
...
async viteFinal(config, { configType }) {
const { config: userConfig } = await loadConfigFromFile(
path.resolve(__dirname, "../vite.config.ts")
);
return mergeConfig(config, {
...userConfig,
// manually specify plugins to avoid conflict
plugins: [],
});
},
}; |
☝️ this is the right answer |
The above answer for // .storybook/main.js
const path = require("path");
const tsconfigPaths = require("vite-tsconfig-paths").default;
module.exports = {
"stories": [
"../frontend/**/*.stories.mdx",
"../frontend/**/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions"
],
"framework": "@storybook/react",
"core": {
"builder": "storybook-builder-vite"
},
/**
* A option exposed by storybook-builder-vite for customising the Vite config.
* @see https://github.com/eirslett/storybook-builder-vite#customize-vite-config
* @param {import("vite").UserConfig} config
* @see https://vitejs.dev/config/
*/
viteFinal: async (config) => {
config.plugins.push(
/** @see https://github.com/aleclarson/vite-tsconfig-paths */
tsconfigPaths({
// My tsconfig.json isn't simply in viteConfig.root,
// so I've passed an explicit path to it:
projects: [path.resolve(path.dirname(__dirname), "frontend", "tsconfig.json")],
})
);
return config;
},
} |
Perhaps const { config: userConfig } = await loadConfigFromFile(configType,
path.resolve(__dirname, "../src/client/vite.config.ts")
); |
My config is somehow neater than what's mentioned but still works: https://stackoverflow.com/a/73105465/9814737 const tsconfigPaths = require("vite-tsconfig-paths");
...
module.exports = {
...
async viteFinal(config) {
return {
...config,
plugins: [...config.plugins, tsconfigPaths.default()],
};
},
}; |
I came across a similar problem recently but for plugins not pulling through and fixed it with the following: async viteFinal(config) {
// pull in our root config
const { config: mainConfig } = await loadConfigFromFile(
path.resolve(__dirname, "../vite.config.ts")
);
// we need to exclude @vitejs/plugin-react from the plugins to prevent a conflict
// @vitejs/plugin-react is an array of objects so we can exclude it by checking for a name key
const filteredPlugins = mainConfig.plugins.filter(item => item.name);
return mergeConfig(config, {
resolve: {
alias: { "@": path.resolve(__dirname, "../src") },
},
plugins: [...filteredPlugins]
});
}, |
Where does |
It can be destructured from the second callback parameter of async viteFinal(config, { configType }) { |
import * as path from 'path'
import tsconfigPaths from 'vite-tsconfig-paths'
// or
const path = require('path');
const tsconfigPaths = require('vite-tsconfig-paths').default;
viteFinal: async (config) => {
config.plugins.push(
/** @see https://github.com/aleclarson/vite-tsconfig-paths */
tsconfigPaths({
projects: [path.resolve(path.dirname(__dirname), 'tsconfig.json')],
})
);
return config;
}, |
I was facing an error when trying to use I solved it by importing I hope it helps anyone facing the same issue. |
Good Work! "vite": "^4.0.0",
"@storybook/react": "^6.5.16", |
Also note that storybook 7.0 will use your project's vite.config file automatically. |
I am using storybook@7.6.3, and I had to implement @Adam-Collier's solution because storybook did not pick up my alias as defined in my vite.config.ts. Tests, tsc, and eslint all did, however. |
@dshelters do you use a nonstandard project directory structure? If you could share a reproduction I can take a quick look. |
@IanVS I'm just created a new project from scratch with the latest version and I can confirm that storybook is not getting my aliases as defined in my vite.config.mts |
My problem was related to reactDocgenTypescriptOptions: {
// Speeds up Storybook build time
compilerOptions: {
baseUrl: 'src', // newly added
allowSyntheticDefaultImports: false,
esModuleInterop: false,
},
// Makes union prop types like variant and size appear as select controls
shouldExtractLiteralValuesFromEnum: true,
// Makes string and boolean types that can be undefined appear as inputs and switches
shouldRemoveUndefinedFromOptional: true,
// Filter out third-party props from node_modules except @mui packages
propFilter: (prop) =>
prop.parent ? !/node_modules\/(?!@mui)/.test(prop.parent.fileName) : true,
}, |
main.js
If use
yarn run
not storybook, config likehttps://github.com/vitejs/vite/issues/279
work run.But i believe vite.config.ts != main.js. This plugin can't handle some
features
like vite.config.tsRequirement:
[1] show case how to use
reactRefresh
plugin like we did in vite.config.ts. Cuz reactRefresh is a must for react to work in ite. In vite. Also mention, there is another plugin@vite/vue
should be imported if develop vue.[2] support
vite-tsconfig-paths
in storybook. So, we can have alias support (there may be quite way to solve, this is the major feature needs to be, for storybook to work with alias)vite.config.ts
tsconfig.json
The text was updated successfully, but these errors were encountered: