As title. This is the longer version:
My target is to setup the tree-shaking feature of Webpack (for es6-modules, or ESM,
.ejsif 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-loaderwill (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-loaderwill 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.jsthere 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.jsfrom.tsxaccording totsconfig.json, should not transpile ESM import too. Please correct me if anything is wrong.Finally, about
package.jsonandwebpack.*.config.js. The optionsideEffects: falseis 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'ssideEffectsfield 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.