码迷,mamicode.com
首页 > 编程语言 > 详细

javascript的压缩合并工具

时间:2014-05-14 02:27:30      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

一、使用RequireJS的压缩合并

  使用RequireJS模块化后代码被拆分成多个JS文件了,在部署生产环境需要压缩合并,RequireJS提供了一个打包压缩工具r.js来对模块进行合并压缩。r.js非常强大,不但可以压缩js,css,甚至可以对整个项目进行打包。

  r.js的压缩工具使用UglifyJS或Closure Compiler。默认使用UglifyJS(jQuery也是使用它压缩)。此外r.js需要node.js环境,当然它也可以运行在Java环境中如Rhino。JAVA环境使用Ant构建可以参考另外一篇RequireJS optimizer Ant task有介绍。node环境参考RequireJS模块化与GruntJS构建。

  本篇介绍require.js官方文档中介绍的方法。

build.xml

bubuko.com,布布扣
 1 {
 2     appDir: ‘../www‘,
 3     baseUrl: ‘js/lib‘,
 4     paths: {
 5         app: ‘../app‘
 6     },
 7     dir: ‘../www-built‘,
 8     modules: [
 9         //First set up the common build layer.
10         {
11             //module names are relative to baseUrl
12             name: ‘../common‘,
13             //List common dependencies here. Only need to list
14             //top level dependencies, "include" will find
15             //nested dependencies.
16             include: [‘jquery‘,
17                       ‘app/lib‘,
18                       ‘app/controller/Base‘,
19                       ‘app/model/Base‘
20             ]
21         },
22  
23         //Now set up a build layer for each page, but exclude
24         //the common one. "exclude" will exclude nested
25         //the nested, built dependencies from "common". Any
26         //"exclude" that includes built modules should be
27         //listed before the build layer that wants to exclude it.
28         //"include" the appropriate "app/main*" module since by default
29         //it will not get added to the build since it is loaded by a nested
30         //require in the page*.js files.
31         {
32             //module names are relative to baseUrl/paths config
33             name: ‘../page1‘,
34             include: [‘app/main1‘],
35             exclude: [‘../common‘]
36         },
37  
38         {
39             //module names are relative to baseUrl
40             name: ‘../page2‘,
41             include: [‘app/main2‘],
42             exclude: [‘../common‘]
43         }
44  
45     ]
46 }
bubuko.com,布布扣

在node环境下执行 node r.js -o build.js 就可以压缩合并模块。

项目参考https://github.com/requirejs/example-multipage/blob/master/tools/build.js

 

二、使用UglifyJS的压缩合并

 

 

 

 

 

 

 

 

javascript的压缩合并工具,布布扣,bubuko.com

javascript的压缩合并工具

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/couxiaozi1983/p/3723877.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!