@@ -10,6 +10,7 @@ import type { AlephConfig, ImportMap, JSXConfig, ModuleLoader } from "./types.ts
10
10
export const regFullVersion = / @ \d + \. \d + \. \d + / ;
11
11
export const builtinModuleExts = [ "tsx" , "ts" , "mts" , "jsx" , "js" , "mjs" ] ;
12
12
13
+ /** Stores and returns the `fn` output in the `globalThis` object */
13
14
export async function globalIt < T > ( name : string , fn : ( ) => Promise < T > ) : Promise < T > {
14
15
const cache : T | undefined = Reflect . get ( globalThis , name ) ;
15
16
if ( cache !== undefined ) {
@@ -22,6 +23,7 @@ export async function globalIt<T>(name: string, fn: () => Promise<T>): Promise<T
22
23
return ret ;
23
24
}
24
25
26
+ /** Stores and returns the `fn` output in the `globalThis` object synchronously. */
25
27
export function globalItSync < T > ( name : string , fn : ( ) => T ) : T {
26
28
const cache : T | undefined = Reflect . get ( globalThis , name ) ;
27
29
if ( cache !== undefined ) {
@@ -34,6 +36,7 @@ export function globalItSync<T>(name: string, fn: () => T): T {
34
36
return ret ;
35
37
}
36
38
39
+ /* Get Aleph.js package URI. */
37
40
export function getAlephPkgUri ( ) : string {
38
41
return globalItSync ( "__ALEPH_PKG_URI" , ( ) => {
39
42
const uriFromEnv = Deno . env . get ( "ALEPH_PKG_URI" ) ;
@@ -49,16 +52,21 @@ export function getAlephPkgUri(): string {
49
52
} ) ;
50
53
}
51
54
55
+ /** Get the UnoCSS generator, return `null` if the presets are empty. */
52
56
export function getUnoGenerator ( ) : UnoGenerator | null {
57
+ const config : AlephConfig | undefined = Reflect . get ( globalThis , "__ALEPH_CONFIG" ) ;
58
+ if ( config === undefined ) {
59
+ return null ;
60
+ }
53
61
return globalItSync ( "__UNO_GENERATOR" , ( ) => {
54
- const config : AlephConfig | undefined = Reflect . get ( globalThis , "__ALEPH_CONFIG" ) ;
55
62
if ( config ?. unocss ?. presets ) {
56
63
return createGenerator ( config . unocss ) ;
57
64
}
58
65
return null ;
59
66
} ) ;
60
67
}
61
68
69
+ /** Get the deployment ID. */
62
70
export function getDeploymentId ( ) : string | null {
63
71
return Deno . env . get ( "DENO_DEPLOYMENT_ID" ) ?? null ;
64
72
}
@@ -101,6 +109,40 @@ export function restoreUrl(pathname: string): string {
101
109
return `${ protocol } ://${ host } ${ port ? ":" + port : "" } /${ rest . join ( "/" ) } ` ;
102
110
}
103
111
112
+ /** init loaders in `CLI` mode, or use prebuild loaders */
113
+ export async function initModuleLoaders ( importMap : ImportMap ) : Promise < ModuleLoader [ ] > {
114
+ const loaders : ModuleLoader [ ] = Reflect . get ( globalThis , "__ALEPH_MODULE_LOADERS" ) || [ ] ;
115
+ if ( Deno . env . get ( "ALEPH_CLI" ) ) {
116
+ for ( const key in importMap . imports ) {
117
+ if ( / ^ \* \. { ? ( \w + , ? ) * \w + } ? $ / i. test ( key ) ) {
118
+ let src = importMap . imports [ key ] ;
119
+ if ( src . endsWith ( "!loader" ) ) {
120
+ src = util . trimSuffix ( src , "!loader" ) ;
121
+ if ( src . startsWith ( "./" ) || src . startsWith ( "../" ) ) {
122
+ src = "file://" + join ( dirname ( importMap . __filename ) , src ) ;
123
+ }
124
+ let { default : loader } = await import ( src ) ;
125
+ if ( typeof loader === "function" ) {
126
+ loader = new loader ( ) ;
127
+ }
128
+ if ( loader !== null && typeof loader === "object" && typeof loader . load === "function" ) {
129
+ const glob = "/**/" + key ;
130
+ const reg = globToRegExp ( glob ) ;
131
+ const Loader = {
132
+ meta : { src, glob } ,
133
+ test : ( pathname : string ) => reg . test ( pathname ) ,
134
+ load : ( pathname : string , env : Record < string , unknown > ) => loader . load ( pathname , env ) ,
135
+ } ;
136
+ loaders . push ( Loader ) ;
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
142
+ return loaders ;
143
+ }
144
+
145
+ /** Load the JSX config base the given import maps and the existing deno config. */
104
146
export async function loadJSXConfig ( importMap : ImportMap ) : Promise < JSXConfig > {
105
147
const jsxConfig : JSXConfig = { } ;
106
148
const denoConfigFile = await findFile ( [ "deno.jsonc" , "deno.json" , "tsconfig.json" ] ) ;
@@ -181,6 +223,7 @@ export async function loadJSXConfig(importMap: ImportMap): Promise<JSXConfig> {
181
223
return jsxConfig ;
182
224
}
183
225
226
+ /** Load the import maps from the working directory. */
184
227
export async function loadImportMap ( ) : Promise < ImportMap > {
185
228
const importMap : ImportMap = { __filename : "" , imports : { } , scopes : { } } ;
186
229
@@ -219,37 +262,17 @@ export async function loadImportMap(): Promise<ImportMap> {
219
262
return importMap ;
220
263
}
221
264
222
- /** init loaders in `CLI` mode, or use prebuild loaders */
223
- export async function initModuleLoaders ( importMap : ImportMap ) : Promise < ModuleLoader [ ] > {
224
- const loaders : ModuleLoader [ ] = Reflect . get ( globalThis , "__ALEPH_MODULE_LOADERS" ) || [ ] ;
225
- if ( Deno . env . get ( "ALEPH_CLI" ) ) {
226
- for ( const key in importMap . imports ) {
227
- if ( / ^ \* \. { ? ( \w + , ? ) * \w + } ? $ / i. test ( key ) ) {
228
- let src = importMap . imports [ key ] ;
229
- if ( src . endsWith ( "!loader" ) ) {
230
- src = util . trimSuffix ( src , "!loader" ) ;
231
- if ( src . startsWith ( "./" ) || src . startsWith ( "../" ) ) {
232
- src = "file://" + join ( dirname ( importMap . __filename ) , src ) ;
233
- }
234
- let { default : loader } = await import ( src ) ;
235
- if ( typeof loader === "function" ) {
236
- loader = new loader ( ) ;
237
- }
238
- if ( loader !== null && typeof loader === "object" && typeof loader . load === "function" ) {
239
- const glob = "/**/" + key ;
240
- const reg = globToRegExp ( glob ) ;
241
- const Loader = {
242
- meta : { src, glob } ,
243
- test : ( pathname : string ) => reg . test ( pathname ) ,
244
- load : ( pathname : string , env : Record < string , unknown > ) => loader . load ( pathname , env ) ,
245
- } ;
246
- loaders . push ( Loader ) ;
247
- }
248
- }
249
- }
265
+ export function applyImportMap ( specifier : string , importMap : ImportMap ) : string {
266
+ if ( specifier in importMap . imports ) {
267
+ return importMap . imports [ specifier ] ;
268
+ }
269
+ for ( const key in importMap . imports ) {
270
+ if ( key . endsWith ( "/" ) && specifier . startsWith ( key ) ) {
271
+ return importMap . imports [ key ] + specifier . slice ( key . length ) ;
250
272
}
251
273
}
252
- return loaders ;
274
+ // todo: support scopes
275
+ return specifier ;
253
276
}
254
277
255
278
export async function parseJSONFile ( jsonFile : string ) : Promise < Record < string , unknown > > {
@@ -274,19 +297,6 @@ export async function parseImportMap(importMapFile: string): Promise<ImportMap>
274
297
return importMap ;
275
298
}
276
299
277
- export function applyImportMap ( specifier : string , importMap : ImportMap ) : string {
278
- if ( specifier in importMap . imports ) {
279
- return importMap . imports [ specifier ] ;
280
- }
281
- for ( const key in importMap . imports ) {
282
- if ( key . endsWith ( "/" ) && specifier . startsWith ( key ) ) {
283
- return importMap . imports [ key ] + specifier . slice ( key . length ) ;
284
- }
285
- }
286
- // todo: support scopes
287
- return specifier ;
288
- }
289
-
290
300
function toStringMap ( v : unknown ) : Record < string , string > {
291
301
const m : Record < string , string > = { } ;
292
302
if ( util . isPlainObject ( v ) ) {
0 commit comments