-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
astroFeaturesValidation.ts
162 lines (144 loc) · 4.09 KB
/
astroFeaturesValidation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import type {
AstroAssetsFeature,
AstroConfig,
AstroFeatureMap,
SupportsKind,
} from '../@types/astro';
import { error, type LogOptions, warn } from '../core/logger/core.js';
import { bold } from 'kleur/colors';
const STABLE = 'stable';
const DEPRECATED = 'deprecated';
const UNSUPPORTED = 'unsupported';
const EXPERIMENTAL = 'experimental';
const UNSUPPORTED_ASSETS_FEATURE: AstroAssetsFeature = {
supportKind: UNSUPPORTED,
isSquooshCompatible: false,
isSharpCompatible: false,
};
// NOTE: remove for Astro 4.0
const ALL_UNSUPPORTED: Required<AstroFeatureMap> = {
serverOutput: UNSUPPORTED,
staticOutput: UNSUPPORTED,
hybridOutput: UNSUPPORTED,
assets: UNSUPPORTED_ASSETS_FEATURE,
};
type ValidationResult = {
[Property in keyof AstroFeatureMap]: boolean;
};
/**
* Checks whether an adapter supports certain features that are enabled via Astro configuration.
*
* If a configuration is enabled and "unlocks" a feature, but the adapter doesn't support, the function
* will throw a runtime error.
*
*/
export function validateSupportedFeatures(
adapterName: string,
featureMap: AstroFeatureMap = ALL_UNSUPPORTED,
config: AstroConfig,
logging: LogOptions
): ValidationResult {
const {
assets = UNSUPPORTED_ASSETS_FEATURE,
serverOutput = UNSUPPORTED,
staticOutput = UNSUPPORTED,
hybridOutput = UNSUPPORTED,
} = featureMap;
const validationResult: ValidationResult = {};
validationResult.staticOutput = validateSupportKind(
staticOutput,
adapterName,
logging,
'staticOutput',
() => config?.output === 'static'
);
validationResult.hybridOutput = validateSupportKind(
hybridOutput,
adapterName,
logging,
'hybridOutput',
() => config?.output === 'hybrid'
);
validationResult.serverOutput = validateSupportKind(
serverOutput,
adapterName,
logging,
'serverOutput',
() => config?.output === 'server'
);
validationResult.assets = validateAssetsFeature(assets, adapterName, config, logging);
return validationResult;
}
function validateSupportKind(
supportKind: SupportsKind,
adapterName: string,
logging: LogOptions,
featureName: string,
hasCorrectConfig: () => boolean
): boolean {
if (supportKind === STABLE) {
return true;
} else if (supportKind === DEPRECATED) {
featureIsDeprecated(adapterName, logging);
} else if (supportKind === EXPERIMENTAL) {
featureIsExperimental(adapterName, logging);
}
if (hasCorrectConfig() && supportKind === UNSUPPORTED) {
featureIsUnsupported(adapterName, logging, featureName);
return false;
} else {
return true;
}
}
function featureIsUnsupported(adapterName: string, logging: LogOptions, featureName: string) {
error(
logging,
`${adapterName}`,
`The feature ${featureName} is not supported by the adapter ${adapterName}.`
);
}
function featureIsExperimental(adapterName: string, logging: LogOptions) {
warn(logging, `${adapterName}`, 'The feature is experimental and subject to issues or changes.');
}
function featureIsDeprecated(adapterName: string, logging: LogOptions) {
warn(
logging,
`${adapterName}`,
'The feature is deprecated and will be moved in the next release.'
);
}
const SHARP_SERVICE = 'astro/assets/services/sharp';
const SQUOOSH_SERVICE = 'astro/assets/services/squoosh';
function validateAssetsFeature(
assets: AstroAssetsFeature,
adapterName: string,
config: AstroConfig,
logging: LogOptions
): boolean {
const {
supportKind = UNSUPPORTED,
isSharpCompatible = false,
isSquooshCompatible = false,
} = assets;
if (config?.image?.service?.entrypoint === SHARP_SERVICE && !isSharpCompatible) {
error(
logging,
'astro',
`The currently selected adapter \`${adapterName}\` is not compatible with the service "Sharp". ${bold(
'Your project will NOT be able to build.'
)}`
);
return false;
}
if (config?.image?.service?.entrypoint === SQUOOSH_SERVICE && !isSquooshCompatible) {
error(
logging,
'astro',
`The currently selected adapter \`${adapterName}\` is not compatible with the service "Squoosh". ${bold(
'Your project will NOT be able to build.'
)}`
);
return false;
}
return validateSupportKind(supportKind, adapterName, logging, 'assets', () => true);
}