|
1 | 1 | import type { ParserOptions, TransformOptions } from '@babel/core' |
2 | 2 | import * as babel from '@babel/core' |
3 | 3 | import { createFilter } from 'vite' |
4 | | -import type { Plugin, PluginOption, ResolvedConfig } from 'vite' |
| 4 | +import type { BuildOptions, Plugin, PluginOption , ResolvedConfig, UserConfig } from 'vite' |
5 | 5 | import MagicString from 'magic-string' |
6 | 6 | import type { SourceMap } from 'magic-string' |
7 | 7 | import { |
@@ -268,7 +268,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] { |
268 | 268 | const viteReactRefresh: Plugin = { |
269 | 269 | name: 'vite:react-refresh', |
270 | 270 | enforce: 'pre', |
271 | | - config: () => ({ |
| 271 | + config: (userConfig) => ({ |
| 272 | + build: silenceUseClientWarning(userConfig), |
272 | 273 | optimizeDeps: { |
273 | 274 | // We can't add `react-dom` because the dependency is `react-dom/client` |
274 | 275 | // 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[] { |
306 | 307 |
|
307 | 308 | viteReact.preambleCode = preambleCode |
308 | 309 |
|
| 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 | + |
309 | 328 | const loadedPlugin = new Map<string, any>() |
310 | 329 | function loadPlugin(path: string): any { |
311 | 330 | const cached = loadedPlugin.get(path) |
|
0 commit comments