Unverified Commit 691ec2a1 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Turbopack: fix chunking for lazy import.meta.glob matches (#100522)

## Summary

Non-eager `import.meta.glob` matches were referenced with a parallel
chunking type, so every matched module was compiled and shipped in the
importing module's chunk group. Only evaluation was deferred to the
thunk, so the "lazy" mode did not actually code-split.

Lazy matches now use an async edge and load through their async loader,
the same as a dynamic `import()`. Each match gets its own chunk, loaded
when its thunk is called. Eager matches are unchanged.

## Verification

- New snapshot test
`turbopack-tests/tests/snapshot/imports/import-meta-glob` shows lazy
matches in separate async chunks and the eager match in the entry chunk.
- Existing `import-meta-glob*` and `side-effects-glob` execution tests
pass.

<!-- NEXT_JS_LLM -->
parent 3c296ea2
...@@ -637,6 +637,7 @@ impl ImportMetaGlobMap { ...@@ -637,6 +637,7 @@ impl ImportMetaGlobMap {
pub struct ImportMetaGlobModuleReference { pub struct ImportMetaGlobModuleReference {
result: ResolvedVc<ModuleResolveResult>, result: ResolvedVc<ModuleResolveResult>,
export: ExportUsage, export: ExportUsage,
eager: bool,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -647,9 +648,14 @@ impl ModuleReference for ImportMetaGlobModuleReference { ...@@ -647,9 +648,14 @@ impl ModuleReference for ImportMetaGlobModuleReference {
} }
fn chunking_type(&self) -> Option<ChunkingType> { fn chunking_type(&self) -> Option<ChunkingType> {
Some(ChunkingType::Parallel { // Lazy matches are loaded on demand, like the target of a dynamic `import()`.
inherit_async: false, Some(if self.eager {
hoisted: false, ChunkingType::Parallel {
inherit_async: false,
hoisted: false,
}
} else {
ChunkingType::Async
}) })
} }
...@@ -1037,6 +1043,7 @@ impl Module for ImportMetaGlobAsset { ...@@ -1037,6 +1043,7 @@ impl Module for ImportMetaGlobAsset {
ImportMetaGlobModuleReference { ImportMetaGlobModuleReference {
result: entry.result, result: entry.result,
export: export.clone(), export: export.clone(),
eager: this.eager,
} }
.resolved_cell(), .resolved_cell(),
) )
...@@ -1091,6 +1098,16 @@ impl EcmascriptChunkPlaceable for ImportMetaGlobAsset { ...@@ -1091,6 +1098,16 @@ impl EcmascriptChunkPlaceable for ImportMetaGlobAsset {
let map = &*self.map().await?; let map = &*self.map().await?;
let minify = chunking_context.minify_type().await?; let minify = chunking_context.minify_type().await?;
// In lazy mode each matched module is in its own async chunk group (see
// `ImportMetaGlobModuleReference::chunking_type`), so it has to be loaded
// through its async loader, like the target of a dynamic `import()`.
let resolve_type =
if !this.eager && chunking_context.chunk_loading().await?.can_split_async() {
ResolveType::AsyncChunkLoader
} else {
ResolveType::ChunkItem
};
let mut glob_map = ObjectLit { let mut glob_map = ObjectLit {
span: DUMMY_SP, span: DUMMY_SP,
props: vec![], props: vec![],
...@@ -1102,7 +1119,7 @@ impl EcmascriptChunkPlaceable for ImportMetaGlobAsset { ...@@ -1102,7 +1119,7 @@ impl EcmascriptChunkPlaceable for ImportMetaGlobAsset {
*this.origin, *this.origin,
chunking_context, chunking_context,
*entry.result, *entry.result,
ResolveType::ChunkItem, resolve_type,
None, None,
) )
.await?; .await?;
......
// Lazy matches are loaded on demand, so each one is split into its own async chunk.
const lazy = import.meta.glob('./lazy/*.js')
// Eager matches are evaluated synchronously, so they stay in the entry chunk.
const eager = import.meta.glob('./eager/*.js', { eager: true })
console.log(eager)
lazy['./lazy/a.js']().then(console.log)
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/0_9x_turbopack-tests_tests_snapshot_imports_import-meta-glob_input_1y3wc3yelnum2._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/eager/c.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
__turbopack_context__.s([
"default",
()=>__TURBOPACK__default__export__
]);
const __TURBOPACK__default__export__ = 'eager c';
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/index.js [test] (ecmascript)", ((__turbopack_context__, module, exports) => {
var __TURBOPACK__import$2e$meta__ = {
get url () {
return __turbopack_context__.F("turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/index.js");
},
env: {
DEV: true,
PROD: false,
MODE: "development",
BASE_URL: "/",
SSR: false
}
};
// Lazy matches are loaded on demand, so each one is split into its own async chunk.
const lazy = __turbopack_context__.r("[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/index.js [test] (import.meta.glob ./lazy/*.js)");
// Eager matches are evaluated synchronously, so they stay in the entry chunk.
const eager = __turbopack_context__.r("[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/index.js [test] (import.meta.glob ./eager/*.js eager)");
console.log(eager);
lazy['./lazy/a.js']().then(console.log);
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/index.js [test] (import.meta.glob ./eager/*.js eager)", (function(__turbopack_context__){
__turbopack_context__.v({
"./eager/c.js": __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/eager/c.js [test] (ecmascript)")
});
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/index.js [test] (import.meta.glob ./lazy/*.js)", (function(__turbopack_context__){
__turbopack_context__.v({
"./lazy/a.js": ()=>__turbopack_context__.A("[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/a.js [test] (ecmascript, async loader)"),
"./lazy/b.js": ()=>__turbopack_context__.A("[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/b.js [test] (ecmascript, async loader)")
});
}),
]);
//# sourceMappingURL=0_9x_turbopack-tests_tests_snapshot_imports_import-meta-glob_input_1y3wc3yelnum2._.js.map
\ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/eager/c.js"],"sourcesContent":["export default 'eager c'\n"],"names":[],"mappings":";;;;uCAAe"}},
{"offset": {"line": 12, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/index.js"],"sourcesContent":["// Lazy matches are loaded on demand, so each one is split into its own async chunk.\nconst lazy = import.meta.glob('./lazy/*.js')\n\n// Eager matches are evaluated synchronously, so they stay in the entry chunk.\nconst eager = import.meta.glob('./eager/*.js', { eager: true })\n\nconsole.log(eager)\nlazy['./lazy/a.js']().then(console.log)\n"],"names":["lazy","eager","console","log","then"],"mappings":";;;;;;;;;;;;AAAA,oFAAoF;AACpF,MAAMA;AAEN,8EAA8E;AAC9E,MAAMC;AAENC,QAAQC,GAAG,CAACF;AACZD,IAAI,CAAC,cAAc,GAAGI,IAAI,CAACF,QAAQC,GAAG"}}]
}
\ No newline at end of file
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1ece_tests_snapshot_imports_import-meta-glob_input_index_06vankbuf27_5.js",
{"otherChunks":["output/1jsg_tests_snapshot_imports_import-meta-glob_input_lazy_02o04660ot30s._.js","output/0_9x_turbopack-tests_tests_snapshot_imports_import-meta-glob_input_1y3wc3yelnum2._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
\ No newline at end of file
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1jsg_tests_snapshot_imports_import-meta-glob_input_lazy_02o04660ot30s._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/a.js [test] (ecmascript, async loader)", ((__turbopack_context__) => {
__turbopack_context__.v((parentImport) => {
return Promise.all([
"output/1jsg_tests_snapshot_imports_import-meta-glob_input_lazy_a_12og6y814bz17.js"
].map((chunk) => __turbopack_context__.l(chunk))).then(() => {
return parentImport("[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/a.js [test] (ecmascript)");
});
});
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/b.js [test] (ecmascript, async loader)", ((__turbopack_context__) => {
__turbopack_context__.v((parentImport) => {
return Promise.all([
"output/1jsg_tests_snapshot_imports_import-meta-glob_input_lazy_b_0zy5rxszx2stl.js"
].map((chunk) => __turbopack_context__.l(chunk))).then(() => {
return parentImport("[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/b.js [test] (ecmascript)");
});
});
}),
]);
\ No newline at end of file
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1jsg_tests_snapshot_imports_import-meta-glob_input_lazy_a_12og6y814bz17.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/a.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
__turbopack_context__.s([
"default",
()=>__TURBOPACK__default__export__
]);
const __TURBOPACK__default__export__ = 'lazy a';
}),
]);
//# sourceMappingURL=1jsg_tests_snapshot_imports_import-meta-glob_input_lazy_a_12og6y814bz17.js.map
\ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/a.js"],"sourcesContent":["export default 'lazy a'\n"],"names":[],"mappings":";;;;uCAAe"}}]
}
\ No newline at end of file
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1jsg_tests_snapshot_imports_import-meta-glob_input_lazy_b_0zy5rxszx2stl.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/b.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
__turbopack_context__.s([
"default",
()=>__TURBOPACK__default__export__
]);
const __TURBOPACK__default__export__ = 'lazy b';
}),
]);
//# sourceMappingURL=1jsg_tests_snapshot_imports_import-meta-glob_input_lazy_b_0zy5rxszx2stl.js.map
\ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/import-meta-glob/input/lazy/b.js"],"sourcesContent":["export default 'lazy b'\n"],"names":[],"mappings":";;;;uCAAe"}}]
}
\ No newline at end of file
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