Skip to content

Commit fd27faf

Browse files
committed
fix: fix AsyncLocalStorage injection
1 parent 9b69463 commit fd27faf

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,12 +943,13 @@ import.meta.hot.on("rsc:update", () => {
943943
code.includes('new AsyncLocalStorage()') &&
944944
!code.includes('__viteRscAyncHooks')
945945
) {
946-
// for build, we cannot use `import` as it confuses rollup commonjs plugin.
946+
// react code can be turned into esm when optimized
947+
const [, , , hasModuleSyntax] = esModuleLexer.parse(code)
947948
return (
948-
(this.environment.mode === 'build' && !isRolldownVite
949-
? `const __viteRscAyncHooks = require("node:async_hooks");`
950-
: `import * as __viteRscAyncHooks from "node:async_hooks";`) +
951-
`globalThis.AsyncLocalStorage = __viteRscAyncHooks.AsyncLocalStorage;` +
949+
(hasModuleSyntax
950+
? `import * as __viteRscAsyncHooks from "node:async_hooks";`
951+
: `const __viteRscAsyncHooks = require("node:async_hooks");`) +
952+
`globalThis.AsyncLocalStorage = __viteRscAsyncHooks.AsyncLocalStorage;` +
952953
code
953954
)
954955
}

0 commit comments

Comments
 (0)