SvelteRender

Hex.pmLicense: MIT

WARNING: This is beta. Don't use in production unless you know what you're doing!

Renders Svelte components as HTML

Documentation

The docs can be found at https://hexdocs.pm/svelte_render.

Installation

def deps do
  [
    {:svelte_render, "~> 0.1.0"}
  ]
end

Getting Started with Phoenix

Taken from: https://jackwhiting.co.uk/posts/setting-up-routing-in-svelte-with-pagejs/

  <script>
    import router from 'page';
    import Home from './routes/Home.svelte';

    let page;
    let params = {name: "svelte"};

    router('/', () => page = Home)

    router.start();
  </script>

  <svelte:component this={page} params={params} />

The postcss configs allow you to import a global.scss file into your App.svelte like so:

  <style global type="scss" >
    @import "../styles/global.scss";
  </style>
  import svelte from 'rollup-plugin-svelte';
  import resolve from '@rollup/plugin-node-resolve';
  import commonjs from '@rollup/plugin-commonjs';
  import { terser } from 'rollup-plugin-terser';
  import sveltePreprocess from 'svelte-preprocess';

  const production = !process.env.ROLLUP_WATCH;
  const preprocess = sveltePreprocess({
    scss: {
      includePaths: ['src'],
    },
    postcss: {
      plugins: [require('postcss-import')({path: ['src/styles']})],
    },
  });

  export default {
    input: 'src/app.js',
    output: {
      sourcemap: true,
      format: 'iife',
      name: 'app',
      file: '../priv/static/js/app.js'
    },
    plugins: [
      svelte({
        hydratable: true,

        // enable run-time checks when not in production
        dev: !production,
        preprocess,

        css: css => {
          css.write('../priv/static/css/app.css');
        }
      }),

      resolve({
        browser: true,
        // a dependency in node_modules may have svelte inside it's node_modules folder
        // dedupe option prevents bundling those duplicates
        dedupe: ['svelte']

      }),
      commonjs(),

      // If we're building for production (npm run build
      // instead of npm run dev), minify
      production && terser()
    ],
    watch: {
      clearScreen: false
    }
  };

You should now be able to run mix phx.server and your Svelte components with update with live reloading

Development

For debugging you can use priv/cli.js:

node priv/cli --component "/Full/path/to/Component.svelte" --props "{name: 'Svelte'}"
// output:
//   { error: null,
//      markup: '<p>Hello from Svelte</p>',
//      component: '/Users/you/Dev/project_folder/assets/src/Component.svelte' }