简体   繁体   English

如何使用Google Closure编译器使巨大的javascript文件更加有效和可维护?

[英]How do I use google closure compiler to make a gigantic javascript file more efficient and maintainable?

I have a javascript file that is about 5000 lines long and may grow bigger in the future. 我有一个大约5000行的javascript文件,将来可能会越来越大。

I have been going through it by myself to try to figure out how to break it up and make it more modular / object oriented. 我自己一直在研究它,试图找出如何分解它并使它更模块化/面向对象的方法。

however, I would really appreciate a tool of some kind that can help me find problems more quickly. 但是,我真的很感激某种可以帮助我更快地发现问题的工具。

I have heard of google closure and I downloaded it. 我听说过Google关闭并下载了它。 However, when I feed my large javascript file into the compiler I get a lot of stuff changed and returned to me. 但是,当我将大的javascript文件输入编译器时,我做了很多更改,然后还给了我。

I don't want to make SO much change that I can't understand the code. 我不想进行太多更改,以致于我无法理解代码。 How do I approach this problem? 我该如何解决这个问题? Should I only feed into the compiler small portions at a time ? 我是否应该一次只向编译器提供一小部分?

The best way to do this would be to use RequireJS and then use the AMD optimizer (r.js) to combine and optimize your code. 最好的方法是使用RequireJS ,然后使用AMD 优化器 (r.js)组合和优化代码。 You can also use the Closure compiler along with r.js to optimize your code further. 您还可以将Closure编译器与r.js一起使用,以进一步优化代码。 I did this exact thing recently for a personal project, where I was working with a large JavaScript file. 我最近在一个个人项目中做了这件事,当时我正在处理一个大型JavaScript文件。 I was able to successfully modularize it and optimize it using RequireJS, r.js, and Closure. 我能够成功地对其进行模块化,并使用RequireJS,r.js和Closure对其进行优化。

Expect to spend sometime bringing yourself up to speed with RequireJS concepts. 期望花一些时间使自己快速熟悉RequireJS概念。 It took me a little while to figure out what I needed to be doing. 我花了一段时间才弄清楚我需要做什么。

Closure compiler won't make a program more maintainable. 闭包编译器不会使程序更具可维护性。 Your code becomes input to Closure Compiler which spits out JavaScript for your browser. 您的代码将输入到Closure Compiler,后者为您的浏览器吐出JavaScript。 It can improve the efficiency (both in terms of size as well as removing utilized variables) and making your code compatible with the "advanced" mode can have the side benefit of forcing you to write more modular code which is by its nature more maintainable. 它可以提高效率(在大小上以及删除使用的变量方面),并使您的代码与“高级”模式兼容,这可能会带来一些副作用,即迫使您编写更多的模块化代码,而这种代码本质上更易于维护。

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

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