-
Notifications
You must be signed in to change notification settings - Fork 169
/
importor.ts
54 lines (46 loc) · 1.58 KB
/
importor.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
import React, { useEffect } from 'https://esm.sh/react'
import type { Config } from './types.ts'
import util, { reStyleModuleExt } from './util.ts'
const serverImports: Set<string> = new Set()
// reset imports and returns them
export function resetImports() {
const a = Array.from(serverImports)
serverImports.clear()
return a
}
interface ImportProps {
from: string
rawPath: string
resolveDir: string
}
export function Import({ from, rawPath, resolveDir }: ImportProps) {
if (reStyleModuleExt.test(rawPath)) {
return React.createElement(StyleLoader, { path: from, rawPath, resolveDir })
}
// todo: more loaders
return null
}
interface LoaderProps {
path: string
rawPath: string
resolveDir: string
}
export function StyleLoader({ path, rawPath, resolveDir }: LoaderProps) {
if (typeof Deno !== 'undefined') {
const { appDir, mode, config } = (window as any).ALEPH_ENV as { appDir: string, mode: string, config: Config }
serverImports.add(util.cleanPath(`${appDir}/.aleph/${mode}.${config.buildTarget}/${resolveDir}/${path}`))
}
useEffect(() => {
import(util.cleanPath(`/_aleph/${resolveDir}/${path}`))
return () => {
const moduleId = util.cleanPath(`./${resolveDir}/${rawPath}`)
const { document } = (window as any)
Array.from(document.head.children).forEach((el: any) => {
if (el.getAttribute('data-module-id') === moduleId) {
document.head.removeChild(el)
}
})
}
}, [])
return null
}