How to use my own version of jQuery with browserified modules

橙三吉。 提交于 2019-12-02 03:51:12

It could be that I'm the only person who ever gets into this situation (Browserify + an asynchronously loaded library that I want to use in all my modules), but I'll share the workaround that I've come up with just in case...

I ended up defining a module that asynchronously loads jQuery and then notifies listeners when it's ready. It's basically a very simple support for asynchronous 'requires'. All of my modules that want to use jQuery then end up with a small bit of boilerplate code like this:

var $; require('./jquery-provider').onLoad(function(jQuery) { $=jQuery; });

It's not perfect but it's simple. And it works because the entry point of my library kicks off my 'jQuery provider' and waits for a ready callback before it calls to all my other modules. So although my modules all get aggressively executed by Browserify as it resolves all the dependencies, none of the functions inside my modules get run until my required library is available and has been passed around to them.

(If this pattern is useful to anyone else, I can share more code.)

in my app.js I have this

var $ = require('jquery')(window);  global.jQuery = require("jquery");

and then use download from npm "plugin" and import de module and execute, like this.

var plugin    = require('plugin');
plugin();

and works fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!