简体   繁体   English

角4个node_modules

[英]Angular 4 node_modules

I am learning Angular 4 and plan to build an App this weekend. 我正在学习Angular 4,并计划在本周末构建一个App。

node_modules is ginormous. node_modules是巨大的。 217 MB for a JavaScript Framework and over 700 subdirectories under node_modules! 217 MB(用于JavaScript框架)和node_modules下的700多个子目录!

Now, I did take a look at the HTML generated and it does not appear to reference node_modules . 现在,我确实查看了生成的HTML,它似乎没有引用node_modules

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MyApp</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
  <script type="text/javascript" src="inline.bundle.js"></script>
  <script type="text/javascript" src="polyfills.bundle.js"></script>
  <script type="text/javascript" src="styles.bundle.js"></script>
  <script type="text/javascript" src="vendor.bundle.js"></script>
  <script type="text/javascript" src="main.bundle.js"></script>
</body>
</html>

Question: can I not deploy node_modules? 问题:我不能部署node_modules吗? Will Angular 4 still function correctly? Angular 4仍然可以正常运行吗?

Angular2 uses Webpack when it's built. Angular2在构建时使用Webpack Additionally, webpack bundles all of the used files from node_modules folder into the vendor.bundle.js . 此外, webpack将node_modules文件夹中的所有已使用文件捆绑到vendor.bundle.js I believe the rest of the Javascript generated is put into the main.bundle.js . 我相信生成的Javascript的其余部分都放在main.bundle.js

The node_modules folder is really only intended to be a development environment and it's not wise to push that folder to production. 实际上, node_modules文件夹仅旨在用作开发环境,将该文件夹推入生产环境并不明智。

running command ng build --prod will convert all your components into a single js file and it generates a dist folder with index.html and all other configurations required to run in production environment. 运行命令ng build --prod会将所有组件转换为一个js文件,并生成一个带有index.html以及在生产环境中运行所需的所有其他配置的dist文件夹。 Just place that folder in your web host. 只需将该文件夹放在您的虚拟主机中即可。

所有你有节点模块以及您可能在将来,如果你安装后缀所有其他依赖--save他们将被添加到package.json文件和模块的依赖性层次也被锁定在package-lock.json您不必在存储库中存储所有巨大的节点模块,从而节省了空间,您可以上传dist文件,或者只需通过命令npm i将节点模块安装在服务器上

VERSION CONTROL 版本控制

You should include node_modules in your .gitignore file, so it gets ignored from being uploaded in your version control. 您应该在.gitignore文件中包括node_modules,以便将其从版本控件中上传时被忽略。

DEPLOYMENT 部署

As long as you have all the dependencies included in the package.json, you can add npm install as a part of the build script in package.json. 只要您在package.json中包含所有依赖项,就可以在package.json中将npm install添加为构建脚本的一部分。 It will install all necessary packages and package them in your builds automatically. 它将安装所有必需的软件包并将其自动打包到您的构建中。

"scripts": {
  "build": "npm install && ng build --prod --build-optimizer"
}

Node_Modules are never be included directly in your deployment ready code. Node_Modules永远不会直接包含在部署就绪代码中。 Webpack is a build tool used by default by Angular which bundles your source files and only the required node_module functions into main.bundle.js and vendor.bundle.js respectively. Webpack是Angular默认使用的构建工具,它将源文件和所需的node_module函数分别捆绑到main.bundle.js和vendor.bundle.js中。 Refer the following link if you want to know what each file consists of in the dist folder created 1 如果您想知道每个文件在dist文件夹中包含什么,请参考以下链接1

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

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