|
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 { |
| 5 | + BuildOptions, |
| 6 | + Plugin, |
| 7 | + PluginOption, |
| 8 | + ResolvedConfig, |
| 9 | + UserConfig, |
| 10 | +} from 'vite' |
5 | 11 | import MagicString from 'magic-string'
|
6 | 12 | import type { SourceMap } from 'magic-string'
|
7 | 13 | import {
|
@@ -268,7 +274,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
268 | 274 | const viteReactRefresh: Plugin = {
|
269 | 275 | name: 'vite:react-refresh',
|
270 | 276 | enforce: 'pre',
|
271 |
| - config: () => ({ |
| 277 | + config: (userConfig) => ({ |
| 278 | + build: silenceUseClientWarning(userConfig), |
272 | 279 | optimizeDeps: {
|
273 | 280 | // We can't add `react-dom` because the dependency is `react-dom/client`
|
274 | 281 | // for React 18 while it's `react-dom` for React 17. We'd need to detect
|
@@ -306,6 +313,24 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
306 | 313 |
|
307 | 314 | viteReact.preambleCode = preambleCode
|
308 | 315 |
|
| 316 | +const silenceUseClientWarning = (userConfig: UserConfig): BuildOptions => ({ |
| 317 | + rollupOptions: { |
| 318 | + onwarn(warning, defaultHandler) { |
| 319 | + if ( |
| 320 | + warning.code === 'MODULE_LEVEL_DIRECTIVE' && |
| 321 | + warning.message.includes('use client') |
| 322 | + ) { |
| 323 | + return |
| 324 | + } |
| 325 | + if (userConfig.build?.rollupOptions?.onwarn) { |
| 326 | + userConfig.build.rollupOptions.onwarn(warning, defaultHandler) |
| 327 | + } else { |
| 328 | + defaultHandler(warning) |
| 329 | + } |
| 330 | + }, |
| 331 | + }, |
| 332 | +}) |
| 333 | + |
309 | 334 | const loadedPlugin = new Map<string, any>()
|
310 | 335 | function loadPlugin(path: string): any {
|
311 | 336 | const cached = loadedPlugin.get(path)
|
|
0 commit comments