如何将JavaScript文件连接到一个文件? [英] How do I concatenate JavaScript files into one file?
问题描述
我想为我的网站创建一个编译的JavaScript文件。对于开发我更喜欢将JavaScript保存在单独的文件中,并且作为我的自动化脚本的一部分将文件连接到一个文件中并在其上运行压缩器。
我的问题是如果我使用旧的DOS复制命令,它也放入压缩器所抱怨的EOF标记:
copy / A * .js compiled.js / Y
其他人在做什么?
YUI Compressor。
http:// yui .github.com / yuicompressor /
在Ant构建xml中放入类似的东西。
它将创建两个文件application.js和application-min.js。
< target name = concatenatedescription =Concatenate all js files>
< concat destfile =build / application.js>
< fileset dir =src / jsincludes =*。js/>
< / concat>
< / target>
< target name =compressdepends =concatenatedescription =将application.js压缩到application-min.js>
< apply executable =javaparallel =false>
< filelist dir =buildfiles =application.js/>
< arg line = - jar/>
< arg path =path / to / yuicompressor-2.4.2.jar/>
< srcfile />
< arg line = - o/>
< mapper type =globfrom =*。jsto =build / * - min.js/>
< targetfile />
< / apply>
< / target>
I want to create a compiled JavaScript file for my website. For development I would prefer to keep the JavaScript in separate files and just as part of my automated scripts concatenate the files into one and run the compressor over it.
My problem is that if I use the old DOS copy command it also puts in the EOF markers which the compressor complains about:
copy /A *.js compiled.js /Y
What are other people doing?
I recommend using Apache Ant and YUI Compressor.
http://yui.github.com/yuicompressor/
Put something like this in the Ant build xml. It will create two files, application.js and application-min.js.
<target name="concatenate" description="Concatenate all js files">
<concat destfile="build/application.js">
<fileset dir="src/js" includes="*.js" />
</concat>
</target>
<target name="compress" depends="concatenate" description="Compress application.js to application-min.js">
<apply executable="java" parallel="false">
<filelist dir="build" files="application.js" />
<arg line="-jar" />
<arg path="path/to/yuicompressor-2.4.2.jar" />
<srcfile />
<arg line="-o" />
<mapper type="glob" from="*.js" to="build/*-min.js" />
<targetfile />
</apply>
</target>
这篇关于如何将JavaScript文件连接到一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!