next.js
44722b80 - Turbopack: import.meta.glob docs + edge case support (#92729)

Commit
9 days ago
Turbopack: import.meta.glob docs + edge case support (#92729) ## What? Adds documentation and TypeScript types for `import.meta.glob` in Turbopack, and implements additional edge cases for Vite compatibility. ### Documentation - New `## import.meta.glob` section in the [Turbopack reference page](/docs/app/api-reference/turbopack) covering: lazy/eager loading, named imports, query strings (including the object form), multiple patterns, negation, TypeScript types, and a full options table. - `import.meta.glob` row added to the Module Resolution feature table. - Migration note added to the [from-Vite guide](/docs/app/guides/migrating/from-vite) explaining that `import.meta.glob` works out of the box with Turbopack, with before/after for the deprecated `as` → `query` migration. ### TypeScript types - Added `ImportMetaGlobOptions` interface and overloaded `glob()` signatures to `ImportMeta` in `packages/next/types/global.d.ts`. - The overloads return `Record<string, unknown>` when `eager: true` is passed and `Record<string, () => Promise<unknown>>` otherwise. ### Implementation (edge cases) - **`import: '*'`** (namespace import): Treated the same as omitting the `import` option (returns the whole module namespace), matching Vite semantics. Previously would have generated broken `module["*"]` access. - **`query` as object literal**: `{ query: { bar: 'foo', raw: true } }` is now supported. Keys and values are URL-encoded and joined into a query string (`?bar=foo&raw=true`). - **Stricter option validation**: A non-object-literal second argument (e.g. `import.meta.glob('./*.js', 'eager')`) is now a compile-time error instead of a warning, since the options cannot be safely defaulted. ### Tests - Execution tests for new features: namespace `import: '*'`, query object, combining query with negation, explicit dotfile patterns. - New `import-meta-glob-errors` execution test with issue snapshots covering: too many arguments, non-string pattern, and non-object options argument. ## Why? 1. The feature shipped in #92640 but had no user-facing documentation and no TypeScript types. 2. Several Vite-compatible edge cases were not handled: namespace imports (`import: '*'`) and query objects. These gaps would cause subtle behavioral differences for users migrating from Vite. 3. Option validation was too lenient — invalid second arguments fell through to defaults silently. ## How? ### Docs & types - `docs/01-app/03-api-reference/08-turbopack.mdx` — new section, table row, TypeScript subsection - `docs/01-app/02-guides/migrating/from-vite.mdx` — migration note - `packages/next/types/global.d.ts` — `ImportMetaGlobOptions` + `ImportMeta.glob()` overloads ### Implementation - `turbopack/crates/turbopack-ecmascript/src/references/import_meta_glob.rs`: - `parse_import_meta_glob()`: `import: '*'` normalized to `None`; query object parsing via `urlencoding::encode()`; non-object options argument now emits an error and returns `None`. - `turbopack/crates/turbopack-ecmascript/Cargo.toml`: added `urlencoding` workspace dep. Note: dotfile/dot-directory filtering is not needed in `import.meta.glob` itself — the underlying `Glob` pattern engine and `read_glob_internal` already handle dotfile semantics correctly (explicit `./.foo/*.js` matches, wildcards include them). ### Tests - `turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob/` — new cases for `import: '*'`, query object, query + negation, explicit dotfile pattern, wildcard including dotfiles - `turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-errors/` — new test suite for fatal parse errors with issue snapshots ### Checklist - [x] `pnpm prettier-fix` / `cargo fmt` run - [x] Documentation follows the [docs contribution guide](https://nextjs.org/docs/community/contribution-guide) - [x] Execution tests pass (`cargo test -p turbopack-tests --test execution -- "import_meta_glob"`) - [x] `cargo clippy` clean - [x] `pnpm --filter=next types` passes <!-- NEXT_JS_LLM_PR --> --------- Co-authored-by: Tobias Koppers <sokra@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
Author
Parents
Loading