Unverified Commit 5453818a authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack: Add __turbopack_load_by_url__ (#76814)

Work in progress for PACK-4071 (implementing `config.deploymentId`)

- [x] Implement `?dpl={config.deploymentId}` in client references
manifest
- [x] Implement `?dpl={config.deploymentId}` in Turbopack chunk loading 
- [x] Add `__turbopack_load_by_url__`
- [ ] Change react-dom-turbopack to use __turbopack_load_by_url__

Last item will be a follow-up PR to this one, given it's already a large
enough change in chunk loading right now.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent c2d28fa7
...@@ -966,6 +966,7 @@ impl Project { ...@@ -966,6 +966,7 @@ impl Project {
self.client_relative_path(), self.client_relative_path(),
Vc::cell("/ROOT".into()), Vc::cell("/ROOT".into()),
self.next_config().computed_asset_prefix(), self.next_config().computed_asset_prefix(),
self.next_config().chunk_suffix_path(),
self.client_compile_time_info().environment(), self.client_compile_time_info().environment(),
self.next_mode(), self.next_mode(),
self.module_id_strategy(), self.module_id_strategy(),
......
...@@ -415,6 +415,7 @@ pub async fn get_client_chunking_context( ...@@ -415,6 +415,7 @@ pub async fn get_client_chunking_context(
client_root: ResolvedVc<FileSystemPath>, client_root: ResolvedVc<FileSystemPath>,
client_root_to_root_path: ResolvedVc<RcStr>, client_root_to_root_path: ResolvedVc<RcStr>,
asset_prefix: ResolvedVc<Option<RcStr>>, asset_prefix: ResolvedVc<Option<RcStr>>,
chunk_suffix_path: ResolvedVc<Option<RcStr>>,
environment: ResolvedVc<Environment>, environment: ResolvedVc<Environment>,
mode: Vc<NextMode>, mode: Vc<NextMode>,
module_id_strategy: ResolvedVc<Box<dyn ModuleIdStrategy>>, module_id_strategy: ResolvedVc<Box<dyn ModuleIdStrategy>>,
...@@ -437,6 +438,7 @@ pub async fn get_client_chunking_context( ...@@ -437,6 +438,7 @@ pub async fn get_client_chunking_context(
next_mode.runtime_type(), next_mode.runtime_type(),
) )
.chunk_base_path(asset_prefix) .chunk_base_path(asset_prefix)
.chunk_suffix_path(chunk_suffix_path)
.minify_type(if *minify.await? { .minify_type(if *minify.await? {
MinifyType::Minify { MinifyType::Minify {
mangle: !*no_mangling.await?, mangle: !*no_mangling.await?,
......
...@@ -83,6 +83,7 @@ pub struct NextConfig { ...@@ -83,6 +83,7 @@ pub struct NextConfig {
pub transpile_packages: Option<Vec<RcStr>>, pub transpile_packages: Option<Vec<RcStr>>,
pub modularize_imports: Option<FxIndexMap<String, ModularizeImportPackageConfig>>, pub modularize_imports: Option<FxIndexMap<String, ModularizeImportPackageConfig>>,
pub dist_dir: Option<RcStr>, pub dist_dir: Option<RcStr>,
pub deployment_id: Option<RcStr>,
sass_options: Option<serde_json::Value>, sass_options: Option<serde_json::Value>,
pub trailing_slash: Option<bool>, pub trailing_slash: Option<bool>,
pub asset_prefix: Option<RcStr>, pub asset_prefix: Option<RcStr>,
...@@ -670,9 +671,6 @@ pub struct ExperimentalConfig { ...@@ -670,9 +671,6 @@ pub struct ExperimentalConfig {
turbo: Option<ExperimentalTurboConfig>, turbo: Option<ExperimentalTurboConfig>,
external_middleware_rewrites_resolve: Option<bool>, external_middleware_rewrites_resolve: Option<bool>,
scroll_restoration: Option<bool>, scroll_restoration: Option<bool>,
use_deployment_id: Option<bool>,
use_deployment_id_server_actions: Option<bool>,
deployment_id: Option<RcStr>,
manual_client_base_path: Option<bool>, manual_client_base_path: Option<bool>,
optimistic_client_cache: Option<bool>, optimistic_client_cache: Option<bool>,
middleware_prefetch: Option<MiddlewarePrefetchType>, middleware_prefetch: Option<MiddlewarePrefetchType>,
...@@ -1393,6 +1391,17 @@ impl NextConfig { ...@@ -1393,6 +1391,17 @@ impl NextConfig {
))) )))
} }
/// Returns the suffix to use for chunk loading.
#[turbo_tasks::function]
pub async fn chunk_suffix_path(self: Vc<Self>) -> Result<Vc<Option<RcStr>>> {
let this = self.await?;
match &this.deployment_id {
Some(deployment_id) => Ok(Vc::cell(Some(format!("?dpl={}", deployment_id).into()))),
None => Ok(Vc::cell(None)),
}
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn enable_ppr(&self) -> Vc<bool> { pub fn enable_ppr(&self) -> Vc<bool> {
Vc::cell( Vc::cell(
......
...@@ -71,12 +71,19 @@ impl ClientReferenceManifest { ...@@ -71,12 +71,19 @@ impl ClientReferenceManifest {
async move { async move {
let mut entry_manifest: ClientReferenceManifest = Default::default(); let mut entry_manifest: ClientReferenceManifest = Default::default();
let mut references = FxIndexSet::default(); let mut references = FxIndexSet::default();
entry_manifest.module_loading.prefix = next_config let chunk_suffix_path = next_config.chunk_suffix_path().await?;
let prefix_path = next_config
.computed_asset_prefix() .computed_asset_prefix()
.await? .await?
.as_ref() .as_ref()
.map(|p| p.clone()) .map(|p| p.clone())
.unwrap_or_default(); .unwrap_or_default();
let suffix_path = chunk_suffix_path
.as_ref()
.map(|p| p.to_string())
.unwrap_or("".into());
entry_manifest.module_loading.prefix = prefix_path;
entry_manifest.module_loading.cross_origin = next_config entry_manifest.module_loading.cross_origin = next_config
.await? .await?
...@@ -195,6 +202,7 @@ impl ClientReferenceManifest { ...@@ -195,6 +202,7 @@ impl ClientReferenceManifest {
// It's possible that a chunk also emits CSS files, that will // It's possible that a chunk also emits CSS files, that will
// be handled separatedly. // be handled separatedly.
.filter(|path| path.ends_with(".js")) .filter(|path| path.ends_with(".js"))
.map(|path| format!("{}{}", path, suffix_path))
.map(RcStr::from) .map(RcStr::from)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
...@@ -355,6 +363,8 @@ impl ClientReferenceManifest { ...@@ -355,6 +363,8 @@ impl ClientReferenceManifest {
for (chunk, chunk_path) in client_chunks_with_path { for (chunk, chunk_path) in client_chunks_with_path {
if let Some(path) = client_relative_path.get_path_to(&chunk_path) { if let Some(path) = client_relative_path.get_path_to(&chunk_path) {
// The entry CSS files and entry JS files don't have prefix and suffix
// applied because it is added by Nex.js during rendering.
let path = path.into(); let path = path.into();
if chunk_path.extension_ref() == Some("css") { if chunk_path.extension_ref() == Some("css") {
entry_css_files_with_chunk.push((path, chunk)); entry_css_files_with_chunk.push((path, chunk));
......
...@@ -2,6 +2,7 @@ import type { AppRenderContext } from './app-render' ...@@ -2,6 +2,7 @@ import type { AppRenderContext } from './app-render'
const isDev = process.env.NODE_ENV === 'development' const isDev = process.env.NODE_ENV === 'development'
const isTurbopack = !!process.env.TURBOPACK const isTurbopack = !!process.env.TURBOPACK
export function getAssetQueryString( export function getAssetQueryString(
ctx: AppRenderContext, ctx: AppRenderContext,
addTimestamp: boolean addTimestamp: boolean
...@@ -12,12 +13,13 @@ export function getAssetQueryString( ...@@ -12,12 +13,13 @@ export function getAssetQueryString(
// reload assets when a new RSC response is received. // reload assets when a new RSC response is received.
// Turbopack handles HMR of assets itself and react doesn't need to reload them // Turbopack handles HMR of assets itself and react doesn't need to reload them
// so this approach is not needed for Turbopack. // so this approach is not needed for Turbopack.
if (isDev && !isTurbopack && addTimestamp) { const shouldAddVersion = isDev && !isTurbopack && addTimestamp
if (shouldAddVersion) {
qs += `?v=${ctx.requestTimestamp}` qs += `?v=${ctx.requestTimestamp}`
} }
if (ctx.renderOpts.deploymentId) { if (ctx.renderOpts.deploymentId) {
qs += `${isDev ? '&' : '?'}dpl=${ctx.renderOpts.deploymentId}` qs += `${shouldAddVersion ? '&' : '?'}dpl=${ctx.renderOpts.deploymentId}`
} }
return qs return qs
} }
...@@ -1787,11 +1787,17 @@ export async function copy_vendor_react(task_) { ...@@ -1787,11 +1787,17 @@ export async function copy_vendor_react(task_) {
// bundles have unique constraints like a runtime bundle. For browser builds this // bundles have unique constraints like a runtime bundle. For browser builds this
// package will be bundled alongside user code and we don't need to introduce the extra // package will be bundled alongside user code and we don't need to introduce the extra
// indirection // indirection
if (
(file.base.startsWith('react-server-dom-turbopack-client') && if (file.base.startsWith('react-server-dom-turbopack-client.browser')) {
!file.base.startsWith( const source = file.data.toString()
'react-server-dom-turbopack-client.browser' let newSource = source.replace(
)) || /__turbopack_load__/g,
'__turbopack_load__'
)
file.data = newSource
} else if (
file.base.startsWith('react-server-dom-turbopack-client') ||
(file.base.startsWith('react-server-dom-turbopack-server') && (file.base.startsWith('react-server-dom-turbopack-server') &&
!file.base.startsWith('react-server-dom-turbopack-server.browser')) !file.base.startsWith('react-server-dom-turbopack-server.browser'))
) { ) {
......
...@@ -17873,7 +17873,8 @@ ...@@ -17873,7 +17873,8 @@
"runtimeError": false "runtimeError": false
}, },
"test/production/deployment-id-handling/deployment-id-handling.test.ts": { "test/production/deployment-id-handling/deployment-id-handling.test.ts": {
"passed": [ "passed": [],
"failed": [
"deployment-id-handling disabled should not append dpl query to all assets for /", "deployment-id-handling disabled should not append dpl query to all assets for /",
"deployment-id-handling disabled should not append dpl query to all assets for /from-app", "deployment-id-handling disabled should not append dpl query to all assets for /from-app",
"deployment-id-handling disabled should not append dpl query to all assets for /from-app/edge", "deployment-id-handling disabled should not append dpl query to all assets for /from-app/edge",
...@@ -17883,9 +17884,7 @@ ...@@ -17883,9 +17884,7 @@
"deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should have deployment id env available", "deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should have deployment id env available",
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should contain deployment id in RSC payload request headers", "deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should contain deployment id in RSC payload request headers",
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should contain deployment id in prefetch request", "deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should contain deployment id in prefetch request",
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should have deployment id env available" "deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should have deployment id env available",
],
"failed": [
"deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /", "deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /",
"deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app", "deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app",
"deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app/edge", "deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app/edge",
...@@ -67,6 +67,11 @@ impl BrowserChunkingContextBuilder { ...@@ -67,6 +67,11 @@ impl BrowserChunkingContextBuilder {
self self
} }
pub fn chunk_suffix_path(mut self, chunk_suffix_path: ResolvedVc<Option<RcStr>>) -> Self {
self.chunking_context.chunk_suffix_path = chunk_suffix_path;
self
}
pub fn runtime_type(mut self, runtime_type: RuntimeType) -> Self { pub fn runtime_type(mut self, runtime_type: RuntimeType) -> Self {
self.chunking_context.runtime_type = runtime_type; self.chunking_context.runtime_type = runtime_type;
self self
...@@ -135,6 +140,9 @@ pub struct BrowserChunkingContext { ...@@ -135,6 +140,9 @@ pub struct BrowserChunkingContext {
/// Base path that will be prepended to all chunk URLs when loading them. /// Base path that will be prepended to all chunk URLs when loading them.
/// This path will not appear in chunk paths or chunk data. /// This path will not appear in chunk paths or chunk data.
chunk_base_path: ResolvedVc<Option<RcStr>>, chunk_base_path: ResolvedVc<Option<RcStr>>,
/// Suffix path that will be appended to all chunk URLs when loading them.
/// This path will not appear in chunk paths or chunk data.
chunk_suffix_path: ResolvedVc<Option<RcStr>>,
/// URL prefix that will be prepended to all static asset URLs when loading /// URL prefix that will be prepended to all static asset URLs when loading
/// them. /// them.
asset_base_path: ResolvedVc<Option<RcStr>>, asset_base_path: ResolvedVc<Option<RcStr>>,
...@@ -180,6 +188,7 @@ impl BrowserChunkingContext { ...@@ -180,6 +188,7 @@ impl BrowserChunkingContext {
should_use_file_source_map_uris: false, should_use_file_source_map_uris: false,
asset_root_path, asset_root_path,
chunk_base_path: ResolvedVc::cell(None), chunk_base_path: ResolvedVc::cell(None),
chunk_suffix_path: ResolvedVc::cell(None),
asset_base_path: ResolvedVc::cell(None), asset_base_path: ResolvedVc::cell(None),
enable_hot_module_replacement: false, enable_hot_module_replacement: false,
enable_tracing: false, enable_tracing: false,
...@@ -209,6 +218,11 @@ impl BrowserChunkingContext { ...@@ -209,6 +218,11 @@ impl BrowserChunkingContext {
*self.chunk_base_path *self.chunk_base_path
} }
/// Returns the asset suffix path.
pub fn chunk_suffix_path(&self) -> Vc<Option<RcStr>> {
*self.chunk_suffix_path
}
/// Returns the minify type. /// Returns the minify type.
pub fn source_maps_type(&self) -> SourceMapsType { pub fn source_maps_type(&self) -> SourceMapsType {
self.source_maps_type self.source_maps_type
......
...@@ -153,6 +153,7 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -153,6 +153,7 @@ impl EcmascriptBrowserEvaluateChunk {
let runtime_code = turbopack_ecmascript_runtime::get_browser_runtime_code( let runtime_code = turbopack_ecmascript_runtime::get_browser_runtime_code(
environment, environment,
chunking_context.chunk_base_path(), chunking_context.chunk_base_path(),
chunking_context.chunk_suffix_path(),
Value::new(chunking_context.runtime_type()), Value::new(chunking_context.runtime_type()),
output_root_to_root_path, output_root_to_root_path,
source_maps, source_maps,
...@@ -163,6 +164,7 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -163,6 +164,7 @@ impl EcmascriptBrowserEvaluateChunk {
let runtime_code = turbopack_ecmascript_runtime::get_browser_runtime_code( let runtime_code = turbopack_ecmascript_runtime::get_browser_runtime_code(
environment, environment,
chunking_context.chunk_base_path(), chunking_context.chunk_base_path(),
chunking_context.chunk_suffix_path(),
Value::new(chunking_context.runtime_type()), Value::new(chunking_context.runtime_type()),
output_root_to_root_path, output_root_to_root_path,
source_maps, source_maps,
......
...@@ -61,7 +61,7 @@ export function connect({ ...@@ -61,7 +61,7 @@ export function connect({
throw new Error("A separate HMR handler was already registered"); throw new Error("A separate HMR handler was already registered");
} }
globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = {
push: ([chunkPath, callback]: [ChunkPath, UpdateCallback]) => { push: ([chunkPath, callback]: [ChunkListPath, UpdateCallback]) => {
subscribeToChunkUpdate(chunkPath, sendMessage, callback); subscribeToChunkUpdate(chunkPath, sendMessage, callback);
}, },
}; };
...@@ -202,7 +202,7 @@ function mergeChunkListChunks( ...@@ -202,7 +202,7 @@ function mergeChunkListChunks(
): Record<ChunkPath, ChunkUpdate> { ): Record<ChunkPath, ChunkUpdate> {
const chunks: Record<ChunkPath, ChunkUpdate> = {}; const chunks: Record<ChunkPath, ChunkUpdate> = {};
for (const [chunkPath, chunkUpdateA] of Object.entries(chunksA)) { for (const [chunkPath, chunkUpdateA] of Object.entries(chunksA) as Array<[ChunkPath, ChunkUpdate]>) {
const chunkUpdateB = chunksB[chunkPath]; const chunkUpdateB = chunksB[chunkPath];
if (chunkUpdateB != null) { if (chunkUpdateB != null) {
const mergedUpdate = mergeChunkUpdates(chunkUpdateA, chunkUpdateB); const mergedUpdate = mergeChunkUpdates(chunkUpdateA, chunkUpdateB);
...@@ -214,7 +214,7 @@ function mergeChunkListChunks( ...@@ -214,7 +214,7 @@ function mergeChunkListChunks(
} }
} }
for (const [chunkPath, chunkUpdateB] of Object.entries(chunksB)) { for (const [chunkPath, chunkUpdateB] of Object.entries(chunksB) as Array<[ChunkPath, ChunkUpdate]>) {
if (chunks[chunkPath] == null) { if (chunks[chunkPath] == null) {
chunks[chunkPath] = chunkUpdateB; chunks[chunkPath] = chunkUpdateB;
} }
...@@ -280,7 +280,7 @@ function mergeEcmascriptChunksUpdates( ...@@ -280,7 +280,7 @@ function mergeEcmascriptChunksUpdates(
const chunks: Record<ChunkPath, EcmascriptMergedChunkUpdate> = {}; const chunks: Record<ChunkPath, EcmascriptMergedChunkUpdate> = {};
for (const [chunkPath, chunkUpdateA] of Object.entries(chunksA)) { for (const [chunkPath, chunkUpdateA] of Object.entries(chunksA) as Array<[ChunkPath, EcmascriptMergedChunkUpdate]>) {
const chunkUpdateB = chunksB[chunkPath]; const chunkUpdateB = chunksB[chunkPath];
if (chunkUpdateB != null) { if (chunkUpdateB != null) {
const mergedUpdate = mergeEcmascriptChunkUpdates( const mergedUpdate = mergeEcmascriptChunkUpdates(
...@@ -295,7 +295,7 @@ function mergeEcmascriptChunksUpdates( ...@@ -295,7 +295,7 @@ function mergeEcmascriptChunksUpdates(
} }
} }
for (const [chunkPath, chunkUpdateB] of Object.entries(chunksB)) { for (const [chunkPath, chunkUpdateB] of Object.entries(chunksB) as Array<[ChunkPath, EcmascriptMergedChunkUpdate]>) {
if (chunks[chunkPath] == null) { if (chunks[chunkPath] == null) {
chunks[chunkPath] = chunkUpdateB; chunks[chunkPath] = chunkUpdateB;
} }
...@@ -530,13 +530,13 @@ function finalizeUpdate() { ...@@ -530,13 +530,13 @@ function finalizeUpdate() {
} }
function subscribeToChunkUpdate( function subscribeToChunkUpdate(
chunkPath: ChunkPath, chunkListPath: ChunkListPath,
sendMessage: SendMessage, sendMessage: SendMessage,
callback: UpdateCallback callback: UpdateCallback
): () => void { ): () => void {
return subscribeToUpdate( return subscribeToUpdate(
{ {
path: chunkPath, path: chunkListPath,
}, },
sendMessage, sendMessage,
callback callback
......
...@@ -118,6 +118,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module { ...@@ -118,6 +118,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
c: moduleCache, c: moduleCache,
M: moduleFactories, M: moduleFactories,
l: loadChunk.bind(null, sourceInfo), l: loadChunk.bind(null, sourceInfo),
L: loadChunkByUrl.bind(null, sourceInfo),
w: loadWebAssembly.bind(null, sourceInfo), w: loadWebAssembly.bind(null, sourceInfo),
u: loadWebAssemblyModule.bind(null, sourceInfo), u: loadWebAssemblyModule.bind(null, sourceInfo),
g: globalThis, g: globalThis,
......
...@@ -51,8 +51,8 @@ type ModuleFactory = ( ...@@ -51,8 +51,8 @@ type ModuleFactory = (
) => undefined ) => undefined
interface DevRuntimeBackend { interface DevRuntimeBackend {
reloadChunk?: (chunkPath: ChunkPath) => Promise<void>; reloadChunk?: (chunkUrl: ChunkUrl) => Promise<void>;
unloadChunk?: (chunkPath: ChunkPath) => void; unloadChunk?: (chunkUrl: ChunkUrl) => void;
restart: () => void; restart: () => void;
} }
...@@ -219,6 +219,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module { ...@@ -219,6 +219,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
c: devModuleCache, c: devModuleCache,
M: moduleFactories, M: moduleFactories,
l: loadChunk.bind(null, sourceInfo), l: loadChunk.bind(null, sourceInfo),
L: loadChunkByUrl.bind(null, sourceInfo),
w: loadWebAssembly.bind(null, sourceInfo), w: loadWebAssembly.bind(null, sourceInfo),
u: loadWebAssemblyModule.bind(null, sourceInfo), u: loadWebAssemblyModule.bind(null, sourceInfo),
g: globalThis, g: globalThis,
...@@ -596,16 +597,18 @@ function applyChunkListUpdate(update: ChunkListUpdate) { ...@@ -596,16 +597,18 @@ function applyChunkListUpdate(update: ChunkListUpdate) {
} }
if (update.chunks != null) { if (update.chunks != null) {
for (const [chunkPath, chunkUpdate] of Object.entries(update.chunks)) { for (const [chunkPath, chunkUpdate] of Object.entries(update.chunks) as Array<[ChunkPath, ChunkUpdate]>) {
const chunkUrl = getChunkRelativeUrl(chunkPath);
switch (chunkUpdate.type) { switch (chunkUpdate.type) {
case "added": case "added":
BACKEND.loadChunk(chunkPath, { type: SourceType.Update }); BACKEND.loadChunk(chunkUrl, { type: SourceType.Update });
break; break;
case "total": case "total":
DEV_BACKEND.reloadChunk?.(chunkPath); DEV_BACKEND.reloadChunk?.(chunkUrl);
break; break;
case "deleted": case "deleted":
DEV_BACKEND.unloadChunk?.(chunkPath); DEV_BACKEND.unloadChunk?.(chunkUrl);
break; break;
case "partial": case "partial":
invariant( invariant(
...@@ -705,7 +708,7 @@ function computeChangedModules( ...@@ -705,7 +708,7 @@ function computeChangedModules(
const modified = new Map(); const modified = new Map();
const deleted: Set<ModuleId> = new Set(); const deleted: Set<ModuleId> = new Set();
for (const [chunkPath, mergedChunkUpdate] of Object.entries(updates)) { for (const [chunkPath, mergedChunkUpdate] of Object.entries(updates) as Array<[ChunkPath, EcmascriptMergedChunkUpdate]>) {
switch (mergedChunkUpdate.type) { switch (mergedChunkUpdate.type) {
case "added": { case "added": {
const updateAdded = new Set(mergedChunkUpdate.modules); const updateAdded = new Set(mergedChunkUpdate.modules);
...@@ -873,7 +876,7 @@ function getAffectedModuleEffects(moduleId: ModuleId): ModuleEffect { ...@@ -873,7 +876,7 @@ function getAffectedModuleEffects(moduleId: ModuleId): ModuleEffect {
}; };
} }
function handleApply(chunkListPath: ChunkPath, update: ServerMessage) { function handleApply(chunkListPath: ChunkListPath, update: ServerMessage) {
switch (update.type) { switch (update.type) {
case "partial": { case "partial": {
// This indicates that the update is can be applied to the current state of the application. // This indicates that the update is can be applied to the current state of the application.
...@@ -1014,7 +1017,7 @@ function removeModuleFromChunk( ...@@ -1014,7 +1017,7 @@ function removeModuleFromChunk(
/** /**
* Disposes of a chunk list and its corresponding exclusive chunks. * Disposes of a chunk list and its corresponding exclusive chunks.
*/ */
function disposeChunkList(chunkListPath: ChunkPath): boolean { function disposeChunkList(chunkListPath: ChunkListPath): boolean {
const chunkPaths = chunkListChunksMap.get(chunkListPath); const chunkPaths = chunkListChunksMap.get(chunkListPath);
if (chunkPaths == null) { if (chunkPaths == null) {
return false; return false;
...@@ -1033,7 +1036,9 @@ function disposeChunkList(chunkListPath: ChunkPath): boolean { ...@@ -1033,7 +1036,9 @@ function disposeChunkList(chunkListPath: ChunkPath): boolean {
// We must also dispose of the chunk list's chunk itself to ensure it may // We must also dispose of the chunk list's chunk itself to ensure it may
// be reloaded properly in the future. // be reloaded properly in the future.
DEV_BACKEND.unloadChunk?.(chunkListPath); const chunkListUrl = getChunkRelativeUrl(chunkListPath)
DEV_BACKEND.unloadChunk?.(chunkListUrl);
return true; return true;
} }
...@@ -1044,9 +1049,10 @@ function disposeChunkList(chunkListPath: ChunkPath): boolean { ...@@ -1044,9 +1049,10 @@ function disposeChunkList(chunkListPath: ChunkPath): boolean {
* @returns Whether the chunk was disposed of. * @returns Whether the chunk was disposed of.
*/ */
function disposeChunk(chunkPath: ChunkPath): boolean { function disposeChunk(chunkPath: ChunkPath): boolean {
const chunkUrl = getChunkRelativeUrl(chunkPath)
// This should happen whether the chunk has any modules in it or not. // This should happen whether the chunk has any modules in it or not.
// For instance, CSS chunks have no modules in them, but they still need to be unloaded. // For instance, CSS chunks have no modules in them, but they still need to be unloaded.
DEV_BACKEND.unloadChunk?.(chunkPath); DEV_BACKEND.unloadChunk?.(chunkUrl);
const chunkModules = chunkModulesMap.get(chunkPath); const chunkModules = chunkModulesMap.get(chunkPath);
if (chunkModules == null) { if (chunkModules == null) {
...@@ -1077,26 +1083,27 @@ function registerChunkList( ...@@ -1077,26 +1083,27 @@ function registerChunkList(
chunkUpdateProvider: ChunkUpdateProvider, chunkUpdateProvider: ChunkUpdateProvider,
chunkList: ChunkList chunkList: ChunkList
) { ) {
const chunkListPath = chunkList.path;
chunkUpdateProvider.push([ chunkUpdateProvider.push([
chunkList.path, chunkListPath,
handleApply.bind(null, chunkList.path), handleApply.bind(null, chunkListPath),
]); ]);
// Adding chunks to chunk lists and vice versa. // Adding chunks to chunk lists and vice versa.
const chunks = new Set(chunkList.chunks.map(getChunkPath)); const chunkPaths = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks); chunkListChunksMap.set(chunkListPath, chunkPaths);
for (const chunkPath of chunks) { for (const chunkPath of chunkPaths) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath); let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
if (!chunkChunkLists) { if (!chunkChunkLists) {
chunkChunkLists = new Set([chunkList.path]); chunkChunkLists = new Set([chunkListPath]);
chunkChunkListsMap.set(chunkPath, chunkChunkLists); chunkChunkListsMap.set(chunkPath, chunkChunkLists);
} else { } else {
chunkChunkLists.add(chunkList.path); chunkChunkLists.add(chunkListPath);
} }
} }
if (chunkList.source === "entry") { if (chunkList.source === "entry") {
markChunkListAsRuntime(chunkList.path); markChunkListAsRuntime(chunkListPath);
} }
} }
......
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
type UpdateCallback = (update: ServerMessage) => void; type UpdateCallback = (update: ServerMessage) => void;
type ChunkUpdateProvider = { type ChunkUpdateProvider = {
push: (registration: [ChunkPath, UpdateCallback]) => void; push: (registration: [ChunkListPath, UpdateCallback]) => void;
}; };
declare var TURBOPACK_CHUNK_UPDATE_LISTENERS: declare var TURBOPACK_CHUNK_UPDATE_LISTENERS:
| ChunkUpdateProvider | ChunkUpdateProvider
| [ChunkPath, UpdateCallback][] | [ChunkListPath, UpdateCallback][]
| undefined; | undefined;
// This is used by the Next.js integration test suite to notify it when HMR // This is used by the Next.js integration test suite to notify it when HMR
// updates have been completed. // updates have been completed.
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
declare var TURBOPACK_WORKER_LOCATION: string; declare var TURBOPACK_WORKER_LOCATION: string;
declare var CHUNK_BASE_PATH: string; declare var CHUNK_BASE_PATH: string;
declare var CHUNK_SUFFIX_PATH: string;
declare function instantiateModule(id: ModuleId, source: SourceInfo): Module; declare function instantiateModule(id: ModuleId, source: SourceInfo): Module;
type RuntimeParams = { type RuntimeParams = {
...@@ -27,7 +28,7 @@ type ChunkRegistration = [ ...@@ -27,7 +28,7 @@ type ChunkRegistration = [
]; ];
type ChunkList = { type ChunkList = {
path: ChunkPath; path: ChunkListPath;
chunks: ChunkData[]; chunks: ChunkData[];
source: "entry" | "dynamic"; source: "entry" | "dynamic";
}; };
...@@ -65,12 +66,12 @@ type SourceInfo = ...@@ -65,12 +66,12 @@ type SourceInfo =
interface RuntimeBackend { interface RuntimeBackend {
registerChunk: (chunkPath: ChunkPath, params?: RuntimeParams) => void; registerChunk: (chunkPath: ChunkPath, params?: RuntimeParams) => void;
loadChunk: (chunkPath: ChunkPath, source: SourceInfo) => Promise<void>; loadChunk: (chunkUrl: ChunkUrl, source: SourceInfo) => Promise<void>;
} }
interface DevRuntimeBackend { interface DevRuntimeBackend {
reloadChunk?: (chunkPath: ChunkPath) => Promise<void>; reloadChunk?: (chunkUrl: ChunkUrl) => Promise<void>;
unloadChunk?: (chunkPath: ChunkPath) => void; unloadChunk?: (chunkUrl: ChunkUrl) => void;
restart: () => void; restart: () => void;
} }
...@@ -90,21 +91,21 @@ const moduleChunksMap: Map<ModuleId, Set<ChunkPath>> = new Map(); ...@@ -90,21 +91,21 @@ const moduleChunksMap: Map<ModuleId, Set<ChunkPath>> = new Map();
/** /**
* Map from a chunk path to all modules it contains. * Map from a chunk path to all modules it contains.
*/ */
const chunkModulesMap: Map<ModuleId, Set<ChunkPath>> = new Map(); const chunkModulesMap: Map<ChunkPath, Set<ModuleId>> = new Map();
/** /**
* Chunk lists that contain a runtime. When these chunk lists receive an update * Chunk lists that contain a runtime. When these chunk lists receive an update
* that can't be reconciled with the current state of the page, we need to * that can't be reconciled with the current state of the page, we need to
* reload the runtime entirely. * reload the runtime entirely.
*/ */
const runtimeChunkLists: Set<ChunkPath> = new Set(); const runtimeChunkLists: Set<ChunkListPath> = new Set();
/** /**
* Map from a chunk list to the chunk paths it contains. * Map from a chunk list to the chunk paths it contains.
*/ */
const chunkListChunksMap: Map<ChunkPath, Set<ChunkPath>> = new Map(); const chunkListChunksMap: Map<ChunkListPath, Set<ChunkPath>> = new Map();
/** /**
* Map from a chunk path to the chunk lists it belongs to. * Map from a chunk path to the chunk lists it belongs to.
*/ */
const chunkChunkListsMap: Map<ChunkPath, Set<ChunkPath>> = new Map(); const chunkChunkListsMap: Map<ChunkPath, Set<ChunkListPath>> = new Map();
const availableModules: Map<ModuleId, Promise<any> | true> = new Map(); const availableModules: Map<ModuleId, Promise<any> | true> = new Map();
...@@ -184,12 +185,9 @@ async function loadChunk( ...@@ -184,12 +185,9 @@ async function loadChunk(
return promise; return promise;
} }
async function loadChunkPath( async function loadChunkByUrl(source: SourceInfo, chunkUrl: ChunkUrl) {
source: SourceInfo,
chunkPath: ChunkPath
): Promise<any> {
try { try {
await BACKEND.loadChunk(chunkPath, source); await BACKEND.loadChunk(chunkUrl, source);
} catch (error) { } catch (error) {
let loadReason; let loadReason;
switch (source.type) { switch (source.type) {
...@@ -206,7 +204,7 @@ async function loadChunkPath( ...@@ -206,7 +204,7 @@ async function loadChunkPath(
invariant(source, (source) => `Unknown source type: ${source?.type}`); invariant(source, (source) => `Unknown source type: ${source?.type}`);
} }
throw new Error( throw new Error(
`Failed to load chunk ${chunkPath} ${loadReason}${ `Failed to load chunk ${chunkUrl} ${loadReason}${
error ? `: ${error}` : "" error ? `: ${error}` : ""
}`, }`,
error error
...@@ -218,6 +216,14 @@ async function loadChunkPath( ...@@ -218,6 +216,14 @@ async function loadChunkPath(
} }
} }
async function loadChunkPath(
source: SourceInfo,
chunkPath: ChunkPath
): Promise<any> {
const url = getChunkRelativeUrl(chunkPath);
return loadChunkByUrl(source, url);
}
/** /**
* Returns an absolute url to an asset. * Returns an absolute url to an asset.
*/ */
...@@ -292,11 +298,11 @@ function instantiateRuntimeModule( ...@@ -292,11 +298,11 @@ function instantiateRuntimeModule(
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ */
function getChunkRelativeUrl(chunkPath: ChunkPath): string { function getChunkRelativeUrl(chunkPath: ChunkPath | ChunkListPath): ChunkUrl {
return `${CHUNK_BASE_PATH}${chunkPath return `${CHUNK_BASE_PATH}${chunkPath
.split("/") .split("/")
.map((p) => encodeURIComponent(p)) .map((p) => encodeURIComponent(p))
.join("/")}`; .join("/")}${CHUNK_SUFFIX_PATH}` as ChunkUrl;
} }
/** /**
...@@ -304,7 +310,7 @@ function getChunkRelativeUrl(chunkPath: ChunkPath): string { ...@@ -304,7 +310,7 @@ function getChunkRelativeUrl(chunkPath: ChunkPath): string {
* runtime chunk list. For instance, integration tests can have multiple chunk * runtime chunk list. For instance, integration tests can have multiple chunk
* groups loaded at runtime, each with its own chunk list. * groups loaded at runtime, each with its own chunk list.
*/ */
function markChunkListAsRuntime(chunkListPath: ChunkPath) { function markChunkListAsRuntime(chunkListPath: ChunkListPath) {
runtimeChunkLists.add(chunkListPath); runtimeChunkLists.add(chunkListPath);
} }
...@@ -322,3 +328,19 @@ function registerChunk([ ...@@ -322,3 +328,19 @@ function registerChunk([
return BACKEND.registerChunk(chunkPath, runtimeParams); return BACKEND.registerChunk(chunkPath, runtimeParams);
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment.
*/
function isJs(chunkUrlOrPath: ChunkUrl | ChunkPath): boolean {
return regexJsUrl.test(chunkUrlOrPath);
}
const regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/;
/**
* Checks if a given path/URL ends with .css, optionally followed by ?query or #fragment.
*/
function isCss(chunkUrl: ChunkUrl): boolean {
return regexCssUrl.test(chunkUrl);
}
...@@ -16,21 +16,20 @@ let DEV_BACKEND: DevRuntimeBackend; ...@@ -16,21 +16,20 @@ let DEV_BACKEND: DevRuntimeBackend;
(() => { (() => {
DEV_BACKEND = { DEV_BACKEND = {
unloadChunk(chunkPath) { unloadChunk(chunkUrl) {
deleteResolver(chunkPath); deleteResolver(chunkUrl);
const chunkUrl = getChunkRelativeUrl(chunkPath);
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped. // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl); const decodedChunkUrl = decodeURI(chunkUrl);
if (chunkPath.endsWith(".css")) { if (isCss(chunkUrl)) {
const links = document.querySelectorAll( const links = document.querySelectorAll(
`link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]` `link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]`
); );
for (const link of Array.from(links)) { for (const link of Array.from(links)) {
link.remove(); link.remove();
} }
} else if (chunkPath.endsWith(".js")) { } else if (isJs(chunkUrl)) {
// Unloading a JS chunk would have no effect, as it lives in the JS // Unloading a JS chunk would have no effect, as it lives in the JS
// runtime once evaluated. // runtime once evaluated.
// However, we still want to remove the script tag from the DOM to keep // However, we still want to remove the script tag from the DOM to keep
...@@ -42,26 +41,24 @@ let DEV_BACKEND: DevRuntimeBackend; ...@@ -42,26 +41,24 @@ let DEV_BACKEND: DevRuntimeBackend;
script.remove(); script.remove();
} }
} else { } else {
throw new Error(`can't infer type of chunk from path ${chunkPath}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
}, },
reloadChunk(chunkPath) { reloadChunk(chunkUrl) {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
if (!chunkPath.endsWith(".css")) { if (!isCss(chunkUrl)) {
reject(new Error("The DOM backend can only reload CSS chunks")); reject(new Error("The DOM backend can only reload CSS chunks"));
return; return;
} }
const chunkUrl = getChunkRelativeUrl(chunkPath);
const decodedChunkUrl = decodeURI(chunkUrl); const decodedChunkUrl = decodeURI(chunkUrl);
const previousLinks = document.querySelectorAll( const previousLinks = document.querySelectorAll(
`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]` `link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`
); );
if (previousLinks.length === 0) { if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkPath}`)); reject(new Error(`No link element found for chunk ${chunkUrl}`));
return; return;
} }
...@@ -108,14 +105,14 @@ let DEV_BACKEND: DevRuntimeBackend; ...@@ -108,14 +105,14 @@ let DEV_BACKEND: DevRuntimeBackend;
restart: () => self.location.reload(), restart: () => self.location.reload(),
}; };
function deleteResolver(chunkPath: ChunkPath) { function deleteResolver(chunkUrl: ChunkUrl) {
chunkResolvers.delete(chunkPath); chunkResolvers.delete(chunkUrl);
} }
})(); })();
function _eval({ code, url, map }: EcmascriptModuleEntry): ModuleFactory { function _eval({ code, url, map }: EcmascriptModuleEntry): ModuleFactory {
code += `\n\n//# sourceURL=${encodeURI( code += `\n\n//# sourceURL=${encodeURI(
location.origin + CHUNK_BASE_PATH + url location.origin + CHUNK_BASE_PATH + url + CHUNK_SUFFIX_PATH
)}`; )}`;
if (map) { if (map) {
code += `\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa( code += `\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(
......
...@@ -51,12 +51,14 @@ async function loadWebAssemblyModule( ...@@ -51,12 +51,14 @@ async function loadWebAssemblyModule(
/** /**
* Maps chunk paths to the corresponding resolver. * Maps chunk paths to the corresponding resolver.
*/ */
const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map();
(() => { (() => {
BACKEND = { BACKEND = {
async registerChunk(chunkPath, params) { async registerChunk(chunkPath, params) {
const resolver = getOrCreateResolver(chunkPath); const chunkUrl = getChunkRelativeUrl(chunkPath);
const resolver = getOrCreateResolver(chunkUrl);
resolver.resolve(); resolver.resolve();
if (params == null) { if (params == null) {
...@@ -64,9 +66,11 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); ...@@ -64,9 +66,11 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map();
} }
for (const otherChunkData of params.otherChunks) { for (const otherChunkData of params.otherChunks) {
const otherChunkPath = getChunkPath(otherChunkData); const otherChunkPath = getChunkPath(otherChunkData)
const otherChunkUrl = getChunkRelativeUrl(otherChunkPath);
// Chunk might have started loading, so we want to avoid triggering another load. // Chunk might have started loading, so we want to avoid triggering another load.
getOrCreateResolver(otherChunkPath); getOrCreateResolver(otherChunkUrl);
} }
// This waits for chunks to be loaded, but also marks included items as available. // This waits for chunks to be loaded, but also marks included items as available.
...@@ -83,13 +87,17 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); ...@@ -83,13 +87,17 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map();
} }
}, },
loadChunk(chunkPath, source) { /**
return doLoadChunk(chunkPath, source); * Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/
loadChunk(chunkUrl, source) {
return doLoadChunk(chunkUrl, source);
}, },
}; };
function getOrCreateResolver(chunkPath: ChunkPath): ChunkResolver { function getOrCreateResolver(chunkUrl: ChunkUrl): ChunkResolver {
let resolver = chunkResolvers.get(chunkPath); let resolver = chunkResolvers.get(chunkUrl);
if (!resolver) { if (!resolver) {
let resolve: () => void; let resolve: () => void;
let reject: (error?: Error) => void; let reject: (error?: Error) => void;
...@@ -106,7 +114,7 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); ...@@ -106,7 +114,7 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map();
}, },
reject: reject!, reject: reject!,
}; };
chunkResolvers.set(chunkPath, resolver); chunkResolvers.set(chunkUrl, resolver);
} }
return resolver; return resolver;
} }
...@@ -115,8 +123,8 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); ...@@ -115,8 +123,8 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map();
* Loads the given chunk, and returns a promise that resolves once the chunk * Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded. * has been loaded.
*/ */
async function doLoadChunk(chunkPath: ChunkPath, source: SourceInfo) { function doLoadChunk(chunkUrl: ChunkUrl, source: SourceInfo) {
const resolver = getOrCreateResolver(chunkPath); const resolver = getOrCreateResolver(chunkUrl);
if (resolver.resolved) { if (resolver.resolved) {
return resolver.promise; return resolver.promise;
} }
...@@ -125,7 +133,7 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); ...@@ -125,7 +133,7 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map();
// We don't need to load chunks references from runtime code, as they're already // We don't need to load chunks references from runtime code, as they're already
// present in the DOM. // present in the DOM.
if (chunkPath.endsWith(".css")) { if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as // CSS chunks do not register themselves, and as such must be marked as
// loaded instantly. // loaded instantly.
resolver.resolve(); resolver.resolve();
...@@ -138,20 +146,20 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); ...@@ -138,20 +146,20 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map();
return resolver.promise; return resolver.promise;
} }
const chunkUrl = getChunkRelativeUrl(chunkPath);
const decodedChunkUrl = decodeURI(chunkUrl);
if (typeof importScripts === "function") { if (typeof importScripts === "function") {
// We're in a web worker // We're in a web worker
if (chunkPath.endsWith(".css")) { if (isCss(chunkUrl)) {
// ignore // ignore
} else if (chunkPath.endsWith(".js")) { } else if (isJs(chunkUrl)) {
importScripts(TURBOPACK_WORKER_LOCATION + chunkUrl); importScripts(TURBOPACK_WORKER_LOCATION + chunkUrl);
} else { } else {
throw new Error(`can't infer type of chunk from path ${chunkPath} in worker`); throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`);
} }
} else { } else {
if (chunkPath.endsWith(".css")) { // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl);
if (isCss(chunkUrl)) {
const previousLinks = document.querySelectorAll( const previousLinks = document.querySelectorAll(
`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]` `link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`
); );
...@@ -173,7 +181,7 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); ...@@ -173,7 +181,7 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map();
}; };
document.body.appendChild(link); document.body.appendChild(link);
} }
} else if (chunkPath.endsWith(".js")) { } else if (isJs(chunkUrl)) {
const previousScripts = document.querySelectorAll( const previousScripts = document.querySelectorAll(
`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]` `script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`
); );
...@@ -197,10 +205,11 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map(); ...@@ -197,10 +205,11 @@ const chunkResolvers: Map<ChunkPath, ChunkResolver> = new Map();
document.body.appendChild(script); document.body.appendChild(script);
} }
} else { } else {
throw new Error(`can't infer type of chunk from path ${chunkPath}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
} }
return resolver.promise; return resolver.promise;
} }
})(); })();
...@@ -112,14 +112,14 @@ async function loadWebAssemblyModule( ...@@ -112,14 +112,14 @@ async function loadWebAssemblyModule(
chunkPath, chunkPath,
params.otherChunks.filter((chunk) => params.otherChunks.filter((chunk) =>
// The none runtime can only handle JS chunks, so we only wait for these // The none runtime can only handle JS chunks, so we only wait for these
getChunkPath(chunk).endsWith(".js") isJs(getChunkPath(chunk))
), ),
params.runtimeModuleIds params.runtimeModuleIds
); );
} }
}, },
loadChunk(_chunkPath, _fromChunkPath) { loadChunk(_chunkUrl, _source) {
throw new Error("chunk loading is not supported"); throw new Error("chunk loading is not supported");
}, },
}; };
......
...@@ -94,7 +94,7 @@ function loadChunk(chunkData: ChunkData, source?: SourceInfo): void { ...@@ -94,7 +94,7 @@ function loadChunk(chunkData: ChunkData, source?: SourceInfo): void {
} }
function loadChunkPath(chunkPath: ChunkPath, source?: SourceInfo): void { function loadChunkPath(chunkPath: ChunkPath, source?: SourceInfo): void {
if (!chunkPath.endsWith(".js")) { if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js. // We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk. // This branch can be hit when trying to load a CSS chunk.
return; return;
...@@ -127,7 +127,7 @@ async function loadChunkAsync( ...@@ -127,7 +127,7 @@ async function loadChunkAsync(
chunkData: ChunkData chunkData: ChunkData
): Promise<any> { ): Promise<any> {
const chunkPath = typeof chunkData === "string" ? chunkData : chunkData.path; const chunkPath = typeof chunkData === "string" ? chunkData : chunkData.path;
if (!chunkPath.endsWith(".js")) { if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js. // We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk. // This branch can be hit when trying to load a CSS chunk.
return; return;
...@@ -173,6 +173,11 @@ async function loadChunkAsync( ...@@ -173,6 +173,11 @@ async function loadChunkAsync(
} }
} }
async function loadChunkAsyncByUrl(source: SourceInfo, chunkUrl: string) {
const path = url.fileURLToPath(new URL(chunkUrl, RUNTIME_ROOT)) as ChunkPath;
return loadChunkAsync(source, path);
}
function loadWebAssembly(chunkPath: ChunkPath, imports: WebAssembly.Imports) { function loadWebAssembly(chunkPath: ChunkPath, imports: WebAssembly.Imports) {
const resolved = path.resolve(RUNTIME_ROOT, chunkPath); const resolved = path.resolve(RUNTIME_ROOT, chunkPath);
...@@ -256,6 +261,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): ModuleWithDirectio ...@@ -256,6 +261,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): ModuleWithDirectio
c: moduleCache, c: moduleCache,
M: moduleFactories, M: moduleFactories,
l: loadChunkAsync.bind(null, { type: SourceType.Parent, parentId: id }), l: loadChunkAsync.bind(null, { type: SourceType.Parent, parentId: id }),
L: loadChunkAsyncByUrl.bind(null, { type: SourceType.Parent, parentId: id }),
w: loadWebAssembly, w: loadWebAssembly,
u: loadWebAssemblyModule, u: loadWebAssemblyModule,
g: globalThis, g: globalThis,
...@@ -337,6 +343,14 @@ function getOrInstantiateRuntimeModule( ...@@ -337,6 +343,14 @@ function getOrInstantiateRuntimeModule(
return instantiateRuntimeModule(moduleId, chunkPath); return instantiateRuntimeModule(moduleId, chunkPath);
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment.
*/
function isJs(chunkUrlOrPath: ChunkUrl | ChunkPath): boolean {
return regexJsUrl.test(chunkUrlOrPath);
}
module.exports = { module.exports = {
getOrInstantiateRuntimeModule, getOrInstantiateRuntimeModule,
loadChunk, loadChunk,
......
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
* specific to the runtime context. * specific to the runtime context.
*/ */
type ChunkPath = string; type ChunkListPath = string & { readonly brand: unique symbol };
type ChunkPath = string & { readonly brand: unique symbol };
type ChunkUrl = string & { readonly brand: unique symbol };
type ModuleId = string; type ModuleId = string;
interface Exports { interface Exports {
...@@ -37,6 +39,7 @@ type ExportNamespace = (namespace: any) => void; ...@@ -37,6 +39,7 @@ type ExportNamespace = (namespace: any) => void;
type DynamicExport = (object: Record<string, any>) => void; type DynamicExport = (object: Record<string, any>) => void;
type LoadChunk = (chunkPath: ChunkPath) => Promise<any> | undefined; type LoadChunk = (chunkPath: ChunkPath) => Promise<any> | undefined;
type LoadChunkByUrl = (chunkUrl: ChunkUrl) => Promise<any> | undefined;
type LoadWebAssembly = ( type LoadWebAssembly = (
wasmChunkPath: ChunkPath, wasmChunkPath: ChunkPath,
imports: WebAssembly.Imports imports: WebAssembly.Imports
...@@ -95,6 +98,7 @@ interface TurbopackBaseContext<M> { ...@@ -95,6 +98,7 @@ interface TurbopackBaseContext<M> {
c: ModuleCache<M>; c: ModuleCache<M>;
M: ModuleFactories; M: ModuleFactories;
l: LoadChunk; l: LoadChunk;
L: LoadChunkByUrl;
w: LoadWebAssembly; w: LoadWebAssembly;
u: LoadWebAssemblyModule; u: LoadWebAssemblyModule;
g: typeof globalThis; g: typeof globalThis;
......
...@@ -18,6 +18,7 @@ use crate::{asset_context::get_runtime_asset_context, embed_js::embed_static_cod ...@@ -18,6 +18,7 @@ use crate::{asset_context::get_runtime_asset_context, embed_js::embed_static_cod
pub async fn get_browser_runtime_code( pub async fn get_browser_runtime_code(
environment: Vc<Environment>, environment: Vc<Environment>,
chunk_base_path: Vc<Option<RcStr>>, chunk_base_path: Vc<Option<RcStr>>,
chunk_suffix_path: Vc<Option<RcStr>>,
runtime_type: Value<RuntimeType>, runtime_type: Value<RuntimeType>,
output_root_to_root_path: Vc<RcStr>, output_root_to_root_path: Vc<RcStr>,
generate_source_map: bool, generate_source_map: bool,
...@@ -80,6 +81,10 @@ pub async fn get_browser_runtime_code( ...@@ -80,6 +81,10 @@ pub async fn get_browser_runtime_code(
let relative_root_path = output_root_to_root_path.await?; let relative_root_path = output_root_to_root_path.await?;
let chunk_base_path = &*chunk_base_path.await?; let chunk_base_path = &*chunk_base_path.await?;
let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str()); let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str());
let chunk_suffix_path = &*chunk_suffix_path.await?;
let chunk_suffix_path = chunk_suffix_path
.as_ref()
.map_or_else(|| "", |f| f.as_str());
writedoc!( writedoc!(
code, code,
...@@ -90,10 +95,12 @@ pub async fn get_browser_runtime_code( ...@@ -90,10 +95,12 @@ pub async fn get_browser_runtime_code(
}} }}
const CHUNK_BASE_PATH = {}; const CHUNK_BASE_PATH = {};
const CHUNK_SUFFIX_PATH = {};
const RELATIVE_ROOT_PATH = {}; const RELATIVE_ROOT_PATH = {};
const RUNTIME_PUBLIC_PATH = {}; const RUNTIME_PUBLIC_PATH = {};
"#, "#,
StringifyJs(chunk_base_path), StringifyJs(chunk_base_path),
StringifyJs(chunk_suffix_path),
StringifyJs(relative_root_path.as_str()), StringifyJs(relative_root_path.as_str()),
StringifyJs(chunk_base_path), StringifyJs(chunk_base_path),
)?; )?;
......
...@@ -54,6 +54,8 @@ pub const TURBOPACK_MODULES: &TurbopackRuntimeFunctionShortcut = ...@@ -54,6 +54,8 @@ pub const TURBOPACK_MODULES: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.M", "M"); &TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.M", "M");
pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.l", "l"); &TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.l", "l");
pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.L", "L");
pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.j", "j"); &TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.j", "j");
pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut =
...@@ -77,7 +79,7 @@ pub const TURBOPACK_REQUIRE_STUB: &TurbopackRuntimeFunctionShortcut = ...@@ -77,7 +79,7 @@ pub const TURBOPACK_REQUIRE_STUB: &TurbopackRuntimeFunctionShortcut =
pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.t", "t"); &TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.t", "t");
pub const TUBROPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 20] = [ pub const TUBROPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 21] = [
("__turbopack_require__", TURBOPACK_REQUIRE), ("__turbopack_require__", TURBOPACK_REQUIRE),
("__turbopack_module_context__", TURBOPACK_MODULE_CONTEXT), ("__turbopack_module_context__", TURBOPACK_MODULE_CONTEXT),
("__turbopack_import__", TURBOPACK_IMPORT), ("__turbopack_import__", TURBOPACK_IMPORT),
...@@ -87,6 +89,7 @@ pub const TUBROPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctio ...@@ -87,6 +89,7 @@ pub const TUBROPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctio
("__turbopack_cache__", TURBOPACK_CACHE), ("__turbopack_cache__", TURBOPACK_CACHE),
("__turbopack_modules__", TURBOPACK_MODULES), ("__turbopack_modules__", TURBOPACK_MODULES),
("__turbopack_load__", TURBOPACK_LOAD), ("__turbopack_load__", TURBOPACK_LOAD),
("__turbopack_load_by_url__", TURBOPACK_LOAD_BY_URL),
("__turbopack_dynamic__", TURBOPACK_DYNAMIC), ("__turbopack_dynamic__", TURBOPACK_DYNAMIC),
( (
"__turbopack_resolve_absolute_path__", "__turbopack_resolve_absolute_path__",
......
...@@ -466,7 +466,7 @@ function loadChunk(chunkData, source) { ...@@ -466,7 +466,7 @@ function loadChunk(chunkData, source) {
} }
} }
function loadChunkPath(chunkPath, source) { function loadChunkPath(chunkPath, source) {
if (!chunkPath.endsWith(".js")) { if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js. // We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk. // This branch can be hit when trying to load a CSS chunk.
return; return;
...@@ -491,7 +491,7 @@ function loadChunkPath(chunkPath, source) { ...@@ -491,7 +491,7 @@ function loadChunkPath(chunkPath, source) {
} }
async function loadChunkAsync(source, chunkData) { async function loadChunkAsync(source, chunkData) {
const chunkPath = typeof chunkData === "string" ? chunkData : chunkData.path; const chunkPath = typeof chunkData === "string" ? chunkData : chunkData.path;
if (!chunkPath.endsWith(".js")) { if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js. // We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk. // This branch can be hit when trying to load a CSS chunk.
return; return;
...@@ -529,6 +529,10 @@ async function loadChunkAsync(source, chunkData) { ...@@ -529,6 +529,10 @@ async function loadChunkAsync(source, chunkData) {
}); });
} }
} }
async function loadChunkAsyncByUrl(source, chunkUrl) {
const path1 = url.fileURLToPath(new URL(chunkUrl, RUNTIME_ROOT));
return loadChunkAsync(source, path1);
}
function loadWebAssembly(chunkPath, imports) { function loadWebAssembly(chunkPath, imports) {
const resolved = path.resolve(RUNTIME_ROOT, chunkPath); const resolved = path.resolve(RUNTIME_ROOT, chunkPath);
return instantiateWebAssemblyFromPath(resolved, imports); return instantiateWebAssemblyFromPath(resolved, imports);
...@@ -607,6 +611,10 @@ function instantiateModule(id, source) { ...@@ -607,6 +611,10 @@ function instantiateModule(id, source) {
type: 1, type: 1,
parentId: id parentId: id
}), }),
L: loadChunkAsyncByUrl.bind(null, {
type: 1,
parentId: id
}),
w: loadWebAssembly, w: loadWebAssembly,
u: loadWebAssemblyModule, u: loadWebAssemblyModule,
g: globalThis, g: globalThis,
...@@ -668,6 +676,12 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { ...@@ -668,6 +676,12 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
} }
return instantiateRuntimeModule(moduleId, chunkPath); return instantiateRuntimeModule(moduleId, chunkPath);
} }
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment.
*/ function isJs(chunkUrlOrPath) {
return regexJsUrl.test(chunkUrlOrPath);
}
module.exports = { module.exports = {
getOrInstantiateRuntimeModule, getOrInstantiateRuntimeModule,
loadChunk loadChunk
......
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