Skip to content

Instantly share code, notes, and snippets.

@paulredmond
Created August 27, 2010 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulredmond/553987 to your computer and use it in GitHub Desktop.
Save paulredmond/553987 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'Sprockets'
require 'Jsmin'
MINFILE = 'app/webroot/js/build-min.js'
CONCATFILE = 'app/webroot/js/concat.js'
secretary = Sprockets::Secretary.new(
:asset_root => 'app/webroot',
:load_path => ['app/webroot/js', 'plugins/*/vendors/js/**', 'app/plugins/*/vendors/js/**'],
:source_files => ['app/webroot/js/*.js', 'plugins/*/vendors/js/**/*.js', 'app/plugins/*/vendors/js/**/*.js'],
:strip_comments => true
)
# generate the concatenation object
concatenation = secretary.concatenation
# write to disk
gluedFiles = concatenation.to_s
concatenation.save_to(CONCATFILE)
# Run through JSMin
File.open(MINFILE, 'w') { |file| file.write(JSMin.minify(gluedFiles)) }
@paulredmond
Copy link
Author

This is tailored for a CakePHP project, but easily adaptable.

Load all javascript files from the webroot and plugins into one application file--minified and non-minified versions. Could get rather beefy if the project has lots of js. A better approach would be hand-rolled js files that include dependencies as needed that can make up an application javascript file. This script would be easy to adapt to move files to CDN.

@paulredmond
Copy link
Author

Plugin paths to js files was updated in CakePHP 1.3. This example is for CakePHP 1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment