Skip to content

Commit 7571704

Browse files
authored
feat: support alternative/private NPM registries (#143)
1 parent 06afe4a commit 7571704

File tree

7 files changed

+62
-1
lines changed

7 files changed

+62
-1
lines changed

mod_test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,38 @@ Deno.test("npm specifiers global resolver - express", async (t) => {
462462
});
463463
});
464464

465+
Deno.test("npm specifiers global resolver with alternative registry", {
466+
ignore: Deno.version.deno.startsWith("1."),
467+
}, async (t) => {
468+
await testLoader(t, ["native"], async (esbuild, loader) => {
469+
if (esbuild === PLATFORMS.wasm) return;
470+
const res = await esbuild.build({
471+
...DEFAULT_OPTS,
472+
plugins: [
473+
...denoPlugins({
474+
loader,
475+
configPath: Deno.realPathSync("./testdata/npmrc/deno.json"),
476+
}),
477+
],
478+
absWorkingDir: Deno.realPathSync("./testdata/npmrc"),
479+
bundle: true,
480+
entryPoints: ["./main.ts"],
481+
platform: "node",
482+
});
483+
assertEquals(res.warnings, []);
484+
assertEquals(res.errors, []);
485+
assertEquals(res.outputFiles.length, 1);
486+
const output = res.outputFiles[0];
487+
assertEquals(output.path, "<stdout>");
488+
assert(!output.text.includes(`npm:`));
489+
const { default: chalk } = await import(
490+
`data:application/javascript;base64,${btoa(output.text)}`
491+
);
492+
assertEquals(typeof chalk, "function");
493+
assertEquals(typeof chalk.red, "function");
494+
});
495+
});
496+
465497
Deno.test("npm specifiers local resolver (manual) - preact", async (t) => {
466498
await testLoader(t, LOADERS, async (esbuild, loader) => {
467499
if (esbuild === PLATFORMS.wasm) return;

src/deno.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export interface NpmPackage {
9595
name: string;
9696
version: string;
9797
dependencies: string[];
98+
registryUrl?: string;
9899
}
99100

100101
export interface InfoOptions {

src/loader_native.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,19 @@ export class NativeLoader implements Loader {
140140
ROOT_INFO_OUTPUT = await ROOT_INFO_OUTPUT;
141141
}
142142
const { denoDir, npmCache } = ROOT_INFO_OUTPUT;
143+
const registryUrl = npmPackage.registryUrl ?? "https://registry.npmjs.org";
144+
const registry = new URL(registryUrl);
145+
143146
const packageDir = join(
144147
npmCache,
145-
"registry.npmjs.org",
148+
registry.hostname,
146149
name,
147150
npmPackage.version,
148151
);
149152
const linkDir = join(
150153
denoDir,
151154
"deno_esbuild",
155+
registry.hostname,
152156
npmPackageId,
153157
"node_modules",
154158
name,

testdata/npmrc/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmmirror.com/

testdata/npmrc/deno.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"imports": {
3+
"chalk": "npm:chalk@^5.3.0"
4+
}
5+
}

testdata/npmrc/deno.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/npmrc/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import chalk from "chalk";
2+
export default chalk;

0 commit comments

Comments
 (0)