-
Notifications
You must be signed in to change notification settings - Fork 169
/
esbuild.ts
47 lines (44 loc) · 1.34 KB
/
esbuild.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
import { join } from 'https://deno.land/std@0.106.0/path/mod.ts'
import { cache } from '../server/cache.ts'
import util from '../shared/util.ts'
// @deno-types="https://deno.land/x/esbuild@v0.12.24/mod.d.ts"
import { build, stop, Plugin } from 'https://deno.land/x/esbuild@v0.12.24/mod.js'
export {
build as esbuild,
stop as stopEsbuild
}
export const esmLoader: Plugin = {
name: 'esm-loader',
setup(build) {
build.onResolve({ filter: /.*/ }, args => {
const isRemote = util.isLikelyHttpURL(args.path)
const path = isRemote ? args.path : util.trimPrefix(args.path, 'file://')
if (
args.kind === 'url-token' ||
(args.kind === 'import-rule' && (isRemote || path.startsWith('/')))
) {
return { path: path, external: true }
}
if (isRemote) {
return {
path,
namespace: 'http-module',
}
}
if (args.namespace === 'http-module') {
return {
path: (new URL(path, args.importer)).toString(),
namespace: 'http-module',
}
}
if (path.startsWith('.')) {
return { path: join(args.resolveDir, path) }
}
return { path }
})
build.onLoad({ filter: /.*/, namespace: 'http-module' }, async args => {
const { content } = await cache(args.path)
return { contents: content }
})
}
}