webpack-stable-module-id-and-hash
webpack-stable-module-id-and-hash copied to clipboard
Cant access jQuery object after use it.
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",
}
I use jquery in my project and only use expose-loader is enough for me.
@naphystart Show me a github demo?I may help fix and PR.
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 You are right !
@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 No problem at all! Thanks for sharing the info 👍
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 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 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 ...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 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 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 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.
@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 It's fine, it still works great without ProvidePlugin : )