Skip to content

Instantly share code, notes, and snippets.

@SlexAxton
Created September 15, 2011 20:30
Show Gist options
  • Save SlexAxton/1220386 to your computer and use it in GitHub Desktop.
Save SlexAxton/1220386 to your computer and use it in GitHub Desktop.
tmpl requirejs plugin
// START Handlebars
var Handlebars = {
precompile: function(){},
compile : function(){},
template : function(){}
};
define( 'Handlebars', [], function(){
return Handlebars;
});
// END Handlebars
define('tmpl',[],function () {
var fs, getXhr,
progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
fetchText = function () {
throw new Error('Environment unsupported.');
},
buildMap = [];
if (typeof window !== "undefined" && window.navigator && window.document) {
// Browser action
getXhr = function () {
//Would love to dump the ActiveX crap in here. Need IE 6 to die first.
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else {
for (i = 0; i < 3; i++) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}
if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
}
if (!xhr) {
throw new Error("getXhr(): XMLHttpRequest not available");
}
return xhr;
};
fetchText = function (url, callback) {
var xhr = getXhr();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
xhr.send(null);
};
} else if (typeof process !== "undefined" &&
process.versions &&
!!process.versions.node) {
//Using special require.nodeRequire, something added by r.js.
fs = require.nodeRequire('fs');
fetchText = function (path, callback) {
callback(fs.readFileSync(path, 'utf8'));
};
}
return {
get: function () {
return Handlebars;
},
write: function (pluginName, name, write) {
if (name in buildMap) {
var text = buildMap[name];
//write.asModule(pluginName + "!" + name, text);
write(text);
}
},
version: '1.0.2beta',
load: function (name, parentRequire, load, config) {
var path = parentRequire.toUrl(name + '.handlebars');
console.log( config );
fetchText(path, function (text) {
// for some reason it doesn't include tmpl _first_ when i don't add it here...
text = "define('tmpl!"+name+"',['tmpl', 'Handlebars'], function( tmpl, Handlebars ){ \n" +
"return Handlebars.template("+Handlebars.precompile(text)+ ");" +
"})";
//Hold on to the transformed text if a build.
if (config.isBuild) {
buildMap[name] = text;
}
//IE with conditional comments on cannot handle the
//sourceURL trick, so skip it if enabled.
/*@if (@_jscript) @else @*/
if (!config.isBuild) {
text += "\r\n//@ sourceURL=" + path;
}
/*@end@*/
load.fromText(name, text);
//Give result to load. Need to wait until the module
//is fully parse, which will happen after this
//execution.
parentRequire([name], function (value) {
load(value);
});
});
}
};
});
Optimizing (standard) CSS file: /Users/alexsexton/Code/photogame/static/build/stylesheets/pg/main.css
Optimizing (standard) CSS file: /Users/alexsexton/Code/photogame/static/build/stylesheets/screen.css
Tracing dependencies for: require-jquery
Tracing dependencies for: main
# I added in a console.log of the 'config' object in the 'load' function.
{ waitSeconds: 7,
baseUrl: '/Users/alexsexton/Code/photogame/static/build/scripts/',
paths: { jquery: 'require-jquery', Handlebars: 'tmpl' },
pkgs: {},
catchError: {},
packagePaths: undefined,
packages: undefined,
appDir: '/Users/alexsexton/Code/photogame/static/dev/',
pragmas: {},
optimize: 'uglify',
optimizeCss: 'standard',
inlineText: true,
isBuild: true,
optimizeAllPluginResources: false,
buildFile: 'dev/scripts/app.build.js',
dir: '/Users/alexsexton/Code/photogame/static/build/',
pragmasOnSave: { excludeCoffeeScript: true },
modules:
[ { name: 'require-jquery',
_sourcePath: '/Users/alexsexton/Code/photogame/static/dev/scripts/require-jquery.js',
_buildPath: '/Users/alexsexton/Code/photogame/static/build/scripts/require-jquery.js',
layer: [Object] },
{ name: 'main',
exclude: [Object],
_sourcePath: '/Users/alexsexton/Code/photogame/static/dev/scripts/main.js',
_buildPath: '/Users/alexsexton/Code/photogame/static/build/scripts/main.js' } ],
dirBaseUrl: '/Users/alexsexton/Code/photogame/static/build/scripts/' }
Tracing dependencies for: jquery
Uglifying file: /Users/alexsexton/Code/photogame/static/build/scripts/_.js
Uglifying file: /Users/alexsexton/Code/photogame/static/build/scripts/app.build.js
Uglifying file: /Users/alexsexton/Code/photogame/static/build/scripts/Backbone.js
Uglifying file: /Users/alexsexton/Code/photogame/static/build/scripts/cs.js
Uglifying file: /Users/alexsexton/Code/photogame/static/build/scripts/main.js
Uglifying file: /Users/alexsexton/Code/photogame/static/build/scripts/r.js
Uglifying file: /Users/alexsexton/Code/photogame/static/build/scripts/require-jquery.js
Uglifying file: /Users/alexsexton/Code/photogame/static/build/scripts/tmpl.js
scripts/require-jquery.js
----------------
scripts/require-jquery.js
scripts/main.js
----------------
tmpl!PG/Template/test
scripts/tmpl.js
scripts/Backbone.js
scripts/_.js
cs!PG/Model/Game
cs!PG/Base
scripts/cs.js
scripts/main.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment