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

'use client' #1049

Open
clementroche opened this issue Jun 19, 2023 · 10 comments
Open

'use client' #1049

clementroche opened this issue Jun 19, 2023 · 10 comments

Comments

@clementroche
Copy link

clementroche commented Jun 19, 2023

Hi, I'm the maintainer of react-lenis package and some people complain about using the library in next.js app context usage.
In fact it requires 'use client' directive on top of the file but I noticed that it's ignored by microbundle "Module level directives cause errors when bundled, 'use client' was ignored."
Do you have a solution for that ?

https://github.com/studio-freight/lenis/issues/170

@rschristian
Copy link
Collaborator

rschristian commented Jun 19, 2023

Does 'use client' still end up in your bundle?

Edit: It does not. I'm wondering if this is something Microbundle should support though, as directives are per-file, so if you were to say bundle a bunch of components with Microbundle, all would be affected by that directive. I'm not sure what the expected behavior here would be.

@developit
Copy link
Owner

developit commented Jun 27, 2023

The only way I can think of to do this would be to string-replace "use client" to something Terser will never minify/inline/fold, then string-replace it back after minification. There are very few replacements that would work for this, but an empty destructured variable declaration is one of them (Terser simply preserves it as-is).

// replace directive with a side effecting statement to keep it in-place during minification:
code = code.replace(/(\{[\n\s]*)(['"])use client\2([;\s])/g, `$1var{}=_USE_CLIENT_$3`);

// then minify:
let minified = (await terser.minify(code)).code;

// then replace the statement back:
minified = minified.replace(/(\{[\n\s]*)var\s*{\s*}\s*=\s*_USE_CLIENT_([;\s])/g, `$1"use client"$2`);

@clementroche
Copy link
Author

clementroche commented Aug 2, 2023

any solution planned @developit ?

this might become a real issue once maintainer switch to Next.js app folder
see next.js notice https://nextjs.org/docs/getting-started/react-essentials#third-party-packages

@ForsakenHarmony
Copy link
Collaborator

ForsakenHarmony commented Aug 3, 2023

terser also has compress.directives which could be set to false

@clementroche does it help to add a mangle.json file with the following content?

{
  "minify": {
    "compress": {
      "directives": false,
    }
  }
}

We might also need rollup-plugin-preserve-directives 🤔

Also see the rollup issue about the subject: rollup/rollup#4699

@clementroche
Copy link
Author

clementroche commented Aug 3, 2023

Now i realise that the issue comes from rollup itself not directly from microbundle, i'm gonna take a look at the thread you shared, thank you

@clementroche
Copy link
Author

clementroche commented Aug 7, 2023

I tried both solutions @ForsakenHarmony, still have this warning Module level directives cause errors when bundled, 'use client' was ignored.

pretty sure rollup.config.js is ignored by microbundle, right ?

@rschristian
Copy link
Collaborator

pretty sure rollup.config.js is ignored by microbundle, right ?

Yes, Microbundle's Rollup configuration is not open to configuration. However, you can of course patch Microbundle's config from node_modules to test out various solutions, if you didn't want to clone Microbundle & dev with the test suite.

@Stan-Stani
Copy link

Stan-Stani commented Aug 22, 2023

Personally, I would just like a way to silence that error for now.

Having a thousand lines like this,

Module level directives cause errors when bundled, 'use client' was ignored.
Module level directives cause errors when bundled, 'use client' was ignored.       
Module level directives cause errors when bundled, 'use client' was ignored.
Module level directives cause errors when bundled, 'use client' was ignored.
Module level directives cause errors when bundled, 'use client' was ignored.
Module level directives cause errors when bundled, 'use client' was ignored.

makes it hard to find when there are actual errors that prevent bundling.

I'm trying to figure out how to directly patch microbundle.js in node_modules, where the config lives, but I think I'm running into issues with yarn caching the files so my edits aren't causing changes :(.

@gtjamesa
Copy link

gtjamesa commented Sep 4, 2023

I'm trying to figure out how to directly patch microbundle.js in node_modules, where the config lives, but I think I'm running into issues with yarn caching the files so my edits aren't causing changes :(.

You can edit the relevant files then use patch-package to create a patch that you can track in version control. It will be reapplied on install

@rschristian
Copy link
Collaborator

rschristian commented Sep 4, 2023

I'm trying to figure out how to directly patch microbundle.js in node_modules, where the config lives, but I think I'm running into issues with yarn caching the files so my edits aren't causing changes :(.

Sorry, I didn't see this. Make sure you're editing node_modules/microbundle/dist/cli.js, rather than node_modules/microbundle/dist/microbundle.js. The former is the CLI app, latter is for programmatic usage.

Yarn v1 has no caching, no idea about v2+ though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants