Skip to content

Commit c546563

Browse files
feat: better adapter type (#15412)
* feat: better adapter type * fix * Discard changes to packages/integrations/cloudflare/src/index.ts * Apply suggestion from @florian-lefebvre * feedback
1 parent cc3c46c commit c546563

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

.changeset/dry-icons-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Improves the `AstroAdapter` type and how legacy adapters are handled

packages/astro/dev-only.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ declare module 'virtual:astro:adapter-entrypoint' {
6666

6767
declare module 'virtual:astro:adapter-config' {
6868
export const args: any;
69-
export const exports: string[] | undefined;
70-
export const adapterFeatures: any;
71-
export const serverEntrypoint: string;
7269
}
7370

7471
declare module 'virtual:astro:dev-css' {

packages/astro/src/core/build/plugins/plugin-ssr.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ import type { BuildInternals } from '../internal.js';
44
import type { StaticBuildOptions } from '../types.js';
55
import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../../constants.js';
66

7-
const SSR_VIRTUAL_MODULE_ID = 'virtual:astro:legacy-ssr-entry';
8-
export const RESOLVED_SSR_VIRTUAL_MODULE_ID = '\0' + SSR_VIRTUAL_MODULE_ID;
7+
type LegacyAdapter = Extract<AstroAdapter, { entryType?: 'legacy-dynamic' }>;
8+
9+
function isLegacyAdapter(adapter: AstroAdapter): adapter is LegacyAdapter {
10+
return adapter.entryType === undefined || adapter.entryType === 'legacy-dynamic';
11+
}
12+
13+
export const LEGACY_SSR_ENTRY_VIRTUAL_MODULE = 'virtual:astro:legacy-ssr-entry';
14+
export const RESOLVED_LEGACY_SSR_ENTRY_VIRTUAL_MODULE = '\0' + LEGACY_SSR_ENTRY_VIRTUAL_MODULE;
915

1016
const ADAPTER_VIRTUAL_MODULE_ID = 'virtual:astro:adapter-entrypoint';
1117
const RESOLVED_ADAPTER_VIRTUAL_MODULE_ID = '\0' + ADAPTER_VIRTUAL_MODULE_ID;
1218

1319
const ADAPTER_CONFIG_VIRTUAL_MODULE_ID = 'virtual:astro:adapter-config';
1420
const RESOLVED_ADAPTER_CONFIG_VIRTUAL_MODULE_ID = '\0' + ADAPTER_CONFIG_VIRTUAL_MODULE_ID;
1521

16-
function vitePluginAdapter(adapter: AstroAdapter): VitePlugin {
22+
function vitePluginAdapter(adapter: LegacyAdapter): VitePlugin {
1723
return {
1824
name: '@astrojs/vite-plugin-astro-adapter',
1925
enforce: 'post',
@@ -49,7 +55,7 @@ export default _serverEntrypoint.default;`,
4955
* Makes adapter config (args, exports, features, entrypoint) available at runtime
5056
* so the adapter can access its own configuration during SSR.
5157
*/
52-
function vitePluginAdapterConfig(adapter: AstroAdapter): VitePlugin {
58+
function vitePluginAdapterConfig(adapter: LegacyAdapter): VitePlugin {
5359
return {
5460
name: '@astrojs/vite-plugin-astro-adapter-config',
5561
enforce: 'post',
@@ -70,17 +76,14 @@ function vitePluginAdapterConfig(adapter: AstroAdapter): VitePlugin {
7076
},
7177
handler() {
7278
return {
73-
code: `export const args = ${adapter.args ? JSON.stringify(adapter.args, null, 2) : 'undefined'};
74-
export const exports = ${adapter.exports ? JSON.stringify(adapter.exports) : 'undefined'};
75-
export const adapterFeatures = ${adapter.adapterFeatures ? JSON.stringify(adapter.adapterFeatures, null, 2) : 'undefined'};
76-
export const serverEntrypoint = ${JSON.stringify(adapter.serverEntrypoint)};`,
79+
code: `export const args = ${adapter.args ? JSON.stringify(adapter.args, null, 2) : 'undefined'};`,
7780
};
7881
},
7982
},
8083
};
8184
}
8285

83-
function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter): VitePlugin {
86+
function vitePluginSSR(internals: BuildInternals, adapter: LegacyAdapter): VitePlugin {
8487
return {
8588
name: '@astrojs/vite-plugin-astro-ssr-server',
8689
enforce: 'post',
@@ -89,15 +92,15 @@ function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter): VitePl
8992
},
9093
resolveId: {
9194
filter: {
92-
id: new RegExp(`^${SSR_VIRTUAL_MODULE_ID}$`),
95+
id: new RegExp(`^${LEGACY_SSR_ENTRY_VIRTUAL_MODULE}$`),
9396
},
9497
handler() {
95-
return RESOLVED_SSR_VIRTUAL_MODULE_ID;
98+
return RESOLVED_LEGACY_SSR_ENTRY_VIRTUAL_MODULE;
9699
},
97100
},
98101
load: {
99102
filter: {
100-
id: new RegExp(`^${RESOLVED_SSR_VIRTUAL_MODULE_ID}$`),
103+
id: new RegExp(`^${RESOLVED_LEGACY_SSR_ENTRY_VIRTUAL_MODULE}$`),
101104
},
102105
handler() {
103106
const exports: string[] = [];
@@ -131,14 +134,17 @@ function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter): VitePl
131134
}
132135

133136
export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals): VitePlugin[] {
134-
// We check before this point if there's an adapter, so we can safely assume it exists here.
135-
const adapter = options.settings.adapter!;
137+
const adapter = options.settings.adapter;
136138
const ssr = options.settings.buildOutput === 'server';
137139

138-
const plugins: VitePlugin[] = [vitePluginAdapter(adapter), vitePluginAdapterConfig(adapter)];
140+
const plugins: VitePlugin[] = [];
141+
142+
if (adapter && isLegacyAdapter(adapter)) {
143+
plugins.push(vitePluginAdapter(adapter), vitePluginAdapterConfig(adapter));
139144

140-
if (ssr) {
141-
plugins.unshift(vitePluginSSR(internals, adapter));
145+
if (ssr) {
146+
plugins.unshift(vitePluginSSR(internals, adapter));
147+
}
142148
}
143149

144150
return plugins;

packages/astro/src/core/build/static-build.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { generatePages } from './generate.js';
2222
import { trackPageData } from './internal.js';
2323
import { getAllBuildPlugins } from './plugins/index.js';
2424
import { manifestBuildPostHook } from './plugins/plugin-manifest.js';
25-
import { RESOLVED_SSR_VIRTUAL_MODULE_ID } from './plugins/plugin-ssr.js';
25+
import { LEGACY_SSR_ENTRY_VIRTUAL_MODULE, RESOLVED_LEGACY_SSR_ENTRY_VIRTUAL_MODULE } from './plugins/plugin-ssr.js';
2626
import { ASTRO_PAGE_EXTENSION_POST_PATTERN } from './plugins/util.js';
2727
import type { StaticBuildOptions } from './types.js';
2828
import { encodeName, getTimeStat, viteBuildReturnToRollupOutputs } from './util.js';
@@ -228,7 +228,7 @@ async function buildEnvironments(opts: StaticBuildOptions, internals: BuildInter
228228
...viteConfig.build?.rollupOptions,
229229
// Setting as `exports-only` allows us to safely delete inputs that are only used during prerendering
230230
preserveEntrySignatures: 'exports-only',
231-
...(useLegacyDynamic ? { input: 'virtual:astro:legacy-ssr-entry' } : {}),
231+
...(useLegacyDynamic ? { input: LEGACY_SSR_ENTRY_VIRTUAL_MODULE } : {}),
232232
output: {
233233
hoistTransitiveImports: false,
234234
format: 'esm',
@@ -264,7 +264,7 @@ async function buildEnvironments(opts: StaticBuildOptions, internals: BuildInter
264264
routes,
265265
);
266266
} else if (
267-
chunkInfo.facadeModuleId === RESOLVED_SSR_VIRTUAL_MODULE_ID ||
267+
chunkInfo.facadeModuleId === RESOLVED_LEGACY_SSR_ENTRY_VIRTUAL_MODULE ||
268268
// This catches the case when the adapter uses `entryType: 'self'. When doing so,
269269
// the adapter must set rollupOptions.input.
270270
isRollupInput(chunkInfo.name) ||

packages/astro/src/types/public/integrations.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,33 @@ export interface AstroAdapterClientConfig {
119119
assetQueryParams?: URLSearchParams;
120120
}
121121

122-
export interface AstroAdapter {
123-
name: string;
122+
interface AdapterLegacyDynamicProperties {
123+
/**
124+
* Determines how the adapter's entrypoint is handled during the build.
125+
* - `'self'`: The adapter defines its own entrypoint and sets rollupOptions.input
126+
* - `'legacy-dynamic'`: Uses the virtual module entrypoint with dynamic exports
127+
* @default 'legacy-dynamic'
128+
*/
129+
entryType?: 'legacy-dynamic';
124130
serverEntrypoint?: string | URL;
125-
previewEntrypoint?: string | URL;
126131
exports?: string[];
127132
args?: any;
128-
adapterFeatures?: AstroAdapterFeatures;
133+
}
134+
135+
interface AdapterSelfProperties {
129136
/**
130137
* Determines how the adapter's entrypoint is handled during the build.
131138
* - `'self'`: The adapter defines its own entrypoint and sets rollupOptions.input
132139
* - `'legacy-dynamic'`: Uses the virtual module entrypoint with dynamic exports
133140
* @default 'legacy-dynamic'
134141
*/
135-
entryType?: 'self' | 'legacy-dynamic';
142+
entryType: 'self';
143+
}
144+
145+
export type AstroAdapter = {
146+
name: string;
147+
previewEntrypoint?: string | URL;
148+
adapterFeatures?: AstroAdapterFeatures;
136149
/**
137150
* List of features supported by an adapter.
138151
*
@@ -143,7 +156,7 @@ export interface AstroAdapter {
143156
* Configuration for Astro's client-side code.
144157
*/
145158
client?: AstroAdapterClientConfig;
146-
}
159+
} & (AdapterLegacyDynamicProperties | AdapterSelfProperties);
147160

148161
export type AstroAdapterFeatureMap = {
149162
/**

0 commit comments

Comments
 (0)