Created
November 14, 2022 00:21
-
-
Save reggi/e535281ffbb73b286549216ff0316d42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {clientImport} from '../main.ts' | |
const e = await clientImport(import.meta.url, "../examples/web_components/content_warning.ts") | |
console.log(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as path from "https://deno.land/std@0.163.0/path/mod.ts"; | |
import { encode } from "https://deno.land/std@0.99.0/encoding/base64.ts"; | |
const genImport = (code: string) => new URL(`data:application/typescript;base64,${encode(code)}`) | |
const template = (asUsed: string, resolved: string) => ` | |
"${asUsed}": _clientImport( | |
"${resolved}", | |
() => import("${resolved}") | |
), | |
` | |
const wrapper = (inner: string) => ` | |
export function _clientImport <G>( | |
path: string, | |
code: () => Promise<G> | |
) { | |
return { path, code } | |
} | |
export const library = { | |
${inner} | |
} | |
` | |
const { library } = await import('../import_library.ts').catch(() => ({ library: undefined })) | |
export async function clientImport <K extends keyof NonNullable<typeof library>>(metaUrl: string, p: K): Promise<NonNullable<typeof library>[K]> { | |
const lib = library ? library : {} as NonNullable<typeof library>; | |
const pInternal: string = p as string | |
const resolve = (v: string) => { | |
return path.resolve(path.dirname(metaUrl.replace('file://', '')), v) | |
} | |
const resolveHere = (v: string) => { | |
return path.resolve(path.dirname(import.meta.url.replace('file://', '')), v) | |
} | |
if (p in lib) { | |
return lib[p] | |
} | |
await Deno.create(resolveHere('../import_library.json')); | |
const data = await Deno.readTextFile(resolveHere('../import_library.json')) | |
const value = data === '' ? {} : JSON.parse(data) | |
const importMap: { clientImport: {asUsed: string, resolved: string }[] } = { ...value, clientImport: []} | |
const { clientImport } = importMap | |
clientImport.push({asUsed: pInternal, resolved: resolve(pInternal)}) | |
const inner = clientImport.map(v => template(v.asUsed, v.resolved)).join('\n') | |
const content = wrapper(inner) | |
await Deno.writeTextFile(resolveHere('../import_library.json'), JSON.stringify({...importMap, clientImport}, null, 2)) | |
await Deno.writeTextFile(resolveHere('../import_library.ts'), content) | |
const contentUrl = genImport(content) | |
const file = await import(contentUrl.href) | |
const { library: library2 } = file | |
if (p in library2) { | |
return library2[p] | |
} | |
throw new Error(`can not find import ${pInternal}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment