Webpack摇摆树木的设置在Webpack Babel-Loader,Typescript Tsconfig Target, @babel/preset-env模块中:false,package.json副作用:false?

发布于 2025-01-19 15:15:53 字数 1733 浏览 4 评论 0原文

作为标题。这是较长的版本:

  1. 我的目标是设置WebPack的树震动功能(对于ES6模块或ESM,.ejs(如果您喜欢)。

  2. 我的混乱是以下配置:webpack v5带有babel-loader(,可以从webpack。 @babel/preset-env(,可以从babel.config.js) + typescript进行调整,可以从tsconfig.json 。详细信息如下:

    • 首先,tsconfig.json,我注意到我不明白是否应该更改为“ target”:“ es5” >“ target”:“ es6”,或者是否根本不重要。这是我的理由:

      即使目标设置为“ ES6”,babel-loader可能会(可能)根据“ browserslist” field i(可能)将其进一步将其转移至“ ES5”。在package.json中具有设置。为了使Webpack震惊,我猜测我不应该太早将代码转换为“ es5”。

      但此参数基于以下事实:babel-loader只会读取“ browserslist”,并且不要给它_ to _ to tsconfig。 JSON。如果有什么错,请纠正我。

    • 第二,让我们谈谈巴别尔(Babel)。在babel.config.js有一个选项模块:false,显然需要命令babel不要将eSM转移到ESM导入中,以便摇摇欲坠。因此,这为上述第一个问题提供了更多上下文:如果我设置模块:false,则应该意味着babel-loader的输入,即。 JS来自.tsx根据tsconfig.json,也不应也不应将ESM导入。如果有什么错,请纠正我。

    • 最后,关于package.jsonwebpack。*。config.js。选项副作用:false才能使摇摇欲坠的工作。但是似乎可以从两个package.json中添加此选项(然后应为“ side-effects”)和webpack。*。配置。 。我实际上还没有测试哪个会起作用。关于在此处选择两个选项的任何建议?

还有一个上下文,我只使用babel-loader,no ts-loader(实际上我不确定这是否正确)。这基于另一个答案来自这个论坛,这是我解决另一个问题时唯一适合我的方法。如果此问题需要TS-Loader,请告诉我,再次感谢。

As title. This is the longer version:

  1. My target is to setup the tree-shaking feature of Webpack (for es6-modules, or ESM, .ejs if you prefer).

  2. My confusion is among these configs: Webpack v5 with babel-loader(, which can be tuned from webpack.*.config.js) + Babel v7 with preset @babel/preset-env(, which can be tuned from babel.config.js) + TypeScript, which can be tuned from tsconfig.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 the package.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 _ to tsconfig.json. Please correct me if anything is wrong.

    • Second, let's talk about babel. Inside the babel.config.js there is an option modules: 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 set modules: false, then it should mean that the input of babel-loader, i.e. the .js from .tsx according to tsconfig.json, should not transpile ESM import too. Please correct me if anything is wrong.

    • Finally, about package.json and webpack.*.config.js. The option sideEffects: false is required for tree-shaking to work. But it seems that the this option can be added from both package.json(then should be the form "side-effects") and webpack.*.config.js's sideEffects field for babel-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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

献世佛 2025-01-26 15:15:53

我的目标是设置震惊树的功能[...]

,默认情况下,它已经在生产模式中启用 。但是您确实需要保留ESM语法才能使其起作用。如果要在开发模式下启用它,请添加underexports:true

[...]打字稿,可以从 tsconfig.json

调谐

[...]或这是否根本不重要。

如果您不使用ts-loader之类的东西,那么答案是(绝对)是YES ,因为Babel不使用它来确定跨性别目标。

[...]应该意味着babel-loader的输入,即.js来自.tsx根据tsconfig.json >,不应也不应向ESM导入。

正确的。

[...]选项副作用:false才能工作。

不正确。

[...]在此处选择两个选项的任何建议?

选择软件包。您可能在Stackoverflow上阅读了太多的帖子,其中有人使用了虚构语法。

[...]我仅使用babel-loader,no ts-loader(实际上我不确定这是否正确)


你可以。而且在您的情况下,这是适当的,因为您的神neovim上有tsserver,它已经为您提供了跨文件类型检查。但是您仍然应该有一些时间尝试。他们似乎进行了许多优化。

My target is to setup the tree-shaking feature [...]

Upon webpack 5, it's enabled by default in production mode already. But you do need to keep ESM syntax to make it works. If you want to enable it in development mode, add usedExports: true.

[...] TypeScript, which can be tuned from tsconfig.json.

[...] or whether this may not matter at all.

If you don't use something like ts-loader then the answer is (an absolute) Yes, since babel doesn't use it to decide tranpilation target.

[...] should mean that the input of babel-loader, i.e. the .js from .tsx according to tsconfig.json, should not transpile ESM import too.

Correct.

[...] The option sideEffects: false is required for tree-shaking to work.

Incorrect.

[...] Any advice on choosing the two options here?

Pick package.json one. You probably read too many posts on StackOverflow where someone used an imaginary syntax.

[...] I only use babel-loader, no ts-loader (actually I'm not sure about whether this is correct)

You can. And it's appropriate in your case, since you have tsserver on your god-dame neovim, which already does the cross-file type-checking for you. But still you should find some time to try it. A lot of optimizations seem to be done by them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文