Skip to content

Instantly share code, notes, and snippets.

@ramesaliyev
Last active February 11, 2024 22:28
Show Gist options
  • Save ramesaliyev/6bb477ee5f42615ec8e7 to your computer and use it in GitHub Desktop.
Save ramesaliyev/6bb477ee5f42615ec8e7 to your computer and use it in GitHub Desktop.
Grunt UMD returnExportsGlobal Template
{{!--
Uses Node, AMD or browser globals to create a module. This example creates
a global even when AMD is used. This is useful if you have some scripts
that are loaded by an AMD loader, but they still want access to globals.
If you do not need to export a global for the AMD case,
see returnExports.js.
If you want something that will work in other stricter CommonJS environments,
or if you need to create a circular dependency, see commonJsStrictGlobal.js
Defines a module "returnExportsGlobal" that depends another module called
"b". Note that the name of the module is implied by the file name. It is
best if the file name and the exported global have matching names.
If the 'b' module also uses this type of boilerplate, then
in the browser, it will create a global .b that is used below.
--}}
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
{{! AMD. Register as an anonymous module. }}
define({{#if amdModuleId}}'{{amdModuleId}}', {{/if}}[{{{amdDependencies}}}], function ({{dependencies}}) {
return ({{#if globalAlias}} root['{{{globalAlias}}}'] = {{/if}}factory({{dependencies}}));
});
} else if (typeof exports === 'object') {
{{!--
Node. Does not work with strict CommonJS, but
only CommonJS-like enviroments that support module.exports,
like Node.
--}}
var module_exports = factory({{{cjsDependencies}}});
module.exports = module_exports;
{{! FIX FOR BROWSERIFY: Set global alias if we in browserify. }}
{{#if globalAlias}}
if(typeof window !== "undefined"){
window['{{{globalAlias}}}'] = module_exports;
}
{{/if}}
} else {
{{! Browser globals }}
{{#if globalAlias}}root['{{{globalAlias}}}'] = {{/if}}factory({{{globalDependencies}}});
}
}(this, function ({{dependencies}}) {
{{{code}}}
{{!--
Just return a value to define the module export.
This example returns an object, but the module
can return a function as the exported value.
--}}
return {{{objectToExport}}};
}));
module.exports = function(grunt){
"use strict";
grunt.config("umd", {
global: {
src : "src/file.js",
dest : "dist/file.js",
template : "returnExportsGlobal.hbs",
objectToExport : "__",
globalAlias : "__",
indent : " ",
deps : {
args : ["_"],
"default" : ["_"],
amd : {
indent : " ",
items : ["lodash"],
prefix : "\"",
separator : ",\n",
suffix : "\""
},
cjs : {
indent : " ",
items : ["lodash"],
prefix : "require(\"",
separator : ",\n",
suffix : "\")"
},
global : {
items: ["_"],
prefix: ""
}
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment