metafloor / bwip-js

Barcode Writer in Pure JavaScript
Other
2.11k stars 304 forks source link

How to import single function for Node / TypeScript #286

Open ammulder opened 1 year ago

ammulder commented 1 year ago

I'm using bwip-js 3.3.0 and @types/bwip-js 3.2.0 and generating code128 barcodes on Node 18 (AWS Lambda).

I see the documentation says:

The ESM also facilitates bundler tree-shaking by providing the individual encoders as named exports. Each exported encoder functions identically to bwipjs.toBuffer().

But I can't figure out how to get this sort of thing to work from TypeScript:

import { code128 } from 'bwip-js';
...
const buffer = await code128({...});

It doesn't seem to be a valid import. I tried marking up the TypeScript type definitions to declare code128 the same as toBuffer and at runtime got an error that code128 is not a function.

Is this meant to work? If so, any tips?

metafloor commented 1 year ago

This appears to be a typescript-specific issue, and I do not have experience with the typescript development environment. The ESM tree-shaking is part of this project's test framework, so I know it is working. The test cases uses webpack. Not sure what typescript uses but I think the actual problem is the typescript definition/type file, and I have zero understanding of how that works...

ammulder commented 1 year ago

OK, I'll take it up on DefinitelyTyped I guess, thanks.

metafloor commented 1 year ago

Have you found a solution to this issue?

ammulder commented 1 year ago

Sadly, I fell victim to laziness and just imported *

joewestcott commented 1 year ago

I think it's safe to assume this is a DefinitelyTyped issue, as it doesn't (yet) have the individual barcode symbologies configured as exports. See here

metafloor commented 1 year ago

I would like to include an updated/improved copy of the DefinitelyTyped schema as part of the bwip-js project but am unsure of the etiquette. For those who use typescript, it considered ok to copy the schema (maintaining the current attributions) and make the changes to the project's copy?

joewestcott commented 1 year ago

DefinitelyTyped is intended as a fallback for when the project doesn't export the type definitions itself. I'm sure every TS developer would prefer the types in the source repo as it would mean TS support without an additional install. Generally the DefinitelyTyped defs would be removed once this happens.

For those who use typescript, it considered ok to copy the schema (maintaining the current attributions) and make the changes to the project's copy?

Assuming the licences are compatible I'd say that's fine.

metafloor commented 1 year ago

Type files are now available in version 4. You can read about them here https://github.com/metafloor/bwip-js/discussions/299#discussioncomment-6631598.