openpgpjs / web-stream-tools

Convenience functions for reading, transforming and working with WhatWG Streams
MIT License
27 stars 15 forks source link

include transpiled ES5 code on npm? #12

Open tomholub opened 2 years ago

tomholub commented 2 years ago

When building/running our TypeScript project which is transpiles into ES5, the TypeScript compiler expects all dependencies to already be also ES5 compatible. And so it trips on import statements etc in this library.

Disclaimer - I don't know much about npm packaging.

Would it be possible to include ES5 build in the npm package? Ideally in a way where you can continue using ES6 for your toolchain that expects ES6, but others could import ES5 when required.

I did resolve our immediate issue by adding a build step that first transpiles the library from node_modules/@openpgp/web-stream-tools ES6 down to an ES5 file that it puts in a local directory, and then our code imports and uses that.

I don't know what the contentions are - I've never had to transpile a library to use it before, so I suppose it's common to include transpiled code. Would that be something that you could consider?

twiss commented 2 years ago

Hey :wave: Yeah, I agree this is something we should do.

The main difficulty is around the way we load the streams polyfill "on demand" (i.e. only when it's not natively supported by the browser): https://github.com/openpgpjs/web-stream-tools/blob/8663e1524421ad4745bc309cb08b47f4d895c9d5/lib/streams.js#L15-L18 which basically requires ES6, unless we add some workaround of inserting script tags or some such. However, the ES5 build could just unconditionally include the polyfill.

tomholub commented 2 years ago

Thanks for the consideration.

It may be cleaner to offer several builds, like lean and full rather than a script tag hack. Regardless, I suppose your own OpenPGP.js builds will continue using the source / remain unaffected.

It's the first time I see this kind of import. That's not understandable by the browser anyway, is it? Rather, meant for the build tool? But if it's for the build tool, then that's long before it sees any browser, so that wouldn't be on-demand. I'm thoroughly confused :-)

twiss commented 2 years ago

Yeah, we could do that indeed. import() is in fact a web standard, see dynamic imports. In the OpenPGP.js lightweight build, we preserve these imports as-is. In the full build, we include these dependencies indeed. Using webpack, it's possible to compile these dynamic imports to the script tag "hack", I believe.