revelt
revelt

Reputation: 2410

esbuild not bundling internal file imports when the "bundle" is turned off

Consider this very contrived npm package:

import clone from "lodash.clonedeep";
import calculate from "./utils/calculate"; // ".ts" omitted

function x(obj:any):number {
  return calculate(clone(obj).a, clone(obj).b);
}
export { x }

Imagine we want an esm build of this.

In Rollup, "bundling" in this sense pertains only for external imports; internal-ones are all placed into one file by default.

Do you know how to set esbuild to include only internal file imports? To get:

import clone from "lodash.clonedeep";

function calculate(a, b) {
  return a * b;
}
function x(obj){
  return calculate(clone(obj).a, clone(obj).b);
}
export { x }

I'm simply trying to make some esm/cjs npm package builds.

Other questions in SO are different: this one is concatenating multiple files into one. Here we're just trying to replicate Rollup's behaviour, to include local imports but not external, like in a normal npm package. Thank you.

Upvotes: 0

Views: 5708

Answers (1)

revelt
revelt

Reputation: 2410

The answer is, same like in Rollup — enable "bundle", but skip bundling external dependencies by setting external. This way, local imports won't be bundled because they are not in package.json.

const path = require("path");
const pkg = require(path.resolve("./package.json"));

const external = [
  ...Object.keys(pkg.dependencies || {}),
  ...Object.keys(pkg.peerDependencies || {}),
];

// ESM
require('esbuild').buildSync({
  entryPoints: ['src/main.ts'],
  format: 'esm',
  bundle: true,
  minify: false,
  sourcemap: false,
  target: ['esnext'],
  outfile: 'dist/yourProgram.esm.js',
  external,
});

Upvotes: 3

Related Questions