Heim >Web-Frontend >js-Tutorial >JavaScript 组件之旅(三):用 Ant 构建组件_javascript技巧
听起来是不是很惬意?Let's go! 我们出发啦~
这期,我们会使用 Ant 将上期编写、整理的代码文件按指定的先后顺序合并成单一的源文件,然后压缩这个文件。这是构建 JavaScript 项目的基本步骤。Ant 是 Apache 的一个顶级开源项目,网上对它的介绍和安装,已经有很多文章,这里就不再赘述了。在构建之前,我们先来看看已有的文件布局:
smart-queue <span style="COLOR: #c0c0c0">// 组件的根目录</span> +--- src <span style="COLOR: #c0c0c0">// JavaScript源文件目录</span> +--- lang.js <span style="COLOR: #c0c0c0">// 前文提到的“外部文件”</span> +--- smart-queue.js <span style="COLOR: #c0c0c0">// Smart Queue 主文件</span>
现在,我们要让它“丰满”起来:
麻雀虽小,五脏俱全。现在 Smart Queue 看上去像是比较专业的 JavaScript 项目了:
smart-queue <span style="COLOR: #c0c0c0">// 组件的根目录</span> +--- lib <span style="COLOR: #c0c0c0">// JavaScript外部程序和库文件目录</span> +--- yuicompressor.jar <span style="COLOR: #c0c0c0">// YUI Compressor</span> +--- test <span style="COLOR: #c0c0c0">// 测试文件目录</span> +--- src <span style="COLOR: #c0c0c0">// JavaScript源文件目录</span> +--- intro.js <span style="COLOR: #c0c0c0">// 介绍和版本信息</span> +--- lang.js <span style="COLOR: #c0c0c0">// 前文提到的“外部文件”</span> +--- smart-queue.js <span style="COLOR: #c0c0c0">// Smart Queue 主文件</span> +--- README <span style="COLOR: #c0c0c0">// 组件自述文件</span> +--- LICENSE <span style="COLOR: #c0c0c0">// 组件授权信息</span>
我们计划将构建出来的文件存放到组件根目录下的 build 子目录,还要通过构建工具创建并销毁它。首次尝试构建前,建议先大概了解一下 Ant 的配置文件——build.xml 的结构:
<span style="COLOR: #b000b0"><span style="COLOR: #c00000">project</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"MyProject"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">default</span>=<span style="COLOR: #008000">"dist"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">basedir</span>=<span style="COLOR: #008000">"."</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">description</span><span style="COLOR: #b000b0">></span> simple example build file <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">description</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #707070"><span style="COLOR: #707070">-- set global properties for this build --</span><span style="COLOR: #707070">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"src"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"src"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"build"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"build"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"dist"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"dist"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"init"</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #707070"><span style="COLOR: #707070">-- Create the time stamp --</span><span style="COLOR: #707070">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">tstamp</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #707070"><span style="COLOR: #707070">-- Create the build directory structure used by compile --</span><span style="COLOR: #707070">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">mkdir</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"compile"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">depends</span>=<span style="COLOR: #008000">"init"</span> <span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">description</span>=<span style="COLOR: #008000">"compile the source "</span><span style="COLOR: #b000b0"> ></span> <span style="COLOR: #707070"><span style="COLOR: #707070">-- Compile the java code from ${src} into ${build} --</span><span style="COLOR: #707070">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">javac</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">srcdir</span>=<span style="COLOR: #008000">"${src}"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">destdir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"clean"</span> <span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">description</span>=<span style="COLOR: #008000">"clean up"</span><span style="COLOR: #b000b0"> ></span> <span style="COLOR: #707070"><span style="COLOR: #707070">-- Delete the ${build} and ${dist} directory trees --</span><span style="COLOR: #707070">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">delete</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">delete</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${dist}"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">project</span><span style="COLOR: #b000b0">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
仔细观察一下,除了 <font face="新宋体">name, description</font>
这些名字都很容易理解外,其他可以看到的规律包括:
<font face="新宋体">project</font>
元素的 <font face="新宋体">default</font>
属性值对应某个 <font face="新宋体">target</font>
元素的 <font face="新宋体">name</font>
属性;
<font face="新宋体">target</font>
元素的 <font face="新宋体">depends</font>
属性值对应其他某些 <font face="新宋体">target</font>
元素的 <font face="新宋体">name</font>
属性;
<font face="新宋体">${somename}</font>
可以引用 <font face="新宋体">property</font>
中定义的值。 下面我们开始写自己的 build.xml.
首先,配置项目的基本信息,以及相关目录名称,将要使用的编码等等:
<span style="COLOR: #b000b0"><span style="COLOR: #c00000">project</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"Smart Queue"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">default</span>=<span style="COLOR: #008000">"compress"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">basedir</span>=<span style="COLOR: #008000">"."</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">description</span><span style="COLOR: #b000b0">></span>Build file for Ant<span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">description</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"src"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"src"</span><span style="COLOR: #b000b0"> /></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"build"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"build"</span><span style="COLOR: #b000b0"> /></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"lib"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"lib"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"inputencoding"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">value</span>=<span style="COLOR: #008000">"utf-8"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"outputencoding"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">value</span>=<span style="COLOR: #008000">"gbk"</span><span style="COLOR: #b000b0">/></span></span></span></span></span></span></span></span>
接着,定义一个用于初始化的 <font face="新宋体">target</font>
, 它负责创建 build 子目录:
<span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"init"</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">mkdir</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span></span></span>
然后定义名为 <font face="新宋体">concat</font>
的 <font face="新宋体">target</font>
, 负责将 src 里的 3 个 JavaScript 文件按先后顺序连接起来。运行它要先运行前面定义的 <font face="新宋体">init</font>
:
<span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"concat"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">depends</span>=<span style="COLOR: #008000">"init"</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">concat</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">destfile</span>=<span style="COLOR: #008000">"${build}/smart-queue.source.js"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">encoding</span>=<span style="COLOR: #008000">"${inputencoding}"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">outputencoding</span>=<span style="COLOR: #008000">"${outputencoding}"</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">filelist</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${src}"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">files</span>=<span style="COLOR: #008000">"intro.js, lang.js, smart-queue.js"</span><span style="COLOR: #b000b0"> /></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">concat</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span></span></span></span>
这样,就可以得到一个可以工作的 JavaScript 文件,下面的 <font face="新宋体">target</font>
负责压缩这个文件,显然它依赖于 <font face="新宋体">concat</font>
, 也依赖于 <font face="新宋体">init</font>
, 但是不必显式指定对 <font face="新宋体">init</font>
的依赖——Ant 能处理这种依赖关系。这里调用 YUI Compressor 并传入适当的参数:
<span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"compress"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">depends</span>=<span style="COLOR: #008000">"concat"</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">java</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">jar</span>=<span style="COLOR: #008000">"${lib}/yuicompressor.jar"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">fork</span>=<span style="COLOR: #008000">"true"</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">arg</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">line</span>=<span style="COLOR: #008000">"--type js --charset utf-8 -o ${build}/smart-queue.js ${build}/smart-queue.js"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">java</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span></span></span></span>
大功告成,<font face="新宋体">compress</font>
处理后的文件就可以部署到生产系统上去了。最后我们做一下清理工作,使你在生成文件后还可以回到最初的状态:
<span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"clean"</span><span style="COLOR: #b000b0">></span> <span style="COLOR: #b000b0"><span style="COLOR: #c00000">delete</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span> <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span></span></span>
到此可以说基本的配置就写完了。怎么使用它呢?以命令行方式进入到组件根目录(或者说 build.xml 所在的目录),然后:
<font face="新宋体">ant concat</font>
, 将得到 ./build/smart-queue.source.js
<font face="新宋体">ant</font>
, 将选择 <font face="新宋体"><project></project></font>
中 <font face="新宋体">default</font>
引用的那个 <font face="新宋体">target</font>
, 即 <font face="新宋体">compress</font>
, 所以会得到 ./build 下的 smart-queue.source.js 和 smart-queue.js
<font face="新宋体">ant clean</font>
, 将删除 ./build 目录,回到最初的状态 这些前提是你已经正确安装或者说设置好了 JDK 和 Ant, 如果有错误提示出来,则可能需要检查它们是否已准备妥当。
一路看下来,是不是觉得本期介绍的东西很简单?那是当然了,构建工具就应该简单易用,否则把大量的时间花在那上面岂非不值?工具的价值在于提升生产力,从而创造更多价值。
最后,你可以在这里查看 Ant 的帮助文档(里面有很多好玩的东东哦),也可以在这里查看本期完整的 build.xml 文件。