As title. This is the longer version:
My target is to setup the tree-shaking feature of Webpack (for es6-modules, or ESM,
.ejs
if you prefer).My confusion is among these configs: Webpack v5 with
babel-loader
(, which can be tuned fromwebpack.*.config.js
) + Babel v7 with preset@babel/preset-env
(, which can be tuned frombabel.config.js
) + TypeScript, which can be tuned fromtsconfig.json
. Details follow:First, from
tsconfig.json
, I noticed that I don't understand whether the"target": "es5"
should be changed to"target": "es6"
, or whether this may not matter at all. This is my reasoning:Even if the target set to "es6", the
babel-loader
will (probably) further transpile it to "es5" according to the"browserslist"
field I have setup in thepackage.json
. And for webpack tree-shaking to work, I guess that I should not transpile my code to "es5" too early.But this argument is based on the fact that
babel-loader
will only read"browserslist"
, and don't give it a _ totsconfig.json
. Please correct me if anything is wrong.Second, let's talk about babel. Inside the
babel.config.js
there is an optionmodules: false
, which is apparently needed to command Babel not to transpile the ESM import so that tree-shaking will work. So this provides more context for the first question above: If I setmodules: false
, then it should mean that the input ofbabel-loader
, i.e. the.js
from.tsx
according totsconfig.json
, should not transpile ESM import too. Please correct me if anything is wrong.Finally, about
package.json
andwebpack.*.config.js
. The optionsideEffects: false
is required for tree-shaking to work. But it seems that the this option can be added from bothpackage.json
(then should be the form"side-effects"
) andwebpack.*.config.js
'ssideEffects
field forbabel-loader
. I actually haven't tested which will work. Any advice on choosing the two options here?
There is one more context that I only use babel-loader
, no ts-loader
(actually I'm not sure about whether this is correct). This is based on another answer from this forum, and it is the only way that works for me when I was solving another problem. If ts-loader
is needed for this problem, please let me know, thank again.