webpack-stable-module-id-and-hash icon indicating copy to clipboard operation
webpack-stable-module-id-and-hash copied to clipboard

Cant access jQuery object after use it.

Open naphystart opened this issue 8 years ago • 15 comments

Can not access global jQuery object after use this plugin, it prompts can not find $,why?

I register jQuery in this way:

new webpack.ProvidePlugin({ 
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            'window.$': 'jquery',
})

And expose to global like:

{
              test: require.resolve('jquery'),
              loader: "expose?$!expose?jQuery",
}

naphystart avatar Nov 10 '16 01:11 naphystart

I use jquery in my project and only use expose-loader is enough for me.

zhenyong avatar Nov 10 '16 01:11 zhenyong

@naphystart Show me a github demo?I may help fix and PR.

zhenyong avatar Nov 10 '16 12:11 zhenyong

I'm sorry, but can you please use English so that others could understand what this issue is about? Or at least have a summary on English in the end of discussion.

NekR avatar Nov 10 '16 12:11 NekR

@NekR You are right !

zhenyong avatar Nov 10 '16 12:11 zhenyong

@NekR Sorry about that. The issue is about jquery not found when I use ProvidePlugin to require jquery. @zhenyong I am not around with my computer now and I will add a demo tomorrow.

naphystart avatar Nov 10 '16 13:11 naphystart

@naphystart No problem at all! Thanks for sharing the info 👍

NekR avatar Nov 10 '16 13:11 NekR

I register jQuery in this way: And expose to global like:

I'm not sure it's a right way to use ProvidePlugin and expose-loader together. As @zhenyong said, if you need it global by some reason, then just use expose-loader. If not -- ProvidePlugin might be enough.

@naphystart do you use those both for better minification or there is some other reason?

NekR avatar Nov 10 '16 13:11 NekR

@NekR expose-loader is necessary while ProvidePlugin is not, but ProvidePlugin will require jquery automatically that you don't need to make a jquery declaration in your code, so it's just for convenient and it does.

naphystart avatar Nov 10 '16 13:11 naphystart

@naphystart I see. I'm not saying you shouldn't use it. Just making some notes here :-)

So another note: if you use expose-loader then jQuery is already global and you don't need to include it every module. I mean, if you will remove that ProvidePlugin from your project -- everything should still work.

NekR avatar Nov 10 '16 14:11 NekR

@NekR ...actually I don't think in that way, I know it's a note or more likely an advise, maybe I don't express myself well

It's that true? In my project, when I remove ProvidePlugin and not adding require('jquery')in entry.js, it will prompts can not find $. I think I should look up more information about that. Thanks for your remind :-)

naphystart avatar Nov 10 '16 14:11 naphystart

@naphystart I think I know that's the issue there. When you use expose-loader you still have to include those files manually because I don't know what you expose. The simplest way is probably by just doing this in your entry file (once):

require('expose-loader?$!expose-loader?jQuery!jquery');

NekR avatar Nov 10 '16 14:11 NekR

@NekR My project is multiple pages application, there are lots of entry files, I have to add that require in each files manually, and I think that is the same as require('jquery'). So I use ProvidePlugin to add it automatically.

naphystart avatar Nov 10 '16 14:11 naphystart

@naphystart by multipage you mean that you load it with require.ensure or something like that? Or you built completely standalone entries with webpack runtime to each page? If it's the last case, then yeah, provide plugin makes sense.

NekR avatar Nov 10 '16 15:11 NekR

@naphystart

I am trying to read source codes in webpack and expose-loader to find out the reasons, can not fix your situation right now, so sorry. #4

zhenyong avatar Dec 07 '16 04:12 zhenyong

@zhenyong It's fine, it still works great without ProvidePlugin : )

naphystart avatar Dec 08 '16 01:12 naphystart