Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle builtin node deps for NPM packages #105

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,27 @@ Deno.test("npm specifiers global resolver - @oramacloud/client", async (t) => {
});
});

Deno.test("npm specifiers global resolver - typo-js", async (t) => {
await testLoader(t, ["native"], async (esbuild, loader) => {
if (esbuild === PLATFORMS.wasm) return;
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [...denoPlugins({ loader })],
bundle: true,
entryPoints: ["npm:typo-js"],
});
assertEquals(res.warnings, []);
assertEquals(res.errors, []);
assertEquals(res.outputFiles.length, 1);
const output = res.outputFiles[0];
assertEquals(output.path, "<stdout>");
assert(!output.text.includes(`npm:`));
const dataURL = `data:application/javascript;base64,${btoa(output.text)}`;
const { default: Typo } = await import(dataURL);
assertEquals(typeof Typo, "function");
});
});

Deno.test("npm specifiers local resolver - preact", async (t) => {
await testLoader(t, LOADERS, async (esbuild, loader) => {
if (esbuild === PLATFORMS.wasm) return;
Expand Down Expand Up @@ -504,7 +525,7 @@ Deno.test("npm specifiers local resolver - @preact/signals", async (t) => {
});
});

Deno.test("npm specifiers local resolver - @preact/signals", async (t) => {
Deno.test("npm specifiers local resolver - @oramacloud/client", async (t) => {
await testLoader(t, LOADERS, async (esbuild, loader) => {
if (esbuild === PLATFORMS.wasm) return;
const entryPoint =
Expand Down Expand Up @@ -535,6 +556,35 @@ Deno.test("npm specifiers local resolver - @preact/signals", async (t) => {
});
});

Deno.test("npm specifiers local resolver - typo-js", async (t) => {
await testLoader(t, LOADERS, async (esbuild, loader) => {
if (esbuild === PLATFORMS.wasm) return;
const tmp = Deno.makeTempDirSync();
if (loader === "portable") {
new Deno.Command(Deno.execPath(), {
args: ["cache", "--node-modules-dir", "npm:typo-js"],
cwd: tmp,
}).outputSync();
}
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [...denoPlugins({ loader, nodeModulesDir: true })],
bundle: true,
absWorkingDir: tmp,
entryPoints: ["npm:typo-js"],
});
assertEquals(res.warnings, []);
assertEquals(res.errors, []);
assertEquals(res.outputFiles.length, 1);
const output = res.outputFiles[0];
assertEquals(output.path, "<stdout>");
assert(output.text.includes(`require("fs")`));
const dataURL = `data:application/javascript;base64,${btoa(output.text)}`;
const { default: Typo } = await import(dataURL);
assertEquals(typeof Typo, "function");
});
});

Deno.test("remote http redirects are de-duped", async (t) => {
await testLoader(t, LOADERS, async (esbuild, loader) => {
const res = await esbuild.build({
Expand Down
62 changes: 62 additions & 0 deletions src/plugin_deno_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,59 @@ export const DEFAULT_LOADER: typeof LOADERS[number] =
? "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.
Expand Down Expand Up @@ -178,6 +231,15 @@ export function denoLoaderPlugin(
args: esbuild.OnResolveArgs,
): Promise<esbuild.OnResolveResult | null | undefined> {
if (args.namespace === "file" && args.pluginData === IN_NODE_MODULES) {
if (
BUILTIN_NODE_MODULES.has(args.path) ||
BUILTIN_NODE_MODULES.has("node:" + args.path)
) {
return {
path: args.path,
external: true,
};
}
if (nodeModulesDir) {
const result = await build.resolve(args.path, {
kind: args.kind,
Expand Down
Loading