-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdn.js
More file actions
223 lines (200 loc) · 5.74 KB
/
Copy pathcdn.js
File metadata and controls
223 lines (200 loc) · 5.74 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const cdnProviders = {
importMap: {
label: 'import map',
host: '',
},
esm: {
label: 'esm.sh',
host: 'https://esm.sh',
},
unpkg: {
label: 'unpkg',
host: 'https://unpkg.com',
},
jspmGa: {
label: 'ga.jspm.io',
host: 'https://ga.jspm.io',
},
}
/*
* Local dev defaults to esm.sh.
* Production can set window.__KNIGHTED_PRIMARY_CDN__ = 'importMap'.
*/
const defaultPrimaryCdnProvider = 'esm'
const configuredPrimaryCdnProvider =
typeof globalThis !== 'undefined' &&
typeof globalThis.__KNIGHTED_PRIMARY_CDN__ === 'string'
? globalThis.__KNIGHTED_PRIMARY_CDN__
: defaultPrimaryCdnProvider
const primaryCdnProvider =
configuredPrimaryCdnProvider in cdnProviders
? configuredPrimaryCdnProvider
: defaultPrimaryCdnProvider
const fallbackCdnProvidersByPrimary = {
importMap: ['esm', 'unpkg', 'jspmGa'],
esm: ['unpkg', 'jspmGa'],
unpkg: ['esm', 'jspmGa'],
jspmGa: ['unpkg', 'esm'],
}
const fallbackCdnProviders = fallbackCdnProvidersByPrimary[primaryCdnProvider] ?? []
export const cdnImportSpecs = {
cssBrowser: {
importMap: '@knighted/css/browser',
esm: '@knighted/css/browser',
jspmGa: 'npm:@knighted/css/browser',
},
jsxDom: {
importMap: '@knighted/jsx',
esm: '@knighted/jsx',
jspmGa: 'npm:@knighted/jsx',
},
jsxTranspile: {
importMap: '@knighted/jsx/transpile',
esm: '@knighted/jsx/transpile',
jspmGa: 'npm:@knighted/jsx/transpile',
},
jsxReact: {
importMap: '@knighted/jsx/react',
esm: '@knighted/jsx/react',
jspmGa: 'npm:@knighted/jsx/react',
},
react: {
importMap: 'react',
esm: 'react@19.2.4',
jspmGa: 'npm:react@19.2.4/index.js',
},
reactDomClient: {
importMap: 'react-dom/client',
esm: 'react-dom@19.2.4/client',
jspmGa: 'npm:react-dom@19.2.4/client.js',
},
sass: {
importMap: 'sass',
esm: [
'sass@1.93.2?conditions=browser',
'sass@1.93.2/sass.default?conditions=browser',
],
unpkg: 'sass@1.93.2/sass.default.js?module',
jspmGa: 'npm:sass@1.93.2/sass.default.js',
},
less: {
importMap: 'less',
esm: 'less',
jspmGa: 'npm:less',
},
lightningCssWasm: {
importMap: '@parcel/css-wasm',
esm: '@parcel/css-wasm',
jspmGa: 'npm:@parcel/css-wasm',
},
codemirrorState: {
importMap: '@codemirror/state',
esm: '@codemirror/state',
jspmGa: 'npm:@codemirror/state@6.5.2',
},
codemirrorView: {
importMap: '@codemirror/view',
esm: '@codemirror/view',
jspmGa: 'npm:@codemirror/view@6.38.6',
},
codemirrorCommands: {
importMap: '@codemirror/commands',
esm: '@codemirror/commands',
jspmGa: 'npm:@codemirror/commands@6.10.0',
},
codemirrorAutocomplete: {
importMap: '@codemirror/autocomplete',
esm: '@codemirror/autocomplete',
jspmGa: 'npm:@codemirror/autocomplete@6.20.0',
},
codemirrorLanguage: {
importMap: '@codemirror/language',
esm: '@codemirror/language',
jspmGa: 'npm:@codemirror/language@6.11.3',
},
codemirrorLezerHighlight: {
importMap: '@lezer/highlight',
esm: '@lezer/highlight',
jspmGa: 'npm:@lezer/highlight@1.2.3',
},
codemirrorLangJavascript: {
importMap: '@codemirror/lang-javascript',
esm: '@codemirror/lang-javascript',
jspmGa: 'npm:@codemirror/lang-javascript@6.2.4',
},
codemirrorLangCss: {
importMap: '@codemirror/lang-css',
esm: '@codemirror/lang-css',
jspmGa: 'npm:@codemirror/lang-css@6.3.1',
},
}
const getProviderPriority = () => {
const ordered = [primaryCdnProvider, ...fallbackCdnProviders]
return [...new Set(ordered)]
}
const getCdnImportCandidates = importKey => {
const specs = cdnImportSpecs[importKey]
if (!specs || typeof specs !== 'object') {
throw new Error(`Unknown CDN import key: ${String(importKey)}`)
}
const candidates = []
for (const provider of getProviderPriority()) {
const configured = specs[provider]
if (!configured) continue
const specifiers = Array.isArray(configured) ? configured : [configured]
for (const specifier of specifiers) {
if (typeof specifier !== 'string' || specifier.length === 0) continue
candidates.push({ provider, specifier })
}
}
if (candidates.length === 0) {
throw new Error(`No CDN candidates configured for import key: ${String(importKey)}`)
}
return candidates
}
export const cdnImports = Object.fromEntries(
Object.keys(cdnImportSpecs).map(importKey => [
importKey,
getCdnImportCandidates(importKey),
]),
)
export const getCdnImportUrl = ({ provider, specifier }) => {
const config = cdnProviders[provider]
if (!config) {
throw new Error(`Unknown CDN provider: ${String(provider)}`)
}
if (provider === 'importMap') {
return specifier
}
return `${config.host}/${specifier}`
}
export const getPrimaryCdnImportUrl = importKey => {
const candidates = cdnImports[importKey]
if (!Array.isArray(candidates) || candidates.length === 0) {
throw new Error(`Unknown CDN import key: ${String(importKey)}`)
}
return getCdnImportUrl(candidates[0])
}
export const getPrimaryCdnImportUrls = importKeys => {
const unique = new Set()
for (const key of importKeys) {
unique.add(getPrimaryCdnImportUrl(key))
}
return [...unique]
}
const importFromCdnCandidateAt = async (importCandidates, index, firstError = null) => {
if (index >= importCandidates.length) {
throw (
firstError ?? new Error('Unknown module loading failure while importing from CDN.')
)
}
const url = getCdnImportUrl(importCandidates[index])
try {
const module = await import(url)
return { module, url }
} catch (error) {
return importFromCdnCandidateAt(importCandidates, index + 1, firstError ?? error)
}
}
export const importFromCdnWithFallback = importCandidates =>
importFromCdnCandidateAt(importCandidates, 0)