-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno-cache.ts
72 lines (64 loc) · 2.68 KB
/
deno-cache.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Plugin, Loader } from 'https://deno.land/x/[email protected]/mod.js';
import * as Cache from 'https://deno.land/x/[email protected]/mod.ts'
import { join } from 'https://deno.land/[email protected]/path/mod.ts'
// Cloned from: https://raw.githubusercontent.com/dalcib/esbuild-plugin-cache/master/deno/mod.ts
// This allows my import-maps with aliases for relative things to work.
// like: "abc": "./src/abc/mod.ts"
export interface ImportMap {
readonly imports:{ [key:string]:string };
}
export interface Config {
readonly importMap:ImportMap;
readonly cacheDirectory?:string;
readonly importMapBaseDirectory?:string;
}
export function denoCache({
importMap = { imports: {} },
cacheDirectory,
importMapBaseDirectory = Deno.cwd()
}: Config): Plugin {
Cache.configure({ directory: cacheDirectory });
return {
name: 'deno-cache',
setup(build) {
build.onResolve({ filter: /.*/ }, async (args) => {
// Was having trouble with official importmap stuff. This works for me.
const mapTo = importMap.imports[args.path];
if (mapTo) {
if (mapTo.startsWith('http')) {
return {
path: mapTo,
namespace: 'deno-cache',
}
}
// This handles relative aliases in the import map
return { path: join(importMapBaseDirectory, mapTo )}
}
// NOT USING IMPORT MAP IF YOU GET HERE.
// Use the Cache thing to download all the stuff.
if (args.path.startsWith('http')) {
return {
path: args.path,
namespace: 'deno-cache',
}
}
if (args.namespace === 'deno-cache') {
return {
// import paths inside downloaded files
path: new URL(args.path, args.importer).toString(),
namespace: 'deno-cache',
}
}
// Normal file import stuff.
return { path: join(args.resolveDir, args.path) }
})
build.onLoad({ filter: /.*/, namespace: 'deno-cache' }, async (args) => {
const file = await Cache.cache(args.path, undefined, 'deps')
const contents = await Deno.readTextFile(file.path)
const ext = file.meta.url.split('.').pop() as Loader
const loader = ext.match(/"j|tsx?$/) ? ext : 'js'
return { contents, loader }
})
}
}
}