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

feat: support alternative/private NPM registries #143

Merged
merged 1 commit into from
Oct 14, 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
32 changes: 32 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<stdout>");
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;
Expand Down
1 change: 1 addition & 0 deletions src/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface NpmPackage {
name: string;
version: string;
dependencies: string[];
registryUrl?: string;
}

export interface InfoOptions {
Expand Down
6 changes: 5 additions & 1 deletion src/loader_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions testdata/npmrc/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmmirror.com/
5 changes: 5 additions & 0 deletions testdata/npmrc/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"chalk": "npm:chalk@^5.3.0"
}
}
16 changes: 16 additions & 0 deletions testdata/npmrc/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions testdata/npmrc/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import chalk from "chalk";
export default chalk;