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

Publish as ESM? #85

Open
achingbrain opened this issue Jan 16, 2024 · 1 comment
Open

Publish as ESM? #85

achingbrain opened this issue Jan 16, 2024 · 1 comment

Comments

@achingbrain
Copy link

This module is authored in ESM but then transpiles that to CJS, then finally uses rollup to create a big code blob.

Image from https://esbuild.github.io/analyze/ :

image

The tsc config could be updated to just publish ESM and rollup could be removed.

This would make it easier to do tree shaking to remove unused code from bundles.

The same change could be made to pvutils and pvtsutils.

@microshine I can open some PRs if you'd fine this useful?

@microshine
Copy link
Contributor

The situation is such that when publishing npm modules, I compile the code into two formats: CommonJS (cjs) and ECMAScript modules (esm). I utilize Rollup to generate bundled files as well as to create the index.d.ts file. In certain instances, modules (like @peculiar/x509) may include additional settings for compiling the final files for browser use. I am aware that the standard TypeScript compiler can be used to produce various module formats, but it generates JavaScript files with the same structure as the TypeScript files, resulting in a multitude of files with redundant imports. Conversely, when using Rollup, the compiled code appears much more succinct.

Regarding the asn1js module, I'm not confident that it can be made significantly more compact through tree shaking since all the structure classes are utilized in the fromBER method, which is used by applications. The only code that might not be used in the asn1js module is the compareSchema function. However, if an application is written without using this function, it would be excluded from the bundle file during the build process. Here is an example of a simple application. From the screenshot, it is apparent that the application file includes the asn1js module, but the compareSchema function is not present.

Example of the application:

import * as asn1js from "asn1js";

const asn = asn1js.fromBER(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]));
console.log(asn);
image

Additionally, I tried using the esbuild bundler, and it also successfully omitted the compareSchema function from the output file.

image

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