Skip to content

Instantly share code, notes, and snippets.

@Offirmo
Last active August 29, 2017 01:33
Show Gist options
  • Select an option

  • Save Offirmo/ec5c7ec9c44377c202f9f8abcacf1061 to your computer and use it in GitHub Desktop.

Select an option

Save Offirmo/ec5c7ec9c44377c202f9f8abcacf1061 to your computer and use it in GitHub Desktop.
[Improved UMD template to work with webpack 2] #JavaScript #umd #webpack
// Iterating on the UMD template from here:
// https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js
// But experimentally improving it so that it works for webpack 2
// UMD template from https://gist.github.com/Offirmo/ec5c7ec9c44377c202f9f8abcacf1061#file-umd-js
(function (root, factory) {
var LIB_NAME = 'Foo'
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(function () {
return (root
? root[LIB_NAME] = factory()
: factory() // root is not defined in webpack 2, but this works
)
});
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory()
} else if (root) {
// Browser globals
root[LIB_NAME] = factory()
} else {
throw new Error('From UMD wrapper of lib "' + LIB_NAME + '": unexpected env, cannot expose my content!')
}
}(this, function () {
"use strict";
return {
...
}
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment