Skip to content

Commit 7bac15b

Browse files
committed
[turbopack] Stop exposing globals from Turbopack runtime
1 parent f74a169 commit 7bac15b

File tree

10 files changed

+23
-13
lines changed

10 files changed

+23
-13
lines changed

packages/next/src/build/define-env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ export function getDefineEnv({
337337
!isTurbopack || (config.experimental.turbopackPersistentCaching ?? false),
338338
'process.env.__NEXT_OPTIMIZE_ROUTER_SCROLL':
339339
config.experimental.optimizeRouterScrolling ?? false,
340+
__next__clear_chunk_cache__: JSON.stringify(null),
340341
}
341342

342343
const userDefines = config.compiler?.define ?? {}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ 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+
}
65+
globalThis.__next__clear_chunk_cache__ = __next__clear_chunk_cache__
66+
6167
// patchFetch makes use of APIs such as `React.unstable_postpone` which are only available
6268
// in the experimental channel of React, so export it from here so that it comes from the bundled runtime
6369
export function patchFetch() {

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

Lines changed: 4 additions & 2 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
*/
@@ -344,8 +346,8 @@ export async function createHotReloaderTurbopack(
344346

345347
// Edge uses the browser runtime which already disposes chunks individually.
346348
// 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__()
349+
if (typeof __next__clear_chunk_cache__ === 'function') {
350+
__next__clear_chunk_cache__()
349351
}
350352

351353
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: 4 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,5 @@ 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+
("__next__clear_chunk_cache__", TURBOPACK_CLEAR_CHUNK_CACHE),
112115
];

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)