Skip to content

Dynamic loading of cdn-hosted Svelte 5 component #13186

Open
@stephanboersma

Description

@stephanboersma

Describe the bug

Hi team,

I am not sure whether this is a bug, not supported or me not having proper configuration. I hope that you or someone is able to help me with clarification.

The requirement I am trying to satisfy is externally compiled and hosted Svelte widgets that can be loaded into my SvelteKit app but when I try to dynamically import and load the Svelte widgets, I get the following error.

image

I can see that the dynamically loaded component taps into the svelte runtime within the sveltekit project, so I got that going for me :)

Looking forward to hear from someone.

Reproduction

1st Repo with Component

  1. Create a barebone vite & svelte 5 project
  2. Create Counter component at src/lib/Counter.svelte.
  3. npm run build
  4. use http-server to host server counter.js: http-server dist --cors

Counter.svelte

<script lang="ts">
  let count: number = $state(0);
  const increment = () => {
    count += 1;
  };
</script>

<button onclick={increment}>
  count is {count}
</button>

vite.config.js

import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';

export default defineConfig({
  plugins: [svelte()],
  build: {
    lib: {
      entry: './src/lib/Counter.svelte',
      formats: ['es'],
      fileName: 'counter',
    },
    rollupOptions: {
      external: ['svelte', 'svelte/internal'],
      output: {
        globals: {
          svelte: 'Svelte',
        },
      },
    },
  },
});

2nd Repo with barebone SvelteKit setup

  1. Create DynamicComponent at src/lib/DynamicComponent.svelte
  2. Render DyanamicComponent in routes/+page.svelte: <DynamicComponent componentUrl="http://localhost:8080/counter.js" />
  3. Start dev server: npm run dev

DynamicaComponent.svelte

<script lang="ts">
	export let componentUrl: string;
</script>

{#await import(componentUrl)}
	<div>Loading...</div>
{:then Component}
	<!-- Dynamically loaded component -->
	<Component.default />
{:catch error}
	<div>Error loading component: {error.message}</div>
{/await}

Both repos have svelte 5 installed.

Logs

No response

System Info

Repo 1:
"devDependencies": {
    "@sveltejs/vite-plugin-svelte": "^4.0.0-next.6",
    "@tsconfig/svelte": "^5.0.4",
    "svelte": "^5.0.0-next.244",
    "svelte-check": "^3.8.5",
    "tslib": "^2.6.3",
    "typescript": "^5.5.3",
    "vite": "^5.4.1"
  }

Repo 2
"devDependencies": {
		"@sveltejs/adapter-auto": "^3.0.0",
		"@sveltejs/kit": "^2.0.0",
		"@sveltejs/vite-plugin-svelte": "^4.0.0-next.6",
		"@types/eslint": "^9.6.0",
		"eslint": "^9.0.0",
		"eslint-config-prettier": "^9.1.0",
		"eslint-plugin-svelte": "^2.36.0",
		"globals": "^15.0.0",
		"prettier": "^3.1.1",
		"prettier-plugin-svelte": "^3.1.2",
		"svelte": "^5.0.0-next.1",
		"svelte-check": "^4.0.0",
		"typescript": "^5.0.0",
		"typescript-eslint": "^8.0.0",
		"vite": "^5.0.3",
		"vitest": "^2.0.0"
	},

Severity

blocking all usage of svelte

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions