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

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

Open
beorn opened this issue Oct 7, 2023 · 4 comments
Open
Labels
bundler Something to do with the bundler enhancement New feature or request

Comments

@beorn
Copy link

beorn commented Oct 7, 2023

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: evanw/esbuild#619

What alternatives have you considered?

No response

@beorn beorn added the enhancement New feature or request label Oct 7, 2023
@Electroid Electroid added the bundler Something to do with the bundler label Oct 9, 2023
@Enalmada
Copy link

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.

@beorn beorn changed the title bun build --external option to treat all node_modules packages as extenral bun build --external option to treat all node_modules packages as external Oct 13, 2023
@tipiirai
Copy link

tipiirai commented Nov 3, 2023

+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
Copy link
Contributor

yukulele commented Feb 1, 2024

I suggest this syntax:

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

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

@garmoshka-mo
Copy link

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bundler Something to do with the bundler enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants