Guide on creating path aliases for Storybook webpack configuration

Currently, I am integrating Storybook with nextjs and webpack. Below is my configuration in .storybook/main.ts:

import type { StorybookConfig } from '@storybook/nextjs';

const config: StorybookConfig = {
  ...
  framework: {
    name: '@storybook/nextjs',
    options: {},
  },
  ...
};

export default config;

This setup seamlessly utilizes TypeScript settings from the tsconfig.json within this workspace, which is a great feature. My goal is to introduce some TypeScript path aliases exclusively for Storybook. Despite attempting various strategies inspired by multiple responses on Stack Overflow, such as defining aliases like @/components, Storybook continues to throw errors.

const config: StorybookConfig = {
  ...
  webpackFinal: (config) => {
    if (config.resolve) {
      config.resolve.alias = {
        ...config.resolve.alias,
        '@/*': [
          './src/*',
          '../../packages/ui-lib/src/*',
        ],
      };
    }

    return config;
  },
};

Interestingly, when I include these aliases directly in my tsconfig.json file, Storybook functions correctly. However, I prefer not to do so because it would mean exposing the source code structure of the ui-lib folder to the overall workspace's tsconfig.

Can anyone shed light on why this approach is failing?

P.S. I also experimented with alternative path configurations like

path.resolve(__dirname, "../../packages/ui-lib/src")
, but unfortunately, that did not yield positive results either.

Answer №1

Have you ever used the tsconfig-paths-webpack-plugin before?

I faced a similar issue and resolved it with the following steps:

yarn add -D tsconfig-paths-webpack-plugin
npm install -D tsconfig-paths-webpack-plugin
//app.ts
import type { WebpackConfig } from "@webpack/webpack5";

const TsPathsPlugin = require("tsconfig-paths-webpack-plugin");

const webpackSettings: WebpackConfig = {
 //...
   optimizeWebpack: async (settings) => {
    if (settings.resolve) {
      settings.resolve.plugins = [
        ...(settings.resolve.plugins || []),
        new TsPathsPlugin({
          extensions: settings.resolve.extensions,
        }),
      ]
    }
    return settings;
  },
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Halt or Keep Running a For Loop in Angular 2/4?

for (let index = 0; index < this.selectedFileType[i].count; index++) { this.modal.show(); } My goal is to display the modal each time it enters the loop, and then proceed with the loop after closing the modal. ...

Allow access to web application API without requiring an API key

Our team is currently in the process of developing a single-page web application with various components: A NextJS container A Django backend for user management A FastAPI Data API that is secured with API keys and also grants access to third parties The ...

"Exploring the new features of Next.js 13: Server-side actions

In my current project using Nextjs 13 and server actions, there is a form in place: const [isUploadingDocs, setIsUploadingDocs] = useState<boolean>(false); ... remaining code ... return ( <form action={async (formData) => { setIsUploadingDo ...

Initiate an interactive pathway using @

Within my NextJS application, utilizing the pages router, there is a specific file called @[username].tsx. This format mirrors YouTube's user profile links and resides in the pages folder. Despite this setup, attempting to access /@lordsarcastic resul ...

Tips for refreshing the 'state' of an Angular Router component

I'm facing an issue with passing data between pages using Angular routing. Everything works as expected when navigating to the "next" page for the first time. However, upon returning to the home page and selecting a different item, the Router's s ...

The Mantine date picker is causing an error stating that objects are not valid as a React child

I'm currently experimenting with utilizing Mantine Date in NextJS. The goal is to have the selected date displayed in the HTML text heading when a user clicks on it. For instance, if a user selects January 1st, 2023, the text should show like this: Da ...

What is the process for importing a .gltf/.glb model into a Three.js application?

After browsing through several related topics on SO, I'm still unable to find a solution to my current issue. The problem I'm facing is that the .glb model simply refuses to load. My Vue application utilizes webpack (specifically the Quasar frame ...

Using Firebase with Angular 4 to fetch data from the database and show it in the browser

Currently diving into Angular 4 and utilizing Firebase database, but feeling a bit lost on how to showcase objects on my application's browser. I'm looking to extract user data and present it beautifully for the end-user. import { Component, OnI ...

Changing the name of a tab within a p-tabview

Setting up a p-tabview with tabs containing specific content involves the following code: <p-tabView class="tabmain" > <ng-container *ngFor="let tab of tabs"> <p-tabPanel [header]="tab.header" > ...

The createContext function mistakenly used the default value instead of the value that was supposed to

When I import and use a context that allows child components to setTheme, the useContext hook returns the default value instead of the passed value. What could be causing this issue and how can I resolve it? I apologize for any poor English in my explana ...

Pull in class definitions from the index.js file within the node_modules directory

In my project, I have the package called "diagram-js" in the node_modules. The file node_modules/diagram-js/lib/model/index.js contains multiple class definitions as shown below: /** * @namespace djs.model */ /** * @memberOf djs.model */ /** * The b ...

Using Highcharts to dynamically color a map based on data values in a React TypeScript project

I'm attempting to generate a map where each country is filled with colors based on its specific data, similar to the example shown in this map. I am looking for a functionality akin to the use of the formatter function within the tooltip. I have expe ...

encountering a glitch during the electron.js build process with nextjs

When attempting to build Electron.js with Next.js, I keep encountering this persistent error. I have updated my packages and reinstalled node modules multiple times, but I am still unable to resolve it. C:\Users\Himanshu\Desktop\claros& ...

Angular 8: Implementing Form Validation with a Boolean Flag

Within my HTML code, I have a function (change)="limitUser($event)". In Typescript, I utilize a for loop to iterate through each element and determine if the value is less than 10. If it exceeds 10, the inValid = true condition is set. All form fields in m ...

Error code TS2749 is indicating that the identifier 'XXX' is being interpreted as a value instead of a type. Perhaps you intended to use 'typeof XXX' instead

I've encountered a strange issue while running my npm run dev command in my project built with Nuxt.js, which includes Vue.js components. While launching the application, I'm encountering errors related to TypeScript like TS2749: 'About&apos ...

An easy guide to preloading header images in Next 13

My goal is to ensure that the page only renders once the header image has completely loaded, avoiding any blank space during the loading process. I'm currently utilizing the Next.JS Image component with the code import Image from 'next/image&apo ...

Using a modulus operator in Angular 6 to conditionally show elements

Trying to achieve a specific layout for an object in an array using ng-For and ng-If. Here is the desired recovery code, and here's what I've attempted so far in my code snippet enter image description here: <div class="recovery-code" *ngFor= ...

Vue3 with Typescript may either display an error message or remain empty when handling props

I've been attempting to utilize the default Quasar card component in order to display data received from props. Unfortunately, I haven't had any success as my component remains empty and I keep encountering various errors with each attempt. Rece ...

Struggling with the Transition from Google Sign-In

Having difficulty transitioning from Google Sign-In. "{error: 'idpiframe_initialization_failed', details: 'You have created a new client application that use…i/web/guides/gis-migration) for more information.'}" How do I u ...

Encountering intellisense problems while declaring or utilizing TypeScript types/interfaces within Vue.js Single File Component (SFC) documents

For my Nuxt 3 project, I am developing a component and attempting to declare an interface for the component props to ensure strong typings. Here is an example: <script setup> interface Props { float: number; } const props = defineProps<Props> ...