Skip to content

Commit ea7ff51

Browse files
authored
astro: namespace for middleware and components (#8101)
* `astro:` namespace for middleware and components * Update errors to use namespace * Create a namespace module just for the astro: stuff
1 parent 4843bff commit ea7ff51

23 files changed

Lines changed: 55 additions & 20 deletions

File tree

.changeset/neat-mugs-end.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
6+
`astro:`namespace aliases for middleware and components
7+
8+
This adds aliases of `astro:middleware` and `astro:components` for the middleware and components modules. This is to make our documentation consistent between are various modules, where some are virtual modules and others are not. Going forward new built-in modules will use this namespace.

examples/middleware/src/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineMiddleware, sequence } from 'astro/middleware';
1+
import { defineMiddleware, sequence } from 'astro:middleware';
22
import htmlMinifier from 'html-minifier';
33

44
const limit = 50;

packages/astro/client.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ declare module 'astro:transitions' {
119119
export const ViewTransitions: ViewTransitionsModule['default'];
120120
}
121121

122+
declare module 'astro:middleware' {
123+
export * from 'astro/middleware/namespace';
124+
}
125+
126+
declare module 'astro:components' {
127+
export * from 'astro/components';
128+
}
129+
122130
type MD = import('./dist/@types/astro').MarkdownInstance<Record<string, any>>;
123131
interface ExportedMarkdownModuleEntities {
124132
frontmatter: MD['frontmatter'];

packages/astro/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
],
2424
"middleware": [
2525
"./dist/core/middleware/index.d.ts"
26+
],
27+
"middleware/namespace": [
28+
"./dist/core/middleware/namespace.d.ts"
2629
]
2730
}
2831
},
@@ -70,6 +73,10 @@
7073
"types": "./dist/core/middleware/index.d.ts",
7174
"default": "./dist/core/middleware/index.js"
7275
},
76+
"./middleware/namespace": {
77+
"types": "./dist/core/middleware/namespace.d.ts",
78+
"default": "./dist/core/middleware/namespace.js"
79+
},
7380
"./transitions": "./dist/transitions/index.js"
7481
},
7582
"imports": {

packages/astro/src/@types/astro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ export interface APIContext<Props extends Record<string, any> = Record<string, a
19201920
*
19211921
* ```ts
19221922
* // src/middleware.ts
1923-
* import {defineMiddleware} from "astro/middleware";
1923+
* import {defineMiddleware} from "astro:middleware";
19241924
*
19251925
* export const onRequest = defineMiddleware((context, next) => {
19261926
* context.locals.greeting = "Hello!";

packages/astro/src/core/create-vite.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ export async function createVite(
170170
find: /^astro$/,
171171
replacement: fileURLToPath(new URL('../@types/astro', import.meta.url)),
172172
},
173+
{
174+
find: 'astro:middleware',
175+
replacement: 'astro/middleware/namespace',
176+
},
177+
{
178+
find: 'astro:components',
179+
replacement: 'astro/components',
180+
},
173181
],
174182
conditions: ['astro'],
175183
// Astro imports in third-party packages should use the same version as root

packages/astro/src/core/errors/errors-data.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ export const ResponseSentError = {
658658
*
659659
* For example:
660660
* ```ts
661-
* import {defineMiddleware} from "astro/middleware";
661+
* import {defineMiddleware} from "astro:middleware";
662662
* export const onRequest = defineMiddleware((context, _) => {
663663
* // doesn't return anything or call `next`
664664
* context.locals.someData = false;
@@ -678,7 +678,7 @@ export const MiddlewareNoDataOrNextCalled = {
678678
*
679679
* For example:
680680
* ```ts
681-
* import {defineMiddleware} from "astro/middleware";
681+
* import {defineMiddleware} from "astro:middleware";
682682
* export const onRequest = defineMiddleware(() => {
683683
* return "string"
684684
* });
@@ -698,7 +698,7 @@ export const MiddlewareNotAResponse = {
698698
*
699699
* For example:
700700
* ```ts
701-
* import {defineMiddleware} from "astro/middleware";
701+
* import {defineMiddleware} from "astro:middleware";
702702
* export const onRequest = defineMiddleware((context, next) => {
703703
* context.locals = 1541;
704704
* return next();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export {
2+
defineMiddleware,
3+
sequence,
4+
} from './index.js';

packages/astro/test/fixtures/astro-component-code/src/pages/basic.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
import {Code} from 'astro/components';
2+
import {Code} from 'astro:components';
33
---
44
<html>
55
<head><title>Code component</title></head>

packages/astro/test/fixtures/astro-component-code/src/pages/css-theme.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
import {Code} from 'astro/components';
2+
import {Code} from 'astro:components';
33
---
44
<html>
55
<head><title>Code component</title></head>

0 commit comments

Comments
 (0)