-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
templates/jqueryPlugin.js doesn't seem to conform to CommonJS spec #87
Comments
@malthejorgensen - no, it's fine, there are both
Real problem with UMD for jQuery plugin and CommonJSThere is different problem with CommonJS and current if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = function(root, jQuery) {
// ...
};
} So when you do plugin(); // or ...
plugin(window); // or ...
plugin(window, $); // or ...
plugin(null, $); Looks complicated. I don't understand - why not to do simply: if (typeof module === 'object' && module.exports) {
// Node/CommonJS
var jQuery = require('jquery');
factory(jQuery);
module.exports = jQuery;
} But the problem described above is not a bug - just do |
Hi @kottenator By that logic CommonJS and Node.js-modules are the same – I don't think that is true – have you read the CommonJS spec – it doesn't mention I think the reality is that Node.js-modules added Maybe the "CommonJS" label should be removed (and replaced with "Node.js-compatible") from the UMD templates? |
Agree, I don't think it's a problem to use Currently it is: // my-plugin.js
module.exports = function(root, jQuery) {
// ...
factory(jQuery);
return jQuery;
}
// your ES5 code
require('my-plugin')();
// your ES6 code
import plugin from 'my-plugin';
plugin(); With your idea it'd become: // my-plugin.js
exports.init = function(root, jQuery) {
// ...
factory(jQuery);
return jQuery;
}
// your ES5 code
require('my-plugin').init();
// your ES6 code
import {init} from 'my-plugin';
init(); What I suggest is: // my-plugin.js
var jQuery = require('jquery');
factory(jQuery);
module.exports = jQuery; // actually, it's not necessary to export anything
// your ES5 code
require('my-plugin');
// your ES6 code
import 'my-plugin'; // or import $ from 'my-plugin'; |
Hey guys I faced that problem yesterday what I did is this: ;(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory)
} else if (typeof exports !== 'undefined') {
module.exports = factory(require('jquery'))
} else {
factory(jQuery)
}
}(function ($) {
// All the normal plugin here
})) Insert your normal |
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 #4557. This closes #4558.
All, I've run into an issue with this template in Select2 (see select2/select2@45a8773#commitcomment-24593031). Any guidance would be appreciated, as Select2 is a fairly large community and this issue could potentially be affecting a lot of users. |
Not sure if I'm looking at the right CommonJS spec (the commonjs.org-site is a bit confusing), but in the current jQuery plugin template (jqueryPlugin.js, line 7)
module.exports
is used instead of the CommonJS standardexports
.You can compare this with commonjsStrict, line 23, which does check and use the
exports
-object – notmodule.exports
.I think
jqueryPlugin.js
should be changed to use theexports
-object.Note: Both files were checked at commit 95563fd (latest master as of 23rd Dec 2016)
The text was updated successfully, but these errors were encountered: