Added build option to wrap in UMD declaration. issue #34

This commit is contained in:
Jasper Palfree 2014-02-08 12:57:40 -05:00
parent f36a8273a1
commit 8890b30685
3 changed files with 3662 additions and 3621 deletions

File diff suppressed because one or more lines are too long

View File

@ -4,5 +4,8 @@ require('./builder.js').build({
"out": "../build/dat.gui.js", "out": "../build/dat.gui.js",
"minify": false, "minify": false,
"shortcut": "dat.GUI", "shortcut": "dat.GUI",
"paths": {} "paths": {},
}); "umd": {
ret: 'dat'
}
});

View File

@ -110,6 +110,10 @@ function build(_params) {
to_write += params.shortcut + ' = ' + params.main.replace(/\//g, '.') + ' = ' + defined[params.main].getClosure() + ';'; to_write += params.shortcut + ' = ' + params.main.replace(/\//g, '.') + ' = ' + defined[params.main].getClosure() + ';';
if ( params.umd ){
to_write = wrap_umd( params.shortcut.split('.')[0], to_write + '\nreturn '+params.umd.ret+';' );
}
if (params.verbose) console.log('Exported: ' + params.main + ' to window.' + params.shortcut); if (params.verbose) console.log('Exported: ' + params.main + ' to window.' + params.shortcut);
if (params.minify) { if (params.minify) {
@ -226,6 +230,29 @@ function define(deps, callback) {
} }
/**
* Wrap the source code in a UMD declaration
* @param {String} name The root namespace (for browser global definition)
* @param {String} src Source code text
* @return {String}
*/
function wrap_umd( name, src ){
return [
"(function (root, factory) {",
" if (typeof define === 'function' && define.amd) {",
" // AMD. Register as an anonymous module.",
" define(factory);",
" } else {",
" // Browser globals",
" root."+ name +" = factory();",
" }",
"}(this, function () {",
src.replace(/\n/g,'\n '),
"}));\n"
].join('\n');
}
function file_exists(path) { function file_exists(path) {
try { try {
var stats = fs.lstatSync(path) var stats = fs.lstatSync(path)