Open baurine opened 2 years ago
This is the way the bundler currently works, so in that sense it's expected. Tree shaking happens after source traversal, and dynamic imports become additional entry points during source traversal. Perhaps dynamic entry points could be treated as a special tree-shakeable form of entry-like source file but this would involve overhauling the current linking algorithm.
Reproduce steps
Initial a react project by
npx esbuild-create-react-app my-app
, chooseTypeScript
template. esbuild-create-react-appInstall react-syntax-highlighter by
yarn add react-syntax-highlighter
Test it in src/App.tsx:
Build it by enabling splitting, esm format:
works fine:
But in the output dir, it generates more than 1 thousand unnecessary js files.
Demo code: https://github.com/miscs-test/esbuild-demo/commit/d0754086a3224e6d8c240dbe1fd432b4ab26a908
Workaround
Replace the following one line code:
The unnecessary js files are disappeared.
Demo code: https://github.com/miscs-test/esbuild-demo/commit/349d1d359e1e3aca0791ee7c10d0c15c20976836
Reason
node_modules/react-syntax-highlighter/dist/esm/index.js
:It exports both sync and async API, for example, Light and LightAsync. The LightAsync finally dynamic imports tons of js files that are related to the kinds of languages highlighting.
Although we only import the sync API,
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'
, esbuild compile the async API as well.Not sure whether this behavior is expected or not.