-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.ts
48 lines (46 loc) · 1.62 KB
/
index.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
import { Plugin, Loader } from './node_modules/esbuild/lib/main'
import * as DenoCache from 'deno-cache'
import { readFile } from 'fs/promises'
import { resolve } from 'deno-importmap'
import { join } from 'path'
import { createRequire } from 'module'
const locaRequire = createRequire(import.meta.url)
interface Config {
importmap?: { imports: { [key: string]: string } }
directory?: string
}
export function cache({ importmap = { imports: {} }, directory }: Config): Plugin {
DenoCache.configure({ directory })
return {
name: 'deno-cache',
setup(build) {
build.onResolve({ filter: /.*/ }, async (args) => {
const resolvedPath = resolve(args.path, importmap)
if (resolvedPath.startsWith('http')) {
return {
path: resolvedPath,
namespace: 'deno-cache',
}
}
if (args.namespace === 'deno-cache') {
return {
path: new URL(resolvedPath, args.importer).toString(),
namespace: 'deno-cache',
}
}
if (resolvedPath !== args.path) {
return resolvedPath.match(/^[./]/)
? { path: join(args.resolveDir, resolvedPath) }
: { path: locaRequire.resolve(resolvedPath) }
}
})
build.onLoad({ filter: /.*/, namespace: 'deno-cache' }, async (args) => {
const file = await DenoCache.cache(args.path, undefined, 'deps')
const contents = await readFile(file.path, 'utf8')
const ext = file.meta.url.split('.').pop()
const loader = ext.match(/"j|tsx?$/) ? ext : ('js' as Loader)
return { contents, loader }
})
},
}
}