Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I concatenate JavaScript files into one file?

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?

like image 408
Phil Hannent Avatar asked Nov 19 '08 10:11

Phil Hannent


People also ask

Should I combine JavaScript files?

With HTTP/2, there is generally no need to combine CSS/JS files as the multiplexing feature eliminates the need for concurrent connections (unlike HTTP/1.1). Don't combine your CSS/JS files when you're on HTTP/2 or if your page has many scripts and stylesheets.

Should I put all my JavaScript in one file?

To avoid multiple server requests, group your JavaScript files into one. Whatever you use for performance, try to minify JavaScript to improve the load time of the web page. If you are using single page application, then group all the scripts in a single file.

How do I connect JavaScript files?

We can link JavaScript to HTML by adding all the JavaScript code inside the HTML file. We achieve this using the script tag which was explained earlier. We can put the <script></script> tag either inside the head of the HTML or at the end of the body.


2 Answers

I recommend using Apache Ant and YUI Compressor.

http://ant.apache.org/

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> 
like image 181
David Brockman Avatar answered Sep 25 '22 22:09

David Brockman


To copy without EOF use binary mode:

copy /B *.js compiled.js /Y 

If the resulting file still has EOFs, that might have come from one of original files, it can be fixed by this variant:

copy /A *.js compiled.js /B /Y 

/A removes trailing EOFs from original files if any and /B prevents appending EOF to the resulting file. If an EOF is not at the end, the source file will be truncated at it. The order of switches is important. If you write

copy /A *.js /B compiled.js /Y   

- EOFs in source files won't be removed but still resulting EOF won't be appended.

Try it yourself, thats where I get it. DOS commands are weird.

like image 31
eugensk Avatar answered Sep 22 '22 22:09

eugensk