WebPack是否包含所有依赖节点模块以及用于生产的创建捆绑包?
我们都知道,当我们点击 npm run build 时,react 脚本使用 Webpack 并创建一个包,遍历所有依赖关系图(遵循导入)并创建一个 bundle.js 并将其放置在 dist/build 文件夹中用于生产。我最近读到,即使是 React 也是一个 devDependency (?),因为一旦创建了包,就不需要在生产时进行反应(基本上不需要 node_modules),因为最终的包是 js 和浏览器可以理解的 css 文件。这是我的问题。
1)请告诉我上面关于React作为devDependency的说法是否正确?
2)由于我们在安装节点包时有很多依赖的节点模块。 Webpack 是否会将所有这些节点模块也捆绑到最终的bundle.js 中?如果是这样,那不是一个很大的包吗?这是如何工作的?
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. 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. Here are my questions.
1) Please let me know if the above statement about React being a devDependency is correct or not?
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? If so, wouldn’t it be a very large bundle? How does this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从技术上讲,React 是捆绑应用程序中的开发依赖项,因为除非您还进行服务器端渲染,否则您在生产中不需要它。考虑这样一种情况,您有一个带有快速服务器的前端应用程序。您使用 webpack 捆绑应用程序并将其部署为静态 JS/CSS 资产,但您的 Express 服务器不再需要它,因为您没有进行 SSR。因此,在部署服务器时,您可以修剪开发依赖项,仅保留服务器所需的内容并保持服务器较小。
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. 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. So for deploying the server, you can prune dev dependencies, keep around only what's needed by the server and keep the server smaller.