expose-loader: Exposing jQuery not working
In my webpack config I have the following line to expose jQuery on the global scope
{ test: require.resolve('jquery'), loader: 'expose?$!expose?jQuery' },
I have a file that uses the $. For example:
.getScript(`${consumerWebUrl}/scripts/shared/trustlogo.js`)
.done(() => {
document.querySelector('#trustlogo_ph').innerHtml($.trustlogo(`${consumerWebUrl}/content/themes/images/trustlogo.png`, 'SC4', 'none'));
});
However, during my webpack build, get the following error:
error '$' is not defined no-undef
Also, if I put the following directly in my file:
require('expose?$!expose?jQuery!jquery');
I get this error:
error Unable to resolve path to module 'expose?$!expose?jQuery!jquery' import/no-unresolved
According to this page, the above should all work: https://webpack.github.io/docs/shimming-modules.html. I must be missing something. Any advice?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 11
- Comments: 20 (1 by maintainers)
Commits related to this issue
- Fore ReactRouterDOM exposing. // https://github.com/webpack-contrib/expose-loader/issues/20. — committed to turris-cz/reforis by bogdanbodnar 4 years ago
Experiencing the described problem with Webpack 4.
I finally got 2 solutions that resolved my issue: first:
second (as @JohnnyFun mentioned ):
Just spent way too much time figuring this one out, but I finally was able to get jquery exposed properly.
This was always working for me:
import 'expose-loader?jQuery!expose-loader?$!jquery'
But I wanted to move that into my config like so:
This didn’t work though because I was also creating an alias to jquery to point to a minified version:
The alias wasn’t allowing expose-loader to find ‘jquery’, so I removed the alias and now
import 'jquery'
works fine.Although this leads me to a new issue (which I can live without for now)… anybody know how to get an alias working with expose-loader?
@Wapweb this approach is also working for me but I’ve faced with that standard way is not working (or maybe I do something wrong)
but this one is working fine
anyway standard is looking much better from my side, any suggestions how to make standard work ?
fwiw, a kind of hacky way I’ve worked around it in the past was to make a module that explicitly sets jquery on the window:
import Jquery from ‘jquery’; window.$ = Jquery; window.jQuery = Jquery; export default Jquery;
Then just import/require that in your entry file and should be good.
But of course favor the way webpack says to do it. This should only be used if you just want to move on with your life and get jquery plugins to shut up and work.
i have this problem toooooo
this is also OK
@gingray Did you manage to find a solution to your problem, as I have the exact same issue where the
import
syntax works but the loader syntax doesn’t.