我想为Angular创建一个简单的hello world应用。
当我按照官方快速入门中的说明安装时,在我的项目中创建了32,000个文件。
我认为这是一个错误或我错过了一些东西,所以我决定使用angular-cli,但在设置项目后,我数了41,000个文件。
我哪里错了?我是不是忽略了一些非常非常明显的东西?
我想为Angular创建一个简单的hello world应用。
当我按照官方快速入门中的说明安装时,在我的项目中创建了32,000个文件。
我认为这是一个错误或我错过了一些东西,所以我决定使用angular-cli,但在设置项目后,我数了41,000个文件。
我哪里错了?我是不是忽略了一些非常非常明显的东西?
当前回答
如果你使用的是angular cli的新版本,请使用ng build——prod
它将创建dist文件夹,其中有更少的文件和项目的速度将提高。
此外,为了在本地测试angular cli的最佳性能,你可以使用ng serve——prod
其他回答
没什么问题。这些都是您在package.json中提到的所有节点依赖项。
如果你已经下载了一些git hub项目,请小心,它可能有很多其他的依赖关系,而这些依赖关系实际上并不是angular 2 first hello world app所需要的:)
确保你有角度依赖 -rxjs 狼吞虎咽地吃 打印稿 -tslint 码头工人
这实际上并不是Angular特有的,几乎所有使用NodeJs / npm生态系统作为工具的项目都会发生这种情况。
这些项目在你的node_modules文件夹中,是你的直接依赖需要运行的传递依赖。
在节点生态系统中,模块通常很小,这意味着我们不需要自己开发东西,而是倾向于以模块的形式导入大部分我们需要的东西。这可以包括像著名的左垫函数这样的小东西,如果不是作为练习,为什么我们自己写呢?
所以有很多文件实际上是一件好事,这意味着一切都是模块化的,模块作者经常重用其他模块。这种模块化的便利性可能是节点生态系统发展如此迅速的主要原因之一。
原则上这应该不会引起任何问题,但似乎你遇到了谷歌应用程序引擎文件计数限制。在这种情况下,我建议不要上传node_modules到应用程序引擎。
相反,在本地构建应用程序,并上传到谷歌应用程序引擎只捆绑文件,但不构建在应用程序引擎本身。
您的开发配置没有问题。
如果你使用Angular CLI,你可以在创建项目时使用——minimal标志
ng new name --minimal
Creating a new project with angular cli recently and the node_modules folder was 270 mb, so yes this is normal but I'm sure most new devs to the angular world question this and is valid. For a simple new project it would make sense to pare the dependencies down maybe a bit;) Not knowing what all the packages depend on can be a bit unnerving especially to new devs trying the cli out for the first time. Add to the fact most basic tutorials don't discuss the deployment settings to get the exported files only needed. I don't believe even the tutorial offered on the angular official website talks about how to deploy the simple project.
正如一些人已经提到的:你的node_modules目录下的所有文件(包的NPM位置)都是你的项目依赖关系(所谓的直接依赖关系)的一部分。除此之外,你的依赖也可以有它们自己的依赖等等(所谓的传递依赖)。几万个文件没什么特别的。
因为你只允许上传10,000个文件(见评论),我会使用捆绑引擎。这个引擎将捆绑你所有的JavaScript, CSS, HTML等,并创建一个单一的捆绑包(或更多,如果你指定)。你的index.html会加载这个包,就这样。
我是webpack的粉丝,所以我的webpack解决方案将创建一个应用程序包和一个供应商包(完整的工作应用程序请参阅这里https://github.com/swaechter/project-collection/tree/master/web-angular2-example):
index . html
<!DOCTYPE html>
<html>
<head>
<base href="/">
<title>Webcms</title>
</head>
<body>
<webcms-application>Applikation wird geladen, bitte warten...</webcms-application>
<script type="text/javascript" src="vendor.bundle.js"></script>
<script type="text/javascript" src="main.bundle.js"></script>
</body>
</html>
webpack.config.js
var webpack = require("webpack");
var path = require('path');
var ProvidePlugin = require('webpack/lib/ProvidePlugin');
var CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
var UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
/*
* Configuration
*/
module.exports = {
devtool: 'source-map',
debug: true,
entry: {
'main': './app/main.ts'
},
// Bundle configuration
output: {
path: root('dist'),
filename: '[name].bundle.js',
sourceMapFilename: '[name].map',
chunkFilename: '[id].chunk.js'
},
// Include configuration
resolve: {
extensions: ['', '.ts', '.js', '.css', '.html']
},
// Module configuration
module: {
preLoaders: [
// Lint all TypeScript files
{test: /\.ts$/, loader: 'tslint-loader'}
],
loaders: [
// Include all TypeScript files
{test: /\.ts$/, loader: 'ts-loader'},
// Include all HTML files
{test: /\.html$/, loader: 'raw-loader'},
// Include all CSS files
{test: /\.css$/, loader: 'raw-loader'},
]
},
// Plugin configuration
plugins: [
// Bundle all third party libraries
new CommonsChunkPlugin({name: 'vendor', filename: 'vendor.bundle.js', minChunks: Infinity}),
// Uglify all bundles
new UglifyJsPlugin({compress: {warnings: false}}),
],
// Linter configuration
tslint: {
emitErrors: false,
failOnHint: false
}
};
// Helper functions
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
}
优点:
完整构建行(TS检测、编译、缩小等) 只有几个Http请求
缺点:
更长的构建时间 不是Http 2项目的最佳解决方案(见免责声明)
免责声明:这是Http 1的一个很好的解决方案。*,因为它最小化了每个Http请求的开销。你只对index.html和每个包有一个请求-但不是100 - 200个文件。目前,这是一条正确的道路。
另一方面,Http 2试图最小化Http开销,因此它基于流协议。该流能够双向通信(Client <——> Server),因此可以实现更智能的资源加载(只加载所需的文件)。流消除了大量的Http开销(更少的Http往返)。
但这和IPv6一样:人们真正使用Http 2还需要几年的时间