Fine-grained Management of JS Code without introducing dependency Libraries

Source: Internet
Author: User

At the beginning of a project, there is usually only one JS file except libraries such as jquery.

However, as the number of functions increasesCodeIt will also continue to expand. When more than a certain degree (more than 500 lines), the Maintenance difficulty will be greatly increased. The organizational logic is chaotic. It is difficult to quickly locate the desired location during debugging, and it is difficult to choose the insert location when writing a new function. Rolling around in a long JS file all day is a headache.

An optional solution is to use dependent libraries, such as requirejs, seajs, And in. js. However, more code needs to be introduced, and additional HTTP requests need to be added to maintain dependency.

In most cases, JS Code does not expand to the point where dependent libraries must be introduced. Is there a way to perform fine-grained maintenance on JS Code without adding new code and HTTP requests?

In the current project, the JS Code after excluding jquery and other class libraries is about KB (58kb after compression). We use this solution:

1. Split JS into multiple files by module or by nature, for example, extract user-related parts into user. JS, and extract the shared methods into helpers. js.

2. Write sh script (Linux) or bat script (Windows) Merge all JS into all. JS, and call yuicompressor for compression to generate a all-min.js.

//Bat script in WindowsDelAll. js all-min.JSTypeSource \ *. js> All.Jsjava-jar YC. Jar -- preserve-semi-O all-min.js all. js

3, in the development environment using all. js (convenient DEUG), production environment using all-min.js.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.