-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
refactor: rework supportedAstroFeatures #11806
Changes from all commits
6cc713a
9619f95
6f15dcd
3ec5e00
a92093b
669a7b3
d835c43
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'astro': major | ||
--- | ||
|
||
Removes the `assets` property on `supportedAstroFeatures` for adapters, as it did not reflect reality properly in many cases. | ||
|
||
Now, relating to assets, only a single `sharpImageService` property is available, determining if the adapter is compatible with the built-in sharp image service. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
The value of the different properties on `supportedAstroFeatures` for adapters can now be objects, with a `support` and `message` properties. The content of the `message` property will be shown in the Astro CLI when the adapter is not compatible with the feature, allowing one to give a better informational message to the user. | ||
|
||
This is notably useful with the new `limited` value, to explain to the user why support is limited. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Adds a new `limited` value for the different properties of `supportedAstroFeatures` for adapters, which indicates that the adapter is compatible with the feature, but with some limitations. This is useful for adapters that support a feature, but not in all cases or with all options. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,18 @@ | ||
import type { Logger } from '../core/logger/core.js'; | ||
import type { AstroSettings } from '../types/astro.js'; | ||
import type { AstroConfig } from '../types/public/config.js'; | ||
import type { | ||
AdapterSupport, | ||
AdapterSupportsKind, | ||
AstroAdapterFeatureMap, | ||
AstroAdapterFeatures, | ||
AstroAssetsFeature, | ||
} from '../types/public/integrations.js'; | ||
|
||
const STABLE = 'stable'; | ||
const DEPRECATED = 'deprecated'; | ||
const UNSUPPORTED = 'unsupported'; | ||
const EXPERIMENTAL = 'experimental'; | ||
|
||
const UNSUPPORTED_ASSETS_FEATURE: AstroAssetsFeature = { | ||
supportKind: UNSUPPORTED, | ||
isSharpCompatible: false, | ||
}; | ||
export const AdapterFeatureStability = { | ||
STABLE: 'stable', | ||
DEPRECATED: 'deprecated', | ||
UNSUPPORTED: 'unsupported', | ||
EXPERIMENTAL: 'experimental', | ||
LIMITED: 'limited', | ||
} as const; | ||
|
||
type ValidationResult = { | ||
[Property in keyof AstroAdapterFeatureMap]: boolean; | ||
|
@@ -33,16 +29,15 @@ export function validateSupportedFeatures( | |
adapterName: string, | ||
featureMap: AstroAdapterFeatureMap, | ||
settings: AstroSettings, | ||
adapterFeatures: AstroAdapterFeatures | undefined, | ||
logger: Logger, | ||
): ValidationResult { | ||
const { | ||
assets = UNSUPPORTED_ASSETS_FEATURE, | ||
serverOutput = UNSUPPORTED, | ||
staticOutput = UNSUPPORTED, | ||
hybridOutput = UNSUPPORTED, | ||
i18nDomains = UNSUPPORTED, | ||
envGetSecret = UNSUPPORTED, | ||
serverOutput = AdapterFeatureStability.UNSUPPORTED, | ||
staticOutput = AdapterFeatureStability.UNSUPPORTED, | ||
hybridOutput = AdapterFeatureStability.UNSUPPORTED, | ||
i18nDomains = AdapterFeatureStability.UNSUPPORTED, | ||
envGetSecret = AdapterFeatureStability.UNSUPPORTED, | ||
sharpImageService = AdapterFeatureStability.UNSUPPORTED, | ||
} = featureMap; | ||
const validationResult: ValidationResult = {}; | ||
|
||
|
@@ -67,9 +62,8 @@ export function validateSupportedFeatures( | |
adapterName, | ||
logger, | ||
'serverOutput', | ||
() => settings.config?.output === 'server', | ||
() => settings.config?.output === 'server' || settings.buildOutput === 'server', | ||
); | ||
validationResult.assets = validateAssetsFeature(assets, adapterName, settings.config, logger); | ||
|
||
if (settings.config.i18n?.domains) { | ||
validationResult.i18nDomains = validateSupportKind( | ||
|
@@ -91,71 +85,93 @@ export function validateSupportedFeatures( | |
() => Object.keys(settings.config?.env?.schema ?? {}).length !== 0, | ||
); | ||
|
||
validationResult.sharpImageService = validateSupportKind( | ||
sharpImageService, | ||
adapterName, | ||
logger, | ||
'sharp', | ||
() => settings.config?.image?.service?.entrypoint === 'astro/assets/services/sharp', | ||
); | ||
|
||
return validationResult; | ||
} | ||
|
||
export function unwrapSupportKind(supportKind?: AdapterSupport): AdapterSupportsKind | undefined { | ||
if (!supportKind) { | ||
return undefined; | ||
} | ||
|
||
return typeof supportKind === 'object' ? supportKind.support : supportKind; | ||
} | ||
|
||
export function getSupportMessage(supportKind: AdapterSupport): string | undefined { | ||
return typeof supportKind === 'object' ? supportKind.message : undefined; | ||
} | ||
|
||
function validateSupportKind( | ||
supportKind: AdapterSupportsKind, | ||
supportKind: AdapterSupport, | ||
adapterName: string, | ||
logger: Logger, | ||
featureName: string, | ||
hasCorrectConfig: () => boolean, | ||
): boolean { | ||
if (supportKind === STABLE) { | ||
return true; | ||
} else if (supportKind === DEPRECATED) { | ||
featureIsDeprecated(adapterName, logger, featureName); | ||
} else if (supportKind === EXPERIMENTAL) { | ||
featureIsExperimental(adapterName, logger, featureName); | ||
} | ||
const supportValue = unwrapSupportKind(supportKind); | ||
const message = getSupportMessage(supportKind); | ||
|
||
if (hasCorrectConfig() && supportKind === UNSUPPORTED) { | ||
featureIsUnsupported(adapterName, logger, featureName); | ||
if (!supportValue) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
function featureIsUnsupported(adapterName: string, logger: Logger, featureName: string) { | ||
logger.error( | ||
'config', | ||
`The adapter ${adapterName} doesn't currently support the feature "${featureName}".`, | ||
); | ||
} | ||
if (supportValue === AdapterFeatureStability.STABLE) { | ||
return true; | ||
} else if (hasCorrectConfig()) { | ||
// If the user has the relevant configuration, but the adapter doesn't support it, warn the user | ||
logFeatureSupport(adapterName, logger, featureName, supportValue, message); | ||
} | ||
|
||
function featureIsExperimental(adapterName: string, logger: Logger, featureName: string) { | ||
logger.warn( | ||
'config', | ||
`The adapter ${adapterName} provides experimental support for "${featureName}". You may experience issues or breaking changes until this feature is fully supported by the adapter.`, | ||
); | ||
return false; | ||
} | ||
|
||
function featureIsDeprecated(adapterName: string, logger: Logger, featureName: string) { | ||
logger.warn( | ||
'config', | ||
`The adapter ${adapterName} has deprecated its support for "${featureName}", and future compatibility is not guaranteed. The adapter may completely remove support for this feature without warning.`, | ||
); | ||
} | ||
|
||
const SHARP_SERVICE = 'astro/assets/services/sharp'; | ||
|
||
function validateAssetsFeature( | ||
assets: AstroAssetsFeature, | ||
function logFeatureSupport( | ||
adapterName: string, | ||
config: AstroConfig, | ||
logger: Logger, | ||
): boolean { | ||
const { supportKind = UNSUPPORTED, isSharpCompatible = false } = assets; | ||
if (config?.image?.service?.entrypoint === SHARP_SERVICE && !isSharpCompatible) { | ||
logger.warn( | ||
null, | ||
`The currently selected adapter \`${adapterName}\` is not compatible with the image service "Sharp".`, | ||
); | ||
return false; | ||
featureName: string, | ||
supportKind: AdapterSupport, | ||
adapterMessage?: string, | ||
) { | ||
switch (supportKind) { | ||
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. Merged the different functions into one here that way you get an exhaustive check on the switch, felt nicer |
||
case AdapterFeatureStability.STABLE: | ||
break; | ||
case AdapterFeatureStability.DEPRECATED: | ||
logger.warn( | ||
'config', | ||
`The adapter ${adapterName} has deprecated its support for "${featureName}", and future compatibility is not guaranteed. The adapter may completely remove support for this feature without warning.`, | ||
); | ||
break; | ||
case AdapterFeatureStability.EXPERIMENTAL: | ||
logger.warn( | ||
'config', | ||
`The adapter ${adapterName} provides experimental support for "${featureName}". You may experience issues or breaking changes until this feature is fully supported by the adapter.`, | ||
); | ||
break; | ||
case AdapterFeatureStability.LIMITED: | ||
logger.warn( | ||
'config', | ||
`The adapter ${adapterName} has limited support for "${featureName}". Certain features may not work as expected.`, | ||
); | ||
break; | ||
case AdapterFeatureStability.UNSUPPORTED: | ||
logger.error( | ||
'config', | ||
`The adapter ${adapterName} does not currently support the feature "${featureName}". Your project may not build correctly.`, | ||
); | ||
break; | ||
} | ||
|
||
return validateSupportKind(supportKind, adapterName, logger, 'assets', () => true); | ||
// If the adapter specified a custom message, log it after the default message | ||
if (adapterMessage) { | ||
logger.warn('adapter', adapterMessage); | ||
} | ||
} | ||
|
||
export function getAdapterStaticRecommendation(adapterName: string): string | undefined { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import type { ViteDevServer, InlineConfig as ViteInlineConfig } from 'vite'; | |
import type { SerializedSSRManifest } from '../../core/app/types.js'; | ||
import type { PageBuildData } from '../../core/build/types.js'; | ||
import type { AstroIntegrationLogger } from '../../core/logger/core.js'; | ||
import type { AdapterFeatureStability } from '../../integrations/features-validation.js'; | ||
import type { getToolbarServerCommunicationHelpers } from '../../integrations/hooks.js'; | ||
import type { DeepPartial } from '../../type-utils.js'; | ||
import type { AstroConfig } from './config.js'; | ||
|
@@ -60,7 +61,15 @@ export interface AstroRenderer { | |
serverEntrypoint: string; | ||
} | ||
|
||
export type AdapterSupportsKind = 'unsupported' | 'stable' | 'experimental' | 'deprecated'; | ||
export type AdapterSupportsKind = | ||
(typeof AdapterFeatureStability)[keyof typeof AdapterFeatureStability]; | ||
Comment on lines
+64
to
+65
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. Moved the source of truth from the type to the runtime check here, that way they're always accurate |
||
|
||
export type AdapterSupportWithMessage = { | ||
support: Exclude<AdapterSupportsKind, 'stable'>; | ||
message: string; | ||
}; | ||
|
||
export type AdapterSupport = AdapterSupportsKind | AdapterSupportWithMessage; | ||
|
||
export interface AstroAdapterFeatures { | ||
/** | ||
|
@@ -92,45 +101,33 @@ export type AstroAdapterFeatureMap = { | |
/** | ||
* The adapter is able serve static pages | ||
*/ | ||
staticOutput?: AdapterSupportsKind; | ||
staticOutput?: AdapterSupport; | ||
|
||
/** | ||
* The adapter is able to serve pages that are static or rendered via server | ||
*/ | ||
hybridOutput?: AdapterSupportsKind; | ||
hybridOutput?: AdapterSupport; | ||
|
||
/** | ||
* The adapter is able to serve SSR pages | ||
*/ | ||
serverOutput?: AdapterSupportsKind; | ||
/** | ||
* The adapter can emit static assets | ||
*/ | ||
assets?: AstroAssetsFeature; | ||
serverOutput?: AdapterSupport; | ||
|
||
/** | ||
* List of features that orbit around the i18n routing | ||
* The adapter is able to support i18n domains | ||
*/ | ||
i18nDomains?: AdapterSupportsKind; | ||
i18nDomains?: AdapterSupport; | ||
|
||
/** | ||
* The adapter is able to support `getSecret` exported from `astro:env/server` | ||
*/ | ||
envGetSecret?: AdapterSupportsKind; | ||
}; | ||
envGetSecret?: AdapterSupport; | ||
|
||
export interface AstroAssetsFeature { | ||
supportKind?: AdapterSupportsKind; | ||
/** | ||
* Whether if this adapter deploys files in an environment that is compatible with the library `sharp` | ||
* The adapter supports image transformation using the built-in Sharp image service | ||
*/ | ||
isSharpCompatible?: boolean; | ||
} | ||
|
||
export interface AstroInternationalizationFeature { | ||
/** | ||
* The adapter should be able to create the proper redirects | ||
*/ | ||
domains?: AdapterSupportsKind; | ||
} | ||
sharpImageService?: AdapterSupport; | ||
}; | ||
|
||
/** | ||
* IDs for different stages of JS script injection: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous check here was weird, it only checked if the config was correct for the unsupported value, but you need to always check it, otherwise you get warning for every experimental feature, even the ones you don't use?