Add error message if Astro.glob
is called outside (#7204)
This commit is contained in:
parent
184870e2e2
commit
52af9ad188
3 changed files with 23 additions and 0 deletions
5
.changeset/moody-coats-develop.md
Normal file
5
.changeset/moody-coats-develop.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Add error message if `Astro.glob` is called outside of an Astro file
|
|
@ -4,6 +4,11 @@ import { ASTRO_VERSION } from '../../core/constants.js';
|
|||
/** Create the Astro.glob() runtime function. */
|
||||
function createAstroGlobFn() {
|
||||
const globHandler = (importMetaGlobResult: Record<string, any>, globValue: () => any) => {
|
||||
if (typeof importMetaGlobResult === 'string') {
|
||||
throw new Error(
|
||||
'Astro.glob() does not work outside of an Astro file. Use `import.meta.glob()` instead.'
|
||||
);
|
||||
}
|
||||
let allEntries = [...Object.values(importMetaGlobResult)];
|
||||
if (allEntries.length === 0) {
|
||||
throw new Error(`Astro.glob(${JSON.stringify(globValue())}) - no matches found.`);
|
||||
|
|
13
packages/astro/test/units/runtime/astro-global.test.js
Normal file
13
packages/astro/test/units/runtime/astro-global.test.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { expect } from 'chai';
|
||||
import { createAstro } from '../../../dist/runtime/server/index.js';
|
||||
|
||||
describe('astro global', () => {
|
||||
it('Glob should error if passed incorrect value', async () => {
|
||||
const Astro = createAstro(undefined);
|
||||
expect(() => {
|
||||
Astro.glob('./**/*.md');
|
||||
}).to.throw(
|
||||
'Astro.glob() does not work outside of an Astro file. Use `import.meta.glob()` instead.'
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue