Here is an example:
1. First modify your html so that it looks something like this:
<html>
<head>
<!--@@myjs.start-->
<script type="text/javascript" src="scripts/MyFile1.js"></script>
<script type="text/javascript" src="scripts/MyFile2.js"></script>
<script type="text/javascript" src="scripts/MyFile3.js"></script>
<!--@@myjs.end-->
</head>
etc..
Notice the @@myjs block. Everything between those comments (inclusive) will be replaced in the next step.
2. Add the following into your Ant build script:
<target name="set-js-includes" depends="init">
<replaceregexp>
<fileset dir="../build/WebContent" includes="**/*.html, **/*.jsp">
</fileset>
<regexp pattern="<!--@@myjs.start-->[-a-zA-Z0-9<>\s'"/=.!_]*<!--@@myjs.end-->"/>
<substitution expression="<script type="text/javascript" src="scripts/MyFile.js"></script>"/>
</replaceregexp>
</target>
You will need to make sure that the paths are pointing to the correct place.
Now you should have something that looks like this:
<html>
<head>
<script type="text/javascript" src="scripts/MyFile.js"></script>
</head>
etc..
After doing all of this there is an important question that must be asked: Why? Well, now you can have all of you javascript code neatly separated into different files during development, but built into one file that is faster to download for production. Just remember to test the output of this procedure as it will modify your code.
Have a good one.
No comments:
Post a Comment