您的位置:首页 > Web前端 > JavaScript

《编写可维护的 JavaScript》读书笔记第16章:文件合并和加工

2014-01-06 15:25 435 查看

1. 文件合并

<tstamp>
<format property="build.time" pattern="yyyy-MM-dd hh:mm:ss"/>
</tstamp>
<target name="concatenate">
<concat destfile="${build.src}/build.js" fixlastline="yes" eol="lf" encoding="utf-8">
<header>/* Build Time: ${build.time} */</header>
<filelist dir="${src.dir}" files="first.js,second.js"/>
<fileset dir="${src.dir}" includes="**/*.js" excludes="first.js,second.js"/>
<footer trimleading="yes">
/* footer */
</footer>
</concat>
</target>


2. 加工文件

2.1 插入 license 文件

<target name="concatenate">
<loadfile property="license" srcfile="license.txt"/>
<concat destfile="${build.src}/build.js" fixlastline="yes" eol="lf">
<header trimleading="yes">/*!
${license}
*/
/* Build Time: ${build.time} */
</header>
<filelist .../>
<fileset .../>
</concat>
</target>


2.2 替换字符串

例如替换版本号:

var MyProject = {
version: "@VERSION@"
};


使用 <replaceregexp> 任务通过匹配正则表达式进行替换:

<target name="concatenate">
<concat destfile="${build.src}/build.js" fixlastline="yes" eol="lf">
<filelist .../>
<fileset .../>

<replaceregexp match="@VERSION@" replace="${version}" flags="g" byline="true" encoding="utf-8">
<fileset dir="${build.dir}" includes="**/*"/>
</replaceregexp>
</concat>
</target>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐