I'm trying to covert to Vite 3 with Vite Ruby (on Rails) from Webpacker & Webpack. One of the main blockers I've noticed is that my system is a CMS. Some of our older customers have some jQuery code directly within <script> tags in <head>. I can't easily change this, and because importing jQuery within Vite and assigning it to window with something like:
import jQuery from 'jquery'
window.$ = window.jQuery = jQuery
happens in a deferred manner with type="module" jquery isn't initialised when the code in the <head> runs. I've resorted to loading jQuery via a CDN <script> tag so that it's loaded synchronously before the <head> code executes. No big deal.
The problem I've encountered from here is when trying to use the jstree jQuery plugin. This is able to be imported, but it seems to automatically load the node_modules jQuery dependency, then mount itself on to that. When then subsequently trying to find $(element).jstree, we're referring to the window copy of jQuery and jstree doesn't exist on that. I can fix this by import $ from 'jquery' just before importing jstree, but then I'm essentially getting the user to download jQuery twice.
What I'd like to do is have the window version of jQuery provided any time a package tries to import 'jQuery'. I've tried rollup's external configuration but that didn't seem to work. Keen to give anything a go really. For now I've resorted to loading jstree from a CDN too but that feels less clean.
I've tried the following config so far:
import { defineConfig } from 'vite'
import RubyPlugin from 'vite-plugin-ruby'
export default defineConfig({
plugins: [
RubyPlugin(),
],
server: {
hmr: {
host: 'vitedevserver.test',
clientPort: 443
}
},
build: {
rollupOptions: {
external: ['jquery'],
output: {
globals: {
jquery: '$'
}
}
}
}
})