oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
72.82k stars 2.64k forks source link

Optional dev dependencies #8171

Open lgarron opened 7 months ago

lgarron commented 7 months ago

What is the problem this feature would solve?

Now that it's practical to use Playwright with bun (https://github.com/oven-sh/bun/issues/2492), it's possible to move fairly complicated web projects entirely to bun. This presents an opportunity to implement a feature that npm declined to implement: optional dev dependencies.

Dependencies like Playwright can be fairly large. They are valuable in CI or for thorough testing, but not needed for everyday development or a small PR. The same applies to special tools that are only run when deploying or publishing code.

I would love to avoid making everyone download these large dependencies and potentially save significant:

when these are not needed.

What is the feature you are proposing to solve the problem?

  1. Support an optionalDevDependencies field in package.json.
  2. In the bun install configuration, add an optionalDev field.
    • If optionalDev is not specified:
    • Fall back to true when optional and dev are both true.
    • Fall back to false if either optional or dev is false.

This allows projects to have a "quick start" dev process without any hacks, by using setup steps with different bun install config files:

# Get started quickly
git clone https://github.com/cubing/cubing.js && cd cubing.js
make quick-setup dev
make test-fast # Run a large range of quick tests

# Install all dev dependencies to run browser tests
make setup test-all
# Potential Makefile example

.PHONY: quick-setup:
quick-setup:
    bun install --config bunfig-quick.toml

.PHONY: setup:
setup:
    bun install # default config

What alternatives have you considered?

In theory it's possible to use a library for this, but that would require either:

  1. vendoring the library code (a maintenance and security liability), or
  2. installing the library (requires two separate install steps, likely to be slower and still kind of complicated).

By contrast, it would be really valuable as built-in functionality for the package manager so that projects can hit the ground running.

lgarron commented 7 months ago

This allows projects to have a "quick start" dev process without any hacks by using setup steps with different bun install config files:

In particular, cubing.js already does this by manually emulating optional dev dependency functionality.

If I set Network Link Conditioner to 20mbps, then I see a significant difference when installing all dev dependencies using bun install:

git clone https://github.com/cubing/cubing.js && cd cubing.js
make clean && rm -rf node_modules && bun pm cache rm
time make quick-setup test-fast # ≈ 12 seconds
git clone https://github.com/cubing/cubing.js && cd cubing.js
make clean && rm -rf node_modules && bun pm cache rm
time make setup test-fast # ≈ 120 seconds

This shows that someone can get started in the project about 10× as fast using optional dev dependencies if they have no dependencies cached. 😱 With a slower connection (say, 3G connection speed, or slow shared Wi-Fi), this can make a huge difference — even if some dependencies are already cached, it could be a difference of many minutes.

bun already makes the local operations fast [^1], but this would help make the overall experience of project development faster starting from right after checkout.

[^1]: And I must commend bun install on being wicked fast when the dependencies are already cached.

disog commented 6 months ago

The workaround for this would be a git branch, but it'd be very cumbersome to maintain.