There are a few caveats with using the washer configuration in RequireJS, referenced at http://requirejs.org/docs/api.html#config-shim . Namely: “Do not mix CDN loading with strip configuration in assembly” when you use the optimizer.
I was looking for a way to use the same jQuery plugin code on sites with and without RequireJS. I found this snippet for jQuery plugins at https://github.com/umdjs/umd/blob/master/jqueryPlugin.js . You port your plugin to this code and it will work anyway.
(function (factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module depending on jQuery. define(['jquery'], factory); } else { // No AMD. Register plugin with global jQuery object. factory(jQuery); } }(function ($) { $.fn.yourjQueryPlugin = function () { // Put your plugin code here }; }));
Credit goes to jrburke; like many javascript, it functions inside functions acting on other functions. But I think I unpacked what he does.
The argument to the factory function in the first line itself is a function that is called to define the plugin in the $ argument. When there is no AMD compatible bootloader, it is called directly to determine the plugin on the jQuery global object. This is exactly the same as the general idiom of a plugin definition:
function($) { $.fn.yourjQueryPlugin = function() {
If there is a module loader, then factory registered as a callback for the loader, which will be called after jQuery is loaded. A downloadable copy of jQuery is an argument. It is equivalent
define(['jquery'], function($) { $.fn.yourjQueryPlugin = function() {
Carl Raymond Aug 09 2018-12-12T00: 00Z
source share