A front end build script that is easy to integrate in your workflow.
Props to the original author (https://github.com/posabsolute). You can see his blog post about it here(http://www.position-absolute.com/?p=4177).
The build.js script required node.js and the node-minify module.
First go to build/build.js. In this file you will see the function concat, as displayed below:
concat({
src : ['file1.js', 'file2.js'],
dest : 'dist/concatenatedFile.js'
});
It's as simple as that, just tell the script what files you would like to concatenante, your are not just confined to javascript/css files, you can also concatenate templates files.
To use the minify script you will need to have node-minify installed in your app. Then much like concat, choose your already concatenated file and minify it using the closure function, passing in the targetFile and destination path.
closure('dist/concatenatedFile.js', 'dist/concatenatedFile.min.js');
To use the minify script you will need to have node-minify installed. As with concatenation, choose your already concatenated CSS file and minify it using the sqwish function.
sqwish('dist/concatenatedStylesheet.css', 'dist/concatenatedStylesheet.min.css');
go into the build folder and then run:
node build.js
then you should see the following logs. Number of files will depend on how many are passed in from the config file.
Concatenating 2 Files
Concatenation complete: dist/templates.html.
Concatenating 4 Files
Concatenation complete: dist/appfiles.js.
Concatenating 2 Files
Concatenation complete: dist/styles.css.
Comprssing Scripts.
Compressing Stylesheets.
Build complete.
and there you go your files has been created. You could also put that command into a post-commit hook for profit!
You probably want to define your js files array somewhere else that will be used by oth your app and your build script.
To do that you can first require your config file.
var conf = require('../app/config')
Then in your config file you need to tell node.js what this module return. As you can expect that do not get so well with your normal app, that's why at the end of the file we got
try{
if(exports) exports.filesArray = filesArray;
}catch(e){
}
It looks weird, but with node.js we do not have any window variable like in the broser and in your app if(export) will throw an error.
Then you can change your concat in your build.js
concat({
src : conf.filesArray,
dest : 'dist/concatenatedFile.js'
});
There you go, now your build script is completely integrated with your app dependencies.