Skip to content

Commit 0fe5036

Browse files
committed
feat: silence "use client" warning
1 parent fbf722f commit 0fe5036

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

packages/plugin-react/src/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ParserOptions, TransformOptions } from '@babel/core'
22
import * as babel from '@babel/core'
33
import { createFilter } from 'vite'
4-
import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
4+
import type { BuildOptions, Plugin, PluginOption , ResolvedConfig, UserConfig } from 'vite'
55
import MagicString from 'magic-string'
66
import type { SourceMap } from 'magic-string'
77
import {
@@ -268,7 +268,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
268268
const viteReactRefresh: Plugin = {
269269
name: 'vite:react-refresh',
270270
enforce: 'pre',
271-
config: () => ({
271+
config: (userConfig) => ({
272+
build: silenceUseClientWarning(userConfig),
272273
optimizeDeps: {
273274
// We can't add `react-dom` because the dependency is `react-dom/client`
274275
// for React 18 while it's `react-dom` for React 17. We'd need to detect
@@ -306,6 +307,24 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
306307

307308
viteReact.preambleCode = preambleCode
308309

310+
const silenceUseClientWarning = (userConfig: UserConfig): BuildOptions => ({
311+
rollupOptions: {
312+
onwarn(warning, defaultHandler) {
313+
if (
314+
warning.code === 'MODULE_LEVEL_DIRECTIVE' &&
315+
warning.message.includes('use client')
316+
) {
317+
return
318+
}
319+
if (userConfig.build?.rollupOptions?.onwarn) {
320+
userConfig.build.rollupOptions.onwarn(warning, defaultHandler)
321+
} else {
322+
defaultHandler(warning)
323+
}
324+
},
325+
},
326+
})
327+
309328
const loadedPlugin = new Map<string, any>()
310329
function loadPlugin(path: string): any {
311330
const cached = loadedPlugin.get(path)

0 commit comments

Comments
 (0)