Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.

feat: silence "use client" warning #78

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Support TS/JSX in node_modules to help the community experiment with it. Note that for now this not supported by TS and errors from these files cannot be silenced if the user is using a stricter configuration than the library author: https://github.com/microsoft/TypeScript/issues/30511. I advise to use it only for internal libraries for now (fixes #53)
- Silence `"use client"` warning when building library like `@tanstack/react-query`
- Fix fast refresh issue when exporting a component with a name that shadow another local component

## 3.2.0
Expand Down
26 changes: 24 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
JscTarget,
transform,
} from "@swc/core";
import { PluginOption } from "vite";
import { PluginOption, UserConfig, BuildOptions } from "vite";
import { createRequire } from "module";

const runtimePublicPath = "/@react-refresh";
Expand Down Expand Up @@ -127,6 +127,9 @@ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
name: "vite:react-swc",
apply: "build",
enforce: "pre", // Run before esbuild
config: (userConfig) => ({
build: silenceUseClientWarning(userConfig),
}),
transform: (code, _id) =>
transformWithOptions(_id.split("?")[0], code, "esnext", options, {
runtime: "automatic",
Expand All @@ -136,7 +139,8 @@ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
: {
name: "vite:react-swc",
apply: "build",
config: () => ({
config: (userConfig) => ({
build: silenceUseClientWarning(userConfig),
esbuild: {
jsx: "automatic",
jsxImportSource: options.jsxImportSource,
Expand Down Expand Up @@ -202,4 +206,22 @@ const transformWithOptions = async (
return result;
};

const silenceUseClientWarning = (userConfig: UserConfig): BuildOptions => ({
rollupOptions: {
onwarn(warning, defaultHandler) {
if (
warning.code === "MODULE_LEVEL_DIRECTIVE" &&
warning.message.includes("use client")
) {
return;
}
if (userConfig.build?.rollupOptions?.onwarn) {
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
} else {
defaultHandler(warning);
}
},
},
});

export default react;