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

20 times faster installation times #669

Closed
jantimon opened this issue Sep 4, 2023 · 11 comments
Closed

20 times faster installation times #669

jantimon opened this issue Sep 4, 2023 · 11 comments

Comments

@jantimon
Copy link

jantimon commented Sep 4, 2023

last week I created an experiment for zx, which might be interesting for you

my experiment is an optimized node bundle of zx created using the Vercel ncc compiler, offering significant size reduction from 13mb (incl dependencies) to 450kb (with 0 dependencies).

du -sh node_modules
shot-6uNcUrYK
shot-qkpNxRzE

you can find it on GitHub here and on npm here

shot-zVD6hOxM

a lot of users of zx will probably prefer to use a non bundle package but still I would love to know what you think :)

@antonmedv
Copy link
Collaborator

Interesting idea. I will think about it.

@njfamirm
Copy link

njfamirm commented Sep 18, 2023

What exactly is done? @jantimon
Can you compare ncc with build system like esbuild?

@jantimon
Copy link
Author

jantimon commented Sep 18, 2023

@njfamirm from the ncc readme on github https://github.com/vercel/ncc

Simple CLI for compiling a Node.js module into a single file,
together with all its dependencies, gcc-style.

Motivation

  • Publish minimal packages to npm
  • Only ship relevant app code to serverless environments
  • Don't waste time configuring bundlers
  • Generally faster bootup time and less I/O overhead
  • Compiled language-like experience (e.g.: go)

Design goals

  • Zero configuration
  • TypeScript built-in
  • Only supports Node.js programs as input / output
  • Support all Node.js patterns and npm modules

In my experiment ncc was used as it bundles everything into a single js files and removes all unnecessary parts with treeshaking.

It also minifies the code:
https://unpkg.com/browse/[email protected]/dist/index.mjs

installation in 500ms

The very same would probably be possible with esbuild (see https://esbuild.github.io/getting-started/#bundling-for-node) but might require a little bit more configuration.

I am not sure what you mean with "What exactly is done?"

Currently the experiment supports the following cases:

npx zx-packed your script
npm i -g zx-packed
zx your-script.js

it also works with a shebang:

#!/usr/bin/env zx
// ...

it also works if you import it:

import "zx-packed";
// ...

typescript typings are also supported:

shows that typescript works properly

repl works too:

shows a very basic example with npx zx-packed --repl

@njfamirm
Copy link

Thank you for your complete explanation ❤️ @jantimon

In my opinion and according to the searches I did, esbuld is a better option than ncc and has extremely high speed!
Configuring it is not that much and I can do it.

@njfamirm
Copy link

The side question I have is where do you get such a screenshot ?

@antonmedv
Copy link
Collaborator

Another thing is zx/glovals endpoint.

@jantimon
Copy link
Author

jantimon commented Sep 18, 2023

Right now globals is not explicitly listed as export in the package.json of my experimental package.

However that could be easily changed and it is already possible with a less pretty import path:

shot-gyNyc4JV

@njfamirm screenshots were taken with https://cleanshot.com/

@antonmedv
Copy link
Collaborator

Looks like globals just import index:
image
Will be cool to have same logic as now.

@jantimon
Copy link
Author

I didn't fully understand index vs global - could you please explain once again the current logic?

@jantimon
Copy link
Author

jantimon commented Sep 19, 2023

@antonmedv - I just released a new version: 0.0.8

both ways are now fully supported - named and global usage:

demo of global and named imports

@antonmedv I switched to esbuild which solved a ncc issue but created other issues :)

Unfortunately it was necessary to patch zx/src/cli.js as esbuild does not allow top level awaits:

https://github.com/jantimon/zx-packed/blob/main/patches/zx%2B7.2.3.patch

A fresh version can be found here:

Using esbuild further reduced the file size from:

  • unpacked: 516kB to 448kB
  • compressed: 467 kB to 409 kB

@antongolub
Copy link
Collaborator

Seems covered by #722

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants