当前位置 : 主页 > 网页制作 > Dojo >

利用dojo打包web项目中的js和css文件

来源:互联网 收集:自由互联 发布时间:2021-06-15
引子: 最近在做一个web项目的优化,遇到一个问题,就是在加载初始页面的时候会引用到很多js/css文件,每引用一个文件就会产生一个http请求,这样项目的性能就会受到很大影响。 所

引子:

      最近在做一个web项目的优化,遇到一个问题,就是在加载初始页面的时候会引用到很多js/css文件,每引用一个文件就会产生一个http请求,这样项目的性能就会受到很大影响。

      所以我们决定将js/css文件打包成一个或者几个较大的文件,这样的话可以大量的减少http请求,从而在一定程度上提高系统的性能。

dojo的optimize优化

      我使用的dojo是1.6.1版本,在util/buildscripts目录下有一个build.js文件,主要用于release的时候定义一些操作。这个文件主要引用到了jslib下的buildUtil.js文件,查看该文件,可以看到在文件开头有一段DojoBuildOptions,其中包括profile,releaseName,optimize等,具体用法参照每个option中的helpText

      其次我们需要用到util/shrinksafe文件夹下的js.jar和shrinksafe.jar,这两个文件分别用来优化和压缩js/css文件

      最后需要一个profile文件,在buildscripts/profiles下新建一个xxx.profile.js文件,该文件用于定义所有需要打包的js文件,写法如下:

 
 
  1. /* ***************************************************************** */ 
  2. /*                                                                   */ 
  3. /* IBM Confidential                                                  */ 
  4. /*                                                                   */ 
  5. /* OCO Source Materials                                              */ 
  6. /*                                                                   */ 
  7. /* Copyright IBM Corp. 2010                                          */ 
  8. /*                                                                   */ 
  9. /* The source code for this program is not published or otherwise    */ 
  10. /* divested of its trade secrets, irrespective of what has been      */ 
  11. /* deposited with the U.S. Copyright Office.                         */ 
  12. /*                                                                   */ 
  13. /* ***************************************************************** */ 
  14.     dependencies = {  
  15.             layers: [  
  16.                      {  
  17.                             name: "../compressed/tms.js",  
  18.                             resourceName: "tms",  
  19.                             layerDependencies: [  
  20.                             ],  
  21.                             dependencies: [  
  22.                             "dojo.cache",  
  23.                             "dojox.widget.TitleGroup",  
  24.                             "dijit.Calendar",   
  25.                             "dijit.Dialog",   
  26.                             "dijit.Menu",   
  27.                             "dijit.MenuBar",   
  28.                             "dijit.MenuItem",   
  29.                             "dijit.PopupMenuBarItem",   
  30.                             "dijit.TitlePane",   
  31.                             "dijit.Tooltip",   
  32.                             "dijit.TooltipDialog",   
  33.                             "dijit._Templated",   
  34.                             "dijit.dijit",   
  35.                             "dijit.form.Button",   
  36.                             "dijit.form.CheckBox",   
  37.                             "dijit.form.ComboBox",   
  38.                             "dijit.form.CurrencyTextBox",   
  39.                             "dijit.form.DateTextBox",   
  40.                             "dijit.form.DropDownButton",   
  41.                             "dijit.form.Form",   
  42.                             "dijit.form.NumberTextBox",   
  43.                             "dijit.form.RadioButton",   
  44.                             "dijit.form.RangeBoundTextBox",   
  45.                             "dijit.form.Select",   
  46.                             "dijit.form.TextBox",   
  47.                             "dijit.form.Textarea",   
  48.                             "dijit.form.TimeTextBox",   
  49.                             "dijit.form.ValidationTextBox",   
  50.                             "dijit.form._FormSelectWidget",   
  51.                             "dijit.layout.BorderContainer",   
  52.                             "dijit.layout.ContentPane",   
  53.                             "dijit.layout.TabContainer",   
  54.                             "dijit.layout._TabContainerBase",   
  55.                             "dojo.data.ItemFileReadStore",   
  56.                             "dojo.data.ItemFileWriteStore",   
  57.                             "dojo.data.ObjectStore",   
  58.                             "dojo.date.locale",   
  59.                             "dojo.dnd.Source",   
  60.                             "dojo.fx",   
  61.                             "dojo.parser",   
  62.                             "dojo.store.Memory",   
  63.                             "dojox.grid.DataGrid",   
  64.                             "dojox.grid.EnhancedGrid",   
  65.                             "dojox.grid._EditManager",   
  66.                             "dojox.grid._RowManager",   
  67.                             "dojox.grid._RowSelector",   
  68.                             "dojox.grid.cells._base",   
  69.                             "dojox.grid.cells.dijit",   
  70.                             "dojox.grid.enhanced._FocusManager",   
  71.                             "dojox.grid.enhanced.plugins.CellMerge",   
  72.                             "dojox.grid.enhanced.plugins.Cookie",   
  73.                             "dojox.grid.enhanced.plugins.DnD",   
  74.                             "dojox.grid.enhanced.plugins.Filter",   
  75.                             "dojox.grid.enhanced.plugins.GridSource",   
  76.                             "dojox.grid.enhanced.plugins.IndirectSelection",   
  77.                             "dojox.grid.enhanced.plugins.Menu",   
  78.                             "dojox.grid.enhanced.plugins.NestedSorting",   
  79.                             "dojox.grid.enhanced.plugins.Pagination",   
  80.                             "dojox.grid.enhanced.plugins.Printer",   
  81.                             "dojox.grid.enhanced.plugins.Search",   
  82.                             "dojox.grid.enhanced.plugins.Selector",   
  83.                             "dojox.grid.enhanced.plugins.exporter.CSVWriter",   
  84.                             "dojox.json.query",   
  85.                             "dojox.layout.ContentPane",   
  86.                             "dojox.widget.Standby",   
  87.                             "com.ibm.tms.Loader",  
  88.                             "tms.screens.TMSBase",   
  89.                             "tms.screens.TMSScreen",   
  90.                             "tms.screens.TMSTemplated",   
  91.                             "tms.screens.TMSNewScreen",   
  92.                             "tms.screens.notesHistory.notesHistory",   
  93.                             "tms.screens.TMSTabContainer",  
  94.                             "tms.common.tms",  
  95.                             "com.ibm.tms.BorderContainer",  
  96.                             "com.ibm.tms.ButtonSelector",   
  97.                             "com.ibm.tms.Customizer",   
  98.                             "com.ibm.tms.DropMultiSelect",   
  99.                             "com.ibm.tms.TMSCheckedMultiSelect",   
  100.                             "com.ibm.tms.TMSComboBox",   
  101.                             "com.ibm.tms.TMSDropDownButton",   
  102.                             "com.ibm.tms.TMSGrid",   
  103.                             "com.ibm.tms.TMSGridPagination",   
  104.                             "com.ibm.tms.TMSIconTooltip",   
  105.                             "com.ibm.tms.TMSMessageBox",   
  106.                             "com.ibm.tms.TMSTooltip",   
  107.                             "com.ibm.tms.TmsSelect",   
  108.                             "com.ibm.tms.form.CarrierRefTextBox",   
  109.                             "com.ibm.tms.form.EmailTextBox",   
  110.                             "com.ibm.tms.form.NumberTextBox",   
  111.                             "com.ibm.tms.form.ValidationTextBox",   
  112.                             "com.ibm.tms.form.ZipCodeTextBox",   
  113.                             "com.ibm.tms.grid.Selection",   
  114.                             "com.ibm.tms.grid._EditManager",   
  115.                             "com.ibm.tms.grid._FocusManager",   
  116.                             "com.ibm.tms.grid._RowManager",   
  117.                             "com.ibm.tms.grid._RowSelector",   
  118.                             "com.ibm.tms.grid.cells._base",   
  119.                             "com.ibm.tms.grid.enhanced.plugins.Selector",   
  120.                             "tms.screens.utils.ScreenUtils",   
  121.                             "tms.screens.utils.TMSValidation" 
  122.                         ]  
  123.                     }  
  124.     ],  
  125.  
  126.     prefixes: [  
  127.         [ "dijit""../dijit" ],  
  128.         [ "dojox""../dojox" ],  
  129.         [ "com.ibm.tms""../com/ibm/tms" ],  
  130.         [ "tms.screens""../../../tms/screens"],  
  131.         [ "tms.common""../../../tms/js/common"]  
  132.     ]  
  133. }  
  134.  
  135.  

好了,准备就绪,开始优化

打包js

我们将打包js作为一个任务集成到ant中:build.xml

 
 
  1. <target name="compressJS"> 
  2.     <echo message="Compress Javascript code..."/> 
  3.       
  4.     <exec executable="java" dir="./webpages/tms/dojo/util/buildscripts" 
  5.            resolveexecutable="true" failonerror="false"> 
  6.            <arg line="-classpath ../shrinksafe/js.jar;../shrinksafe/shrinksafe.jar org.mozilla.javascript.tools.shell.Main build.js action=clean,release cssOptimize=comments profile=TMS releaseName=release"/> 
  7.        </exec> 
  8. </target> 

BUILD SUCCESSFUL以后,在dojo下会生成一个release文件夹,将compressed目录下的tms.js文件和nls文件夹拷贝到根目录下,然后在初始页面中引用:

 
 
  1. <script src="nls/tms_en-us.js"></script> 

注意,dojo.js还是得继续引用

打包css

css文件不能在profile中自定义,所以我们直接使用命令行的方式处理,build.xml:

 
 
  1. <target name="compressCSS"> 
  2.     <echo message="Compress CSS ..."/> 
  3.         <exec executable="java" dir="${src}/webpages/tms/dojo/util/buildscripts" 
  4.             resolveexecutable="true" failonerror="false"> 
  5.             <arg line="-classpath ../shrinksafe/js.jar${class_separatetoken}../shrinksafe/shrinksafe.jar org.mozilla.javascript.tools.shell.Main build_css.js"/> 
  6.         </exec>     
  7. </target> 

BUILD SUCCESSFUL以后,/css文件夹下有几个文件会变大(几十k或者一百多k),他们包含了所有你需要的css文件(打包原理就是所有被import的css文件全部被完全拷贝进来),我们只需要在页面中引用这几个css文件:

 
 
  1. <link type="text/css" href="css/dojoStyle.css" rel="stylesheet"/> 
  2. <link type="text/css" href="css/tms.css" rel="stylesheet"/> 

结束语

      至此,打包的工作算是圆满结束。总结一下,我们利用dojo提供的工具来进行js/css文件的压缩,主要用到js.jar和shrinksafe.jar两个文件。打包原理是将所有引用的文件全部去掉注释后整体引用进来,变成一个大的文件,最后在页面中引用这些打包后的文件。

      打包以后http请求数量减少了3/4左右。css的http请求只剩下两个,js的请求数有十几个,主要是_base下的js。

      总算是解决这个问题了,Mark一下,实习第三周。

网友评论