Skip to content

Commit

Permalink
feat: silence "use client" warning
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Apr 5, 2023
1 parent fbf722f commit 0fe5036
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ParserOptions, TransformOptions } from '@babel/core'
import * as babel from '@babel/core'
import { createFilter } from 'vite'
import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
import type { BuildOptions, Plugin, PluginOption , ResolvedConfig, UserConfig } from 'vite'
import MagicString from 'magic-string'
import type { SourceMap } from 'magic-string'
import {
Expand Down Expand Up @@ -268,7 +268,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
const viteReactRefresh: Plugin = {
name: 'vite:react-refresh',
enforce: 'pre',
config: () => ({
config: (userConfig) => ({
build: silenceUseClientWarning(userConfig),
optimizeDeps: {
// We can't add `react-dom` because the dependency is `react-dom/client`
// for React 18 while it's `react-dom` for React 17. We'd need to detect
Expand Down Expand Up @@ -306,6 +307,24 @@ export default function viteReact(opts: Options = {}): PluginOption[] {

viteReact.preambleCode = preambleCode

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)
}
},
},
})

const loadedPlugin = new Map<string, any>()
function loadPlugin(path: string): any {
const cached = loadedPlugin.get(path)
Expand Down

0 comments on commit 0fe5036

Please sign in to comment.