gildas-lormeau / zip.js

JavaScript library to zip and unzip files supporting multi-core compression, compression streams, zip64, split files and encryption.
https://gildas-lormeau.github.io/zip.js
BSD 3-Clause "New" or "Revised" License
3.38k stars 510 forks source link

Package.json has room for improvements #396

Closed Hexagon closed 1 year ago

Hexagon commented 1 year ago

I'm encountering errors while trying to use zip.js along with dnt (deno-to-node-transform). I'm pretty sure that the root cause is that zip.js has an incomplete package.json.

For a dual mode (esm+cjs) Node+Deno package, there is a couple of requirements. To support both the past and the future. I've found that this approach works best:

{
   "type": "module",
   "main": "./cjs/mod.cjs", // Here you've got the esm-script today? This is needed by Node for legacy reasons
   "module:" "./esm/mod.js", // This is missing -  This is also needed by Node for legacy reasons
   "types": "./types/mod.d.ts", // Node - legacy reasons
    "exports": {                        // This is the new subpath standard, which is missing too
      "./package.json": "./package.json", // This is nice to include too, required by some build tools
      ".": {
        "import": {
          "types": "./types/typings.d.ts",
          "default": "./esm/mod.js"
        },
        "require": {
          "types": "./types/mod.d.ts",
          "default": "./script/mod.js"
        }
      }
   }
}

See # 28 and # 136 here for some background -> https://github.com/Hexagon/croner/issues?q=is%3Aissue+package.json+is%3Aclosed

Can send you a PR on this if you're interested

gildas-lormeau commented 1 year ago

Thank you for the suggestions. I added the dual-mode support quite recently and I did not check if the package.json should have been impacted. Also, I did not originally plan for the library to be compatible with Node.js. I'm going to merge your PR.

gildas-lormeau commented 1 year ago

Your changes have been published on NPM in the version 2.6.63. Thank you :)

Hexagon commented 1 year ago

Cool, will try it out later today 👍

Hexagon commented 1 year ago

Oh yeah, works!

dnt is producing node-code where both the cjs and esm code relies on the npm package.

Used import {...} from "https://deno.land/x/zipjs@v2.6.63/index.js" in deps.ts

... and the following mapping in the dnt script

  mappings: {
    "https://deno.land/x/zipjs@v2.6.63/index.js": {
      name: "@zip.js/zip.js",
      version: "^2.6.63"
    },
  },

Like a glove

gildas-lormeau commented 1 year ago

Thank you for the feedback!