The (Java|Coffee)Script and (CSS|Less) (Builder|Bundler|Packer|Minifier|Merger|Checker)
The (Java|Coffee)Script and (CSS|Less) (Builder|Bundler|Packer|Minifier|Merger|Checker)
Install dependencies for image compression
On OSX
ruby -e "$(curl -fsSLk https://gist.github.com/raw/323731/install_homebrew.rb)"
brew install gifsicle libjpeg optipng pngcrush
On Apt Linux
sudo apt-get update && sudo apt-get install gifsicle libjpeg-progs optipng pngcrush
On Yum Linux
sudo yum -y install gifsicle libjpeg-progs optipng pngcrush
Windows
Hahahahaha
Before you use Buildr, you must specify some configuration for it. The available configuration is:
{
# Options
name: null # (name to be outputted in log messages) String or null
log: true # (log status updates to console?) true or false
watch: false # (automatically rebuild on file change?) true or false
# Handlers
buildHandler: false # (fired when build completed) function or false
rebuildHandler: false # (fired when rebuild completed) function or false
successHandler: false # (fired when (re)build completed successfully) function or false
# Paths
srcPath: false # String
outPath: false # String or false
# Checking
checkScripts: true # Array or true or false
checkStyles: true # Array or true or false
jshintOptions: false # Object or false
csslintOptions: false # Object or false
# Compression (requires outPath)
compressScripts: true # Array or true or false
compressStyles: true # Array or true or false
compressImages: true # Array or true or false
# Order
scriptsOrder: false # Array or false
stylesOrder: false # Array or false
# Bundling (requires Order)
bundleScriptPath: false # String or false
bundleStylePath: false # String or false
deleteBundledFiles: true # (requires outPath) true or false
# Loaders (requires Order)
srcLoaderHeader: false # String or false
srcLoaderPath: false # String or false
}
The above values are the default values for those options. The settings which are set to true
will auto-detect the files for you.
There are currently two options available, the log
and watch
options.
log
option when enabled will output all status messages, by default this is enabled.watch
option when enabled will allow buildr to run in the background watching for changes in our srcPath
, if a change is detected then our project is automatically rebuilt for us, by default this is disabled.There are two handlers you can configure, they are the buildHandler
and the rebuildHandler
.
buildHandler
is fired after our project has been built.rebuildHandler
is fired after our project has been rebuilt. Our project is rebuilt when we utilise the watch: true
config option, which scans for changes in the background and automatically rebuilds our project on change. If this isn’t specified, then the buildHandler
will automatically be used as the rebuildHandler
.They are both passed a single argument called err
which is either an Error
instance, or false
if no error occurred. They both also have default values, so you don’t need to specify them if you don’t want to.
To pass your scripts through jshint and your styles through csslint, you’d want the following configuration:
{
# Paths
srcPath: 'src' # String
# Checking
checkScripts: true # Array or true or false
checkStyles: true # Array or true or false
jshintOptions: false # Object or false
csslintOptions: false # Object or false
}
To copy your src
directory to an out
directory, then compile and compress all your styles and scripts in the out
directory, you’d want the following configuration:
{
# Paths
srcPath: 'src' # String
outPath: 'out' # String or false
# Compression (without outPath only the generated bundle files are compressed)
compressScripts: true # Array or true or false
compressStyles: true # Array or true or false
compressImages: true # Array or true or false
}
If your outPath
is the same as your srcPath
then the only files which will be compressed are the generated bundle files.
To bundle all your style files into one file called out/bundled.css
and all your script files into one file called out/bundled.js
, you’d want the following configuration:
{
# Paths
srcPath: 'src' # String
outPath: 'out' # String or false
# Order
scriptsOrder: [
'script1.js'
'script2.coffee'
] # Array or false
stylesOrder: [
'style1.css'
'style2.less'
] # Array or false
# Bundling (requires Order)
bundleScriptPath: false # String or false
bundleStylePath: false # String or false
deleteBundledFiles: true # (requires outPath) true or false
}
To generate a source loader file called src/loader.js
which will load in all your source styles and scripts into the page, you can use the following:
{
# Paths
srcPath: 'src' # String
# Order
scriptsOrder: [
'script1.js'
'script2.coffee'
] # Array or false
stylesOrder: [
'style1.css'
'style2.less'
] # Array or false
# Loaders (requires Order)
srcLoaderHeader: '''
# Prepare
myprojectEl = document.getElementById('myproject-include')
myprojectBaseUrl = myprojectEl.src.replace(/\\?.*$/,'').replace(/loader\\.js$/, '').replace(/\\/+$/, '')+'/'
# Load in with Buildr
myprojectBuildr = new window.Buildr {
baseUrl: myprojectBaseUrl
beforeEl: myprojectEl
serverCompilation: window.serverCompilation or false
scripts: scripts
styles: styles
}
myprojectBuildr.load()
''' # note, all \ in this are escaped due to it being in a string
srcLoaderPath: 'src/myproject.loader.js' # String or false
}
Then include into your page with the following html:
<script id="myproject-include" src="../../loader.js"></script>
This is incredibly useful for developing apps which have lots of files, as instead of updating all your demo page’s html with the new script and style files all the time, you just include the loader.
You can feel free to combine any of the configurations above to get something which checks, compiles, compresses, bundles, and generates loaders too. Though compression and bundling is dependent on having an outPath
which is different from your srcPath
.
Within your application folder
Install Buildr Globally
npm -g install buildr
Stick your configuration in buildr.cson
Run the global buildr
buildr
You may specify the filename for configuring by passing -f
Within your application folder
Install Buildr Locally
npm install buildr
Code buildr.coffee
buildr = require 'buildr'
config = {} # your configuration
myBuildr = buildr.createInstance(config)
myBuildr.process (err) ->
throw err if err
console.log 'Building completed'
Run your buildr file
coffee buildr.coffee
Licensed under the MIT License
Copyright 2011 Benjamin Arthur Lupton
v0.8.7 March 24, 2013
v0.8.6 March 17, 2013
v0.8.5 November 11, 2012
v0.8.4 November 5, 2012
v0.8.3 October 28, 2012
v0.8 September 27, 2011
v0.7 August 22, 2011
watch
, buildHandler
and rebuildHandler
optionsv0.6 July 21, 2011
v0.5 July 9, 2011
v0.4 July 1, 2011
v0.3 May 31, 2011
v0.2 April 2, 2011
v0.1 March 23, 2011