question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

alias not working

See original GitHub issue

main.js

const path = require("path")
const reactRefresh = require("@vitejs/plugin-react-refresh")

module.exports = {
    stories: [
        // "../src/**/*.stories.@(js|jsx|ts|tsx)"
        "../src/components/**/*.stories.@(ts|tsx)",
    ],
    // addons: ["@storybook/addon-links", "@storybook/addon-essentials"],
    core: {
        builder: "storybook-builder-vite",
    },
    resolve: {
        alias: [
            {
                find: "@",
                replacement: path.resolve(__dirname, "./src"),
            },
            {
                find: "@assets",
                replacement: path.resolve(__dirname, "./src/assets"),
            },
            {
                find: "@components",
                replacement: path.resolve(__dirname, "../src/components"),
            },
        ],
    },
    async viteFinal(config, { configType }) {
        // customize the Vite config here
        config.resolve.alias.foo = "bar"

        reactRefresh()

        return config
    },
}

If use yarn run not storybook, config like https://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.ts

Requirement:

[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

import { defineConfig } from "vite"
// import tsconfigPaths from "vite-tsconfig-paths"
import reactRefresh from "@vitejs/plugin-react-refresh"
// import envCompatible from "vite-plugin-env-compatible"
import path from "path"

export default defineConfig({
    plugins: [reactRefresh()],
    build: {
        outDir: "build",
    },
    // alias: {
    //     "@": path.resolve(__dirname, "./src"),
    // },
    // resolve: {
    alias: [
        {
            find: "@",
            replacement: path.resolve(__dirname, "./src"),
        },
        {
            find: "@assets",
            replacement: path.resolve(__dirname, "./src/assets"),
        },
        {
            find: "@components",
            replacement: path.resolve(__dirname, "./src/components"),
        },
    ],
    // },
    // plugins: [tsconfigPaths(), envCompatible(/* options */), reactRefresh()],
})

tsconfig.json

{
    "compilerOptions": {
        "allowJs": true,
        "allowSyntheticDefaultImports": true,
        "baseUrl": "src",
        "paths": {
          "@components/*": ["./components/*"],
          "@assets/*": ["./assets/*"],
        },
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "isolatedModules": true,
        "jsx": "react-jsx",
        "lib": ["dom", "dom.iterable", "esnext"],
        "module": "esnext",
        "moduleResolution": "node",
        "noEmit": true,
        "noFallthroughCasesInSwitch": true,
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "strict": true,
        "target": "es5",
        "types": ["node", "vite/client"]
    },
    "include": ["./src"]
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

123reactions
ThinCatscommented, Aug 18, 2021

In the function viteFinal(config, { configType }), the parameter config is the config object for vite, which is used by storybook-builder-vite. You can return a new config object.

// 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 mergeConfig and loadConfigFromFile exposed by vite.

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: [],
    });
  },
};

11reactions
eirslettcommented, Aug 18, 2021

☝️ this is the right answer

Read more comments on GitHub >

github_iconTop Results From Across the Web

why alias names defined in .bashrc file are not working?
Usually your aliases in .bashrc are defined before the /etc/bashrc call. Try to define them after. Here an example of your .bashrc ...
Read more >
Why is my alias now not working? - command line - Ask Ubuntu
You have a space between the = and the opening ' . Remove it. $ alias foo= 'bar baz' bash: alias: bar baz:...
Read more >
My Bash aliases don't work - Stack Overflow
bash_aliases is sourced from the default .bashrc on Ubuntu boxes; I don't have access to one to confirm. However, it is not a...
Read more >
kali linux - Why bash aliases commands are not working?
When you source ~/.bash_aliases nothing happens because you are sourcing a file that doesn't have your alias up.
Read more >
A Quick Guide on How to Use Bash Alias - Shell Tips!
This post cover the Bash builtin commands alias and unalias. You will often find Bash alias in a customized .bashrc to improve your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found