bun
bun copied to clipboard
`bun build --external` option to treat all node_modules packages as external
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
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.
+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.
I suggest this syntax:
bun build ./src/foo.ts --external='*,!./*,!../*'
(external = all (*
) except local files (./*
and ../*
)
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()
this should be closed. finished by #12562
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
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
![]()
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.