简体   繁体   English

为什么我们需要在 webpack 中排除 node_modules?

[英]why we need to exclude node_modules in webpack?

below is a webpack config file:下面是 webpack 配置文件:

module.exports = {
    mode: "development",
    entry: "./src/index.ts",
    output: { filename: "bundle.js" },
    resolve: { extensions: [".ts"] },
    module: {
        rules: [
            { test: /\.ts/, use: "ts-loader", exclude: /node_modules/ }
           }
        ]
    }
};

I don't understand why we need to exclude node_modules when dealing with typescript files?我不明白为什么我们在处理 typescript 文件时需要排除 node_modules ? Below is my points:以下是我的观点:

1-Firstly, nearly all packages are written in js not in ts, it is not going to harm if we include node_modules. 1-首先,几乎所有的包都是用js而不是ts编写的,如果我们包含node_modules不会有什么坏处。

2-If we are referencing a package that is written in ts, we definitely want ts code to be compiled to js code, then we have to include node_modules to make sure everything works, don't we? 2-如果我们引用一个用 ts 编写的 package,我们肯定希望 ts 代码编译为 js 代码,那么我们必须包含 node_modules 以确保一切正常,不是吗?

1-Firstly, nearly all packages are written in js not in ts, it is not going to harm if we include node_modules. 1-首先,几乎所有的包都是用js而不是ts编写的,如果我们包含node_modules不会有什么坏处。

Excluding node_modules at the transpiling stage increases performance which could otherwise get a hit.在转译阶段排除node_modules会提高性能,否则可能会受到影响。

If we are referencing a package that is written in ts, we definitely want ts code to be compiled to js code, then we have to include node_modules to make sure everything works, don't we?如果我们引用的是用 ts 编写的 package,我们肯定希望 ts 代码编译为 js 代码,那么我们必须包含 node_modules 以确保一切正常,不是吗?

Yes, and then is the key here.是的,然后是这里的关键。 Excluding node_modules at the transpiling stage doesn't prevent webpack from using its content at the bundling stage.在转译阶段排除node_modules不会阻止 webpack 在捆绑阶段使用其内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM