linkedom icon indicating copy to clipboard operation
linkedom copied to clipboard

List optional dependencies in `peerDependencies` + `peerDependenciesMeta`

Open moshest opened this issue 2 years ago • 3 comments

For example in package.json:

{
  "name": "linkedom",
  "version": "0.16.1",
  "peerDependencies": {
    "canvas": ">= 2",
  },
  "peerDependenciesMeta": {
    "canvas": {
      "optional": true
    }
  }
}

Please see docs here: https://docs.npmjs.com/cli/v7/configuring-npm/package-json#peerdependenciesmeta

moshest avatar Nov 15 '23 08:11 moshest

this "smells like a PR" 😄

WebReflection avatar Nov 15 '23 09:11 WebReflection

When is a fix for the warnings going to be released? I'm on latest 0.18.5

WARNING in ./node_modules/linkedom/commonjs/perf_hooks.cjs 3:24-45 Module not found: Error: Can't resolve 'perf_hooks' in 'C:\Dev\TestProject\node_modules\linkedom\commonjs' @ ./node_modules/linkedom/cjs/interface/document.js 2:22-62 @ ./node_modules/linkedom/cjs/index.js 3:30-64

jlara005 avatar Sep 26 '24 18:09 jlara005

When is a fix for the warnings going to be released?

When the PR lands

WebReflection avatar Sep 27 '24 08:09 WebReflection

For everyone who encounters this issue with vite, use pnpm patch to modify commonjs/perf_hooks.cjs to stop importing performance from perf_hooks, and instead directly use globalThis.performance, which has been working normally since nodejs 16.

diff --git a/commonjs/perf_hooks.cjs b/commonjs/perf_hooks.cjs
index d574aefcd9422511b31930c0b567562728534ca6..9749ac1c0ca9b9d6a5c8c960c009646d07fb354b 100644
--- a/commonjs/perf_hooks.cjs
+++ b/commonjs/perf_hooks.cjs
@@ -1,9 +1,3 @@
 /* c8 ignore start */
-try {
-  const {performance} = require('perf_hooks');
-  exports.performance = performance;
-}
-catch (fallback) {
-  exports.performance = {now() { return +new Date; }};
-}
+exports.performance = globalThis.performance ?? { now() { return Date.now(); } };
 /* c8 ignore stop */

@WebReflection If you're willing to accept it, I can create a small PR for this.

rxliuli avatar Aug 21 '25 03:08 rxliuli

@rxliuli thanks for the hint ... it looks like that file could just go away entirely at this point as NodeJS 16 is long-time-ago LTS ... and if that's the case, I wonder if the other PR would still be needed or not 🤔

WebReflection avatar Aug 21 '25 07:08 WebReflection

@rxliuli thanks for the hint ... it looks like that file could just go away entirely at this point as NodeJS 16 is long-time-ago LTS ... and if that's the case, I wonder if the other PR would still be needed or not 🤔

I haven't encountered any peerDependencies related issues, just a warning when building with vite, which was resolved by no longer importing perf_hooks. https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility

I was responding to @jlara005's question, and I'm not sure what that PR is doing.

rxliuli avatar Aug 21 '25 08:08 rxliuli

@rxliuli fair enough ... PR to remove performance welcomed but I might do it myself soon as it's always a pleasure to drop legacy try catches and trust modern environments instead ... although your quick and easy shim around it might still be needed to avoid breaking very old projects that carelessly use latest linkedom whatever happens.

WebReflection avatar Aug 21 '25 08:08 WebReflection

@WebReflection I created a small PR to fix this issue. If you're willing to merge and release a minor version, I can remove my local patch. https://github.com/WebReflection/linkedom/pull/311

rxliuli avatar Aug 21 '25 08:08 rxliuli

@WebReflection I just want to let you know that the new version requires explicitly pnpm installing the canvas package, otherwise it will throw an error, while previous versions didn't have this error.

Uncaught Error: Could not resolve "canvas" imported by "linkedom". Is it installed?

rxliuli avatar Aug 21 '25 11:08 rxliuli

@rxliuli does npm or bun complain? I can't write apparently a package for all runtimes ... but if peerDependency is the way to go, I'd rather ask you to file an issue to pnpm instead ...

WebReflection avatar Aug 21 '25 12:08 WebReflection