Skip to content

Add exports field to package.json - #397

Open
trackoor wants to merge 1 commit into
yWorks:masterfrom
trackoor:add-exports-field
Open

trackoor wants to merge 1 commit into
yWorks:masterfrom
trackoor:add-exports-field

Conversation

@trackoor

Copy link
Copy Markdown

Fixes #394.

Since the package became "type": "module", main pointed Node, Vitest and SSR resolvers at dist/svg2pdf.umd.min.js, which then ran as an ES module. The UMD wrapper falls through to the browser-global branch and throws, and there's no named svg2pdf export. This PR adds an exports map:

  • import resolves to dist/svg2pdf.es.min.js with types.d.ts
  • require resolves to dist/svg2pdf.umd.min.cjs, a new rollup output that is the same minified UMD build with a .cjs extension, with dist/types.d.cts
  • ./dist/* and ./package.json stay importable, so deep imports like svg2pdf.js/dist/svg2pdf.es.min.js keep working

main now points to the .cjs file. dist/types.d.cts is copied from types.d.ts during npm run build, so the two can't drift. It's needed because TypeScript treats types.d.ts as ESM in a "type": "module" package and rejects it for require under moduleResolution: node16 (TS1479). The existing dist files come out byte-identical from npm run build, and the new dist files are committed because publishing uses the checked-in dist.

The browser suite can't cover package resolution, so I tested an npm pack tarball installed into a scratch project with jspdf 4.2.1 on Node 22:

  • import 'svg2pdf.js', import { svg2pdf } from 'svg2pdf.js' and require('svg2pdf.js') all load, and jsPDF.API.svg is a function. With 2.9.0 from npm, the first and third throw the TypeError and the named import is a SyntaxError.
  • The deep dist/ import and svg2pdf.js/package.json still work.
  • A Vitest test and Vite ssrLoadModule that import svg2pdf.js work.
  • tsc passes with .mts and .cts consumers under node16 and nodenext, and with bundler. 2.9.0 fails node16 with TS1479.

npm run lint, npm run format:check and npm run typecheck pass. npm test gives the same result as on master locally: 92 passed and 12 failed, all in the text and diagram snapshot tests, which the README says can differ because of local text measurement.

I used an AI coding assistant to help write this; I reviewed the change and ran the tests.

Point import at the ES build and require at a .cjs copy of the minified UMD build, with matching .d.ts/.d.cts types, so Node, Vitest and SSR no longer load the UMD bundle as an ES module. Fixes yWorks#394.
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

Successfully merging this pull request may close these issues.

2.8.0+ throws on import/require under Node, Vitest, and SSR because "type": "module" runs UMD main as ESM

1 participant