Created
April 27, 2013 16:24
-
-
Save MalucoMarinero/5473658 to your computer and use it in GitHub Desktop.
Create a javascript loader for your application, that won't require you to change the html come deployment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require 'fs' | |
path = require 'path' | |
mm = require 'minimatch' | |
wrench = require 'wrench' | |
coffee = require 'coffee-script' | |
module.exports = (grunt) -> | |
grunt.initConfig { | |
loader: | |
desktopClient: | |
srcDir: 'www/clients/desktop/js' | |
dest: 'www/clients/desktop/js/loader.js' | |
priorityMatches: ['**/app.js', '**/module.js'] | |
ignorePattern: 'loader.js' | |
prefix: 'js/' | |
varName: 'pogoDesktopLoader' | |
} | |
grunt.registerMultiTask 'loader', 'Create a script loader from a file list', -> | |
options = @options() | |
done = @async() | |
@files.forEach (f) -> | |
console.log "Compiling loader for #{ f.srcDir } to #{ f.dest }" | |
dest = f.dest | |
loaderCode = "window.#{ f.varName } = (cb) -> head.js " | |
loaderPaths = [] | |
files = wrench.readdirSyncRecursive(f.srcDir).filter mm.filter "**/*.js" | |
files.forEach (filePath) -> | |
if not mm(filePath, f.ignorePattern) | |
loaderPaths.push f.prefix + filePath | |
rankPath = (path) -> | |
i = 0 | |
for priorityMatch in f.priorityMatches | |
if mm(path, priorityMatch) | |
return i | |
i += 1 | |
return i | |
loaderPaths.sort (a, b) -> | |
if rankPath(a) < rankPath(b) then return -1 | |
if rankPath(a) > rankPath(b) then return 1 | |
return 0 | |
loaderPaths = loaderPaths.map (path) -> '"' + path + '"' | |
loaderCode += loaderPaths.join ', ' | |
loaderCode += ', -> cb()' | |
loaderJS = coffee.compile loaderCode, bare: true | |
fs.writeFile dest, loaderJS, (err) -> | |
if err then console.log err | |
done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment