-
Notifications
You must be signed in to change notification settings - Fork 39
/
plugin_deno_loader.ts
367 lines (353 loc) · 12.6 KB
/
plugin_deno_loader.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import type * as esbuild from "./esbuild_types.ts";
import { dirname, join } from "@std/path";
import { NativeLoader } from "./loader_native.ts";
import { PortableLoader } from "./loader_portable.ts";
import { isInNodeModules } from "./shared.ts";
import {
esbuildResolutionToURL,
isNodeModulesResolution,
Loader,
readDenoConfig,
urlToEsbuildResolution,
} from "./shared.ts";
/** Options for the {@link denoLoaderPlugin}. */
export interface DenoLoaderPluginOptions {
/**
* Specify which loader to use. By default this will use the `native` loader,
* unless the `--allow-run` permission has not been given.
*
* See {@link denoLoaderPlugin} for more information on the different loaders.
*/
loader?: "native" | "portable";
/**
* Specify the path to a deno.json config file to use. This is equivalent to
* the `--config` flag to the Deno executable. This path must be absolute.
*
* NOTE: Import maps in the config file are not used to inform resolution, as
* this has already been done by the `denoResolverPlugin`. This option is only
* used when specifying `loader: "native"` to more efficiently load modules
* from the cache. When specifying `loader: "native"`, this option must be in
* sync with the `configPath` option for `denoResolverPlugin`.
*/
configPath?: string;
/**
* Specify a URL to an import map file to use when resolving import
* specifiers. This is equivalent to the `--import-map` flag to the Deno
* executable. This URL may be remote or a local file URL.
*
* If this option is not specified, the deno.json config file is consulted to
* determine what import map to use, if any.
*
* NOTE: Import maps in the config file are not used to inform resolution, as
* this has already been done by the `denoResolverPlugin`. This option is only
* used when specifying `loader: "native"` to more efficiently load modules
* from the cache. When specifying `loader: "native"`, this option must be in
* sync with the `importMapURL` option for `denoResolverPlugin`.
*/
importMapURL?: string;
/**
* Specify the path to a lock file to use. This is equivalent to the `--lock`
* flag to the Deno executable. This path must be absolute.
*
* If this option is not specified, the deno.json config file is consulted to
* determine what import map to use, if any.
*
* A lockfile must be present to resolve `jsr:` specifiers with the `portable`
* loader. When using the `native` loader, a lockfile is not required, but to
* ensure dependencies are de-duplicated correctly, it is recommended to use a
* lockfile.
*
* NOTE: when using `loader: "portable"`, integrity checks are not performed
* for ESM modules.
*/
lockPath?: string;
/**
* Specify whether to generate and use a local `node_modules` directory when
* using the `native` loader. This is equivalent to the `--node-modules-dir`
* flag to the Deno executable.
*
* This option is ignored when using the `portable` loader, as the portable
* loader always uses a local `node_modules` directory.
*/
nodeModulesDir?: boolean;
}
const LOADERS = ["native", "portable"] as const;
/** The default loader to use. */
export const DEFAULT_LOADER: "native" | "portable" =
await Deno.permissions.query({ name: "run" })
.then((res) => res.state !== "granted")
? "portable"
: "native";
const BUILTIN_NODE_MODULES = new Set([
"assert",
"assert/strict",
"async_hooks",
"buffer",
"child_process",
"cluster",
"console",
"constants",
"crypto",
"dgram",
"diagnostics_channel",
"dns",
"dns/promises",
"domain",
"events",
"fs",
"fs/promises",
"http",
"http2",
"https",
"module",
"net",
"os",
"path",
"path/posix",
"path/win32",
"perf_hooks",
"process",
"punycode",
"querystring",
"repl",
"readline",
"stream",
"stream/consumers",
"stream/promises",
"stream/web",
"string_decoder",
"sys",
"test",
"timers",
"timers/promises",
"tls",
"tty",
"url",
"util",
"util/types",
"v8",
"vm",
"worker_threads",
"zlib",
]);
/**
* The Deno loader plugin for esbuild. This plugin will load fully qualified
* `file`, `http`, `https`, and `data` URLs.
*
* **Note** that this plugin does not do relative->absolute specifier
* resolution, or import map resolution. You must use the `denoResolverPlugin`
* _before_ the `denoLoaderPlugin` to do that.
*
* This plugin can be backed by two different loaders, the `native` loader and
* the `portable` loader.
*
* ### Native Loader
*
* The native loader shells out to the Deno executable under the hood to load
* files. Requires `--allow-read` and `--allow-run`. In this mode the download
* cache is shared with the Deno executable. This mode respects deno.lock,
* DENO_DIR, DENO_AUTH_TOKENS, and all similar loading configuration. Files are
* cached on disk in the same Deno cache as the Deno executable, and will not be
* re-downloaded on subsequent builds.
*
* NPM specifiers can be used in the native loader without requiring a local
* `node_modules` directory. NPM packages are resolved, downloaded, cached, and
* loaded in the same way as the Deno executable does.
*
* JSR specifiers can be used without restrictions in the native loader. To
* ensure dependencies are de-duplicated correctly, it is recommended to use a
* lockfile.
*
* ### Portable Loader
*
* The portable loader does module downloading and caching with only Web APIs.
* Requires `--allow-read` and/or `--allow-net`. This mode does not respect
* deno.lock, DENO_DIR, DENO_AUTH_TOKENS, or any other loading configuration. It
* does not cache downloaded files. It will re-download files on every build.
*
* NPM specifiers can be used in the portable loader, but require a local
* `node_modules` directory. The `node_modules` directory must be created prior
* using Deno's `--node-modules-dir` flag.
*
* JSR specifiers require a lockfile to be present to resolve.
*/
export function denoLoaderPlugin(
options: DenoLoaderPluginOptions = {},
): esbuild.Plugin {
const loader = options.loader ?? DEFAULT_LOADER;
if (LOADERS.indexOf(loader) === -1) {
throw new Error(`Invalid loader: ${loader}`);
}
return {
name: "deno-loader",
setup(build) {
const cwd = build.initialOptions.absWorkingDir ?? Deno.cwd();
let nodeModulesDir: string | null = null;
if (options.nodeModulesDir) {
nodeModulesDir = join(cwd, "node_modules");
}
let loaderImpl: Loader;
const packageIdByNodeModules = new Map<string, string>();
build.onStart(async function onStart() {
packageIdByNodeModules.clear();
switch (loader) {
case "native":
loaderImpl = new NativeLoader({
infoOptions: {
cwd,
config: options.configPath,
importMap: options.importMapURL,
lock: options.lockPath,
nodeModulesDir: options.nodeModulesDir,
},
});
break;
case "portable": {
let lockPath: string | undefined = options.lockPath;
if (lockPath === undefined && options.configPath !== undefined) {
const config = await readDenoConfig(options.configPath);
if (typeof config.lock === "string") {
lockPath = join(dirname(options.configPath), config.lock);
} else if (config.lock !== false) {
lockPath = join(dirname(options.configPath), "deno.lock");
}
}
loaderImpl = new PortableLoader({
lock: lockPath,
});
}
}
});
async function onResolve(
args: esbuild.OnResolveArgs,
): Promise<esbuild.OnResolveResult | undefined> {
if (isNodeModulesResolution(args)) {
if (
BUILTIN_NODE_MODULES.has(args.path) ||
BUILTIN_NODE_MODULES.has("node:" + args.path)
) {
return {
path: args.path,
external: true,
};
}
if (nodeModulesDir) {
return undefined;
} else if (
loaderImpl.nodeModulesDirForPackage &&
loaderImpl.packageIdFromNameInPackage
) {
let parentPackageId: string | undefined;
let path = args.importer;
while (true) {
const packageId = packageIdByNodeModules.get(path);
if (packageId) {
parentPackageId = packageId;
break;
}
const pathBefore = path;
path = dirname(path);
if (path === pathBefore) break;
}
if (!parentPackageId) {
throw new Error(
`Could not find package ID for importer: ${args.importer}`,
);
}
if (args.path.startsWith(".")) {
return undefined;
} else {
let packageName: string;
let pathParts: string[];
if (args.path.startsWith("@")) {
const [scope, name, ...rest] = args.path.split("/");
packageName = `${scope}/${name}`;
pathParts = rest;
} else {
const [name, ...rest] = args.path.split("/");
packageName = name;
pathParts = rest;
}
const packageId = loaderImpl.packageIdFromNameInPackage(
packageName,
parentPackageId,
);
const id = packageId ?? parentPackageId;
const resolveDir = await loaderImpl.nodeModulesDirForPackage(id);
packageIdByNodeModules.set(resolveDir, id);
const path = [packageName, ...pathParts].join("/");
return await build.resolve(path, {
kind: args.kind,
resolveDir,
importer: args.importer,
});
}
} else {
throw new Error(
`To use "npm:" specifiers, you must specify "nodeModulesDir: true", or use "loader: native".`,
);
}
}
const specifier = esbuildResolutionToURL(args);
// Once we have an absolute path, let the loader resolver figure out
// what to do with it.
const res = await loaderImpl.resolve(specifier);
switch (res.kind) {
case "esm": {
const { specifier } = res;
return urlToEsbuildResolution(specifier);
}
case "npm": {
let resolveDir: string;
if (nodeModulesDir) {
resolveDir = nodeModulesDir;
} else if (loaderImpl.nodeModulesDirForPackage) {
resolveDir = await loaderImpl.nodeModulesDirForPackage(
res.packageId,
);
packageIdByNodeModules.set(resolveDir, res.packageId);
} else {
throw new Error(
`To use "npm:" specifiers, you must specify "nodeModulesDir: true", or use "loader: native".`,
);
}
const path = `${res.packageName}${res.path ?? ""}`;
return await build.resolve(path, {
kind: args.kind,
resolveDir,
importer: args.importer,
});
}
case "node": {
return {
path: res.path,
external: true,
};
}
}
}
build.onResolve({ filter: /.*/, namespace: "file" }, onResolve);
build.onResolve({ filter: /.*/, namespace: "http" }, onResolve);
build.onResolve({ filter: /.*/, namespace: "https" }, onResolve);
build.onResolve({ filter: /.*/, namespace: "data" }, onResolve);
build.onResolve({ filter: /.*/, namespace: "npm" }, onResolve);
build.onResolve({ filter: /.*/, namespace: "jsr" }, onResolve);
build.onResolve({ filter: /.*/, namespace: "node" }, onResolve);
function onLoad(
args: esbuild.OnLoadArgs,
): Promise<esbuild.OnLoadResult | null> | undefined {
if (args.namespace === "file" && isInNodeModules(args.path)) {
// inside node_modules, just let esbuild do it's thing
return undefined;
}
const specifier = esbuildResolutionToURL(args);
return loaderImpl.loadEsm(specifier);
}
// TODO(lucacasonato): once https://github.com/evanw/esbuild/pull/2968 is fixed, remove the catch all "file" handler
build.onLoad({ filter: /.*/, namespace: "file" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "http" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "https" }, onLoad);
build.onLoad({ filter: /.*/, namespace: "data" }, onLoad);
},
};
}