Skip to content

Commit 49d5f20

Browse files
Add use client at the top and ignore warnings (calcom#26184)
1 parent c2b7dcb commit 49d5f20

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

packages/embeds/embed-react/vite.config.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "path";
33
import { defineConfig } from "vite";
44

55
import viteBaseConfig from "../vite.config";
6-
6+
const useClientBanner = '"use client";';
77
// https://vitejs.dev/config/
88
export default defineConfig({
99
...viteBaseConfig,
@@ -18,13 +18,35 @@ export default defineConfig({
1818
// make sure to externalize deps that shouldn't be bundled
1919
// into your library
2020
external: ["react", "react/jsx-runtime", "react-dom", "react-dom/client"],
21+
onwarn(warning, defaultHandler) {
22+
// If "use client" directive has been added to the top of the file, then we can ignore the warnings related to it
23+
if (useClientBanner) {
24+
if (
25+
// vite-plugin-react also ignores this warning - https://github.com/vitejs/vite-plugin-react/blob/3748fc7493cf0a07a2ae275fbd1ae035f01010cc/packages/plugin-react/src/index.ts#L307-L323
26+
warning.code === 'MODULE_LEVEL_DIRECTIVE' &&
27+
warning.message.includes('use client')
28+
) {
29+
return
30+
}
31+
// It comes due to use client directive being ignored in the middle of the file
32+
// Even though this could happen in other cases, but there would always be another corresponding error logged which could still highlight any issue during build
33+
if (warning.message?.includes("Can't resolve original location of error")) {
34+
return
35+
}
36+
}
37+
defaultHandler(warning)
38+
},
2139
output: {
2240
exports: "named",
41+
// Add "use client" directive to the top of the bundle for Next.js App Router compatibility
42+
// During bundling "use client" directive comes in b/w the file which is ignored/removed by Rollup because it makes sense at the top only
43+
banner: useClientBanner,
2344
// Provide global variables to use in the UMD build
2445
// for externalized deps
2546
globals: {
2647
react: "React",
2748
"react-dom": "ReactDOM",
49+
"react/jsx-runtime": "jsxRuntime",
2850
},
2951
},
3052
},

0 commit comments

Comments
 (0)