简体   繁体   中英

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.

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. However, when I feed my large javascript file into the compiler I get a lot of stuff changed and returned to me.

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. You can also use the Closure compiler along with r.js to optimize your code further. I did this exact thing recently for a personal project, where I was working with a large JavaScript file. I was able to successfully modularize it and optimize it using RequireJS, r.js, and Closure.

Expect to spend sometime bringing yourself up to speed with RequireJS concepts. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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