A few days i was making a mistake using <script> on Astro, but affortunatly support ai and people helped me with it, but now I´m fecing a new trouble, I´m using JQuery library and librearies designed on JQuery also, but when I try to use them inside of astro <script> tag i have errors where basic jquery sintax is not recognized, but i would like to say that it works from anyway but vscode is complaining by my code, i know that is TS but how can i resolve this? is there an aproach to handle this? i mean an aproach to use jquery inside of script tag
#JQuery inside of script
73 messages · Page 1 of 1 (latest)
Still waiting for an answer?
It looks like no-one has responded to your question yet. People might not be available right now or don’t know how to answer your question. Want an answer while you wait? Try asking our experimental bot in #1095492539085230272.
I have the same problem 😦 did you have any luck solving this issue?
Bump…
const $ = jQuery;
try this hahaha
It doesn’t work… it doesn’t make JQuery available on the window object
Many plugins look for it on the window object
You can just use a script tag to get it from cdn at runtime ( same as always ) or inject it into window if you're bundling... but note jquery has been substantially replaced by builtins and vanilla libs, so you probably don't need it
I do need it for my use case - our app is 15 years old… I’m migrating it
One of the few legitimate use cases 😄
Yep. It’s a monster - a single file a bazillion lines long. Astro gives us a way to replace it piece by piece with components
How do you inject it into window? I’ve tried from within my script file that I links to - but it doesn’t play ball
You can check if its in window and set it if not
like ```html
<script is:inline>
if(window.$ === undefined) window['$'] = jquery
...
where jquery is your import
That’s what I’ve been doing… only I’ve been pulling the file in as a script tag / link
No - as in from my own script file
That imports jQuery
And imports all the jQuery plugin
hmm, idk if chaining it would work
It was using webpack - I’ve swapped out the webpack stuff for vite
You probably need to import it once and inject it
If your other stuff needs it to be in window
So load as script tag and then inject it? I’ve noticed if i include jQuery in layout it doesn’t filter through - but it does if on the page
Yeah, I would try just importing and adding to window and then having everything else use the window object
I can't remember how well it plays with bundlers since most of it was before they existed
Yep. I might even just build my script and stick it in public
Sounds good, probably would make migration easier too
Should still be able to replace parts of it via components
Yep is there a way I can run build to generate the code and stick it in public when I run dev?
With vite directly, but astro overrides some stuff... I'd probably just set up another config if it doesn't work or not in docs
Yeah, you just can't use the default config name
So I think Astro must be overriding it
Gotcha
So I’d need a to run vite outside of my Astro directory
Gotcha - and use vite to run it?
Yep, just add another script to package.json
But I probably wouldn't go too far down packaging jquery rabbit hole... it works from cdn
So use that to kick off building my dev JS and then tell it to run astro dev
Or copy the files you need to public folder and do script import from there
It’s all the dependencies - hundreds of them… and they need to be built cos someone a few years back started splitting things off for webpack
I mean, I use that to do config for stuff and run it separate from astro
Well, that sounds terrible
good luck though 🙂
The scss files for example - ideally I’d build them and send them to public. It is a monster. It’s stable, battle tested…
But old and zero test coverage
So migrating to astro is an attempt to address that
Once they are built, they should be pretty static though, so I would probably try to keep it separate ( the build process ) from astro
Then bits can be picked off
That’s right
So build - move to public folder, making the paths match the current paths
And it should work as it does now. Then bits can be replaced
Until it’s all replaced 😁
Nobody has touched it - too scared too. I put my hand up - I’m an idiot 😁
Haha
But yeah, I think building them to public folder and importing (not bundle import) from there would probably be easiest transition
And I could use the old webpack to do the build
Then once in astro land, i can use astro components
And anything I replace - I remove from jQuery land
Yeah, thats smarter than switching to vite
So, I built it.. unminified so I can read it… the bundle file is nearly 250,000 lines long
If ever there was a project that needed migration to astro…
Why fix something when you can make people download 10mb of plugins 😄
In this Theme I do have a dependency that is using jQuery as far as I know https://github.com/MicroWebStacks/astro-big-doc/blob/fea2517ce172685f49a0fb5f73a6b48f0bc36990/package.json#L20
I'm not sure if you need to go through bundling yourself and placing in public. Could you maybe share a repo with a minimal example of what you're trying to do and I'll have a look.
Thanks - I’ll need to make one. Will do this tonight
I have kept jQuery outside as a script tag - removed all imports to jQuery, kept imports to jQuery plugins within their own file… it seems to work 😁
And… I’ve managed to get svelte, vue and react components (to prove I can make it work) working with the old code base
Used a nanostore for cross world comms