Home > Web Front-end > JS Tutorial > body text

Compress JavaScript files for the entire project using r.js

高洛峰
Release: 2016-11-26 09:22:45
Original
1404 people have browsed it

r.js is part of RequireJS (optimizer). It depends on UglifyJS, which 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 also use it for compression. This article discusses compressing the js files of the entire project when the AMD method is not used to organize the code.

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

Compress JavaScript files for the entire project using r.js

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

The scripts-build directory places compressed js files. The structure is the same as scripts, but an additional build.txt file will be generated.

The build.js file needs to be configured by ourselves. r.js will use it to find the compressed directory and the compressed placement directory. It's roughly as follows

({

appDir: "scripts",

baseUrl: "scripts",

dir: "scripts-build"})

The following is the project I tested (before compression)

Compress JavaScript files for the entire project using r.js

You can see that scripts-build is currently empty. Okay, let's start compressing.

1, cmd to enter the command line console, my project is in e:/work/myapp

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

3, execute the command: node r .js -o build.js The effect is as shown in the figure

Compress JavaScript files for the entire project using r.js

At this time, the compression is successful. Look at the scripts-build directory again. It has the same structure as scripts, and there is an additional build.txt.

Compress JavaScript files for the entire project using r.js

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!