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

Add "use client" to FormField #92

Open
sabigara opened this issue Sep 4, 2023 · 0 comments
Open

Add "use client" to FormField #92

sabigara opened this issue Sep 4, 2023 · 0 comments

Comments

@sabigara
Copy link
Contributor

sabigara commented Sep 4, 2023

  • "use client" in TS files are removed by esbuild.
  • Below plugin (from this issue) can add it as a banner.
  • However, with tsup's treeshake: false option enabled, Module level directives cause errors when bundled warning is thrown by Rollup.
import fs from "fs/promises";

import { Plugin } from "esbuild";

interface ESBuildUseClientOptions {
  filter?: RegExp;
}

export const esbuildUseClient = ({
  filter = /\.client\.tsx$/,
}: ESBuildUseClientOptions = {}): Plugin => ({
  name: "use-client",
  setup(build): void {
    build.onLoad({ filter }, async (args) => {
      const source = await fs.readFile(args.path, "utf8");
      const loader = args.path.endsWith(".tsx") ? "tsx" : "ts";

      const data = await build.esbuild.transform(source, {
        loader,
        banner: '"use client";',
      });

      return {
        warnings: data.warnings,
        contents: data.code + `//# sourceMappingURL=${data.map}`,
      };
    });
  },
});
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

1 participant