-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Some dependencies that rely on window don't work #333
Comments
We're seeing a lot of this, too, trying to get some legacy code working with Parcel |
This seems like a valid use case for a |
some of my problems here seem to go away by using jquery off of CDN instead of bundling in a node package. Probably more performant that way, anyways... Still investigating to see if that fixes everything. |
Sometimes corporate policy requires self hosting all dependencies
|
Hi. I tried using a CDN, a local link, and node modules, nothing works. This is a very simple HTML that I used to test.
|
@DeMoorJasper @brandon93s I'm not sure how |
@benhutton parcelignore could possibly just copy over the files without any processing making this exactly the same as using a cdn for example and hopefully mounting to the window correctly. |
@DeMoorJasper so I would still somehow be able to leverage jquery from npm? |
Yes I would be pretty happy if it can just bundle our code without touching the 3rd party libraries. |
@benhutton is that not currently possible? If u approach jQuery as a node_module it should work just fine, because it never has the need to mount to the window? something like const $ = require('jQuery');
$("#somethin").... |
Also another way to fix this would be to detect how many packages are being bundled and just leave sole packages/libraries without imports alone. Like discussed in another issue the proper end-game for bundling would be to remove the need of the prelude entirely, so that code is just bundled in a smaller package and doesn't perform any unexpected behaviour. |
unfortunately, |
@benhutton Since bootstrap (at least the self downloaded one) seems to require jQuery on the window scope, and fails if not found, I'm not sure how to solve that multi-dependency. |
Like @benhutton I have also been stymied by the fact that |
same problem, how to use jquery with parcel? need help |
This is because jQuery disables setting itself on the window global in a CommonJS environment (which Parcel is). See https://github.com/jquery/jquery/blob/master/src/wrapper.js#L29 and https://github.com/jquery/jquery/blob/2d4f53416e5f74fa98e0c1d66b6f3c285a12f0ce/src/exports/global.js#L30-L32. If you need it to be accessible on window, you could do something like |
I'm using TypeScript but this did expose '$' to 'window' for jquery-ui etc to consume it.
I have a question, but how can I access any of the classes being declared in the bundled JS without individually exporting them like above? I found that they're listed in 'window.require.cache[N].exports.default' where N is some number I haven't specified but is there a way to access those by class names? How am I supposed to access class methods in the bundled JS from within inline HTML such as through 'onclick' properties? Currently I'm exporting every classes that need exposed the same way as jQuery above which is bit of a work since I can't iterate over a directory in the entry point script but have to manually list them all (which needs updating from time to time). |
Hitting this when using EaselJS 1.x versions.
complains |
I'm not sure what we should do about this. The cause of this issue is that jQuery relies on Perhaps we could make it so that if you imported a script from HTML, it would retain |
I solved this by adding this code
|
2 and a half years and nobody posted at least a workaround? This worked for me in a project that uses bootstrap and jquery. In my app.js (entry point for parcel): import jQuery from "jquery";
window.$ = window.jQuery = jQuery; // workaround for https://github.com/parcel-bundler/parcel/issues/333
import 'bootstrap/dist/js/bootstrap.bundle'; Import order matters. Couldn't get it to work with the normal bootstrap js include because it couldn't resolve the dependency for Popper, even though I'm not using tooltips or popper anywhere. |
So only workaround? I think gulp doesn't have this issue. |
@taducquang gulp isn't a bundler. |
The vendored jquery version was 1.9.1 from 2013-02-04. Let's replace it with the most recent one from the 1.x branch (1.12.4 from 2016-05-20). The modification in rjquery.js is needed because recent jQuery versions changed their behaviour, and do not set themselves on the global window object. See: parcel-bundler/parcel#333 (comment) This will be the lastest jQuery 1.x version ever, because 1.x branch is definitively EOLed (see jquery/jquery.com#162). This is a stopgap measure to get the latest security fixes. Going forward, another strategy will be needed. Closes #3640
The workaround from @sundayz import jQuery from "jquery";
window.$ = window.jQuery = jQuery;
import "lib-that-depends-on-jQuery"; didn't work for me, since the library that depends on jQuery does so in a sync way, and because parcel ends up generating "use strict";
var _ = _interopRequireDefault(require("jquery"));
require("lib-that-depends-on-jQuery");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
window.$ = window.jQuery = _.default; assigning jQuery on window happens too late. Combining the workaround with the hint from @devongovett does the trick for me: const jQuery = require("jquery")
window.$ = window.jQuery = jQuery;
require("lib-that-depends-on-jQuery") since parcel generates in that case var jQuery = require("jquery");
window.$ = window.jQuery = jQuery;
require("lib-that-depends-on-jQuery"); |
var jQuery = require("jquery");
window.$ = window.jQuery = jQuery;
require("lib-that-depends-on-jQuery"); This works for me if However .. select2 extends jQuery functionality, but the "upgraded" jQuery instance is not available. That is: the jQuery instance is not mutated (to be upgraded) Does someone have this working with a library that extends jQuery ?? |
My suspicion is that another library can make use of jQuery through 3 methods:
As example of 3 const jQuery_tmp = require('cash-dom/dist/cash.min.js'); // using jQuery compatible library
const $ = require('select2/dist/js/select2.js')(jQuery_tmp); // Let the plugin do it's thing |
@flip111 That's all fine and good if you are the library owner. If you are just importing something like Bootstrap, however, you as a dev don't really have control over how a dependency like jQuery is looked for. |
@mix3d you shouldn't take this too literally. Replace "another library" by "your app". You see here that if you include bootstrap, including jQuery is a separate step that is not within the code of bootstrap itself. This is the same if you are using import statements. If you have code that is going "to look for jQuery", you should select the method of making jQuery available in such a way that it corresponds to the ways the code is looking for jQuery. |
This worked for me: |
BUG:
An older website that imports jQuery and Bootstrap from locally downloaded files don't have their dependencies properly running; Bootstrap complains that
jQuery
and$
are not found. It seems that either the order from the HTML is not respected, or that JS that installs to the rootwindow
are not working correctly.🎛 Configuration (.babelrc, package.json, cli command)
$> parcel index.html
🤔 Expected Behavior
Imported scripts run correctly
😯 Current Behavior
Bootstrap complains about not finding jquery. Not sure if import order or
window
problem🔦 Context
Can't use Parcel if this doesn't work for older / retrofit websites.
🌍 Your Environment
The text was updated successfully, but these errors were encountered: