0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

Deprecate Astro.glob (#11826)

This deprecated Astro.glob, see the changeset for details.
This commit is contained in:
Matthew Phillips 2024-08-27 10:51:15 -04:00 committed by GitHub
parent 05139ef8b4
commit 7315050fc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 0 deletions

View file

@ -0,0 +1,19 @@
---
'astro': major
---
Deprecate Astro.glob
The `Astro.glob` function has been deprecated in favor of Content Collections and `import.meta.glob`.
- If you want to query for markdown and MDX in your project, use Content Collections.
- If you want to query source files in your project, use `import.meta.glob`(https://vitejs.dev/guide/features.html#glob-import).
Also consider using glob packages from npm, like [fast-glob](https://www.npmjs.com/package/fast-glob), especially if statically generating your site, as it is faster for most use-cases.
The easiest path is to migrate to `import.meta.glob` like so:
```diff
- const posts = Astro.glob('./posts/*.md');
+ const posts = Object.values(import.meta.glob('./posts/*.md', { eager: true }));
```

View file

@ -5,6 +5,10 @@ import type { AstroGlobalPartial } from '../../types/public/context.js';
/** Create the Astro.glob() runtime function. */ /** Create the Astro.glob() runtime function. */
function createAstroGlobFn() { function createAstroGlobFn() {
const globHandler = (importMetaGlobResult: Record<string, any>) => { const globHandler = (importMetaGlobResult: Record<string, any>) => {
// This is created inside of the runtime so we don't have access to the Astro logger.
console.warn(`Astro.glob is deprecated and will be removed in a future major version of Astro.
Use import.meta.glob instead: https://vitejs.dev/guide/features.html#glob-import`);
if (typeof importMetaGlobResult === 'string') { if (typeof importMetaGlobResult === 'string') {
throw new AstroError({ throw new AstroError({
...AstroErrorData.AstroGlobUsedOutside, ...AstroErrorData.AstroGlobUsedOutside,

View file

@ -219,6 +219,7 @@ export interface AstroGlobalPartial {
* ``` * ```
* *
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroglob) * [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroglob)
* @deprecated Astro.glob is deprecated and will be removed in the next major version of Astro. Use `import.meta.glob` instead: https://vitejs.dev/guide/features.html#glob-import
*/ */
glob(globStr: `${any}.astro`): Promise<AstroInstance[]>; glob(globStr: `${any}.astro`): Promise<AstroInstance[]>;
glob<T extends Record<string, any>>( glob<T extends Record<string, any>>(