Skip to content
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

[Bug]: Storybook 7 does not work with @vitejs/plugin-react-swc #22381

Closed
emildudnyk opened this issue May 3, 2023 · 10 comments
Closed

[Bug]: Storybook 7 does not work with @vitejs/plugin-react-swc #22381

emildudnyk opened this issue May 3, 2023 · 10 comments

Comments

@emildudnyk
Copy link

Describe the bug

Hello, if we are using @vitejs/plugin-react-swc instead of @vitejs/plugin-react in the vite config vite.config.ts storybook
is failing to build.

Error: Build failed with 4 errors:
../node_modules/react-textarea-autosize/dist/react-textarea-autosize.browser.esm.js:3:67: ERROR: Could not resolve "react"
../node_modules/use-composed-ref/dist/use-composed-ref.esm.js:1:36: ERROR: Could not resolve "react"
../node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.js:1:32: ERROR: Could not resolve "react"
../node_modules/use-latest/dist/use-latest.esm.js:1:23: ERROR: Could not resolve "react"

I tried to use @vitejs/plugin-react in viteFinal config section but it doesn't solve the problem.

Temporary solution is to remove vite:react-swc plugins and use @vitejs/plugin-react for example:

import react from '@vitejs/plugin-react';

const removePlugin = (plugins: PluginOption[] | undefined, removePluginName: string): PluginOption[] | undefined => {
  if (!plugins) {
    return undefined;
  }
  return plugins.reduce<PluginOption[]>((acc, cur) => {
    if (Array.isArray(cur)) {
      acc.push(removePlugin(cur, removePluginName));
    } else {
      if (!(cur as Plugin)?.name?.includes(removePluginName)) {
        acc.push(cur);
      }
    }
    return acc;
  }, []);
};

const config: StorybookConfig = {
  async viteFinal(config) {
    config.plugins = removePlugin(config.plugins, 'vite:react-swc');

    return mergeConfig(config, {
      plugins: [react()],
    });
  },
...
}

Could you please suggest a solution.

To Reproduce

https://github.com/emildudnyk/storybook7-react-vite-swc
yarn install & yarn storybook

System

System:
    OS: macOS 13.1
    CPU: (10) x64 Apple M1 Pro
  Binaries:
    Node: 16.17.1 - ~/.nvm/versions/node/v16.17.1/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 9.6.3 - ~/.nvm/versions/node/v16.17.1/bin/npm
  Browsers:
    Chrome: 112.0.5615.137
    Firefox: 108.0.1
    Safari: 16.2
  npmPackages:
    @storybook/addon-actions: 7.0.8 => 7.0.8 
    @storybook/addon-essentials: 7.0.8 => 7.0.8 
    @storybook/addon-interactions: 7.0.8 => 7.0.8 
    @storybook/addon-links: 7.0.8 => 7.0.8 
    @storybook/addon-mdx-gfm: 7.0.8 => 7.0.8 
    @storybook/react: 7.0.8 => 7.0.8 
    @storybook/react-vite: 7.0.8 => 7.0.8 
    @storybook/testing-library: 0.1.0 => 0.1.0

Additional context

No response

@shilman
Copy link
Member

shilman commented May 8, 2023

@IanVS is this something you've run into before?

@IanVS
Copy link
Member

IanVS commented May 8, 2023

I pulled down the reproduction, ran yarn install then yarn storybook and yarn build-storybook, and it's all working fine for me. The @vitejs/plugin-react-swc should indeed be supported.

@connorjs
Copy link

@storybook/react-vite declares a dependency on @vitejs/plugin-react.

Is it possible to make this an optional peerDependency and add @vitejs/plugin-react-swc as an alternative, optional peedDependency?

Let me know if I should make a separate issue. I'm currently writing notes on my own side about what all brings in babel b/c that really explodes our installed node_modules.

@IanVS
Copy link
Member

IanVS commented Nov 27, 2023

@connorjs the reason for this is to provide a default, fallback experience for users who do not have their own existing vite config. That plugin is only used in that situation, and unfortunately it needs to be a dependency for it to work that way.

That said, there have been discussions amongst the maintainers to change the way this is handled in 8.0, and we are leaning towards not bundling such framework vite plugins, and instead helping the user to create a vite config and install an appropriate plugin if they do not already have a config already (e.g. some design systems do not have their own vite config).

@connorjs
Copy link

Thanks for sharing some 8.0 thoughts! That sounds similar to my thinking.

@connorjs
Copy link

connorjs commented Feb 7, 2024

I am playing around with using overrides field in package.json.

"overrides": {
  "@vitejs/plugin-react": "@vitejs/plugin-react-swc@^3.6.0",
}

Storybook seemed to run fine, and this prevented @vitejs/plugin-react from being installed ("extra" dependency + Storybook's current version of it (3.x) conflicted with Vite 5 it seemed).

Sharing more for "oh no, please don't do that!" replies if applicable 😅.

@IanVS
Copy link
Member

IanVS commented Feb 7, 2024

Clever. I think that should work fine, @connorjs, so long as you don't need to provide any arguments to @vitejs/plugin-react-swc.

The other thing you can do is to just add @vitejs/plugin-react-swc to your own vite.config.js, in which case the fallback @vitejs/plugin-react won't be used (though it will still throw a warning during installation, which you can safely ignore).

@IanVS
Copy link
Member

IanVS commented Feb 7, 2024

Oh, and the 8.0 beta has been released, which has removed the fallback plugins as discussed above. Please try it out if you can, and report any issues you come across!

@valentinpalkovic
Copy link
Contributor

I am closing this since it is fixed on the latest Storybook 8 beta release and will not be an issue anymore in Storybook 8. There are no plans to patch this back, though. If you'd like to fix the bug in Storybook 7, you can use the mentioned workarounds.

wesleyboar added a commit to TACC/tup-ui that referenced this issue Feb 26, 2024
STEPS
1. `npm add -D @nx/storybook@17.2.8`
2. `npx nx@17.2.8 g @nx/storybook:configuration core-components --uiFramework="@storybook/react-vite" --bundler="vite"`
3. "Do you want to set up Storybook interaction tests? (Y/n) ›  false"

CONCERNS
1. Generator installed `@swc-node/register` and `@swc/core`.
2. `@vitejs/plugin-react-swc` is already isntalled.
3. [Storybook 7 does not work with @vitejs/plugin-react-swc](storybookjs/storybook#22381)
4. I worry I cannot move to Storybook 8 via NX, because it requires NX v18.
@kaceycleveland
Copy link

I had some tangentially related issues to this that were fixed when switching to swc; for others who might be in the same boat and want to wait until 8.0, this was my fix in altering the vite config from storybook (factor out the non swc vite plugin and use the swc one):

  async viteFinal(config) {
    const finalConfig = mergeConfig(
      {
        plugins: [
          react(), // this is the swc plugin
        ],
      },
      config,
    );

    finalConfig.plugins = finalConfig.plugins?.filter((plugin: any) => {
      if (Array.isArray(plugin) && plugin[0].name === 'vite:react-babel') return false;
      return true;
    });

    return finalConfig;
  },

wesleyboar added a commit to TACC/tup-ui that referenced this issue May 16, 2024
* feat: tup-700 storybook

STEPS
1. `npm add -D @nx/storybook@17.2.8`
2. `npx nx@17.2.8 g @nx/storybook:configuration core-components --uiFramework="@storybook/react-vite" --bundler="vite"`
3. "Do you want to set up Storybook interaction tests? (Y/n) ›  false"

CONCERNS
1. Generator installed `@swc-node/register` and `@swc/core`.
2. `@vitejs/plugin-react-swc` is already isntalled.
3. [Storybook 7 does not work with @vitejs/plugin-react-swc](storybookjs/storybook#22381)
4. I worry I cannot move to Storybook 8 via NX, because it requires NX v18.

* deps: tup-700 uninstall `@swc-…`

- Vite already uses `@vitejs/plugin-react-swc` (notice the SWC).
- Storybook seems to work fine without these.
- I read that in v8, Storybook will stop isntalling backup compilers.

* feat: tup-700 Button stories, working Controls

* fix: tup-700 ignore stories during build

* fix: nx serve/build/test core-components

* feat: tup-700 button story

* style: npx nx format:write

* docs(tup-ui): tup-700 README "Contributing"

* fix: tup-700 npm ci core-components workspace ★

Running npm ci at this point:
- DOES reinstall deps
- does NOT create libs/core-components/node_modules
- does NOT create libs/core-components/package-lock.json

* npx nx format:write

* docs: do not reference lib names

This causes more work when renaming a library.

* docs: do reference (other) lib names

These references are outside of this lib, so yeah, reference lib name.

* docs(README): add `make start` step

* feat: make storybook optional

* feat: make storybook optional (not peer)

* docs(README): fix serve/start command

* fix: make storybook optional (not peer)

* chore: (redundantly) include optional dependences

Optional dependencies are installed by default.

We can not disable "install optional by default", cuz NX will fail:
nrwl/nx#20617

We can not configure NPM to omit optional deps only per workspace, cuz:
npm/cli#6099

* fix: limit install overhead for core-components

For core-components, tell dev to only install for core-components.

* fix: add missing dependencies for core-components

All this change was required to run `npx nx serve core-components`.

* fix: add missing `build-storybook` npm command

* fix: a TypeScript lint error

Error occurred as red squiggly udnerline in code editor when importing a component from within a component.

* fix: do not pass size to `<Button type="link">`

* feat: load global css via typescript not html
wesleyboar added a commit to TACC/tup-ui that referenced this issue May 16, 2024
* test(@tacc/core-components): TUP-700 (WIP)

* fix: tup-700 do not commit dist

* docs(README): tup-700 add `build core-components`

* feat: tup-700 peer dependencies

* feat: tup-700 build (no tested on a client yet)

* feat: tup-700 1st attempt to split files/css build

* fix: tup-700 add main file to dist

* build: tup-700 do not build tests

* fix: tup-700 add types & fix resulting errors

1. import & use `dts`
    - Fixes build error:
      > error TS2307: Cannot find module './….module.css' or its corresponding type declarations.
      > … import styles from './….module.css';

2. `"composite": true`
    - Fixes Typescript lint error:
      > Referenced project '/…/tup-ui/libs/core-components/tsconfig.lib.json' must have setting "composite": true.

3. `"declaration": true,`
    - Seemingly caused by `"composite": true`.
    - Fixes Vite build error:
    > \> nx run core-components:build:production
    > error TS6304: Composite projects may not disable declaration emit.

* fix: tup-700 tag types in package.json

* style: tup-700 move rollup opts input glob inline

* style: npx nx format:write

* style: tup-700 peerDependencies

"So the consuming application is aware that it must have React installed to use this package."
— https://dev.to/receter/how-to-create-a-react-component-library-using-vites-library-mode-4lma#dependencies

* feat: tup-700 add CSS as sideEffects

"To prevent the CSS files from being accidentally removed by the consumer's tree-shaking efforts…"
—https://dev.to/receter/how-to-create-a-react-component-library-using-vites-library-mode-4lma#side-effects

* feat: tup-700 ensure build before publish

* fix: tup-700 do 2d5ab1c, 6336f2c, 233f165 correct

Move the changes from thsoe commits to be in core-components package.json, not root package.json.

- style: tup-700 peerDependencies

    "So the consuming application is aware that it must have React installed to use this package."
    — https://dev.to/receter/how-to-create-a-react-component-library-using-vites-library-mode-4lma#dependencies

- feat: tup-700 add CSS as sideEffects

    "To prevent the CSS files from being accidentally removed by the consumer's tree-shaking efforts…"
    — https://dev.to/receter/how-to-create-a-react-component-library-using-vites-library-mode-4lma#side-effects

- feat: tup-700 ensure build before publish

* chore: tup-700 resolve vite.config.ts comment code

Resolve all commented code in `vite.config.ts`.

- `outDir` DELETED cuz `rollupOptions` > `output` exists
- `reportCompressedSize` RESTORED cuz why not & it was already there
- `commonjsOptions` & `transformMixedEsModules` DELETED cuz…
    - no `require` i.e. common js modules
- `lib`'s `name` and `fileName` DELETED cuz…
    - `rollupOptions` > `output` > `assetFileNames` exists
    - `rollupOptions` > `output` > `entryFileNames` exists

* fix: tup-700 scripts inaccurate

* docs: tup-700 README

* feat: tup-700 core-components deps via workspace

* fix: tup-700 core-components deps via workspace

* npx nx format:write

* fix: core-components/package-lock.json should not exist

Only 1 root package-lock.json in an Nx project such as tup-ui.

Having this may have been cause of errors when starting CMS.

* chore: rename Navbar → NavbarWrapper

* chore: rename FieldWrapperFormik → FormikFieldWrapper

* npx nx format:write

* chore: move withBuilder to core-components

* feat: move core-wrappers to core-components

Because the distinction between them is not accurate.

Both have components that import other components.

* lint: rename some index.js to index.ts

* lint: import via rel. path not lib name

* deps: add core-wrappers deps to peerDeps

* docs: do not reference lib names

This causes more work when renaming a library.

* docs: do reference (other) lib names

These references are outside of this lib, so yeah, reference lib name.

* docs(README): remove duplicate step

* feat: TUP-700 @tacc/core-components - Storybook (#435)

* feat: tup-700 storybook

STEPS
1. `npm add -D @nx/storybook@17.2.8`
2. `npx nx@17.2.8 g @nx/storybook:configuration core-components --uiFramework="@storybook/react-vite" --bundler="vite"`
3. "Do you want to set up Storybook interaction tests? (Y/n) ›  false"

CONCERNS
1. Generator installed `@swc-node/register` and `@swc/core`.
2. `@vitejs/plugin-react-swc` is already isntalled.
3. [Storybook 7 does not work with @vitejs/plugin-react-swc](storybookjs/storybook#22381)
4. I worry I cannot move to Storybook 8 via NX, because it requires NX v18.

* deps: tup-700 uninstall `@swc-…`

- Vite already uses `@vitejs/plugin-react-swc` (notice the SWC).
- Storybook seems to work fine without these.
- I read that in v8, Storybook will stop isntalling backup compilers.

* feat: tup-700 Button stories, working Controls

* fix: tup-700 ignore stories during build

* fix: nx serve/build/test core-components

* feat: tup-700 button story

* style: npx nx format:write

* docs(tup-ui): tup-700 README "Contributing"

* fix: tup-700 npm ci core-components workspace ★

Running npm ci at this point:
- DOES reinstall deps
- does NOT create libs/core-components/node_modules
- does NOT create libs/core-components/package-lock.json

* npx nx format:write

* docs: do not reference lib names

This causes more work when renaming a library.

* docs: do reference (other) lib names

These references are outside of this lib, so yeah, reference lib name.

* docs(README): add `make start` step

* feat: make storybook optional

* feat: make storybook optional (not peer)

* docs(README): fix serve/start command

* fix: make storybook optional (not peer)

* chore: (redundantly) include optional dependences

Optional dependencies are installed by default.

We can not disable "install optional by default", cuz NX will fail:
nrwl/nx#20617

We can not configure NPM to omit optional deps only per workspace, cuz:
npm/cli#6099

* fix: limit install overhead for core-components

For core-components, tell dev to only install for core-components.

* fix: add missing dependencies for core-components

All this change was required to run `npx nx serve core-components`.

* fix: add missing `build-storybook` npm command

* fix: a TypeScript lint error

Error occurred as red squiggly udnerline in code editor when importing a component from within a component.

* fix: do not pass size to `<Button type="link">`

* feat: load global css via typescript not html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants