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

Commit 0ed5b22

Browse files
authored
feat: silence "use client" warning (#78)
1 parent 4371853 commit 0ed5b22

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- 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)
6+
- Silence `"use client"` warning when building library like `@tanstack/react-query`
67
- Fix fast refresh issue when exporting a component with a name that shadow another local component
78

89
## 3.2.0

src/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
JscTarget,
1010
transform,
1111
} from "@swc/core";
12-
import { PluginOption } from "vite";
12+
import { PluginOption, UserConfig, BuildOptions } from "vite";
1313
import { createRequire } from "module";
1414

1515
const runtimePublicPath = "/@react-refresh";
@@ -132,6 +132,9 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
132132
name: "vite:react-swc",
133133
apply: "build",
134134
enforce: "pre", // Run before esbuild
135+
config: (userConfig) => ({
136+
build: silenceUseClientWarning(userConfig),
137+
}),
135138
transform: (code, _id) =>
136139
transformWithOptions(_id.split("?")[0], code, "esnext", options, {
137140
runtime: "automatic",
@@ -141,7 +144,8 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
141144
: {
142145
name: "vite:react-swc",
143146
apply: "build",
144-
config: () => ({
147+
config: (userConfig) => ({
148+
build: silenceUseClientWarning(userConfig),
145149
esbuild: {
146150
jsx: "automatic",
147151
jsxImportSource: options.jsxImportSource,
@@ -207,4 +211,22 @@ const transformWithOptions = async (
207211
return result;
208212
};
209213

214+
const silenceUseClientWarning = (userConfig: UserConfig): BuildOptions => ({
215+
rollupOptions: {
216+
onwarn(warning, defaultHandler) {
217+
if (
218+
warning.code === "MODULE_LEVEL_DIRECTIVE" &&
219+
warning.message.includes("use client")
220+
) {
221+
return;
222+
}
223+
if (userConfig.build?.rollupOptions?.onwarn) {
224+
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
225+
} else {
226+
defaultHandler(warning);
227+
}
228+
},
229+
},
230+
});
231+
210232
export default react;

0 commit comments

Comments
 (0)