oven-sh / bun

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

`bun build --external` option to treat all node_modules packages as external #6351

Closed beorn closed 3 months ago

beorn commented 1 year ago

What is the problem this feature would solve?

When building node/electron packages in a monorepo it's often useful to treat anything in node_modules as external as you don't want to bundle your local dependencies. Currently you have to manually specify --external= for every package you want to make external, which is error-prone and tedious.

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

This is similar to this esbuild feature request: https://github.com/evanw/esbuild/issues/619

What alternatives have you considered?

No response

Enalmada commented 1 year ago

Until there is a way to treat all node_modules as external, I am using this build script which populates external from package.json:

import packageJson from './package.json';

function getExternalsFromPackageJson(): string[] {
  const sections: (keyof typeof packageJson)[] = [
    'dependencies',
    'devDependencies',
    'peerDependencies',
  ];
  const externals: string[] = [];

  for (const section of sections) {
    if (packageJson[section]) {
      externals.push(...Object.keys(packageJson[section]));
    }
  }

  // Removing potential duplicates between dev and peer
  return Array.from(new Set(externals));
}

async function buildWithExternals(): Promise<void> {
  const externalDeps = getExternalsFromPackageJson();

  await Bun.build({
    entrypoints: ['./src/index.ts'],
    outdir: './dist',
    target: 'node',
    external: externalDeps,
    root: './src',
  });
}

buildWithExternals();

Despite trying to be very careful with keeping my external in sync manually, when I ran this script for the first time on one project with dependencies that started getting long, I found I had missed one just reinforcing how error prone it is.

tipiirai commented 11 months ago

+1 for this feature. I often use Bun to minify individual files, so having an option like external: '*' or all_external: true would be awesome.

yukulele commented 8 months ago

I suggest this syntax:

bun build ./src/foo.ts --external='*,!./*,!../*'

(external = all (*) except local files (./* and ../*)

garmoshka-mo commented 8 months ago

here is fixed getExternalsFromPackageJson() function for js:

function getExternalsFromPackageJson() {
  const packageJson = JSON.parse(fs.readFileSync("./package.json"))

  const sections = [
    'dependencies',
    'devDependencies',
    'peerDependencies',
  ], externals = new Set()

  for (const section of sections)
    if (packageJson[section])
      Object.keys(packageJson[section]).forEach(_ => externals.add(_))

  console.log('externals', externals)

  return Array.from(externals)
}

then use in build config: external: getExternalsFromPackageJson()

huseeiin commented 3 months ago

this should be closed. finished by #12562

Jarred-Sumner commented 3 months ago

This is added in Bun v1.1.21, thanks to @zpix1

To mark all node_modules as external:

bun build --packages=external <script>

To get the release early:

bun upgrade --canary
Scalahansolo commented 3 months ago

CleanShot 2024-07-22 at 14 43 39@2x CleanShot 2024-07-22 at 14 43 43@2x

For a test repo I have set up, when I try to run bun build --packages=external src/main.ts I end up hitting the following error.

➜  api git:(main) ✗ bun build --packages=external src/main.ts
2 | import { AppModule } from './app.module';
                              ^
error: Could not resolve: "./app.module"
    at src/main.ts:2:27

Repo with the code. https://github.com/Scalahansolo/bun-nest

zpix1 commented 3 months ago

CleanShot 2024-07-22 at 14 43 39@2x CleanShot 2024-07-22 at 14 43 43@2x

For a test repo I have set up, when I try to run bun build --packages=external src/main.ts I end up hitting the following error.

➜  api git:(main) ✗ bun build --packages=external src/main.ts
2 | import { AppModule } from './app.module';
                              ^
error: Could not resolve: "./app.module"
    at src/main.ts:2:27

Repo with the code. https://github.com/Scalahansolo/bun-nest

Looks like src/main.ts is considered as external module, this is a bug. Try bun build --packages=external ./src/main.ts as a workaround.