From 45a877345482956021161203ac789c25f40a7d5e Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 29 Aug 2016 14:40:04 -0400 Subject: [PATCH] Fix UMD commonjs support This is based on the UMDJS jQuery template available at https://github.com/umdjs/umd/blob/95563fd6b46f06bda0af143ff67292e7f6ede6b7/templates/jqueryPlugin.j://github.com/umdjs/umd/blob/95563fd6b46f06bda0af143ff67292e7f6ede6b7/templates/jqueryPlugin.js This closes https://github.com/select2/select2/issues/4557. This closes https://github.com/select2/select2/pull/4558. --- src/js/wrapper.start.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/js/wrapper.start.js b/src/js/wrapper.start.js index 76caabaf..4df0e054 100644 --- a/src/js/wrapper.start.js +++ b/src/js/wrapper.start.js @@ -9,15 +9,30 @@ if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); - } else if (typeof exports === 'object') { + } else if (typeof module === 'object' && module.exports) { // Node/CommonJS - factory(require('jquery')); + module.exports = function (root, jQuery) { + if (jQuery === undefined) { + // require('jQuery') returns a factory that requires window to + // build a jQuery instance, we normalize how we use modules + // that require this pattern but the window provided is a noop + // if it's defined (how jquery works) + if (typeof window !== 'undefined') { + jQuery = require('jquery'); + } + else { + jQuery = require('jquery')(root); + } + } + factory(jQuery); + return jQuery; + }; } else { // Browser globals factory(jQuery); } -}(function (jQuery) { +} (function (jQuery) { // This is needed so we can catch the AMD loader configuration and use it // The inner file should be wrapped (by `banner.start.js`) in a function that // returns the AMD loader references. - var S2 = + var S2 = \ No newline at end of file