Last active
August 29, 2017 01:33
-
-
Save Offirmo/ec5c7ec9c44377c202f9f8abcacf1061 to your computer and use it in GitHub Desktop.
[Improved UMD template to work with webpack 2] #JavaScript #umd #webpack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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