Unverified Commit b83d8f57 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Turbopack: lazily compile lazy import.meta.glob matches (#100523)

## Summary

When lazy compilation of dynamic imports is enabled
(`experimental.turbopackLazyDynamicImports`), lazy `import.meta.glob`
matches now get the same treatment: each matched module is compiled only
once its thunk is first called. Eager matches are unaffected.


## Verification

- Added a `/glob` case to
`test/development/app-dir/lazy-dynamic-imports`.

<!-- NEXT_JS_LLM -->
parent 6204bf03
'use client'
import { useState } from 'react'
const targets = import.meta.glob<{ value: string }>('./targets/*.ts')
export function GlobDemo() {
const [value, setValue] = useState('idle')
return (
<button
id="load-glob"
onClick={async () => {
const module = await targets['./targets/a.ts']()
setValue(module.value)
}}
>
{value}
</button>
)
}
import { GlobDemo } from './glob-demo'
export default function Page() {
return <GlobDemo />
}
......@@ -406,6 +406,22 @@ export const invalid = ;`
).toHaveLength(1)
})
it('compiles an import.meta.glob match only when its thunk is called', async () => {
const browser = await next.browser('/glob')
expect(await browser.elementByCss('#load-glob').text()).toBe('idle')
expect(await assetsContaining('glob-marker-a-5e1d')).toHaveLength(0)
expect(await assetsContaining('glob-marker-b-8f3c')).toHaveLength(0)
await browser.elementByCss('#load-glob').click()
await retry(async () => {
expect(await browser.elementByCss('#load-glob').text()).toBe(
'glob-marker-a-5e1d'
)
})
expect(await assetsContaining('glob-marker-a-5e1d')).not.toHaveLength(0)
expect(await assetsContaining('glob-marker-b-8f3c')).toHaveLength(0)
})
it('shares a proxy for repeated imports', async () => {
const browser = await next.browser('/duplicate')
const getActivationKeys = async (): Promise<string[]> =>
......
......@@ -491,6 +491,7 @@ impl ImportMetaGlobMap {
key_base: Option<FileSystemPath>,
query: Option<RcStr>,
eager: bool,
lazy_compilation: bool,
issue_source: Option<IssueSource>,
error_mode: ResolveErrorMode,
) -> Result<Vc<Self>> {
......@@ -514,6 +515,10 @@ impl ImportMetaGlobMap {
let reference_sub_type = if eager {
EcmaScriptModulesReferenceSubType::Import
} else if lazy_compilation {
// Resolves to a proxy that compiles the matched module only once its
// thunk is first called.
EcmaScriptModulesReferenceSubType::LazyDynamicImport
} else {
EcmaScriptModulesReferenceSubType::DynamicImport
};
......@@ -679,6 +684,7 @@ impl ModuleReference for ImportMetaGlobModuleReference {
fn modifier(
patterns: &[RcStr],
eager: bool,
lazy_compilation: bool,
import: &Option<RcStr>,
query: &Option<RcStr>,
base: &Option<RcStr>,
......@@ -688,6 +694,9 @@ fn modifier(
if eager {
s.push_str(" eager");
}
if lazy_compilation {
s.push_str(" lazy-compilation");
}
if let Some(named) = import {
s.push_str(" import=");
s.push_str(named);
......@@ -711,6 +720,9 @@ pub struct ImportMetaGlobAsset {
pub origin: ResolvedVc<Box<dyn ResolveOrigin>>,
pub patterns: Vec<RcStr>,
pub eager: bool,
/// Whether the matched modules are compiled only once their thunk is first
/// called. Never set in eager mode, where the matches are evaluated up front.
pub lazy_compilation: bool,
pub import: Option<RcStr>,
pub query: Option<RcStr>,
pub base: Option<RcStr>,
......@@ -885,6 +897,7 @@ impl ImportMetaGlobAsset {
self.base.is_some().then_some(base_dir),
self.query.clone(),
self.eager,
self.lazy_compilation,
self.issue_source,
self.error_mode,
))
......@@ -973,6 +986,7 @@ impl Module for ImportMetaGlobAsset {
.with_modifier(modifier(
&self.patterns,
self.eager,
self.lazy_compilation,
&self.import,
&self.query,
&self.base,
......@@ -1238,6 +1252,7 @@ impl ImportMetaGlobAssetReference {
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
patterns: Vec<RcStr>,
eager: bool,
lazy_compilation: bool,
import: Option<RcStr>,
query: Option<RcStr>,
base: Option<RcStr>,
......@@ -1249,6 +1264,7 @@ impl ImportMetaGlobAssetReference {
origin,
patterns: patterns.clone(),
eager,
lazy_compilation: lazy_compilation && !eager,
import,
query,
base,
......
......@@ -2527,6 +2527,7 @@ where
origin,
options.patterns,
options.eager,
state.lazy_compilation,
options.import,
options.query,
options.base,
......
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