Repository navigation
implement import.meta.glob
#21459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement import.meta.glob
#21459
Changes from 34 commits
581d72a
5812103
2f9ff3f
6a60b82
e0bd157
5ddea5c
61f2d9e
6894f8a
892a20d
65d491e
8150288
6cd626a
5083f26
18bf62d
e86f0c6
642fb9d
ddb56e8
ce1756a
ccaef4b
95bd24e
ed3f2d6
fe18872
8ba8e96
f530a16
0e5c48b
b69193c
72cb84c
7236daa
c431a72
490c551
4c7e8f0
533011d
69d9f83
8a6710f
bd38223
abcfcd5
caa0dda
bb81ebd
a6ecf14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1344,13 +1344,126 @@ interface ImportMeta { | |
| */ | ||
| main: boolean; | ||
|
|
||
| /** | ||
| * Import multiple modules using glob patterns. | ||
| * Inspired by [Vite's `import.meta.glob`](https://vite.dev/guide/features.html#glob-import). | ||
| * | ||
| * @param pattern - A glob pattern or array of glob patterns to match files | ||
| * @param options - Options for how imports are handled | ||
| * @returns An object mapping file paths to dynamic import functions | ||
| * | ||
| * @example | ||
| * const modules = import.meta.glob('./src/*.ts') | ||
| * const module = await modules['./src/foo.ts']() | ||
| * | ||
| * // equivalent to | ||
| * const modules = { | ||
| * './src/foo.ts': () => import('./src/foo.ts'), | ||
| * './src/bar.ts': () => import('./src/bar.ts'), | ||
| * } | ||
| * const module = await modules['./src/foo.ts']() | ||
| */ | ||
| glob<Eager extends boolean = false, TModule = unknown>( | ||
| pattern: string | string[], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like we support multiple patterns, but it's not 100% clear how to use that, perhaps some docs would be neat? |
||
| options?: ImportMetaGlobOptions<Eager>, | ||
| ): Eager extends true ? Record<string, TModule> : Record<string, () => Promise<TModule>>; | ||
|
RiskyMH marked this conversation as resolved.
Resolved
|
||
| glob<TModule = unknown>( | ||
| pattern: string | string[], | ||
| options?: ImportMetaGlobOptions<false>, | ||
| ): Record<string, () => Promise<TModule>>; | ||
| glob<TModule = unknown>(pattern: string | string[], options?: ImportMetaGlobOptions<true>): Record<string, TModule>; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| /** Alias of `import.meta.dir`. Exists for Node.js compatibility */ | ||
| dirname: string; | ||
|
|
||
| /** Alias of `import.meta.path`. Exists for Node.js compatibility */ | ||
| filename: string; | ||
| } | ||
|
|
||
| interface ImportMetaGlobOptions<Eager extends boolean = false> { | ||
| // todo: | ||
| // /** | ||
| // * If true, imports all modules eagerly as if they were top level imports. | ||
| // * If false (default), returns functions that import modules lazily with dynamic imports. | ||
| // * | ||
| // * @example | ||
| // * const eager = import.meta.glob('./src/*.ts', { eager: true }) | ||
| // * const normal = import.meta.glob('./src/*.ts', { eager: false }) | ||
| // * | ||
| // * // equivalent to | ||
| // * import * as __modules_foo from './src/foo.ts' | ||
| // * import * as __modules_bar from './src/bar.ts' | ||
| // * | ||
| // * const eager = { | ||
| // * './src/foo.ts': __modules_foo, | ||
| // * './src/bar.ts': __modules_bar, | ||
| // * } | ||
| // * const normal = { | ||
| // * './src/foo.ts': () => import('./src/foo.ts'), | ||
| // * './src/bar.ts': () => import('./src/bar.ts'), | ||
| // * } | ||
| // */ | ||
| // eager?: Eager; | ||
| /** Right now Bun doesn't support eager mode */ | ||
| eager?: false; | ||
|
Comment on lines
+1384
to
+1408
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs could use a little bit of love, imo. Also, why expose this if it is not currently supported?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Realized I didn't leave a very helpful message when I said they could use a little bit more love. What I'm specifically referring to is:
My opinions -- feel free to disagree
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, the docs are basically written the same way as vite. But yeah dynamic imports may be confusing. I dont really know of a better way to describe the limbo land of not proper import but a function that returns promise of imported file.
RiskyMH marked this conversation as resolved.
Resolved
|
||
| /** | ||
| * Specify a named export to import from matched modules. | ||
| * If not specified, imports the entire module. | ||
| * | ||
| * @example | ||
| * const modules = import.meta.glob('./src/*.ts', { import: 'default' }) | ||
| * | ||
| * // equivalent to | ||
| * const modules = { | ||
| * './src/bar.ts': () => import('./src/bar.ts').then((m) => m.default), | ||
| * './src/foo.ts': () => import('./src/foo.ts').then((m) => m.default), | ||
| * } | ||
| */ | ||
| import?: "default" | (string & {}); | ||
| /** | ||
| * Add a query string to the end of the "generated" dynamic import. | ||
| * | ||
| * @example | ||
| * const modules = import.meta.glob('./assets/*.txt', { query: '?something' }) | ||
| * | ||
| * // equivalent to | ||
| * const modules = { | ||
| * './assets/file.txt': () => import('./assets/file.txt?something'), | ||
| * } | ||
| */ | ||
| query?: string; | ||
| /** | ||
| * Import attributes to pass to the import statement. | ||
| * This is like the standard way to specify import options to a normal dynamic import. | ||
| * | ||
| * @example | ||
| * const modules = import.meta.glob('./assets/*.txt', { with: { type: 'text' } }) | ||
| * | ||
| * // equivalent to | ||
| * const modules = { | ||
| * './assets/file.txt': () => import('./assets/file.txt', { with: { type: 'text' } }), | ||
| * } | ||
| */ | ||
| with?: ImportAttributes; | ||
| /** | ||
| * The base path to prepend to the imports. | ||
| * Basically changing the "cwd" of the directory to scan. | ||
| * | ||
| * @example | ||
| * const modules = import.meta.glob('./*.txt', { base: '../public' }) | ||
| * | ||
| * // equivalent to | ||
| * const modules = { | ||
| * './file.txt': () => import('../public/file.txt'), | ||
| * } | ||
| */ | ||
| base?: string; | ||
| } | ||
|
|
||
| interface ImportAttributes { | ||
| type: "css" | "file" | "json" | "jsonc" | "toml" | "yaml" | "txt" | "text" | "html" | (string & {}); | ||
| } | ||
|
|
||
| /** | ||
| * NodeJS-style `require` function | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.