benwoodward / elixir_svelte_render

MIT License
32 stars 0 forks source link

ARCHIVED

This code works, however, due to the way rendered HTML is passed from node CLI back to Elixir (through stdout), it is very difficult to debug errors. This could probably be fixed but not without a lot of work which I don't have bandwidth for right now. Another big issue is that it doesn't work with es module builds, i.e. if you specify format: 'esm' in your rollup config. This package is tightly coupled with https://github.com/revelrylabs/elixir-nodejs, to get this working with es modules would require a lot of work on both packages.

SvelteRender

Hex.pm License: 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' }