简体   繁体   English

如何在一个js文件中组装angularjs项目

[英]How to assemble angularjs project in one js file

I have a huge angularjs app with many .js files. 我有一个巨大的angularjs应用程序与许多.js文件。

As far I know browser should open a new connections to reach every single file. 据我所知,浏览器应该打开一个新的连接来访问每个文件。 File size doesn't matter for performance but opening new connections do. 文件大小与性能无关,但打开新连接可以。

So that's why I'm looking for a some js-assembler to make single .js file. 所以这就是为什么我正在寻找一些js-assembler来制作单个.js文件。

I am not sure I should do this in case of angular app? 我不确定在角度应用的情况下我应该这样做吗? Can anyone advice me which js assembler I should choose? 任何人都可以建议我应该选择哪个js汇编程序?

Take a look at grunt: http://www.gruntjs.com/ 看看咕噜声: http//www.gruntjs.com/

This is a tool that can automatize this kind of process, among others (like minifying your files before merging them) really useful for javascript apps development. 这是一个可以自动化这种过程的工具,比如在合并它们之前缩小文件,这对于javascript应用程序开发非常有用。

I think what you really need is a tool doing concatenate and minify or compress JavaScript and CSS assets . 我认为你真正需要的是一个连接和缩小或压缩JavaScript和CSS资产的工具 Just like asset-pipeline in the Rails world. 就像Rails世界中的资产管道一样。

As you mentioned you have two projects one's backend is Nodejs, the other is Java, so I will address this question into this two aspects: 如你所说,你有两个项目,一个是后端是Nodejs,另一个是Java,所以我将把这个问题解决到这两个方面:

  1. Nodejs side Nodejs方面

    Just like Antoine said, you can use some building tools like gruntjs( http://www.gruntjs.com/ ) to write a simple task to concatenate all your js files. 就像Antoine所说,你可以使用像gruntjs( http://www.gruntjs.com/ )这样的构建工具编写一个简单的任务来连接所有的js文件。 Or you can serve the concatenated file from backbend using tools like connect-assets (if you happen use connect web framework), more details please refer this question: Concat and minify JS files in Node . 或者你可以使用connect-assets等工具从后端提供连接文件(如果你碰巧使用连接web框架),更多细节请参考这个问题: Concat并在Node中缩小JS文件

  2. Java side Java方面

    wro4j is pretty handy, which provide pretty much everything you need to deal with assets in Java project. wro4j非常方便,它提供了处理Java项目中资产所需的几乎所有内容。 You can check out the details usage in their github page. 您可以在他们的github页面中查看详细信息用法。 Other than this, Google Closure Compiler is also a good choice. 除此之外, Google Closure Compiler也是一个不错的选择。

You can just use whatever tools you like to concatenate/minify/uglify multiple javascript files, but some tools might provide easy integration or other features(like server dynamically in server side). 您可以使用您喜欢的任何工具来连接/缩小/丑化多个javascript文件,但某些工具可能提供简单的集成或其他功能(如服务器端的动态服务器)。 So please check out each tools and decide which one you want to use. 因此,请查看每个工具并确定要使用哪个工具。

So all in all, Angularjs's assets management has no difference with any other Javascript framworks/files, except the template async loading.(if you write template in separate files, please make sure front end can access that template file directly and also skipping the concatenate process of template files). 总而言之,Angularjs的资产管理与任何其他Javascript framworks /文件没有区别,除了模板异步加载。(如果你在单独的文件中编写模板,请确保前端可以直接访问该模板文件并跳过连接模板文件的过程)。

Hope it helps, thanks. 希望它有所帮助,谢谢。

Use ngmin + Closure Compiler 使用ngmin + Closure Compiler

Taken from: http://briantford.com/blog/huuuuuge-angular-apps 摘自: http//briantford.com/blog/huuuuuge-angular-apps

...Build Process ......构建过程

Admittedly, this is one thing Angular needs to be better at, and one of my huge aims for 2013 is to help on this front. 不可否认,这是Angular需要做的一件事,我2013年的一个重要目标就是在这方面提供帮助。 I've released ngmin, a tool that I hope will ultimately solve the problem of minimizing AngularJS apps for production. 我发布了ngmin,这个工具我希望最终能解决最小化AngularJS应用程序的问题。

For now, I think your best bet is concatenating your JavaScript files with app.js first, then using ngmin to annotate DI functions, and finally minifying with Closure Compiler with the flag --compilation_level SIMPLE_OPTIMIZATIONS. 现在,我认为最好的办法是首先将您的JavaScript文件与app.js连接起来,然后使用ngmin来注释DI函数,最后使用标志--compilation_level SIMPLE_OPTIMIZATIONS将Closure Compiler缩小。 You can see an example of this at work in the build process for angular.js. 您可以在angular.js的构建过程中看到这样的示例。

I don't recommend using RequireJS with AngularJS. 我不建议将RequireJS与AngularJS一起使用。 Although it's certainly possible, I haven't seen any instance where RequireJS was beneficial in practice. 虽然这当然是可能的,但我还没有看到任何RequireJS在实践中有益的例子。

Other links: 其他链接:

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

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