how to remove duplicate codes when bundling with esbuild

140 Views Asked by At

I'm currently working on a backend project and I'm using nodejs and express. I also use esbuild to bundle my code. here's my build script :

"build": "esbuild src/server.ts --bundle --platform=node --outfile=dist/bundle.js --format=esm --packages=external",

the problem is that in the bundle file i see things like :

import express3 from "express";
import express from "express";
import express2 from "express";

this is really strange to me because I expected to see only one express import in the whole bundle file but it turns out there's one for each usage in different files. for example in my server.ts:

import express from "express";
const app = express();

or in my routers :

import express from "express";
const router = express.Router();

is there a problem with the build script or it's esbuild normal behavior ? because I think removing duplicate codes is one of the main thing a module bundler is supposed to do.

0

There are 0 best solutions below