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

[Vite] Noisy error “Module level directives cause errors when bundled, "use client" was ignored.” when running remix vite:build on a project that includes a dependency file which includes the "use client" directive. #8891

Closed
dtinth opened this issue Feb 26, 2024 · 5 comments

Comments

@dtinth
Copy link

dtinth commented Feb 26, 2024

Reproduction

If I got more time, I may submit a PR with a failing test, but for now, here’s an issue.

To reproduce:

git clone https://github.com/dtinth/fewshot
cd fewshot
git checkout 82d6437be436f23c506294998d85a5222501a22d
corepack enable
pnpm install
pnpm run build

System Info

System:
    OS: macOS 14.2.1
    CPU: (8) arm64 Apple M1
    Memory: 53.09 MB / 16.00 GB
    Shell: 3.6.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 21.5.0 - ~/.asdf/installs/nodejs/21.5.0/bin/node
    npm: 10.2.4 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 8.15.4 - /opt/homebrew/bin/pnpm
    bun: 1.0.29 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 101.1.38.111
    Chrome: 121.0.6167.184
    Edge: 122.0.2365.52
    Safari: 17.2.1
  npmPackages:
    @remix-run/dev: ^2.7.2 => 2.7.2 
    @remix-run/node: ^2.7.2 => 2.7.2 
    @remix-run/react: ^2.7.2 => 2.7.2 
    vite: ^5.1.0 => 5.1.4

Used Package Manager

pnpm

Expected Behavior

Build output should be short. Vite should not complain about "use client" directive.

Note that there is a similar issue in another project.

Based on the above issue, @vitejs/plugin-react should suppress this error for us. However, it seems that Remix’s vitePlugin does not come with Vite’s React plugin and implements its own React support.

Perhaps Remix’s Vite plugin should add the warning silencer just like @vitejs/plugin-react? The warning silencer seems to implemented here: https://github.com/vitejs/vite-plugin-react/blob/814ed8043d321f4b4679a9f4a781d1ed14f185e4/packages/plugin-react/src/index.ts#L303-L319

Actual Behavior

I get a wall of warning in the build output that looks like this:

image

@wKovacs64
Copy link
Contributor

It would be nice if it was handled automatically, but I've been doing this in the meantime:

// vite.config.ts
import { defineConfig } from "vite";

export default defineConfig({
  build: {
    rollupOptions: {
      onwarn(warning, warn) {
        // Suppress "Module level directives cause errors when bundled" warnings
        if (warning.code === "MODULE_LEVEL_DIRECTIVE") {
          return;
        }
        warn(warning);
      },
    },
  },
  // your other Vite config stuff...
});

You probably already found something similar, but dropping it here just in case it's helpful for somebody.

@markdalgleish
Copy link
Member

Thanks for raising this. Fixed by #8897.

Copy link
Contributor

🤖 Hello there,

We just published version 2.8.0-pre.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Copy link
Contributor

🤖 Hello there,

We just published version 2.8.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@anztrax
Copy link

anztrax commented Jul 9, 2024

It would be nice if it was handled automatically, but I've been doing this in the meantime:

// vite.config.ts
import { defineConfig } from "vite";

export default defineConfig({
  build: {
    rollupOptions: {
      onwarn(warning, warn) {
        // Suppress "Module level directives cause errors when bundled" warnings
        if (warning.code === "MODULE_LEVEL_DIRECTIVE") {
          return;
        }
        warn(warning);
      },
    },
  },
  // your other Vite config stuff...
});

You probably already found something similar, but dropping it here just in case it's helpful for somebody.

awesome it's works , thanks bro 👍

radnan added a commit to radnan/optiaxiom that referenced this issue Jul 10, 2024
radnan added a commit to optimizely-axiom/optiaxiom that referenced this issue Jul 10, 2024
sungik-choi pushed a commit to channel-io/bezier-react that referenced this issue Nov 4, 2024
components that use react hook or memo...

<!--
  How to write a good PR title:
- Follow [the Conventional Commits
specification](https://www.conventionalcommits.org/en/v1.0.0/).
  - Give as much context as necessary and as little as possible
  - Prefix it with [WIP] while it’s a work in progress
-->

## Self Checklist

- [x] I wrote a PR title in **English** and added an appropriate
**label** to the PR.
- [x] I wrote the commit message in **English** and to follow [**the
Conventional Commits
specification**](https://www.conventionalcommits.org/en/v1.0.0/).
- [x] I [added the
**changeset**](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md)
about the changes that needed to be released. (or didn't have to)
- [x] I wrote or updated **documentation** related to the changes. (or
didn't have to)
- [x] I wrote or updated **tests** related to the changes. (or didn't
have to)
- [x] I tested the changes in various browsers. (or didn't have to)
  - Windows: Chrome, Edge, (Optional) Firefox
  - macOS: Chrome, Edge, Safari, (Optional) Firefox

## Related Issue
#2456 (not close or fixes)
<!-- Please link to issue if one exists -->

<!-- Fixes #0000 -->

## Summary

<!-- Please brief explanation of the changes made -->
- Add `use client` directives

## Details

<!-- Please elaborate description of the changes -->
- I add `use client` directives to all component filds
- I didn't see any code that used the window API.
- ~~I am not sure about files that `Radix` exports without additional
logic.~~
- As suggested in the comments, I tested it in the code sandbox and
found no problems.
- Currently, directives have been added including this one. When I
checked, I saw that hooks, etc. were used in the internal
implementation.
```typescript
// page.tsx
import * as Dialog from "@radix-ui/react-dialog";
import * as Tooltip from "@radix-ui/react-tooltip";
import * as Separator from "@radix-ui/react-separator";
import * as Switch from "@radix-ui/react-switch";
import * as VisuallyHidden from "@radix-ui/react-visually-hidden";

export default function Home() {
  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      {/* code with radix-ui ...*/}
    </main>
  );
}
```
- Add the changeset
- To ignore `Module level directives cause errors when bundled`
warnings, I returned onWarn early when the warning appeared.It seemed
appropriate to write it inside the `generateConfig`.

### Breaking change? (Yes/No)

<!-- If Yes, please describe the impact and migration path for users -->
No

## References

<!-- Please list any other resources or points the reviewer should be
aware of -->
remix-run/remix#8891
rollup/rollup#4699
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

4 participants