Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting Separate Custom Elements from Svelte Components

I'm trying to find out if it's possible to export each Svelte component as a separate Custom Element (with Shadow DOM) in its own js file (with imports for any child elements - i.e. dependencies aren't included in the same file). Is it even possible?

Thanks

like image 264
Paul Avatar asked Sep 01 '25 10:09

Paul


1 Answers

I'm assuming you're using rollup and rollup-plugin-svelte

the way to do it is to use code splitting. You can define the inputs separately and that will create individual outputs. Instead of using a file name output, you would use an output dir.

example:

import svelte from 'rollup-plugin-svelte';

export default [
    {
        input: ['src/main-a.js', 'src/main-b.js'],
        output: {
            dir: 'public/module',
            format: 'es',
            sourcemap: true
        },
        plugins: [svelte()],
        experimentalCodeSplitting: true,
        experimentalDynamicImport: true
    },
];

source/reference/example: https://github.com/Rich-Harris/rollup-svelte-code-splitting

like image 144
Daniel Avatar answered Sep 05 '25 23:09

Daniel