Skip to content

Commit dcf27c6

Browse files
eps1lonalii
authored andcommitted
[turbopack] Stop exposing globals from Turbopack runtime (vercel#81727)
1 parent 6477c4b commit dcf27c6

File tree

9 files changed

+36
-15
lines changed

9 files changed

+36
-15
lines changed

packages/next/src/server/app-render/entry-base.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ if (process.env.NODE_ENV === 'development') {
5858
SegmentViewStateNode = mod.SegmentViewStateNode
5959
}
6060

61+
// For hot-reloader
62+
declare global {
63+
var __next__clear_chunk_cache__: (() => void) | null | undefined
64+
var __turbopack_clear_chunk_cache__: () => void | null | undefined
65+
}
66+
// hot-reloader modules are not bundled so we need to inject `__next__clear_chunk_cache__`
67+
// into globalThis from this file which is bundled.
68+
if (process.env.TURBOPACK) {
69+
globalThis.__next__clear_chunk_cache__ = __turbopack_clear_chunk_cache__
70+
} else {
71+
// Webpack does not have chunks on the server
72+
globalThis.__next__clear_chunk_cache__ = null
73+
}
74+
6175
// patchFetch makes use of APIs such as `React.unstable_postpone` which are only available
6276
// in the experimental channel of React, so export it from here so that it comes from the bundled runtime
6377
export function patchFetch() {

packages/next/src/server/dev/hot-reloader-turbopack.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ const isTestMode = !!(
109109

110110
const sessionId = Math.floor(Number.MAX_SAFE_INTEGER * Math.random())
111111

112+
declare const __next__clear_chunk_cache__: (() => void) | null | undefined
113+
112114
/**
113115
* Replaces turbopack:///[project] with the specified project in the `source` field.
114116
*/
@@ -342,10 +344,11 @@ export async function createHotReloaderTurbopack(
342344

343345
resetFetch()
344346

345-
// Edge uses the browser runtime which already disposes chunks individually.
346-
// TODO: process.env.NEXT_RUNTIME is 'nodejs' even though Node.js runtime is not used.
347-
if ('__turbopack_clear_chunk_cache__' in globalThis) {
348-
;(globalThis as any).__turbopack_clear_chunk_cache__()
347+
// Not available in:
348+
// - Pages Router (no server-side HMR)
349+
// - Edge Runtime (uses browser runtime which already disposes chunks individually)
350+
if (typeof __next__clear_chunk_cache__ === 'function') {
351+
__next__clear_chunk_cache__()
349352
}
350353

351354
const serverPaths = writtenEndpoint.serverPaths.map(({ path: p }) =>

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base/dev-base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ function instantiateModule(moduleId: ModuleId, source: SourceInfo): Module {
212212
n: exportNamespace.bind(null, module, devModuleCache),
213213
m: module,
214214
c: devModuleCache,
215+
C: null,
215216
M: moduleFactories,
216217
l: loadChunk.bind(null, sourceInfo),
217218
L: loadChunkByUrl.bind(null, sourceInfo),

turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/runtime.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
302302
type: SourceType.Parent,
303303
parentId: id,
304304
}),
305+
C: clearChunkCache,
305306
w: loadWebAssembly,
306307
u: loadWebAssemblyModule,
307308
P: resolveAbsolutePath,
@@ -381,9 +382,6 @@ function isJs(chunkUrlOrPath: ChunkUrl | ChunkPath): boolean {
381382
return regexJsUrl.test(chunkUrlOrPath)
382383
}
383384

384-
// For hot-reloader
385-
;(globalThis as any).__turbopack_clear_chunk_cache__ = clearChunkCache
386-
387385
module.exports = {
388386
getOrInstantiateRuntimeModule,
389387
loadChunk,

turbopack/crates/turbopack-ecmascript/src/runtime_functions.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut =
5656
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.l", "l");
5757
pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut =
5858
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.L", "L");
59+
pub const TURBOPACK_CLEAR_CHUNK_CACHE: &TurbopackRuntimeFunctionShortcut =
60+
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.C", "C");
5961
pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut =
6062
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.j", "j");
6163
pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut =
@@ -81,7 +83,7 @@ pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut =
8183

8284
/// Adding an entry to this list will automatically ensure that `__turbopack_XXX__` can be called
8385
/// from user code (by inserting a replacement into free_var_references)
84-
pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 21] = [
86+
pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 22] = [
8587
("__turbopack_require__", TURBOPACK_REQUIRE),
8688
("__turbopack_module_context__", TURBOPACK_MODULE_CONTEXT),
8789
("__turbopack_import__", TURBOPACK_IMPORT),
@@ -109,4 +111,8 @@ pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctio
109111
("__turbopack_refresh__", TURBOPACK_REFRESH),
110112
("__turbopack_require_stub__", TURBOPACK_REQUIRE_STUB),
111113
("__turbopack_require_real__", TURBOPACK_REQUIRE_REAL),
114+
(
115+
"__turbopack_clear_chunk_cache__",
116+
TURBOPACK_CLEAR_CHUNK_CACHE,
117+
),
112118
];

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_build_runtime/output/[turbopack]_runtime.js

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_build_runtime/output/[turbopack]_runtime.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/b1abf_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_73aab4ae.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/b1abf_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_73aab4ae.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)