forked from lucacasonato/esbuild_deno_loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
192 lines (167 loc) · 6.19 KB
/
mod.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {
esbuild,
fromFileUrl,
ImportMap,
resolveImportMap,
resolveModuleSpecifier,
toFileUrl,
} from "./deps.js";
import { load as nativeLoad } from "./src/native_loader.js";
import { load as portableLoad } from "./src/portable_loader.js";
import { ModuleEntry } from "./src/deno.js";
import { getNodeModulesPath } from './src/node.js';
import { existsSync } from 'fs';
import { readFile, realpath } from 'fs/promises';
import { transformExtern, DeepkitPluginOptions } from '@gjsify/esbuild-plugin-deepkit';
export interface DenoPluginOptions {
/**
* Specify the URL to an import map to use when resolving import specifiers.
* The URL must be fetchable with `fetch`.
*/
importMapURL?: URL;
/**
* Specify which loader to use. By default this will use the `native` loader,
* unless the `--allow-run` permission has not been given.
*
* - `native`: Shells out to the Deno execuatble under the hood to load
* files. Requires --allow-read and --allow-run.
* - `portable`: Do module downloading and caching with only Web APIs.
* Requires --allow-read and/or --allow-net.
*/
loader?: "native" | "portable";
}
/** The default loader to use. */
export const DEFAULT_LOADER: "native" | "portable" = "portable";
export function denoPlugin(options: DenoPluginOptions & DeepkitPluginOptions = {}): esbuild.Plugin {
const loader = options.loader ?? DEFAULT_LOADER;
return {
name: "deno",
setup(build) {
const infoCache = new Map<string, ModuleEntry>();
let importMap: ImportMap | null = null;
build.onStart(async function onStart() {
if (options.importMapURL !== undefined) {
let txt: string;
if(options.importMapURL.href.startsWith('file://')) {
const url = new URL(options.importMapURL.href);
txt = await readFile(url.pathname, { encoding: 'utf-8'});
} else {
const resp = await fetch(options.importMapURL.href);
txt = await resp.text();
}
importMap = resolveImportMap(JSON.parse(txt), options.importMapURL);
} else {
importMap = null;
}
});
build.onResolve({ filter: /.*/ }, async function onResolve(
args: esbuild.OnResolveArgs,
): Promise<esbuild.OnResolveResult | null | undefined> {
// If this is a node module
if(args.kind === 'import-statement' || args.kind === 'require-call') {
const nodeModulePath = await getNodeModulesPath(args.path);
if(nodeModulePath) {
return null
}
}
if(args.path.endsWith('/')) {
return null;
}
if (args.path.startsWith('gi://')) {
return {
path: args.path,
external: true,
}
}
if (args.path.startsWith('ext:')) {
const path = args.path;
let importModule: string;
if (path.startsWith('ext:deno_node/')) {
importModule = path.replace(/^ext:deno_node\//, '@gjsify/deno-runtime-2/ext/node/polyfills/');
}else if (path.startsWith('ext:deno_')) {
importModule = path.replace(/^ext:deno_/, '@gjsify/deno-runtime-2/ext/');
}else if (path.startsWith('ext:runtime/')) {
importModule = path.replace(/^ext:runtime\//, '@gjsify/deno-runtime-2/runtime/js/');
} else if (path.startsWith('ext:core/')) {
importModule = path.replace(/^ext:core\//, '@gjsify/deno-core/');
} else {
throw new Error(`Unknown ext: module ${path}`);
}
// .ts -> .js
importModule = importModule.replace(/\.ts$/, '.js');
try {
const resolvedModule = (await build.resolve(importModule, {
importer: args.importer,
kind: args.kind,
namespace: args.namespace,
resolveDir: args.resolveDir,
pluginData: args.pluginData,
}));
if (resolvedModule.errors.length > 0) {
console.error(resolvedModule.errors);
}
return resolvedModule;
} catch (error) {
console.error(error);
throw error;
}
// console.debug('ext:', args, resolvedModule);
}
const resolveDir = args.resolveDir
? `${toFileUrl(args.resolveDir).href}/`
: "";
const referrer = args.importer
? `${args.namespace}:${args.importer}`
: resolveDir;
let resolved: URL;
if (importMap !== null) {
const res = resolveModuleSpecifier(
args.path,
importMap,
new URL(referrer) || undefined,
);
resolved = new URL(res);
} else {
resolved = new URL(args.path, referrer);
}
const protocol = resolved.protocol;
if (protocol === "file:") {
let path = fromFileUrl(resolved);
if(existsSync(path)) {
path = await realpath(path);
return { path, namespace: "file" };
} else {
return null;
}
}
const path = resolved.href.slice(protocol.length);
return { path, namespace: protocol.slice(0, -1) };
});
async function onLoad(
args: esbuild.OnLoadArgs,
): Promise<esbuild.OnLoadResult | null> {
let url;
if (args.namespace === "file") {
url = toFileUrl(args.path);
} else {
url = new URL(`${args.namespace}:${args.path}`);
}
let result: esbuild.OnLoadResult | null = null
switch (loader) {
case "native":
result = await nativeLoad(infoCache, url, options);
case "portable":
result = await portableLoad(url, options);
}
if(result?.contents && options.reflection) {
return transformExtern(options, args, result);
}
return result;
}
build.onLoad({ filter: /.*\.json/, namespace: "file" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "http" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "https" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "data" }, onLoad);
},
};
}