且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 JavaScript 文件连接成一个文件?

更新时间:2023-11-05 10:11:58

我推荐使用 Apache Ant 和 YUI Compressor.

I recommend using Apache Ant and YUI Compressor.

http://ant.apache.org/

http://yui.github.com/yuicompressor/

在 Ant 构建 xml 中放入类似的内容.它将创建两个文件,application.js 和 application-min.js.

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>