Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include transpiled ES5 code on npm? #12

Open
tomholub opened this issue Feb 13, 2022 · 3 comments
Open

include transpiled ES5 code on npm? #12

tomholub opened this issue Feb 13, 2022 · 3 comments

Comments

@tomholub
Copy link

tomholub commented Feb 13, 2022

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
Copy link
Member

twiss commented Feb 14, 2022

Hey 👋 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):

const [ponyfill, adapter] = await Promise.all([
import('web-streams-polyfill/ponyfill/es6'),
import('@mattiasbuelens/web-streams-adapter')
]);
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
Copy link
Author

tomholub commented Feb 16, 2022

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
Copy link
Member

twiss commented Feb 17, 2022

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.

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

No branches or pull requests

2 participants