From 2ba3fb9bffb03eb07022f7b72c734b5077478072 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 27 Sep 2024 15:57:50 +0200 Subject: [PATCH] feat: support alternative/private NPM registries --- mod_test.ts | 32 ++++++++++++++++++++++++++++++++ src/deno.ts | 1 + src/loader_native.ts | 6 +++++- testdata/npmrc/.npmrc | 1 + testdata/npmrc/deno.json | 5 +++++ testdata/npmrc/deno.lock | 16 ++++++++++++++++ testdata/npmrc/main.ts | 2 ++ 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 testdata/npmrc/.npmrc create mode 100644 testdata/npmrc/deno.json create mode 100644 testdata/npmrc/deno.lock create mode 100644 testdata/npmrc/main.ts diff --git a/mod_test.ts b/mod_test.ts index 8353a68..82a37ba 100644 --- a/mod_test.ts +++ b/mod_test.ts @@ -462,6 +462,38 @@ Deno.test("npm specifiers global resolver - express", async (t) => { }); }); +Deno.test("npm specifiers global resolver with alternative registry", { + ignore: Deno.version.deno.startsWith("1."), +}, async (t) => { + await testLoader(t, ["native"], async (esbuild, loader) => { + if (esbuild === PLATFORMS.wasm) return; + const res = await esbuild.build({ + ...DEFAULT_OPTS, + plugins: [ + ...denoPlugins({ + loader, + configPath: Deno.realPathSync("./testdata/npmrc/deno.json"), + }), + ], + absWorkingDir: Deno.realPathSync("./testdata/npmrc"), + bundle: true, + entryPoints: ["./main.ts"], + platform: "node", + }); + assertEquals(res.warnings, []); + assertEquals(res.errors, []); + assertEquals(res.outputFiles.length, 1); + const output = res.outputFiles[0]; + assertEquals(output.path, ""); + assert(!output.text.includes(`npm:`)); + const { default: chalk } = await import( + `data:application/javascript;base64,${btoa(output.text)}` + ); + assertEquals(typeof chalk, "function"); + assertEquals(typeof chalk.red, "function"); + }); +}); + Deno.test("npm specifiers local resolver (manual) - preact", async (t) => { await testLoader(t, LOADERS, async (esbuild, loader) => { if (esbuild === PLATFORMS.wasm) return; diff --git a/src/deno.ts b/src/deno.ts index e1962b8..5839439 100644 --- a/src/deno.ts +++ b/src/deno.ts @@ -95,6 +95,7 @@ export interface NpmPackage { name: string; version: string; dependencies: string[]; + registryUrl?: string; } export interface InfoOptions { diff --git a/src/loader_native.ts b/src/loader_native.ts index aaa92a4..2b3d3f0 100644 --- a/src/loader_native.ts +++ b/src/loader_native.ts @@ -126,15 +126,19 @@ export class NativeLoader implements Loader { ROOT_INFO_OUTPUT = await ROOT_INFO_OUTPUT; } const { denoDir, npmCache } = ROOT_INFO_OUTPUT; + const registryUrl = npmPackage.registryUrl ?? "https://registry.npmjs.org"; + const registry = new URL(registryUrl); + const packageDir = join( npmCache, - "registry.npmjs.org", + registry.hostname, name, npmPackage.version, ); const linkDir = join( denoDir, "deno_esbuild", + registry.hostname, npmPackageId, "node_modules", name, diff --git a/testdata/npmrc/.npmrc b/testdata/npmrc/.npmrc new file mode 100644 index 0000000..918fa4a --- /dev/null +++ b/testdata/npmrc/.npmrc @@ -0,0 +1 @@ +registry=https://registry.npmmirror.com/ \ No newline at end of file diff --git a/testdata/npmrc/deno.json b/testdata/npmrc/deno.json new file mode 100644 index 0000000..e679bd1 --- /dev/null +++ b/testdata/npmrc/deno.json @@ -0,0 +1,5 @@ +{ + "imports": { + "chalk": "npm:chalk@^5.3.0" + } +} diff --git a/testdata/npmrc/deno.lock b/testdata/npmrc/deno.lock new file mode 100644 index 0000000..5251241 --- /dev/null +++ b/testdata/npmrc/deno.lock @@ -0,0 +1,16 @@ +{ + "version": "4", + "specifiers": { + "npm:chalk@^5.3.0": "5.3.0" + }, + "npm": { + "chalk@5.3.0": { + "integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==" + } + }, + "workspace": { + "dependencies": [ + "npm:chalk@^5.3.0" + ] + } +} diff --git a/testdata/npmrc/main.ts b/testdata/npmrc/main.ts new file mode 100644 index 0000000..0250d12 --- /dev/null +++ b/testdata/npmrc/main.ts @@ -0,0 +1,2 @@ +import chalk from "chalk"; +export default chalk;