umd-wrap
Wrap a block of code into a UMD bundle, with the option to define external dependencies.
The resulting bundle can be used with Node, Browserify, RequireJS, or simply via the window
object.
Install
$ npm install umd-wrap
Usage
// make-bundle.jsvar wrap = ; var options = code: '(function() { console.log(\'hello world\'); });' exports: 'robot' dependencies: name: 'lodash' exports: '_' name: 'moment' ; ;
$ node make-bundle > bundle.js
// output { if typeof exports === 'object' moduleexports = ; else if typeof define === 'function' && defineamd ; else root'robot' = ; }this { { return 'lodash': _ 'moment': momentname; } var _bundleExports = { console; }; return _bundleExports;};
Options
code: '(function() { console.log(\'hello world\'); });' // Block of code to wrap // Needs to return what needs to be exported, // and be assignable to a variable (hint: wrap it in parentheses) exports: 'robot' // Name to expose on the `window` object // Supports namespacing with dot notation dependencies: name: 'lodash' exports: '_' name: 'moment' // Optional list of external dependencies required in wrapped block of code // If one exposes a different name on the `window` object, use `exports` quoteChar: '\'' // Quote character used in your `require()` statements // Default: (') headTemplate: path // Lo-Dash template used to create the UMD bundle head // Change only if you need a custom one tailTemplate: path // Lo-Dash template used to create the UMD bundle tail // Change only if you need a custom one requireDepFunctionName: '_requireDep' // Name of function used to require external dependencies // Override only if you are already using that name bundleExportsVariableName: '_bundleExports' // Name of variable used to return bundle exports // Override only if you are already using that name
Use with pure-cjs
See https//github.com/nicolashery/cjs-umd.
Use with Browserify
(Hack, could be automated)
$ npm install -g browserify
Go through your source files (you can make a copy of the whole tree), and replace any external dependency require()
statements with _requireDep()
, for example:
var _ = ;// becomes...var _ = ;
Create a Browserify bundle, exporting your main module:
$ browserify -r ./index.js:robot index.js > robot.js
It will look something like this:
require={ // a bunch of code...}{}"H99CHA"
Remove the require=
, and call the whole block with your module name, for example:
{ // a bunch of code...}{}"H99CHA"'robot';
Replace every require
with _require
(this is to be able to use this bundle into another Browserified source):
$ sed "s/require/_require/g" robot.js > robot.js# We replaced our `_requireDep` too, so change them back $ sed "s/__requireDep/_requireDep/g" robot.js > robot.js
Wrap the resulting block of code with umd-wrap
.
Contributors
Thank you for your contributions:
License
MIT