Skip to content
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

Cannot import properly in a vite environment #843

Open
jrochkind opened this issue Oct 26, 2022 · 3 comments
Open

Cannot import properly in a vite environment #843

jrochkind opened this issue Oct 26, 2022 · 3 comments

Comments

@jrochkind
Copy link
Contributor

jrochkind commented Oct 26, 2022

I am in the process of trying to switch from webpack to vite. I am far from an expert in any of this js build tooling stuff, just trying to muddle through, apologies if I'm saying weird things.

Once I switched to vite, import 'devbridge-autocomplete'; no longer is succesfully installing the Autocomplete plugin into JQuery -- the plugin is not actually available.

Trying to debug, it looks like the browserify branch of the load code is being triggered:

} else if (typeof exports === 'object' && typeof require === 'function') {
// Browserify
factory(require('jquery'));

But doesn't actually work for some reason. Although no error appears in console. I am actually not sure why that branch is being triggered, to me it seems typeof require if undefined. But a debugger says somehow that condition is triggered, and does not work.

If the last else branch for "browser globals" is used, then everything works fine for me (because I have specifically set window.$ and window.jQuery for legacy code, I guess).

} else {
// Browser globals
factory(jQuery);

Trying to debug further... I think the built-in initialize code is somehow loading a second copy of jQuery and installing the plugin in that, instead of in my window.jQuery where I want it. But I could be misunderstanding what's going on. And there doesn't seem to be any way to exersize any control over this anyway.

I'm not quite sure what to do to get the plugin to install in this environment. Any ideas welcome.

jrochkind added a commit to sciencehistory/scihist_digicoll that referenced this issue Oct 26, 2022
… released one

We couldn't figure out how to get our vite setup to load it properly otherwise. See #1916 and devbridge/jQuery-Autocomplete#843

We don't love this whole autocomplete implementation, but keep it working for now. Haven't found any alternatives we love. Extensive comments left in source on analysis of tech issues and options.
@tkirda
Copy link
Member

tkirda commented Oct 27, 2022

I think you are right regarding: loading a second copy of jQuery and installing the plugin in that .

If this is bundling in the server side, then it will load jquery via require('jquery') and will use that instance.

@jrochkind
Copy link
Contributor Author

jrochkind commented Oct 27, 2022

Thanks @tkirda -- it's hard to figure out how to avoid that, since in a vite environment require isn't even available in my actual local JS code... it's confusingly somehow available via the build process only in dependencies I guess(?), where the build translates from commonJS require to ES6 import... but in a way that (apparently) results in a different copy of JQuery referred to in the autocomplete require than in my own code when I do an import.

Basically the autocomplete code trying to auto-detect what kind of a build environemnt it's in and where to find the jQuery to add the plugin to -- is failing in a modern environment.

I think the only reasonable fix would be providing some way for the consumer to manually specify the jQuery it wants autocomplete plugin added to, when loading jquery-autocomplete. Right now there is no way to do that, there is just a single JS file which insists on auto-detecting. I'm not myself sure how that could be added in a simple non-disruptive backwards-compat way though without making any significant code changes nobody has appetite for here.

@tkirda
Copy link
Member

tkirda commented Oct 27, 2022

I'm open for suggestions or PR on how this could be improved. This project is not actively maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants