简体   繁体   English

Webpack 是否包括所有依赖节点模块以及为生产创建的包?

[英]Does Webpack include all dependent node modules along with the created bundle for production?

We all know that when we hit the npm run build, the react scripts uses the Webpack and creates a bundle going through all the dependency graph (follow the imports) and creates a bundle.js and places it inside dist/build folder which is used for production.我们都知道,当我们点击 npm run build 时,react 脚本使用 Webpack 并创建一个 bundle 遍历所有依赖关系图(跟随导入)并创建一个 bundle.js 并将其放置在 dist/build 文件夹中,它被使用用于生产。 I recently read that even React is a devDependency (?) because once the bundle has been created there's no need of react (basically no need of node_modules) at production because the final bundle is a mix of js and css files which the browser can understand.我最近读到甚至React 也是一个 devDependency (?),因为一旦创建了包,就不需要在生产中做出反应(基本上不需要 node_modules),因为最终的包是 js 和 css 文件的混合,浏览器可以理解. Here are my questions.这是我的问题。

1) Please let me know if the above statement about React being a devDependency is correct or not? 1) 请让我知道上面关于 React 是一个 devDependency 的说法是否正确?

2) Since we have a lot of dependent node modules when we install a node package. Is Webpack going to bundle all these node modules as well into the final bundle.js? 2)由于我们在安装节点package时有很多依赖节点模块。Webpack是否会将所有这些节点模块也捆绑到最终的bundle.js中? If so, wouldn't it be a very large bundle?如果是这样,那不是一个非常大的包吗? How does this work?这是如何运作的?

Technically speaking, React is a dev dependency in a bundled app, because you don't need it in production unless you also do server side rendering.从技术上讲,React 是捆绑应用程序中的开发依赖项,因为除非您还进行服务器端渲染,否则在生产中不需要它。 Consider a case where you have a frontend app with an express server.考虑一种情况,您有一个带有快速服务器的前端应用程序。 You bundle the app using webpack and deploy it as static JS/CSS asset, but you express server no longer needs it as you are not doing SSR.您使用 webpack 捆绑应用程序并将其部署为 static JS/CSS 资产,但您表示服务器不再需要它,因为您没有进行 SSR。 So for deploying the server, you can prune dev dependencies, keep around only what's needed by the server and keep the server smaller.因此,为了部署服务器,您可以修剪开发依赖性,只保留服务器需要的东西,并使服务器更小。

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

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