Unverified Commit 5b374215 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[turbopack] Stop exposing globals from Turbopack runtime (#81727)

parent 10fb5bc6
......@@ -58,6 +58,20 @@ if (process.env.NODE_ENV === 'development') {
SegmentViewStateNode = mod.SegmentViewStateNode
}
// For hot-reloader
declare global {
var __next__clear_chunk_cache__: (() => void) | null | undefined
var __turbopack_clear_chunk_cache__: () => void | null | undefined
}
// hot-reloader modules are not bundled so we need to inject `__next__clear_chunk_cache__`
// into globalThis from this file which is bundled.
if (process.env.TURBOPACK) {
globalThis.__next__clear_chunk_cache__ = __turbopack_clear_chunk_cache__
} else {
// Webpack does not have chunks on the server
globalThis.__next__clear_chunk_cache__ = null
}
// patchFetch makes use of APIs such as `React.unstable_postpone` which are only available
// in the experimental channel of React, so export it from here so that it comes from the bundled runtime
export function patchFetch() {
......
......@@ -109,6 +109,8 @@ const isTestMode = !!(
const sessionId = Math.floor(Number.MAX_SAFE_INTEGER * Math.random())
declare const __next__clear_chunk_cache__: (() => void) | null | undefined
/**
* Replaces turbopack:///[project] with the specified project in the `source` field.
*/
......@@ -342,10 +344,11 @@ export async function createHotReloaderTurbopack(
resetFetch()
// Edge uses the browser runtime which already disposes chunks individually.
// TODO: process.env.NEXT_RUNTIME is 'nodejs' even though Node.js runtime is not used.
if ('__turbopack_clear_chunk_cache__' in globalThis) {
;(globalThis as any).__turbopack_clear_chunk_cache__()
// Not available in:
// - Pages Router (no server-side HMR)
// - Edge Runtime (uses browser runtime which already disposes chunks individually)
if (typeof __next__clear_chunk_cache__ === 'function') {
__next__clear_chunk_cache__()
}
const serverPaths = writtenEndpoint.serverPaths.map(({ path: p }) =>
......
......@@ -212,6 +212,7 @@ function instantiateModule(moduleId: ModuleId, source: SourceInfo): Module {
n: exportNamespace.bind(null, module, devModuleCache),
m: module,
c: devModuleCache,
C: null,
M: moduleFactories,
l: loadChunk.bind(null, sourceInfo),
L: loadChunkByUrl.bind(null, sourceInfo),
......
......@@ -302,6 +302,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
type: SourceType.Parent,
parentId: id,
}),
C: clearChunkCache,
w: loadWebAssembly,
u: loadWebAssemblyModule,
P: resolveAbsolutePath,
......@@ -381,9 +382,6 @@ function isJs(chunkUrlOrPath: ChunkUrl | ChunkPath): boolean {
return regexJsUrl.test(chunkUrlOrPath)
}
// For hot-reloader
;(globalThis as any).__turbopack_clear_chunk_cache__ = clearChunkCache
module.exports = {
getOrInstantiateRuntimeModule,
loadChunk,
......
......@@ -56,6 +56,8 @@ pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.l", "l");
pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.L", "L");
pub const TURBOPACK_CLEAR_CHUNK_CACHE: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.C", "C");
pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.j", "j");
pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut =
......@@ -81,7 +83,7 @@ pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut =
/// Adding an entry to this list will automatically ensure that `__turbopack_XXX__` can be called
/// from user code (by inserting a replacement into free_var_references)
pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 21] = [
pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 22] = [
("__turbopack_require__", TURBOPACK_REQUIRE),
("__turbopack_module_context__", TURBOPACK_MODULE_CONTEXT),
("__turbopack_import__", TURBOPACK_IMPORT),
......@@ -109,4 +111,8 @@ pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctio
("__turbopack_refresh__", TURBOPACK_REFRESH),
("__turbopack_require_stub__", TURBOPACK_REQUIRE_STUB),
("__turbopack_require_real__", TURBOPACK_REQUIRE_REAL),
(
"__turbopack_clear_chunk_cache__",
TURBOPACK_CLEAR_CHUNK_CACHE,
),
];
......@@ -665,6 +665,7 @@ function instantiateModule(id, source) {
type: 1,
parentId: id
}),
C: clearChunkCache,
w: loadWebAssembly,
u: loadWebAssemblyModule,
P: resolveAbsolutePath,
......@@ -724,9 +725,6 @@ const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
*/ function isJs(chunkUrlOrPath) {
return regexJsUrl.test(chunkUrlOrPath);
}
// For hot-reloader
;
globalThis.__turbopack_clear_chunk_cache__ = clearChunkCache;
module.exports = {
getOrInstantiateRuntimeModule,
loadChunk
......
......@@ -783,6 +783,7 @@ function instantiateModule(moduleId, source) {
n: exportNamespace.bind(null, module, devModuleCache),
m: module,
c: devModuleCache,
C: null,
M: moduleFactories,
l: loadChunk.bind(null, sourceInfo),
L: loadChunkByUrl.bind(null, sourceInfo),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment