Replace direct node js types imports#319
Open
reaktivo wants to merge 1 commit into
Open
Conversation
Importing from `jsdom` and `canvas` at the top level of src/types/index.ts, src/core/QRCodeStyling.ts, and src/core/QRSVG.ts caused browser bundlers to pull in — or fail to resolve — those Node.js-only packages. Replace all three with minimal structural interfaces (BrowserWindow, NodeCanvasFactory, NodeCanvasElement, NodeCanvasImage, JSDOMConstructor) that describe only the surface the library actually uses, so the build is usable in a browser with no extraneous Node.js dependencies.
244c159 to
d608fbd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When attempting to integrate this library into a project I noticed I started receiving compilation errors due to dependencies in Node specific types, although my project is web only.
src/types/index.ts,src/core/QRCodeStyling.ts, andsrc/core/QRSVG.tsall contain top-level imports fromjsdomandcanvas, this prevents the library from being compiled correctly on projects withskipLibCheckset to true.These are Node.js-only packages. Their presence as unconditional top-level imports causes two problems for browser consumers:
jsdomandcanvasand either error out or produce warnings about missing Node built-ins..d.tsfiles reference the packages — anyone using the published type declarations needs@types/jsdomandcanvasinstalled, even for a browser-only project.Solution
Replace the three package imports with minimal structural (duck-type) interfaces defined inside the library itself:
DOMWindowfromjsdomBrowserWindow(document + XMLSerializer + Image + XMLHttpRequest + FileReader)typeof JSDOMfromjsdomJSDOMConstructortypeof nodeCanvasfromcanvasNodeCanvasFactoryCanvasfromcanvasNodeCanvasElementImagefromcanvasNodeCanvasImageThe interfaces capture only the surface the library actually uses, so:
new QRCodeStyling({ nodeCanvas, jsdom })) continues to work — the realcanvasandjsdomobjects satisfy the interfaces structurally.jsdomorcanvas..d.tsfiles no longer expose the external packages.Verification