Unverified Commit 1b689957 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Turbopack: test import.meta.glob code splitting with an execution test (#100532)

## Summary

Replaces the `import.meta.glob` code-splitting snapshot test added in
#100522 with an execution test. The new test checks the behavior
directly: lazy matches load only when called, and each one loads
separately.

The test checks the runtime module factory map, `__turbopack_modules__`:

- The eager match is in the map at startup and evaluates correctly.
- Neither lazy match is in the map before its thunk is called.
- Loading the first lazy match adds it to the map, but not the second
one.
- Loading the second lazy match then adds it too.

The check targets each module's `(ecmascript)` entry. The map also
contains `(ecmascript, async loader)` stubs, and those ship in the
importing chunk, so matching on the source path alone would show the
lazy targets as already loaded.

This is a separate fixture because the existing
`resolving/import-meta-glob` test imports the same files both lazily and
eagerly, so it can't show that a lazy target isn't loaded yet. The
snapshot fixture and its generated chunks are removed. There are no
production code changes.

## Verification

- With
`turbopack/crates/turbopack-ecmascript/src/references/import_meta_glob.rs`
temporarily set back to its state before #100522, the new execution test
fails because a lazy match is already in the module map before its thunk
is called.
- With the current implementation, all `import_meta_glob*` execution
tests pass, and the remaining `snapshot/imports` tests pass.

<!-- NEXT_JS_LLM -->


<!-- fleet ca9e5cd1-7e86-4f88-b9f3-22f348cd6f50 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 65675c37
// 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 })
// Whether the module itself (not just its async loader, which ships in the
// importing chunk) has been registered in the module factory map.
function isLoaded(file) {
return Array.from(__turbopack_modules__.keys()).some((id) =>
id.endsWith(`/input/${file} [test] (ecmascript)`)
)
}
it('should include eager matches in the entry chunk', () => {
expect(isLoaded('eager/c.js')).toBe(true)
expect(eager['./eager/c.js'].default).toBe('eager c')
})
it('should load each lazy match in its own chunk when its thunk is called', async () => {
expect(Object.keys(lazy).sort()).toEqual(['./lazy/a.js', './lazy/b.js'])
// Nothing from the lazy glob is loaded until a thunk is called.
expect(isLoaded('lazy/a.js')).toBe(false)
expect(isLoaded('lazy/b.js')).toBe(false)
const a = await lazy['./lazy/a.js']()
expect(a.default).toBe('lazy a')
// Loading one match does not pull in the others.
expect(isLoaded('lazy/a.js')).toBe(true)
expect(isLoaded('lazy/b.js')).toBe(false)
const b = await lazy['./lazy/b.js']()
expect(b.default).toBe('lazy b')
expect(isLoaded('lazy/b.js')).toBe(true)
})
// 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