-
Notifications
You must be signed in to change notification settings - Fork 170
/
module.ts
40 lines (34 loc) · 1.01 KB
/
module.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
import util from '../../shared/util.ts'
export const builtinModuleExts = ['tsx', 'jsx', 'ts', 'js', 'mjs']
export function importModule(basePath: string, url: string, forceRefetch = false): Promise<any> {
const { __ALEPH__: ALEPH } = window as any
if (ALEPH) {
return ALEPH.import(url, forceRefetch)
}
let src = util.cleanPath(basePath + '/_aleph/' + trimBuiltinModuleExts(url) + '.js')
if (forceRefetch) {
src += '?t=' + Date.now()
}
return import(src)
}
export function toPagePath(url: string): string {
let pathname = trimBuiltinModuleExts(url)
if (pathname.startsWith('/pages/')) {
pathname = util.trimPrefix(pathname, '/pages')
}
if (pathname.endsWith('/index')) {
pathname = util.trimSuffix(pathname, '/index')
}
if (pathname === '') {
pathname = '/'
}
return pathname
}
export function trimBuiltinModuleExts(url: string) {
for (const ext of builtinModuleExts) {
if (url.endsWith('.' + ext)) {
return url.slice(0, -(ext.length + 1))
}
}
return url
}