-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REGRESSION] Calling $(document).foundation() fails in production builds #11586
Comments
Hi @josephcsible 👋, Could you tell me:
|
It doesn't work with either of those versions. |
Ok, thank you. I think it comes from #11445 and the fact that Foundation is not imported because it is not used directly, but I'll do some checks tonight to verify that. |
For now you can try the following: import $ from 'jquery';
(...)
// Foundation JS relies on a global varaible.
// In ES6, all imports are hoisted to the top of the file
// so if we used `import` to import Foundation, it would
// execute earlier than we have assigned the global
// variable. This is why we have to use CommonJS require()
// here since it doesn't have the hoisting behavior.
window.jQuery = $;
require('foundation-sites'); This will also force your build to import Foundation, because there is no tree shaking with CommonJS. |
Note to myself: A lot of people are using Foundation via its jQuery integration even in a ESM environment, thus rely on Foundation side-effects (on jQuery). This could be a reason not to consider Foundation as without side-effects and to remove Unless we find a better way to implement that, a true tree shaking support will require some changes in the way Foundation can be imported, initialized and used. |
Is there an alternative to that? It's the only way I've really seen documented. |
Well it is possible to manually initialize the DOM: Foundation.reflow($(document)); Or to manually instantiate plugins: var accordion = new Foundation.Accordion($('#some-accordion'), options); https://foundation.zurb.com/sites/docs/javascript.html But I guess this is not documented enough. |
I keep this issue open because there is at least the documentation to update. |
Despite the previous attempts to make Foundation compatible with tree shaking (foundation#11445, foundation#11497 and foundation#11508), Foundation is used with side-effects (on jQuery) and cannot be considered as "without side-effects" for tree-shaking purposes. This commit remove the "sideEffects: false" label and thus disable tree-shaking support, until the Foundation API and usage is changed to not implicitely rely on the global jQuery object. Closes foundation#11586 Related to: - foundation#11508 - foundation#11497 - foundation#11445
Despite the previous attempts to make Foundation compatible with tree shaking (foundation#11445, foundation#11497 and foundation#11508), Foundation is used with side-effects (on jQuery) and cannot be considered as "without side-effects" for tree-shaking purposes. This commit remove the "sideEffects: false" label and thus disable tree-shaking support, until the Foundation API and usage is changed to not implicitely rely on the global jQuery object. Closes foundation#11586 Related to: - foundation#11508 - foundation#11497 - foundation#11445
chore: temporary disable tree-shaking #11586
What should happen?
Calling
$(document).foundation()
in production React app builds should work, as it did in Foundation 6.5.0-rc.2.What happens instead?
Calling
$(document).foundation()
in production React app builds results in an error likeTypeError: w(...)(...).foundation is not a function
.Test Case and/or Steps to Reproduce (for bugs)
How to reproduce:
npx create-react-app my-app
cd my-app
yarn add jquery foundation-sites@6.5.0
import 'foundation-sites';
,import $ from 'jquery';
, and$(document).foundation();
tosrc/index.js
yarn build
yarn global add serve
serve -s build
Context
I’d like to be able to use the latest version of Foundation in a production React app. This is a regression, as my reproduction steps don't cause the error with Foundation 6.5.0-rc.2. Also, the error only occurs in production builds, not in development (which you can see by doing
yarn start
in place of steps 5, 6, and 7, and then going to http://localhost:3000).Your Environment
node:10-alpine
Checklist
The text was updated successfully, but these errors were encountered: