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

Specifying peer dependency on NPM package does not work #433

Open
joeftiger opened this issue Oct 15, 2024 · 2 comments
Open

Specifying peer dependency on NPM package does not work #433

joeftiger opened this issue Oct 15, 2024 · 2 comments

Comments

@joeftiger
Copy link

Hello

I noticed that NPM imports inside deno.json cannot be specified as a peer dependency using @deno/dnt and always end up inside dependencies. When specifying the import using esm.sh/ it works, however.

Minimal example using Blockly as dependency:

deno.json:

{
  "exports": {
    ".": "./mod.ts"
  },
  "imports": {
    "@deno/dnt": "jsr:@deno/dnt@^0.41.3",
    "blockly": "npm:blockly@^11.1.1"
    // "blockly": "https://esm.sh/blockly@^11.1.1"
  }
}

mod.ts:

import "blockly";

build_npm.ts:

import { build, emptyDir } from "@deno/dnt";

await emptyDir("./npm");

await build({
  entryPoints: ["./mod.ts"],
  outDir: "./npm",
  shims: {
    deno: true,
  },
  typeCheck: "both",
  mappings: {
    "npm:blockly@^11.1.1": {
    // "https://esm.sh/blockly@^11.1.1": {
      name: "blockly",
      version: "^11.1.1",
      peerDependency: true,
    },
  },
  package: {
    name: "pure-deno",
    version: "0.0.1",
  },
  importMap: "./deno.json",
});

Replacing the import with the esm.sh/ version correctly transforms blockly as peer dependency while using the npm: version keeps it as direct dependency.

As a workaround one has to keep a separate import map or manually specify dependencies inside package in the build script.

@joeftiger
Copy link
Author

joeftiger commented Oct 15, 2024

If anyone is running into the same problem right now, my current workaround is the following code snippet to (dirtily) cut out peer dependencies:

const importMap = JSON.parse(Deno.readTextFileSync("./deno.json"));
const blocklyVersion = (<string>importMap["imports"]["blockly"]).split("@")[1];
delete importMap["imports"]["blockly"];
Deno.writeTextFileSync("./deno-npm.json", JSON.stringify(importMap, null, 2));

/// ...
await build({
  // ...
  package: {
    // ...
    peerDependencies: {
      blockly: blocklyVersion,
    },
  },
  importMap: "./deno-npm.json",
});

@harrysolovay
Copy link

harrysolovay commented Dec 16, 2024

I'm running into the same problem. For now solving it with a different approach. I overwrite the generated package.json with the unintentional dependencies as dev dependencies.

import type { SpecifierMappings } from "@deno/dnt/transform"

const mappings: SpecifierMappings = {
  "npm:@anthropic-ai/sdk@^0.32.1": {
    name: "@anthropic-ai",
    version: "^0.32.1",
    peerDependency: true,
  },
  "npm:openai@^4.76.0": {
    name: "openai",
    version: "^4.76.0",
    peerDependency: true,
  },
}

await build({
  // ...
  mappings,
})

const packageJsonPath = path.join(outDir, "package.json")
await Deno.readTextFile(packageJsonPath).then(async (v) => {
  const initial = JSON.parse(v)
  const { "@anthropic-ai/sdk": anthropic, openai } = initial.dependencies
  delete initial.dependencies
  initial.peerDependencies = { "@anthropic-ai/sdk": anthropic, openai }
  await Deno.writeTextFile(packageJsonPath, JSON.stringify(initial, null, 2))
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants