Closed simshaun closed 7 years ago
having a very similar problem 👍
@simshaun @stonehz I found that adding this to the webpack.config.js worked for me, or at least I think this is related to your problem but I might be wrong.
// webpack.config.js
module.exports = {
...
resolve: {
alias: {
jquery: "jquery/src/jquery"
}
}
};
Here is the stackoverflow for context https://stackoverflow.com/questions/28969861/managing-jquery-plugin-dependency-in-webpack#28989476
That got me down the right path to get it fixed. Thanks.
module.exports = {
...
resolve: {
alias: {
jquery: path.resolve(__dirname, 'node_modules/jquery/dist/jquery.js'),
},
},
};
The difference is I needed to point to the dist version of jQuery because the src version of jQuery expects "define" to be available. (I have disabled AMD-style loading in Webpack to make some other JS libs behave.)
For anybody that's curious, this config tells Webpack to force your version of jQuery when anything requires it.
Oh, and be careful if supporting Internet Explorer and importing directly from Foundation's src. It uses some ES2015 syntax that you need to make sure is transpiled to ES5. e.g.
module: {
rules: [
{
test: /.js$/,
exclude: {
test: path.resolve(__dirname, 'node_modules'),
exclude: path.resolve(__dirname, 'node_modules/foundation-sites'),
},
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
[
'env',
{
targets: {
browsers: 'ie 10',
},
},
],
],
},
},
],
},
],
}
I was excited to upgrade to 6.4 and shift to importing specific JS modules using Webpack. However, I'm finding that
js/foundation.util.triggers.js
isn't playing nicely because it's importing its own version of jQuery (node_modules/foundation-sites/node_modules/jquery/
).Clicking my button that has a
data-open
attribute is not opening the Reveal modal. Here's the weird part:I can add a few
console.log
lines innode_modules/foundation-sites/js/foundation.util.triggers.js
and see that theTriggers.Listeners.Basic.openListener
is being called when I click my button. I can also see that.triggerHandler('open.zf.trigger' ...)
is being called on the modal element just as it should be. But the modal doesn't open.I've been trying to figure this out for hours now, and from what I can tell, it's because Foundation is using its own instance/version of jQuery. Inspecting the modal element in the browser shows that jQuery has attached its metadata in two separate namespaces (one for my app's jQuery, one for Foundation's jQuery):
If I bind my app's version of jQuery to
window.$
and modify thetriggers
method innode_modules/foundation-sites/js/foundation.util.triggers.js
to callwindow.$
instead of just$
, then the modal does open.How to reproduce this bug:
foundation.js
that I'm importing specific modules with:Set up a modal with
new Reveal($modalEl)
and a button to open it that has thedata-open="idOfModal"
attribute.Click the button.
What should happen:
The Reveal modal should open, but doesn't.
Browser(s) and Device(s) tested on:
Win/Chrome
Foundation Version(s) you are using:
6.4.1
Other things I've tried.
import Foundation from 'foundation-sites';
thennew Foundation.Reveal(...)
. That didn't work.import 'foundation-sites/dist/js/foundation';
thennew Foundation.Reveal(...)
. That didn't work either.Other things affected
This isn't just limited to
data-open
. When the modal is open, the button that has adata-close
attribute won't close the modal either. I can fix that as well by changingTriggers.Listeners.Basic.closeListener
to callwindow.$(this).trigger('close.zf.trigger')
instead of just$(this).trigger('close.zf.trigger')
. Obviously relying on that global is not a good idea, but I don't know how to properly fix this.My temporary solution
If I manually trigger the needed events using my app's jQuery instance, the Reveal opens and closes as it should: