Repository navigation
fix: add package.json exports map so Node, Vitest and SSR can import the package - #395
pcbeingused333 wants to merge 3 commits into
Conversation
…the package With "type": "module" and "main" pointing at a .js UMD bundle, resolvers evaluate the UMD file as ESM and it throws at import time. Add an exports map (import -> ES build, require -> new .cjs bundle) and a node:test regression test. Fixes yWorks#394
|
awesome! Also: can we also test proper typescript tsc --noemit? |
Cover Node16, NodeNext and Bundler resolution for the package exports. Refs yWorks#395.
|
Agreed that this metadata change should wait for a release beyond a bugfix release. Added No version bump is included; release scheduling remains with the maintainers. This comment was drafted with Claude Code. |
HackbrettXXX
left a comment
There was a problem hiding this comment.
Looks good! Can you add the new test:node script to the continuous-integration workflow?
Builds dist/ first because the Node consumer test imports the built package. Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
Fixes #394
Since 2.8.0
package.jsonsets"type": "module"while"main"still points to the UMD bundledist/svg2pdf.umd.min.jsand there is noexportsmap. Node, Vitest and SSR resolvers therefore evaluate the UMD file as an ES module, the UMD wrapper falls through to its browser-global branch and throwsTypeError: Cannot read properties of undefined (reading 'jsPDF')at import time. The named import{ svg2pdf }also fails because the UMD bundle has no ESM export.Add an
exportsmap:importresolves to the existing ES build,requireto a new CommonJS bundledist/svg2pdf.umd.min.cjs(a.cjsextension is required because"type": "module"makes.jsfiles ESM).mainnow points to the.cjsfile, and./dist/*stays exported so the existing UMD/script-tag paths (README, playground) keep working. The new bundle is produced by an extra output inrollup.config.mjs; the other dist files are byte-identical after rebuilding.Reproduction
In a project with
svg2pdf.jsandjspdfinstalled (as in the issue),node repro.mjswith:On current master this throws
TypeError: Cannot read properties of undefined (reading 'jsPDF')fromdist/svg2pdf.umd.min.js;import { svg2pdf } from 'svg2pdf.js'fails withSyntaxError: ... does not provide an export named 'svg2pdf'. With this change both work, andrequire('svg2pdf.js')loads the CJS build.Testing
test/node-entry/node-entry.test.mjs(npm run test:node, plainnode:test, afternpm run build) builds a throwaway consumer project (temp dir withnode_modules/svg2pdf.js-> this checkout andjspdf) and runsnodeon the three cases above (side-effect import, named import,require). 0/3 on master, 3/3 with the change.oxfmt --checkandoxlintare clean on the touched files. The browser vitest suite was not run (needs Playwright); it does not go through package resolution.dist/is not part of this diff (it only changes in release commits via theversionscript, which will adddist/svg2pdf.umd.min.cjsand its map);npm run buildgenerates the new files locally.AI assistance: this change and its regression test were prepared using Claude Code.