In my Svelte + Rollup project src
folder I have two subfolders, components
and pages
. Let me describe my dream, and hopefully somebody can shed some light on whether it's possible.
The components and pages are exported as their own files (I have that working via rollup-plugin-multi-input).
When a page is hit, it requests all the components it needs.
The component JS files are then cached. All future page calls that need the same components draw from the cache.
Is this reasonable, or would it be better to just export the pages individually and bundle the components into each page, duplicating code?
Another potential idea
Do an App.svelte
file that imports all the components, aggregate those into an object, pass that object into the page component as a prop, then do <svelte:component this={components.Input}>
for all components inside the page.
App.svelte:
<script>
import { onMount } from 'svelte';
import Input from 'Input.svelte';
import Popup from 'Popup.svelte';
const components = {Input, Popup};
let page;
function getPage(url) {
/* AJAX call to fetch the page component and set it to the page variable */
}
onMount(() => getPage(location.pathname))
</script>
<svelte:component this={page} components>
Page1.svelte:
<script>
export let components;
</script>
<p>Type your name:</p>
<svelte:component this={components.Input}>
With this method, all the components are included in the bundle even if the user never hits a page that contains some of those.