Unveiling the power of experimental decorators in Storybook 8 with NextJS/SWC

I am facing an issue with experimental class decorators in my code, causing the Storybook build to crash.

Module build failed (from ./node_modules/@storybook/nextjs/dist/swc/next-swc-loader-patch.js):
Error:
  × Expression expected

Despite reading the confusing SWC documentation in the Storybook docs, I am using Storybook 8 with the "@storybook/nextjs" framework. Upon launching Storybook, the console message shows "info => Using SWC as compiler", even though SWC is not supposed to be enabled by default, and I have not enabled it in the configuration. Why is SWC being used?

Following the SWC and Storybook docs, I have added the following code to my Storybook main.ts file:

const config: StorybookConfig = {
...
swc: (config: Options, options): Options => {
        return {
            ...config,
            jsc: {
                parser: {
                    decorators: true
                }
            }
        };
    },
}

However, the code above is not being executed. I could throw an exception there, but it is not being thrown.

How can I enable decorator support with Storybook 8 and NextJs?

Answer №1

Here is an interesting TypeScript code snippet:

// The following TypeScript code is an attempt to set up Storybook with SWC for decorators with updated type definitions.
import { StorybookConfig } from '@storybook/nextjs';
import { Options } from '@swc/core';

const config: StorybookConfig = {
  stories: [],
  addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
  framework: '@storybook/nextjs',
  core: {
    builder: '@storybook/builder-webpack5'
  },
  typescript: {
    check: false,
    reactDocgen: 'react-docgen-typescript',
    reactDocgenTypescriptOptions: {
      shouldExtractLiteralValuesFromEnum: true,
      propFilter: (prop) => (prop.parent ? !/node_modules/.test(prop.parent.fileName) : true),
    },
  },
  swc: (config: Options, options: Options): Options => {
    return {
      ...config,
      jsc: {
        parser: {
          syntax: 'typescript',
          decorators: true,
          dynamicImport: true
        },
        transform: {
          legacyDecorator: true,
          decoratorMetadata: true
        }
      }
    };
  }
};

console.log('SWC Configuration for decorators:', config.swc);

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

The Next.js application encounters a crash when trying to integrate Google Firebase authentication

I'm encountering an issue while trying to integrate authentication using firebase (via Google) in my next.js application, and the app crashes consistently. I will provide the code for the auth.js page component, as well as where I set up firebase and ...

The `createAction` function does not preserve the data type when used with `ofType`

I'm currently developing a mobile application that allows users to choose snacks from a list of available options retrieved from an external API. To handle actions and dispatch API requests, I am utilizing redux-observable. Below is my existing code, ...

Why is my NextJs app loading slowly on Safari but quickly on Chrome?

Currently, I am in the process of developing a web app using nextjs. I have encountered some issues with linking to pages, particularly the home page which contains multiple svgs and images. The performance lags when using Safari, as it does not show a loa ...

What is the solution for this problem in TypeScript involving an API service call?

Trying to utilize the API Service to fetch data and display the response as an object created by a class constructor Currently executing a Typescript code that interacts with the API Service import * as request from "request"; import { Users } from "./Us ...

I need to access the dynamic parameter/slug in the URL within an API Route Handler located in the app folder of Next.js. How can I achieve

Incorporating Next.js 13 into the app directory, I've explored various methods but haven't been able to find comprehensive documentation on this. My goal is to retrieve data from the URL /api/stripe/${sessionId} to a server-side API path structu ...

How can I preloaded self-hosted fonts in Next.js without receiving a console warning for not using the resource within a few seconds?

I am working on a project in Next.js and I want to preload my self-hosted fonts. In my code, I have the following setup: Inside my _app.js file: <Head> <link rel="preload" href="/fonts/leira/Leira-Lite.t ...

Exploring the world of child routing in Angular 17

I've been experimenting with child routing in Angular and encountered some confusion. Can someone explain the difference between the following two routing configurations? {path:'products',component:ProductsComponent,children:[{path:'de ...

Tips for getting Atom cucumber step jump package to function properly on a Windows operating system

Just recently, I installed the Atom text editor along with the cucumber-step package available at this link. However, after pressing CTRL+ALT+j, it failed to jump to the step implementation/definition. My operating system is Windows 10 and I am utilizing ...

Removing an Image from Amazon S3 in a Web Application using Next.js, Mongodb, and Mongoose

Currently, I am working on implementing a deleteImage function in my front end to interact with the backend through API routes. Below is the function I have for uploading images: const uploadImages = async (ev) => { const files = ev.target?.files; ...

The Vercel NextJS app seems to be experiencing issues with communicating with the Express API hosted on Her

Currently, I am developing a Next.js + Express application with Vercel for the Next.js portion and Heroku for the Express server. Localhost frontend and Localhost backend --> Operational ✔️ Localhost frontend and Heroku Hosted backend --> Op ...

Challenges with deploying Next.js and Express with Zeit Now

I recently developed a SSR Next.js webpage with an attached Express server for handling email functionalities related to a contact form. After deploying the project on now, I encountered a 502 error when making network requests to my Express server despit ...

Using Handlebars.js with Angular CLI versions 6 and above: A Step-by-Step Guide

Looking to create a customizable customer letter in either plain text or HTML format that can be edited further by the customer. Considering using Handlebars.js to render an HTML template with mustache tags, using a JSON object for business information. T ...

Tips for getting rid of Next.js' default loading indicator

While working on my project using Next.js, I've noticed that whenever I change the route, a default loading indicator appears in the corner of the screen. https://i.sstatic.net/FVWEU.gif Does anyone know how to remove this default loading indicator ...

Ensure that the input field consistently shows numbers with exactly two decimal places

Below is an input field that I want to always display the input value with 2 decimal places. For example, if I input 1, it should show as 1.00 in the input field. How can this be achieved using formControl since ngModel is not being used? Thank you. I att ...

When toggling between light and dark themes using the useMediaQuery hook, the Material-ui styling is being overridden

While working with next.js and material-ui, I encountered an issue where the theme would change based on user preference. However, when switching to light mode, the JSS Styles that I had set were being overwritten. This problem only seemed to occur in ligh ...

Looking for assistance with properly updating pm2 in my Next.js project

When I update my NextJS app on ubuntu using Nginx and pm2, I typically add new text to my codebase. Here's the process I follow: git add . git commit -m 'new commit' git push After making changes, I navigate to my file directory in the cons ...

Error: The property 'count' cannot be destructured from '(0, react_redux__WEBPACK_IMPORTED_MODULE_3__.useSelector)(...)' because it is not defined

After watching this helpful tutorial: https://www.youtube.com/watch?v=iBUJVy8phqw I attempted to incorporate Redux into my Next.js app, following the steps outlined. However, I encountered a roadblock and can't figure out why. I've double-checke ...

Identify any missing periods and combine the years into a single range

I am working on restructuring year ranges with gaps and consolidating them. For example, converting [{start: 2002, end: 2020}, {start: 2020, end: null}] to {start: 2002, end: null} or [{2002, 2004},{2006, 2008}, {2008, null}] to [{2002-2004}, {2006-null}]. ...

How to use $$[n] in Spectron/WebdriverIO to target the nth child element instead of the selector

Attempting to utilize Spectron for testing my Electron application has been challenging. According to the documentation, in order to locate the nth child element, you can either use an nth-child selector or retrieve all children that match a selector using ...

Authentication based on user roles in a Next.js application

I have created a middleware for role-based authentication in Next.js to restrict access to certain routes based on user roles. import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; import { Roles } ...