Solid as a global variable
I have several small SolidJS JavaScript applications in the same webpage. Currently, each application bundle contains an copy of the SolidJs framework. I would like to use the framework more like with jQuery: is it possible to put the SolidJS framework in a global variable, and then all the SolidJS applications rely on the same global variable?
10 Replies
Make the following modifications to your
vite.config.ts
:
You also need to add resolveJsonModule
to the ts.config.json
for the JSON import to work.
The idea is to separate the Solid chunk into a separate vendor bundle while the "app bits" go into their own asset bundle.
Now the page can load the Solid bundle separately to be reused by the various asset bundles.
Of course all the asset bundles have to use the same version of SolidJS.
Thank you! I'm going to try that.
So, each application will generate a distinct
dist/[email protected]
file, but all these files are equal/interchangeable, then I can serve only one?
I'm definitely going to try that. Thanks a lot!all these files are equal/interchangeable, then I can serve only one?Turns out, there's one wrinkle; even
manualChunks
bundles are subject to treeshaking.
So when generating the [email protected]
bundle to be shared I'd temporarily set:
From tree-shaken index-hash.js
:
From NON-treeshaken index-hash.js
:
Rollup seems to generate stable deterministic names (not so for the order) for the exports (and it also bothers to remap those names to internally more volatile ones) so this could work.
Plans are useless but planning is indispensible.Dwight D. Eisenhower In that spirit here is plan Z: Get the ESM modules from a CDN: Configure Vite/Rollup to ignore
solid-js
Build
Patch asset to use the CDN exports
it's probably better to combine alias + external
module federation also comes to mind if you're willing to go to that effort
Doesn't work (the names are aliased in the output but the non-aliased names are presented to
external
and the solid code still ends up in the bundle).
I wrote the above because I gave up on @rollup/plugin-alias
. But I did a little more digging and this does work (without patching with sed
):
the alias does work. just gave it the wrong options
the correct option is
the latter should have been
it's basically handwritten alias plugin
output.paths
seems to be the one we need here thoughThanks a lot @mdynnl ! Sorry for the late answer. I will implement your solution on our plugins in a few day.
I don't know if this is something we should suggest here but if you want, I could write the question on StackOverflow and you copy/paste your answer on it?
this server uses https://www.answeroverflow.com/ so support threads should already be indexed
output.paths seems to be the one we need here though
But then I'm not sure what is the easiest strategy? Should I use the
resolveId
property somewhere?this