Unverified Commit d155ba9e authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

[turbopack] Lazily compile dynamic imports in development (client side) (#97203)

## Summary

Defer compiling client-side dynamic import targets in Turbopack
development until the browser requests their manifest chunk. This avoids
compiling untouched dynamic imports while preserving server-side
imports, CSS loading, Server Actions, source maps, and Fast Refresh
behavior.


One caveat on this is next/dynamic still does some eagerness. I looked a
bit at changing things. But it breaks some of the guarantees there and
decided to leave it off the table.

---------
Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent c5741d52
...@@ -52,7 +52,7 @@ use turbopack_core::{ ...@@ -52,7 +52,7 @@ use turbopack_core::{
asset::AssetContent, asset::AssetContent,
chunk::{ chunk::{
ChunkGroupResult, ChunkingContext, ChunkingContextExt, EvaluatableAsset, EvaluatableAssets, ChunkGroupResult, ChunkingContext, ChunkingContextExt, EvaluatableAsset, EvaluatableAssets,
availability_info::AvailabilityInfo, HmrChunkListSource, availability_info::AvailabilityInfo,
}, },
file_source::FileSource, file_source::FileSource,
ident::{AssetIdent, Layer}, ident::{AssetIdent, Layer},
...@@ -1434,7 +1434,11 @@ impl AppEndpoint { ...@@ -1434,7 +1434,11 @@ impl AppEndpoint {
let client_reference_chunks = let client_reference_chunks =
get_client_references_chunks_for_hmr(*client_references_chunks); get_client_references_chunks_for_hmr(*client_references_chunks);
client_chunking_context client_chunking_context
.hmr_chunk_list(client_components_chunks_ident, client_reference_chunks) .hmr_chunk_list(
client_components_chunks_ident,
client_reference_chunks,
HmrChunkListSource::Entry,
)
.await? .await?
.iter() .iter()
.copied() .copied()
......
...@@ -87,6 +87,9 @@ use turbopack_core::{ ...@@ -87,6 +87,9 @@ use turbopack_core::{
NotFoundVersion, OptionVersionedContent, Update, Version, VersionState, VersionedContent, NotFoundVersion, OptionVersionedContent, Update, Version, VersionState, VersionedContent,
}, },
}; };
use turbopack_ecmascript::async_chunk::proxy::{
activation_key_from_chunk_path, lazy_compilation_state,
};
#[cfg(all(feature = "process_pool", not(target_family = "wasm")))] #[cfg(all(feature = "process_pool", not(target_family = "wasm")))]
use turbopack_node::child_process_backend; use turbopack_node::child_process_backend;
use turbopack_node::execution_context::ExecutionContext; use turbopack_node::execution_context::ExecutionContext;
...@@ -456,6 +459,17 @@ fn project_operation(project: ResolvedVc<ProjectContainer>) -> Vc<Project> { ...@@ -456,6 +459,17 @@ fn project_operation(project: ResolvedVc<ProjectContainer>) -> Vc<Project> {
project.project() project.project()
} }
/// Activates the lazy dynamic import that `chunk_path` names, returning whether it named one. The
/// caller has to rebuild the owning entrypoint before serving the request.
#[turbo_tasks::function(operation, root)]
pub async fn activate_lazy_chunk_operation(chunk_path: RcStr) -> Result<Vc<bool>> {
let Some(key) = activation_key_from_chunk_path(&chunk_path) else {
return Ok(Vc::cell(false));
};
lazy_compilation_state(key).await?.activate();
Ok(Vc::cell(true))
}
#[turbo_tasks::function(operation, root)] #[turbo_tasks::function(operation, root)]
fn project_fs_operation(project: ResolvedVc<Project>) -> Vc<DiskFileSystem> { fn project_fs_operation(project: ResolvedVc<Project>) -> Vc<DiskFileSystem> {
project.project_fs() project.project_fs()
...@@ -1599,6 +1613,9 @@ impl Project { ...@@ -1599,6 +1613,9 @@ impl Project {
.turbo_nested_async_chunking(self.next_mode(), true), .turbo_nested_async_chunking(self.next_mode(), true),
shared_runtime: self.next_config().turbo_shared_runtime(self.next_mode()), shared_runtime: self.next_config().turbo_shared_runtime(self.next_mode()),
per_page_module_graph: self.per_page_module_graph(), per_page_module_graph: self.per_page_module_graph(),
lazy_dynamic_imports: self
.next_config()
.turbopack_lazy_dynamic_imports(*self.next_mode().await?),
debug_ids: self.next_config().turbopack_debug_ids(), debug_ids: self.next_config().turbopack_debug_ids(),
worker_asset_prefix: self.next_config().turbopack_worker_asset_prefix(), worker_asset_prefix: self.next_config().turbopack_worker_asset_prefix(),
should_use_absolute_url_references: self.next_config().inline_css(), should_use_absolute_url_references: self.next_config().inline_css(),
......
...@@ -379,6 +379,9 @@ pub async fn get_client_module_options_context( ...@@ -379,6 +379,9 @@ pub async fn get_client_module_options_context(
mangle_export_names: *next_config.turbopack_mangle_export_names(mode).await?, mangle_export_names: *next_config.turbopack_mangle_export_names(mode).await?,
cjs_scope_hoisting: *next_config.turbopack_cjs_scope_hoisting().await?, cjs_scope_hoisting: *next_config.turbopack_cjs_scope_hoisting().await?,
cross_module_constants: *next_config.turbopack_cross_module_constants().await?, cross_module_constants: *next_config.turbopack_cross_module_constants().await?,
lazy_compilation: *next_config
.turbopack_lazy_dynamic_imports(*next_mode)
.await?,
preset_env_config, preset_env_config,
..Default::default() ..Default::default()
}, },
...@@ -411,6 +414,7 @@ pub async fn get_client_module_options_context( ...@@ -411,6 +414,7 @@ pub async fn get_client_module_options_context(
enable_typeof_window_inlining: None, enable_typeof_window_inlining: None,
// Ignore e.g. import(`${url}`) requests in node_modules. // Ignore e.g. import(`${url}`) requests in node_modules.
ignore_dynamic_requests: true, ignore_dynamic_requests: true,
lazy_compilation: false,
// Don't inject core-js polyfills into node_modules — only user code // Don't inject core-js polyfills into node_modules — only user code
// should be processed by preset_env's usage/entry mode. // should be processed by preset_env's usage/entry mode.
preset_env_config: None, preset_env_config: None,
...@@ -433,6 +437,7 @@ pub async fn get_client_module_options_context( ...@@ -433,6 +437,7 @@ pub async fn get_client_module_options_context(
enable_jsx: Some(JsxTransformOptions::default().resolved_cell()), enable_jsx: Some(JsxTransformOptions::default().resolved_cell()),
// Don't inject core-js polyfills into framework internals. // Don't inject core-js polyfills into framework internals.
preset_env_config: None, preset_env_config: None,
lazy_compilation: false,
..module_options_context.ecmascript.clone() ..module_options_context.ecmascript.clone()
}, },
enable_postcss_transform: None, enable_postcss_transform: None,
...@@ -492,6 +497,7 @@ pub struct ClientChunkingContextOptions { ...@@ -492,6 +497,7 @@ pub struct ClientChunkingContextOptions {
pub nested_async_chunking: Vc<bool>, pub nested_async_chunking: Vc<bool>,
pub shared_runtime: Vc<bool>, pub shared_runtime: Vc<bool>,
pub per_page_module_graph: Vc<bool>, pub per_page_module_graph: Vc<bool>,
pub lazy_dynamic_imports: Vc<bool>,
pub debug_ids: Vc<bool>, pub debug_ids: Vc<bool>,
pub worker_asset_prefix: Vc<Option<RcStr>>, pub worker_asset_prefix: Vc<Option<RcStr>>,
pub should_use_absolute_url_references: Vc<bool>, pub should_use_absolute_url_references: Vc<bool>,
...@@ -541,6 +547,7 @@ pub async fn get_client_chunking_context( ...@@ -541,6 +547,7 @@ pub async fn get_client_chunking_context(
nested_async_chunking, nested_async_chunking,
shared_runtime, shared_runtime,
per_page_module_graph, per_page_module_graph,
lazy_dynamic_imports,
debug_ids, debug_ids,
worker_asset_prefix, worker_asset_prefix,
should_use_absolute_url_references, should_use_absolute_url_references,
...@@ -618,7 +625,9 @@ pub async fn get_client_chunking_context( ...@@ -618,7 +625,9 @@ pub async fn get_client_chunking_context(
builder = builder builder = builder
.hot_module_replacement() .hot_module_replacement()
.source_map_source_type(SourceMapSourceType::AbsoluteFileUri) .source_map_source_type(SourceMapSourceType::AbsoluteFileUri)
.dynamic_chunk_content_loading(true); .dynamic_chunk_content_loading(true)
// A manifest chunk keeps a lazily compiled import's URL stable across activation.
.manifest_chunks(*lazy_dynamic_imports.await?);
} else { } else {
builder = builder builder = builder
.chunking_config( .chunking_config(
......
...@@ -1416,6 +1416,9 @@ pub struct ExperimentalConfig { ...@@ -1416,6 +1416,9 @@ pub struct ExperimentalConfig {
turbopack_worker_asset_prefix: Option<RcStr>, turbopack_worker_asset_prefix: Option<RcStr>,
turbopack_client_side_nested_async_chunking: Option<bool>, turbopack_client_side_nested_async_chunking: Option<bool>,
turbopack_server_side_nested_async_chunking: Option<bool>, turbopack_server_side_nested_async_chunking: Option<bool>,
/// Compile client dynamic import targets when their runtime proxy is first activated.
/// Development only.
turbopack_lazy_dynamic_imports: Option<bool>,
turbopack_import_type_bytes: Option<bool>, turbopack_import_type_bytes: Option<bool>,
/// Disable automatic configuration of the sass loader. /// Disable automatic configuration of the sass loader.
#[serde(default)] #[serde(default)]
...@@ -2741,6 +2744,17 @@ impl NextConfig { ...@@ -2741,6 +2744,17 @@ impl NextConfig {
})) }))
} }
#[turbo_tasks::function]
pub async fn turbopack_lazy_dynamic_imports(&self, next_mode: NextMode) -> Vc<bool> {
Vc::cell(
next_mode.is_development()
&& self
.experimental
.turbopack_lazy_dynamic_imports
.unwrap_or(false),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn turbopack_import_type_bytes(&self) -> Vc<bool> { pub async fn turbopack_import_type_bytes(&self) -> Vc<bool> {
Vc::cell( Vc::cell(
......
...@@ -30,7 +30,7 @@ use next_api::{ ...@@ -30,7 +30,7 @@ use next_api::{
}, },
project::{ project::{
DebugBuildPaths, DefineEnv, DraftModeOptions, PartialProjectOptions, Project, DebugBuildPaths, DefineEnv, DraftModeOptions, PartialProjectOptions, Project,
ProjectContainer, ProjectOptions, WatchOptions, ProjectContainer, ProjectOptions, WatchOptions, activate_lazy_chunk_operation,
}, },
project_asset_hashes_manifest::immutable_hashes_manifest_asset_if_enabled, project_asset_hashes_manifest::immutable_hashes_manifest_asset_if_enabled,
route::{Endpoint, EndpointGroupKey, Route}, route::{Endpoint, EndpointGroupKey, Route},
...@@ -747,6 +747,23 @@ pub async fn project_update( ...@@ -747,6 +747,23 @@ pub async fn project_update(
.await .await
} }
#[tracing::instrument(level = "info", name = "activate lazy chunk", skip_all)]
#[napi]
pub async fn project_activate_lazy_chunk(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: &External<ProjectInstance>,
chunk_path: RcStr,
) -> napi::Result<bool> {
let ctx = &project.turbopack_ctx;
ctx.turbo_tasks()
.run(async move {
Ok(*activate_lazy_chunk_operation(chunk_path)
.read_strongly_consistent()
.await?)
})
.await
.map_err(|error| napi::Error::from_reason(PrettyPrintError(&error.into()).to_string()))
}
/// Invalidates the filesystem cache so that it will be deleted next time that a turbopack project /// Invalidates the filesystem cache so that it will be deleted next time that a turbopack project
/// is created with filesystem cache enabled. /// is created with filesystem cache enabled.
#[napi] #[napi]
......
...@@ -567,6 +567,11 @@ export declare function parse( ...@@ -567,6 +567,11 @@ export declare function parse(
signal?: AbortSignal | undefined | null signal?: AbortSignal | undefined | null
): Promise<string> ): Promise<string>
export declare function projectActivateLazyChunk(
project: { __napiType: 'Project' },
chunkPath: RcStr
): Promise<boolean>
export declare function projectClientHmrChunkNamesSubscribe( export declare function projectClientHmrChunkNamesSubscribe(
project: { __napiType: 'Project' }, project: { __napiType: 'Project' },
func: (err: Error, value: TurbopackResult<HmrChunkNames>) => void func: (err: Error, value: TurbopackResult<HmrChunkNames>) => void
......
...@@ -696,6 +696,10 @@ function bindingToApi( ...@@ -696,6 +696,10 @@ function bindingToApi(
) )
} }
async activateLazyChunk(chunkPath: string): Promise<boolean> {
return binding.projectActivateLazyChunk(this._nativeProject, chunkPath)
}
async writeAnalyzeData( async writeAnalyzeData(
appDirOnly: boolean appDirOnly: boolean
): Promise<TurbopackResult<void>> { ): Promise<TurbopackResult<void>> {
......
...@@ -317,6 +317,8 @@ export interface UpdateInfo { ...@@ -317,6 +317,8 @@ export interface UpdateInfo {
export interface Project { export interface Project {
update(options: Partial<ProjectOptions>): Promise<void> update(options: Partial<ProjectOptions>): Promise<void>
activateLazyChunk(chunkPath: string): Promise<boolean>
writeAnalyzeData(appDirOnly: boolean): Promise<TurbopackResult<void>> writeAnalyzeData(appDirOnly: boolean): Promise<TurbopackResult<void>>
getAllCompilationIssues(): Promise<TurbopackResult<void>> getAllCompilationIssues(): Promise<TurbopackResult<void>>
......
...@@ -416,6 +416,7 @@ export const experimentalSchema = { ...@@ -416,6 +416,7 @@ export const experimentalSchema = {
turbopackWorkerAssetPrefix: z.string().optional(), turbopackWorkerAssetPrefix: z.string().optional(),
turbopackClientSideNestedAsyncChunking: z.boolean().optional(), turbopackClientSideNestedAsyncChunking: z.boolean().optional(),
turbopackServerSideNestedAsyncChunking: z.boolean().optional(), turbopackServerSideNestedAsyncChunking: z.boolean().optional(),
turbopackLazyDynamicImports: z.boolean().optional(),
turbopackImportTypeBytes: z.boolean().optional(), turbopackImportTypeBytes: z.boolean().optional(),
turbopackUseBuiltinBabel: z.boolean().optional(), turbopackUseBuiltinBabel: z.boolean().optional(),
turbopackUseBuiltinSass: z.boolean().optional(), turbopackUseBuiltinSass: z.boolean().optional(),
......
...@@ -948,6 +948,13 @@ export interface ExperimentalConfig { ...@@ -948,6 +948,13 @@ export interface ExperimentalConfig {
*/ */
turbopackServerSideNestedAsyncChunking?: boolean turbopackServerSideNestedAsyncChunking?: boolean
/**
* Compile client dynamic import targets when they are first used in development.
*
* Defaults to `false`.
*/
turbopackLazyDynamicImports?: boolean
/** /**
* Enable filesystem cache for the turbopack dev server. * Enable filesystem cache for the turbopack dev server.
* *
......
...@@ -404,6 +404,7 @@ export async function createHotReloaderTurbopack( ...@@ -404,6 +404,7 @@ export async function createHotReloaderTurbopack(
const dev = true const dev = true
const buildId = 'development' const buildId = 'development'
const { nextConfig, dir: projectPath } = opts const { nextConfig, dir: projectPath } = opts
const lazyDynamicImports = nextConfig.experimental.turbopackLazyDynamicImports
const bindings = getBindingsSync() const bindings = getBindingsSync()
...@@ -1436,6 +1437,34 @@ export async function createHotReloaderTurbopack( ...@@ -1436,6 +1437,34 @@ export async function createHotReloaderTurbopack(
} }
} }
// Requesting the manifest chunk of a lazily compiled dynamic import is what activates it,
// so the owning entrypoints have to be rebuilt before the static handler reads it off disk.
if (
lazyDynamicImports &&
req.url?.startsWith('/_next/static/chunks/') &&
req.url.includes('lazy-compilation-')
) {
// Turbopack names chunks relative to `/_next`, and so does `AssetMapper`.
const chunkPath = decodeURIComponent(
req.url.split('?')[0].slice('/_next/'.length)
)
const keys = assetMapper.getKeysByAsset(chunkPath)
try {
if (keys.length > 0 && (await project.activateLazyChunk(chunkPath))) {
for (const key of keys) {
await hotReloader.ensurePage({
page: splitEntryKey(key).page,
clientOnly: false,
definition: undefined,
url: req.url,
})
}
}
} catch (err) {
console.error(err)
}
}
for (const middleware of middlewares) { for (const middleware of middlewares) {
let calledNext = false let calledNext = false
......
'use server'
export async function lazyAction() {
return 'lazy-action-result'
}
'use client'
import dynamic from 'next/dynamic'
import { useState } from 'react'
const Target = dynamic(() => import('./target'))
export default function Page() {
const [show, setShow] = useState(false)
return (
<>
<button id="show-next-dynamic" onClick={() => setShow(true)}>
show
</button>
{show ? <Target /> : <p id="next-dynamic-idle">not rendered</p>}
</>
)
}
export default function Target() {
return <p>next-dynamic-parse-marker</p>
}
'use client'
import { useState } from 'react'
import { sharedValue } from './shared'
export function Demo() {
const [Target, setTarget] = useState<React.ComponentType | null>(null)
const [overlapResult, setOverlapResult] = useState('overlap idle')
return (
<>
<p id="eager">eager-marker-7c2a</p>
<p id="eager-shared">{sharedValue}</p>
<button
id="load-overlap"
onClick={async () => {
const mod = await import('./shared')
setOverlapResult(mod.sharedValue)
}}
>
load 100% overlap
</button>
<p id="overlap-result">{overlapResult}</p>
<button
id="load"
onClick={async () => {
const mod = await import('./target')
setTarget(() => mod.Target)
}}
>
load
</button>
{Target ? <Target /> : <p id="idle">idle</p>}
</>
)
}
'use client'
import { useState } from 'react'
export function DirectCssDemo() {
const [result, setResult] = useState('direct CSS idle')
const [moduleClass, setModuleClass] = useState('')
return (
<>
<button
id="load-direct-css"
onClick={async () => {
await import('../direct.css')
setResult(
getComputedStyle(document.querySelector('#direct-css-result')!)
.color
)
}}
>
load direct CSS
</button>
<p className="direct-css-target" id="direct-css-result">
{result}
</p>
<button
id="load-direct-css-module"
onClick={async () => {
const namespace = await import('../direct.module.css')
setModuleClass(namespace.default.target)
}}
>
load direct CSS Module
</button>
<p className={moduleClass} id="direct-css-module-result">
{moduleClass || 'direct CSS Module idle'}
</p>
</>
)
}
import { DirectCssDemo } from './direct-css-demo'
export default function Page() {
return <DirectCssDemo />
}
.direct-css-target {
color: rgb(12, 34, 56);
--direct-css-marker: direct-css-marker-f6a1;
}
'use client'
import { useState } from 'react'
import { loadTarget } from './load-target'
export function DuplicateDemo() {
const [first, setFirst] = useState('first idle')
const [second, setSecond] = useState('second idle')
const [third, setThird] = useState('third idle')
return (
<>
<button
id="load-first"
onClick={async () => setFirst((await import('./target')).value)}
>
{first}
</button>
<button
id="load-second"
onClick={async () => setSecond((await import('./target')).value)}
>
{second}
</button>
<button
id="load-third"
onClick={async () => setThird(await loadTarget())}
>
{third}
</button>
</>
)
}
export async function loadTarget() {
return (await import('./target')).value
}
import { DuplicateDemo } from './duplicate-demo'
export default function Page() {
return <DuplicateDemo />
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { Demo } from './demo'
export default function Page() {
return <Demo />
}
import { ParseErrorDemo } from './parse-error-demo'
export default function Page() {
return <ParseErrorDemo />
}
'use client'
export function ParseErrorDemo() {
return (
<>
<button
id="load-parse-error"
onClick={() => {
if (typeof window !== 'undefined') {
return import('./parse-error-target')
}
}}
>
parse target
</button>
<p id="parse-error-idle">not parsed</p>
</>
)
}
export const parseError = 'parse-error-proves-target-was-analyzed'
import { PatternDemo } from './pattern-demo'
export default function Page() {
return <PatternDemo />
}
'use client'
import { useState } from 'react'
export function PatternDemo() {
const [value, setValue] = useState('idle')
return (
<button
id="load-pattern"
onClick={async () => {
const name = location.hash === '#b' ? 'b' : 'a'
const module = await import(`./targets/${name}`)
setValue(module.value)
}}
>
{value}
</button>
)
}
'use client'
import { useState } from 'react'
export default function Page() {
const [first, setFirst] = useState('first idle')
const [second, setSecond] = useState('second idle')
return (
<>
<button
id="load-extensionless"
onClick={async () => setFirst((await import('./target')).value)}
>
{first}
</button>
<button
id="load-explicit-extension"
onClick={async () => setSecond((await import('./target.ts')).value)}
>
{second}
</button>
</>
)
}
'use server'
export async function hiddenAction() {
return 'hidden-action-result'
}
'use client'
import { useState } from 'react'
export function HiddenClient({ action }: { action: () => Promise<string> }) {
const [hydrated, setHydrated] = useState(false)
const [actionResult, setActionResult] = useState('action idle')
return (
<>
<button id="hydrate-client" onClick={() => setHydrated(true)}>
hydrate client
</button>
<p id="hydration-result">{hydrated ? 'hydrated' : 'not hydrated'}</p>
<button
id="run-hidden-action"
onClick={async () => setActionResult(await action())}
>
run hidden action
</button>
<p id="hidden-action-result">{actionResult}</p>
</>
)
}
import { hiddenAction } from './actions'
import { HiddenClient } from './hidden-client'
export function LazyServerContent() {
return (
<>
<p id="server-graph-marker">server-graph-marker</p>
<HiddenClient action={hiddenAction} />
</>
)
}
import Link from 'next/link'
export default async function Page({
searchParams,
}: {
searchParams: Promise<{ show?: string }>
}) {
if ((await searchParams).show !== '1') {
return (
<Link id="reveal-server-graph" href="?show=1" prefetch={false}>
reveal server graph
</Link>
)
}
const { LazyServerContent } = await import('./lazy-server-content')
return <LazyServerContent />
}
export default async function Page() {
const { value } = await import('./value')
return <p id="server-import">{value}</p>
}
import dynamic from 'next/dynamic'
const Value = dynamic(() => import('./value'))
export default function Page() {
return <Value />
}
export default function Value() {
return <p id="server-next-dynamic">server-next-dynamic</p>
}
export const sharedValue = 'shared-dependency-marker-b31f'
'use client'
import { useState } from 'react'
import { lazyAction } from './actions'
import { sharedValue } from './shared'
import styles from './target.module.css'
export function Target() {
const [actionResult, setActionResult] = useState('action idle')
return (
<>
<p id="target" className={styles.target}>
lazy-marker-9a4e
</p>
<p id="lazy-shared">{sharedValue}</p>
<button
id="run-action"
onClick={async () => setActionResult(await lazyAction())}
>
run action
</button>
<p id="action-result">{actionResult}</p>
</>
)
}
'use client'
import { useState } from 'react'
export default function Page() {
const [result, setResult] = useState('top-level await idle')
return (
<button
id="load-top-level-await"
onClick={async () => setResult((await import('./target')).value)}
>
{result}
</button>
)
}
export const value = await Promise.resolve('top-level await target')
import { UntouchedDemo } from './untouched-demo'
export default function Page() {
return <UntouchedDemo />
}
'use client'
import { useState } from 'react'
export function UntouchedDemo() {
const [Target, setTarget] = useState<React.ComponentType | null>(null)
return (
<>
<button
id="load-untouched"
onClick={async () => {
const mod = await import('./untouched-target')
setTarget(() => mod.UntouchedTarget)
}}
>
load untouched
</button>
{Target ? <Target /> : <p id="untouched-idle">untouched idle</p>}
</>
)
}
export function UntouchedTarget() {
return <p id="untouched-target">untouched-marker-2d8c</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbopackLazyDynamicImports: true,
},
}
module.exports = nextConfig
...@@ -10,11 +10,11 @@ use turbo_tasks_hash::HashAlgorithm; ...@@ -10,11 +10,11 @@ use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ chunk::{
AssetSuffix, Chunk, ChunkGroupResult, ChunkItem, ChunkLoadRetry, ChunkType, AssetSuffix, Chunk, ChunkGroupResult, ChunkItem, ChunkItemOrBatchWithAsyncModuleInfo,
ChunkableModule, ChunkingConfig, ChunkingConfigs, ChunkingContext, ContentHashing, ChunkItemWithAsyncModuleInfo, ChunkLoadRetry, ChunkType, ChunkableModule, ChunkingConfig,
CrossOrigin, EntryChunkGroupResult, EvaluatableAsset, EvaluatableAssets, MinifyType, ChunkingConfigs, ChunkingContext, ContentHashing, CrossOrigin, EntryChunkGroupResult,
SourceMapSourceType, SourceMapsType, UnusedReferences, UrlBehavior, EvaluatableAsset, EvaluatableAssets, HmrChunkListSource, MinifyType, SourceMapSourceType,
WorkerConfigurationOptions, SourceMapsType, UnusedReferences, UrlBehavior, WorkerConfigurationOptions,
availability_info::AvailabilityInfo, availability_info::AvailabilityInfo,
chunk_group::{MakeChunkGroupResult, make_chunk_group}, chunk_group::{MakeChunkGroupResult, make_chunk_group},
chunk_id_strategy::ModuleIdStrategy, chunk_id_strategy::ModuleIdStrategy,
...@@ -32,7 +32,9 @@ use turbopack_core::{ ...@@ -32,7 +32,9 @@ use turbopack_core::{
}; };
use turbopack_ecmascript::{ use turbopack_ecmascript::{
async_chunk::module::AsyncLoaderModule, async_chunk::module::AsyncLoaderModule,
chunk::{EcmascriptChunk, EcmascriptChunkContent, EcmascriptChunkType}, chunk::{
EcmascriptChunk, EcmascriptChunkContent, EcmascriptChunkPlaceable, EcmascriptChunkType,
},
manifest::{chunk_asset::ManifestAsyncModule, loader_module::ManifestLoaderModule}, manifest::{chunk_asset::ManifestAsyncModule, loader_module::ManifestLoaderModule},
}; };
use turbopack_ecmascript_runtime::RuntimeType; use turbopack_ecmascript_runtime::RuntimeType;
...@@ -1075,6 +1077,7 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1075,6 +1077,7 @@ impl ChunkingContext for BrowserChunkingContext {
self: Vc<Self>, self: Vc<Self>,
ident: Vc<AssetIdent>, ident: Vc<AssetIdent>,
chunks: Vc<OutputAssets>, chunks: Vc<OutputAssets>,
source: HmrChunkListSource,
) -> Result<Vc<OutputAssets>> { ) -> Result<Vc<OutputAssets>> {
let this = self.await?; let this = self.await?;
if !this.enable_hot_module_replacement { if !this.enable_hot_module_replacement {
...@@ -1088,7 +1091,10 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1088,7 +1091,10 @@ impl ChunkingContext for BrowserChunkingContext {
ident, ident,
EvaluatableAssets::empty(), EvaluatableAssets::empty(),
chunks, chunks,
EcmascriptDevChunkListSource::Entry, match source {
HmrChunkListSource::Entry => EcmascriptDevChunkListSource::Entry,
HmrChunkListSource::Dynamic => EcmascriptDevChunkListSource::Dynamic,
},
) )
.to_resolved() .to_resolved()
.await?, .await?,
...@@ -1221,7 +1227,14 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1221,7 +1227,14 @@ impl ChunkingContext for BrowserChunkingContext {
.emit(); .emit();
return Ok(module.as_chunk_item(module_graph, *chunking_context)); return Ok(module.as_chunk_item(module_graph, *chunking_context));
} }
Ok(if self.await?.manifest_chunks { let use_manifest = self.await?.manifest_chunks
// This guard is in place so that only javascript goes
// this path for lazy loading dynamic imports not things like css.
&& ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(
module.to_resolved().await?,
)
.is_some();
Ok(if use_manifest {
let manifest_asset = ManifestAsyncModule::new( let manifest_asset = ManifestAsyncModule::new(
module, module,
module_graph, module_graph,
...@@ -1236,12 +1249,49 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1236,12 +1249,49 @@ impl ChunkingContext for BrowserChunkingContext {
}) })
} }
#[turbo_tasks::function]
async fn standalone_chunk(
self: Vc<Self>,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Result<Vc<Box<dyn OutputAsset>>> {
let chunk_type = chunk_item
.into_trait_ref()
.await?
.ty()
.to_resolved()
.await?;
let chunk = chunk_type
.chunk(
Vc::upcast(self),
vec![ChunkItemOrBatchWithAsyncModuleInfo::ChunkItem(
ChunkItemWithAsyncModuleInfo {
chunk_item,
chunk_type,
module: None,
async_info: None,
},
)],
Vec::new(),
Vec::new(),
)
.to_resolved()
.await?;
Ok(*self.generate_chunk(chunk).await?)
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn async_loader_chunk_item_ident( async fn async_loader_chunk_item_ident(
self: Vc<Self>, self: Vc<Self>,
module: Vc<Box<dyn ChunkableModule>>, module: Vc<Box<dyn ChunkableModule>>,
) -> Result<Vc<AssetIdent>> { ) -> Result<Vc<AssetIdent>> {
Ok(if self.await?.manifest_chunks { let use_manifest = self.await?.manifest_chunks
// This guard is in place so that only javascript goes
// this path for lazy loading dynamic imports not things like css.
&& ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(
module.to_resolved().await?,
)
.is_some();
Ok(if use_manifest {
ManifestLoaderModule::asset_ident_for(module) ManifestLoaderModule::asset_ident_for(module)
} else { } else {
AsyncLoaderModule::asset_ident_for(module) AsyncLoaderModule::asset_ident_for(module)
......
...@@ -293,6 +293,21 @@ pub struct ChunkingConfig { ...@@ -293,6 +293,21 @@ pub struct ChunkingConfig {
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct ChunkingConfigs(FxHashMap<ResolvedVc<Box<dyn ChunkType>>, ChunkingConfig>); pub struct ChunkingConfigs(FxHashMap<ResolvedVc<Box<dyn ChunkType>>, ChunkingConfig>);
/// turbopack-browser needs to know the original
/// source of the hmr chunk list to properly map to a
/// EcmascriptDevChunkListSource and provide the correct
/// updates. This maps one to one with that.
/// We could consider lifting EcmascriptDevChunkListSource to
/// core instead if this grows. Or using this type in browser instead.
#[turbo_tasks::task_input]
#[derive(
Eq, PartialEq, Debug, Clone, Copy, Hash, TraceRawVcs, Serialize, Deserialize, Encode, Decode,
)]
pub enum HmrChunkListSource {
Entry,
Dynamic,
}
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Debug, Clone, Copy, Hash, Default, Deserialize)] #[derive(Debug, Clone, Copy, Hash, Default, Deserialize)]
pub enum SourceMapSourceType { pub enum SourceMapSourceType {
...@@ -463,6 +478,14 @@ pub trait ChunkingContext { ...@@ -463,6 +478,14 @@ pub trait ChunkingContext {
fn async_loader_chunk_item_ident(&self, module: Vc<Box<dyn ChunkableModule>>) fn async_loader_chunk_item_ident(&self, module: Vc<Box<dyn ChunkableModule>>)
-> Vc<AssetIdent>; -> Vc<AssetIdent>;
/// Places a synthesized chunk item into a standalone output chunk without module graph
/// traversal.
#[turbo_tasks::function]
fn standalone_chunk(
self: Vc<Self>,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Vc<Box<dyn OutputAsset>>;
#[turbo_tasks::function] #[turbo_tasks::function]
fn chunk_group( fn chunk_group(
self: Vc<Self>, self: Vc<Self>,
...@@ -500,6 +523,7 @@ pub trait ChunkingContext { ...@@ -500,6 +523,7 @@ pub trait ChunkingContext {
self: Vc<Self>, self: Vc<Self>,
_ident: Vc<AssetIdent>, _ident: Vc<AssetIdent>,
_chunks: Vc<OutputAssets>, _chunks: Vc<OutputAssets>,
_source: HmrChunkListSource,
) -> Vc<OutputAssets> { ) -> Vc<OutputAssets> {
OutputAssets::empty() OutputAssets::empty()
} }
......
...@@ -28,8 +28,8 @@ pub use crate::chunk::{ ...@@ -28,8 +28,8 @@ pub use crate::chunk::{
}, },
chunking_context::{ chunking_context::{
AssetSuffix, ChunkGroupResult, ChunkGroupType, ChunkingConfig, ChunkingConfigs, AssetSuffix, ChunkGroupResult, ChunkGroupType, ChunkingConfig, ChunkingConfigs,
ChunkingContext, ChunkingContextExt, EntryChunkGroupResult, MangleType, MinifyType, ChunkingContext, ChunkingContextExt, EntryChunkGroupResult, HmrChunkListSource, MangleType,
SourceMapSourceType, SourceMapsType, UnusedReferences, UrlBehavior, MinifyType, SourceMapSourceType, SourceMapsType, UnusedReferences, UrlBehavior,
WorkerConfigurationOptions, WorkerConfigurationOptions,
}, },
data::{ChunkData, ChunkDataOption, ChunksData}, data::{ChunkData, ChunkDataOption, ChunksData},
......
...@@ -454,6 +454,14 @@ impl IssueSource { ...@@ -454,6 +454,14 @@ impl IssueSource {
} }
} }
/// Drops the precise range while preserving the source file.
pub fn without_range(self) -> Self {
IssueSource {
range: None,
..self
}
}
pub fn from_line_col( pub fn from_line_col(
source: ResolvedVc<Box<dyn Source>>, source: ResolvedVc<Box<dyn Source>>,
start: SourcePos, start: SourcePos,
......
...@@ -61,6 +61,7 @@ pub enum EcmaScriptModulesReferenceSubType { ...@@ -61,6 +61,7 @@ pub enum EcmaScriptModulesReferenceSubType {
module_type: Option<RcStr>, module_type: Option<RcStr>,
}, },
DynamicImport, DynamicImport,
LazyDynamicImport,
Emit, Emit,
Custom(u8), Custom(u8),
#[default] #[default]
...@@ -329,6 +330,18 @@ pub enum ReferenceTypeCondition { ...@@ -329,6 +330,18 @@ pub enum ReferenceTypeCondition {
impl ReferenceTypeCondition { impl ReferenceTypeCondition {
pub fn includes(&self, other: &ReferenceType) -> bool { pub fn includes(&self, other: &ReferenceType) -> bool {
if matches!(
self,
ReferenceTypeCondition::EcmaScriptModules(Some(
EcmaScriptModulesReferenceSubType::DynamicImport
))
) && matches!(
other,
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::LazyDynamicImport)
) {
return true;
}
if matches!( if matches!(
self, self,
ReferenceTypeCondition::Css(Some(CssReferenceSubType::AtImport(_))) ReferenceTypeCondition::Css(Some(CssReferenceSubType::AtImport(_)))
...@@ -383,3 +396,20 @@ impl ReferenceTypeCondition { ...@@ -383,3 +396,20 @@ impl ReferenceTypeCondition {
false false
} }
} }
#[cfg(test)]
mod tests {
use super::{EcmaScriptModulesReferenceSubType, ReferenceType, ReferenceTypeCondition};
#[test]
fn lazy_dynamic_import_matches_dynamic_import_condition() {
assert!(
ReferenceTypeCondition::EcmaScriptModules(Some(
EcmaScriptModulesReferenceSubType::DynamicImport
))
.includes(&ReferenceType::EcmaScriptModules(
EcmaScriptModulesReferenceSubType::LazyDynamicImport
))
);
}
}
This diff is collapsed.
...@@ -24,6 +24,7 @@ pub mod magic_identifier; ...@@ -24,6 +24,7 @@ pub mod magic_identifier;
pub mod manifest; pub mod manifest;
mod merged_module; mod merged_module;
pub mod minify; pub mod minify;
pub mod module_canonicalization;
pub mod module_fragments; pub mod module_fragments;
pub mod parse; pub mod parse;
mod path_visitor; mod path_visitor;
...@@ -250,6 +251,8 @@ pub struct EcmascriptOptions { ...@@ -250,6 +251,8 @@ pub struct EcmascriptOptions {
pub cjs_scope_hoisting: bool, pub cjs_scope_hoisting: bool,
/// Whether to enable cross-module constant inlining. Defaults to false. /// Whether to enable cross-module constant inlining. Defaults to false.
pub cross_module_constants: bool, pub cross_module_constants: bool,
/// Whether dynamic import targets are compiled after their runtime proxy is activated.
pub lazy_compilation: bool,
} }
#[turbo_tasks::value(task_input)] #[turbo_tasks::value(task_input)]
......
...@@ -5,17 +5,18 @@ use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc}; ...@@ -5,17 +5,18 @@ use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::{ use turbopack_core::{
chunk::{ chunk::{
AsyncModuleInfo, ChunkData, ChunkableModule, ChunkingContext, ChunkingContextExt, AsyncModuleInfo, ChunkData, ChunkableModule, ChunkingContext, ChunkingContextExt,
ChunksData, availability_info::AvailabilityInfo, ChunksData, HmrChunkListSource, availability_info::AvailabilityInfo,
}, },
ident::AssetIdent, ident::AssetIdent,
module::{Module, ModuleSideEffects}, module::{Module, ModuleSideEffects},
module_graph::{ module_graph::{
ModuleGraph, chunk_group_info::ChunkGroup, module_batch::ChunkableModuleOrBatch, ModuleGraph, chunk_group_info::ChunkGroup, module_batch::ChunkableModuleOrBatch,
}, },
output::OutputAssetsWithReferenced, output::{OutputAssets, OutputAssetsWithReferenced},
}; };
use crate::{ use crate::{
async_chunk::proxy::LazyCompilationProxyModule,
chunk::{ chunk::{
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
data::EcmascriptChunkData, ecmascript_chunk_item, data::EcmascriptChunkData, ecmascript_chunk_item,
...@@ -94,11 +95,29 @@ impl ManifestAsyncModule { ...@@ -94,11 +95,29 @@ impl ManifestAsyncModule {
.cell()); .cell());
} }
} }
Ok(this.chunking_context.chunk_group_assets( let chunk_item = self.as_chunk_item(*this.module_graph, *this.chunking_context);
let chunk = this
.chunking_context
.standalone_chunk(chunk_item)
.to_resolved()
.await?;
Ok(OutputAssetsWithReferenced {
assets: ResolvedVc::cell(vec![chunk]),
referenced_assets: ResolvedVc::cell(vec![]),
references: ResolvedVc::cell(vec![]),
}
.cell())
}
/// Without a chunk list of its own, modules that are only reachable through this dynamic
/// import never receive updates.
#[turbo_tasks::function]
async fn hmr_chunk_list(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?;
Ok(this.chunking_context.hmr_chunk_list(
self.ident(), self.ident(),
ChunkGroup::Async(ResolvedVc::upcast(self)), *self.chunk_group().await?.assets,
*this.module_graph, HmrChunkListSource::Dynamic,
this.availability_info,
)) ))
} }
...@@ -128,7 +147,10 @@ impl ManifestAsyncModule { ...@@ -128,7 +147,10 @@ impl ManifestAsyncModule {
let this = self.await?; let this = self.await?;
Ok(ChunkData::from_assets( Ok(ChunkData::from_assets(
this.chunking_context.output_root().owned().await?, this.chunking_context.output_root().owned().await?,
*self.chunk_group().await?.assets, self.chunk_group()
.await?
.assets
.concatenate(self.hmr_chunk_list()),
)) ))
} }
} }
...@@ -141,12 +163,22 @@ fn manifest_chunk_reference_description() -> RcStr { ...@@ -141,12 +163,22 @@ fn manifest_chunk_reference_description() -> RcStr {
impl Module for ManifestAsyncModule { impl Module for ManifestAsyncModule {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> { async fn ident(&self) -> Result<Vc<AssetIdent>> {
Ok(self let ident = self
.inner .inner
.ident() .ident()
.owned() .owned()
.await? .await?
.with_modifier(manifest_chunk_reference_description()) .with_modifier(manifest_chunk_reference_description());
// Requesting the manifest chunk of a lazily compiled dynamic import is what activates it,
// so the key has to survive into the file name, and the path is the only part of an ident
// that appears there literally. It must not move to the proxy's own ident, which also
// names chunks that ship with the entrypoint and would activate the import on page load.
let Some(proxy) = ResolvedVc::try_downcast_type::<LazyCompilationProxyModule>(self.inner)
else {
return Ok(ident.into_vc());
};
Ok(ident
.rename_as(&format!("*.{}.js", proxy.await?.key))
.into_vc()) .into_vc())
} }
...@@ -225,5 +257,8 @@ impl EcmascriptChunkPlaceable for ManifestAsyncModule { ...@@ -225,5 +257,8 @@ impl EcmascriptChunkPlaceable for ManifestAsyncModule {
_module_graph: Vc<ModuleGraph>, _module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced> { ) -> Vc<OutputAssetsWithReferenced> {
self.chunk_group() self.chunk_group()
.concatenate(OutputAssetsWithReferenced::from_assets(
self.hmr_chunk_list(),
))
} }
} }
...@@ -145,21 +145,41 @@ impl EcmascriptChunkPlaceable for ManifestLoaderModule { ...@@ -145,21 +145,41 @@ impl EcmascriptChunkPlaceable for ManifestLoaderModule {
// will perform the actual chunk traversal and generate load statements. // will perform the actual chunk traversal and generate load statements.
let chunks_server_data = &*self.chunks_data().await?.iter().try_join().await?; let chunks_server_data = &*self.chunks_data().await?.iter().try_join().await?;
// We also need the manifest chunk item's id, which points to a CJS module that // We also need the id of the module that we're actually trying to
// exports a promise for all of the necessary chunk loads.
let item_id = this
.manifest
.chunk_item_id(*manifest.chunking_context)
.await?;
// Finally, we need the id of the module that we're actually trying to
// dynamically import. // dynamically import.
let placeable = let placeable =
ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(manifest.inner) ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(manifest.inner)
.ok_or_else(|| anyhow!("asset is not placeable in ecmascript chunk"))?; .ok_or_else(|| anyhow!("asset is not placeable in ecmascript chunk"))?;
let dynamic_id = placeable.chunk_item_id(*manifest.chunking_context).await?; let dynamic_id = placeable.chunk_item_id(*manifest.chunking_context).await?;
// This is the code that will be executed when the dynamic import is reached. if chunks_server_data.is_empty() {
// No chunks need to be loaded and thus the module is already available,
// so we can just require the module.
writedoc!(
code,
r#"
{TURBOPACK_EXPORT_VALUE}(() => {{
return Promise.resolve().then(() => {TURBOPACK_REQUIRE}({dynamic_id}));
}});
"#,
dynamic_id = StringifyModuleId(&dynamic_id),
)?;
return Ok(EcmascriptChunkItemContent {
inner_code: code.into(),
..Default::default()
}
.cell());
}
// The manifest chunk item's id points to a CJS module that exports the
// chunks required by the dynamic import.
let item_id = this
.manifest
.chunk_item_id(*manifest.chunking_context)
.await?;
// This code will be executed when the dynamic import is reached.
// It will load the manifest chunk, which will load all the chunks needed by // It will load the manifest chunk, which will load all the chunks needed by
// the dynamic import, and finally we'll be able to import the module we're // the dynamic import, and finally we'll be able to import the module we're
// trying to dynamically import. // trying to dynamically import.
......
use anyhow::{Result, bail};
use bincode::{Decode, Encode};
use turbo_tasks::{ResolvedVc, Vc, trace::TraceRawVcs};
use turbopack_core::resolve::ModulePart;
use crate::{
EcmascriptModuleAsset,
chunk::EcmascriptChunkPlaceable,
module_fragments::part::module::EcmascriptModulePartAsset,
references::{FollowExportsResult, follow_reexports},
rename::module::EcmascriptModuleRenameModule,
side_effect_optimization::{
facade::module::EcmascriptModuleFacadeModule, locals::module::EcmascriptModuleLocalsModule,
},
};
#[turbo_tasks::task_input]
#[derive(Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, Encode, Decode)]
pub enum EcmascriptModuleCanonicalization {
None,
ModuleFragments(ModulePart),
FollowReexports(Option<ModulePart>),
}
#[turbo_tasks::function]
pub async fn canonicalize_ecmascript_module(
module: ResolvedVc<EcmascriptModuleAsset>,
canonicalization: EcmascriptModuleCanonicalization,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
Ok(match canonicalization {
EcmascriptModuleCanonicalization::None => Vc::upcast(*module),
EcmascriptModuleCanonicalization::ModuleFragments(part) => {
EcmascriptModulePartAsset::select_part(*module, part)
}
EcmascriptModuleCanonicalization::FollowReexports(part) => {
if *module.get_exports().split_locals_and_reexports().await? {
if let Some(part) = part {
match part {
ModulePart::Evaluation => {
Vc::upcast(EcmascriptModuleLocalsModule::new(*module))
}
ModulePart::Export(_) => {
apply_reexport_tree_shaking(
Vc::upcast(
*EcmascriptModuleFacadeModule::new(Vc::upcast(*module))
.to_resolved()
.await?,
),
part,
)
.await?
}
_ => bail!(
"Invalid module part \"{}\" for reexports only tree shaking mode",
part
),
}
} else {
Vc::upcast(EcmascriptModuleFacadeModule::new(Vc::upcast(*module)))
}
} else {
Vc::upcast(*module)
}
}
})
}
async fn apply_reexport_tree_shaking(
module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
part: ModulePart,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
if let ModulePart::Export(export) = &part {
let FollowExportsResult {
module: final_module,
export_name: new_export,
..
} = &*follow_reexports(module, export.clone(), true).await?;
return Ok(if let Some(new_export) = new_export {
if *new_export == *export {
**final_module
} else {
Vc::upcast(EcmascriptModuleRenameModule::new(
**final_module,
ModulePart::renamed_export(new_export.clone(), export.clone()),
))
}
} else {
Vc::upcast(EcmascriptModuleRenameModule::new(
**final_module,
ModulePart::renamed_namespace(export.clone()),
))
});
}
Ok(module)
}
...@@ -24,6 +24,8 @@ use turbopack_resolve::ecmascript::esm_resolve; ...@@ -24,6 +24,8 @@ use turbopack_resolve::ecmascript::esm_resolve;
use crate::{ use crate::{
analyzer::imports::ImportAnnotations, analyzer::imports::ImportAnnotations,
async_chunk::proxy::LazyCompilationProxyModule,
chunk::EcmascriptChunkPlaceable,
code_gen::{CodeGen, CodeGeneration, IntoCodeGenReference}, code_gen::{CodeGen, CodeGeneration, IntoCodeGenReference},
create_visitor, create_visitor,
references::{ references::{
...@@ -46,6 +48,8 @@ pub struct EsmAsyncAssetReference { ...@@ -46,6 +48,8 @@ pub struct EsmAsyncAssetReference {
/// callback destructuring, or webpackExports/turbopackExports comments. /// callback destructuring, or webpackExports/turbopackExports comments.
pub export_usage: ExportUsage, pub export_usage: ExportUsage,
pub resolve_override: Option<ResolvedVc<Box<dyn Module>>>, pub resolve_override: Option<ResolvedVc<Box<dyn Module>>>,
/// Whether the target is compiled only after its runtime proxy is activated.
pub lazy_compilation: bool,
} }
impl EsmAsyncAssetReference { impl EsmAsyncAssetReference {
...@@ -59,6 +63,7 @@ impl EsmAsyncAssetReference { ...@@ -59,6 +63,7 @@ impl EsmAsyncAssetReference {
import_externals: bool, import_externals: bool,
export_usage: ExportUsage, export_usage: ExportUsage,
resolve_override: Option<ResolvedVc<Box<dyn Module>>>, resolve_override: Option<ResolvedVc<Box<dyn Module>>>,
lazy_compilation: bool,
) -> Result<Self> { ) -> Result<Self> {
// Apply any annotation-driven transition eagerly so the stored origin is final and the // Apply any annotation-driven transition eagerly so the stored origin is final and the
// `annotations` don't need to be retained on the reference. // `annotations` don't need to be retained on the reference.
...@@ -79,6 +84,7 @@ impl EsmAsyncAssetReference { ...@@ -79,6 +84,7 @@ impl EsmAsyncAssetReference {
import_externals, import_externals,
export_usage, export_usage,
resolve_override, resolve_override,
lazy_compilation,
}) })
} }
} }
...@@ -87,14 +93,27 @@ impl EsmAsyncAssetReference { ...@@ -87,14 +93,27 @@ impl EsmAsyncAssetReference {
impl ModuleReference for EsmAsyncAssetReference { impl ModuleReference for EsmAsyncAssetReference {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> { async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
if let Some(resolved) = &self.resolve_override { if let Some(resolved) = self.resolve_override {
return Ok(*ModuleResolveResult::module(*resolved)); if self.lazy_compilation
&& let Some(module) =
ResolvedVc::try_sidecast::<Box<dyn EcmascriptChunkPlaceable>>(resolved)
{
let proxy = LazyCompilationProxyModule::new_direct(*module)
.to_resolved()
.await?;
return Ok(*ModuleResolveResult::module(ResolvedVc::upcast(proxy)));
}
return Ok(*ModuleResolveResult::module(resolved));
} }
esm_resolve( esm_resolve(
*self.origin, *self.origin,
*self.request, *self.request,
EcmaScriptModulesReferenceSubType::DynamicImport, if self.lazy_compilation {
EcmaScriptModulesReferenceSubType::LazyDynamicImport
} else {
EcmaScriptModulesReferenceSubType::DynamicImport
},
self.error_mode, self.error_mode,
Some(self.issue_source), Some(self.issue_source),
) )
......
...@@ -514,6 +514,7 @@ struct AnalysisState<'a> { ...@@ -514,6 +514,7 @@ struct AnalysisState<'a> {
module_fragments_enabled: bool, module_fragments_enabled: bool,
cjs_tree_shaking: bool, cjs_tree_shaking: bool,
cross_module_constants: bool, cross_module_constants: bool,
lazy_compilation: bool,
import_externals: bool, import_externals: bool,
ignore_dynamic_requests: bool, ignore_dynamic_requests: bool,
url_rewrite_behavior: Option<UrlRewriteBehavior>, url_rewrite_behavior: Option<UrlRewriteBehavior>,
...@@ -882,6 +883,7 @@ async fn analyze_ecmascript_module_internal( ...@@ -882,6 +883,7 @@ async fn analyze_ecmascript_module_internal(
module_fragments_enabled: options.module_fragments_enabled, module_fragments_enabled: options.module_fragments_enabled,
cjs_tree_shaking: options.cjs_tree_shaking, cjs_tree_shaking: options.cjs_tree_shaking,
cross_module_constants: options.cross_module_constants, cross_module_constants: options.cross_module_constants,
lazy_compilation: options.lazy_compilation,
import_externals: options.import_externals, import_externals: options.import_externals,
ignore_dynamic_requests: options.ignore_dynamic_requests, ignore_dynamic_requests: options.ignore_dynamic_requests,
url_rewrite_behavior: options.url_rewrite_behavior, url_rewrite_behavior: options.url_rewrite_behavior,
...@@ -1791,6 +1793,7 @@ async fn handle_dynamic_import<'a>( ...@@ -1791,6 +1793,7 @@ async fn handle_dynamic_import<'a>(
origin, origin,
source, source,
ignore_dynamic_requests, ignore_dynamic_requests,
lazy_compilation,
.. ..
} = state; } = state;
...@@ -1823,6 +1826,7 @@ async fn handle_dynamic_import<'a>( ...@@ -1823,6 +1826,7 @@ async fn handle_dynamic_import<'a>(
state.import_externals, state.import_externals,
export_usage, export_usage,
link_context, link_context,
lazy_compilation,
) )
.await .await
} }
...@@ -1841,6 +1845,7 @@ async fn handle_dynamic_import_with_linked_args( ...@@ -1841,6 +1845,7 @@ async fn handle_dynamic_import_with_linked_args(
import_externals: bool, import_externals: bool,
export_usage: ExportUsage, export_usage: ExportUsage,
link_context: ValueLinkContext, link_context: ValueLinkContext,
lazy_compilation: bool,
) -> Result<()> { ) -> Result<()> {
if linked_args.len() == 1 || linked_args.len() == 2 { if linked_args.len() == 1 || linked_args.len() == 2 {
let pat = js_value_to_pattern(&linked_args[0]); let pat = js_value_to_pattern(&linked_args[0]);
...@@ -1901,6 +1906,7 @@ async fn handle_dynamic_import_with_linked_args( ...@@ -1901,6 +1906,7 @@ async fn handle_dynamic_import_with_linked_args(
import_externals, import_externals,
export_usage, export_usage,
resolve_override, resolve_override,
lazy_compilation,
) )
.await?, .await?,
ast_path.to_vec().into(), ast_path.to_vec().into(),
...@@ -2230,6 +2236,7 @@ where ...@@ -2230,6 +2236,7 @@ where
state.import_externals, state.import_externals,
export_usage, export_usage,
link_context, link_context,
state.lazy_compilation,
) )
.await?; .await?;
} }
......
...@@ -9,10 +9,11 @@ use turbo_tasks_hash::HashAlgorithm; ...@@ -9,10 +9,11 @@ use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ chunk::{
AssetSuffix, Chunk, ChunkGroupResult, ChunkItem, ChunkType, ChunkableModule, AssetSuffix, Chunk, ChunkGroupResult, ChunkItem, ChunkItemOrBatchWithAsyncModuleInfo,
ChunkingConfig, ChunkingConfigs, ChunkingContext, ContentHashing, EntryChunkGroupResult, ChunkItemWithAsyncModuleInfo, ChunkType, ChunkableModule, ChunkingConfig, ChunkingConfigs,
EvaluatableAsset, MinifyType, SourceMapSourceType, SourceMapsType, UnusedReferences, ChunkingContext, ContentHashing, EntryChunkGroupResult, EvaluatableAsset, MinifyType,
UrlBehavior, WorkerConfigurationOptions, SourceMapSourceType, SourceMapsType, UnusedReferences, UrlBehavior,
WorkerConfigurationOptions,
availability_info::AvailabilityInfo, availability_info::AvailabilityInfo,
chunk_group::{MakeChunkGroupResult, make_chunk_group}, chunk_group::{MakeChunkGroupResult, make_chunk_group},
chunk_id_strategy::ModuleIdStrategy, chunk_id_strategy::ModuleIdStrategy,
...@@ -717,7 +718,14 @@ impl ChunkingContext for NodeJsChunkingContext { ...@@ -717,7 +718,14 @@ impl ChunkingContext for NodeJsChunkingContext {
Vc::upcast::<Box<dyn ChunkingContext>>(self) Vc::upcast::<Box<dyn ChunkingContext>>(self)
.to_resolved() .to_resolved()
.await?; .await?;
Ok(if self.await?.manifest_chunks { let use_manifest = self.await?.manifest_chunks
// This guard is in place so that only javascript goes
// this path for lazy loading dynamic imports not things like css.
&& ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(
module.to_resolved().await?,
)
.is_some();
Ok(if use_manifest {
let manifest_asset = ManifestAsyncModule::new( let manifest_asset = ManifestAsyncModule::new(
module, module,
module_graph, module_graph,
...@@ -734,12 +742,49 @@ impl ChunkingContext for NodeJsChunkingContext { ...@@ -734,12 +742,49 @@ impl ChunkingContext for NodeJsChunkingContext {
}) })
} }
#[turbo_tasks::function]
async fn standalone_chunk(
self: Vc<Self>,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Result<Vc<Box<dyn OutputAsset>>> {
let chunk_type = chunk_item
.into_trait_ref()
.await?
.ty()
.to_resolved()
.await?;
let chunk = chunk_type
.chunk(
Vc::upcast(self),
vec![ChunkItemOrBatchWithAsyncModuleInfo::ChunkItem(
ChunkItemWithAsyncModuleInfo {
chunk_item,
chunk_type,
module: None,
async_info: None,
},
)],
Vec::new(),
Vec::new(),
)
.to_resolved()
.await?;
Ok(*self.generate_chunk(chunk).await?)
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn async_loader_chunk_item_ident( async fn async_loader_chunk_item_ident(
self: Vc<Self>, self: Vc<Self>,
module: Vc<Box<dyn ChunkableModule>>, module: Vc<Box<dyn ChunkableModule>>,
) -> Result<Vc<AssetIdent>> { ) -> Result<Vc<AssetIdent>> {
Ok(if self.await?.manifest_chunks { let use_manifest = self.await?.manifest_chunks
// This guard is in place so that only javascript goes
// this path for lazy loading dynamic imports not things like css.
&& ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(
module.to_resolved().await?,
)
.is_some();
Ok(if use_manifest {
ManifestLoaderModule::asset_ident_for(module) ManifestLoaderModule::asset_ident_for(module)
} else { } else {
AsyncLoaderModule::asset_ident_for(module) AsyncLoaderModule::asset_ident_for(module)
......
import { identity as staticIdentity } from './modules/identity'
it('should share module identity between static and dynamic imports', async () => {
const { identity: dynamicIdentity } = await import('./modules/identity')
expect(dynamicIdentity).toBe(staticIdentity)
})
it('should mark all exports as used with non-destructured dynamic import', async () => { it('should mark all exports as used with non-destructured dynamic import', async () => {
const lib = await import('./modules/all') const lib = await import('./modules/all')
expect(lib.cat).toBe('cat') expect(lib.cat).toBe('cat')
......
...@@ -50,16 +50,10 @@ use turbopack_css::{CssModule, EcmascriptCssModule}; ...@@ -50,16 +50,10 @@ use turbopack_css::{CssModule, EcmascriptCssModule};
use turbopack_ecmascript::{ use turbopack_ecmascript::{
AnalyzeMode, EcmascriptInputTransforms, EcmascriptModuleAsset, EcmascriptModuleAssetType, AnalyzeMode, EcmascriptInputTransforms, EcmascriptModuleAsset, EcmascriptModuleAssetType,
EcmascriptOptions, EcmascriptOptions,
chunk::EcmascriptChunkPlaceable, async_chunk::proxy::LazyCompilationProxyModule,
module_fragments::part::module::EcmascriptModulePartAsset, module_canonicalization::{EcmascriptModuleCanonicalization, canonicalize_ecmascript_module},
references::{ references::external_module::{
FollowExportsResult, CachedExternalModule, CachedExternalTracingMode, CachedExternalType,
external_module::{CachedExternalModule, CachedExternalTracingMode, CachedExternalType},
follow_reexports,
},
rename::module::EcmascriptModuleRenameModule,
side_effect_optimization::{
facade::module::EcmascriptModuleFacadeModule, locals::module::EcmascriptModuleLocalsModule,
}, },
}; };
use turbopack_node::transforms::webpack::{WebpackLoaderItem, WebpackLoaderItems, WebpackLoaders}; use turbopack_node::transforms::webpack::{WebpackLoaderItem, WebpackLoaderItems, WebpackLoaders};
...@@ -100,6 +94,10 @@ async fn apply_module_type( ...@@ -100,6 +94,10 @@ async fn apply_module_type(
_ => None, _ => None,
}; };
let is_evaluation = matches!(&part, Some(ModulePart::Evaluation)); let is_evaluation = matches!(&part, Some(ModulePart::Evaluation));
let is_lazy_dynamic_import = matches!(
&reference_type,
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::LazyDynamicImport)
);
let module_type = &*module_type.await?; let module_type = &*module_type.await?;
let module = match module_type { let module = match module_type {
...@@ -200,46 +198,29 @@ async fn apply_module_type( ...@@ -200,46 +198,29 @@ async fn apply_module_type(
} }
} }
if module_fragments_enabled { let canonicalization = if module_fragments_enabled {
Vc::upcast(EcmascriptModulePartAsset::select_part( EcmascriptModuleCanonicalization::ModuleFragments(
*module, part.cloned().unwrap_or_else(ModulePart::facade),
part.cloned().unwrap_or(ModulePart::facade()), )
))
} else if follow_reexports { } else if follow_reexports {
if *module.get_exports().split_locals_and_reexports().await? { EcmascriptModuleCanonicalization::FollowReexports(part.cloned())
if let Some(part) = part { } else {
match part { EcmascriptModuleCanonicalization::None
ModulePart::Evaluation => { };
Vc::upcast(EcmascriptModuleLocalsModule::new(*module))
} if is_lazy_dynamic_import {
ModulePart::Export(_) => { ResolvedVc::upcast(
apply_reexport_tree_shaking( LazyCompilationProxyModule::new_deferred(*module, canonicalization)
Vc::upcast(
*EcmascriptModuleFacadeModule::new(Vc::upcast(*module))
.to_resolved() .to_resolved()
.await?, .await?,
),
part.clone(),
) )
.await?
}
_ => bail!(
"Invalid module part \"{}\" for reexports only tree shaking \
mode",
part
),
}
} else {
Vc::upcast(EcmascriptModuleFacadeModule::new(Vc::upcast(*module)))
}
} else {
Vc::upcast(*module)
}
} else { } else {
Vc::upcast(*module) ResolvedVc::upcast(
} canonicalize_ecmascript_module(*module, canonicalization)
.to_resolved() .to_resolved()
.await? .await?,
)
}
} }
} }
ModuleType::Raw => ResolvedVc::upcast(RawModule::new(*source).to_resolved().await?), ModuleType::Raw => ResolvedVc::upcast(RawModule::new(*source).to_resolved().await?),
...@@ -307,36 +288,6 @@ async fn apply_module_type( ...@@ -307,36 +288,6 @@ async fn apply_module_type(
Ok(ProcessResult::Module(module).cell()) Ok(ProcessResult::Module(module).cell())
} }
async fn apply_reexport_tree_shaking(
module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
part: ModulePart,
) -> Result<Vc<Box<dyn Module>>> {
if let ModulePart::Export(export) = &part {
let FollowExportsResult {
module: final_module,
export_name: new_export,
..
} = &*follow_reexports(module, export.clone(), true).await?;
let module = if let Some(new_export) = new_export {
if *new_export == *export {
Vc::upcast(**final_module)
} else {
Vc::upcast(EcmascriptModuleRenameModule::new(
**final_module,
ModulePart::renamed_export(new_export.clone(), export.clone()),
))
}
} else {
Vc::upcast(EcmascriptModuleRenameModule::new(
**final_module,
ModulePart::renamed_namespace(export.clone()),
))
};
return Ok(module);
}
Ok(Vc::upcast(module))
}
#[turbo_tasks::value] #[turbo_tasks::value]
#[derive(Debug)] #[derive(Debug)]
pub struct ModuleAssetContext { pub struct ModuleAssetContext {
......
...@@ -250,6 +250,7 @@ impl ModuleOptions { ...@@ -250,6 +250,7 @@ impl ModuleOptions {
mangle_export_names, mangle_export_names,
cjs_scope_hoisting, cjs_scope_hoisting,
cross_module_constants, cross_module_constants,
lazy_compilation,
ref preset_env_config, ref preset_env_config,
.. ..
}, },
...@@ -347,6 +348,7 @@ impl ModuleOptions { ...@@ -347,6 +348,7 @@ impl ModuleOptions {
mangle_export_names, mangle_export_names,
cjs_scope_hoisting, cjs_scope_hoisting,
cross_module_constants, cross_module_constants,
lazy_compilation,
..Default::default() ..Default::default()
}; };
let ecmascript_options_vc = ecmascript_options.resolved_cell(); let ecmascript_options_vc = ecmascript_options.resolved_cell();
......
...@@ -296,6 +296,8 @@ pub struct EcmascriptOptionsContext { ...@@ -296,6 +296,8 @@ pub struct EcmascriptOptionsContext {
/// Whether to enable cross-module constant inlining. Defaults to false. /// Whether to enable cross-module constant inlining. Defaults to false.
pub cross_module_constants: bool, pub cross_module_constants: bool,
/// Whether dynamic import targets are compiled after their runtime proxy is activated.
pub lazy_compilation: bool,
/// Additional SWC preset-env options (mode, coreJs, include, exclude, etc.). /// Additional SWC preset-env options (mode, coreJs, include, exclude, etc.).
pub preset_env_config: Option<ResolvedVc<PresetEnvConfig>>, pub preset_env_config: Option<ResolvedVc<PresetEnvConfig>>,
......
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