Use r. js to compress the JavaScript file of the entire project

Source: Internet
Author: User

R. js is part of RequireJS (optimizer ). It depends on UglifyJS, while UglifyJS is based on nodejs. Most of the time, r. js is combined and compressed with the modular (AMD) Writing Method. If your code does not use AMD, you can use it for compression. This article discusses how to compress the js file of the entire project without using AMD to organize code.

 

Assume that all the js files in the project are in the scripts directory.



 

The project contains three directories: css, html, and scripts, which correspond to css, html, and js files respectively. The downloaded r. js file is placed in parallel with myapp.

The JS file behind the scripts-builddirectory is structured in the same way as scripts, but a build.txt file is generated.

The build. js file needs to be configured by ourselves. r. js will find the compressed directory and the compressed directory. Probably as follows:

({

AppDir: "scripts ",

BaseUrl: "scripts ",

Dir: "scripts-build "})
 

The following are my tested projects (before compression)



 

As you can see, scripts-build is currently empty. Okay, start compressing.

 

1. Run cmd to go to the command line console. My project is on e:/work/myapp.

2. cd to the current project (enter the E drive, cd work/myapp)

3. Execute the command: node r. js-o build. js Effect



 

At this time, the computation was successful. Now we can see the scripts-builddirectory, And the scriptsstructure. We also have an additional build.txt.




From Snandy

Related Article

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.