Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't initialize state in a Class #231

Open
AnzeKop opened this issue Dec 6, 2024 · 3 comments
Open

Can't initialize state in a Class #231

AnzeKop opened this issue Dec 6, 2024 · 3 comments

Comments

@AnzeKop
Copy link

AnzeKop commented Dec 6, 2024

The example for initialising state inside a class like described in the docs doesn't work and throws an error

[!] (plugin svelte) CompileError: $state(...) can only be used as a variable declaration initializer or a class field

export class Todo {
  done = $state(false);
  text = $state();

  constructor(text: string) {
    this.text = text;
  }

  reset() {
    this.text = "";
    this.done = false;
  }
}
@Conduitry
Copy link
Member

Do you have another earlier Rollup plugin that's transpiling the class to a function? Please share a complete minimal reproduction, including your Rollup config.

@AnzeKop
Copy link
Author

AnzeKop commented Dec 6, 2024

Had to swap back to stores for now, here is the config

import "dotenv/config";
import typescript from "@rollup/plugin-typescript";
import svelte from "rollup-plugin-svelte";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
import css from "rollup-plugin-css-only";
import copy from "rollup-plugin-copy";

import { sveltePreprocess } from "svelte-preprocess";

const production = !process.argv.includes("--dev");

export default {
  input: "src/server/ui/main.js",
  output: {
    sourcemap: false,
    format: "iife",
    name: "app",
    dir: "build/server/public",
  },
  plugins: [
    typescript({
      tsconfig: "src/server/ui/tsconfig.json",
      compilerOptions: {
        outDir: undefined,
      },
    }),

    svelte({
      extensions: [".svelte", ".svelte.js", ".svelte.ts"],
      include: "src/server/ui/**/*.{svelte,svelte.js,svelte.ts}",
      // needed for typescript support
      preprocess: sveltePreprocess(),

      compilerOptions: { dev: true, runes: true },
    }),
    css({ output: "bundle.css" }),

    // If you have external dependencies installed from
    // npm, you'll most likely need these plugins. In
    // some cases you'll need additional configuration -
    // consult the documentation for details:
    // https://github.com/rollup/plugins/tree/master/packages/commonjs
    resolve({
      browser: true,
      dedupe: ["svelte"],
      exportConditions: ["svelte"],
      extensions: [".svelte", ".svelte.js", "svelte.ts"],
    }),
    commonjs(),

    copy({
      targets: [
        { src: "src/**/*.html", dest: "build/server/public" },
        { src: "src/**/*.css", dest: "build/server/public" },
      ],
    }),

    !production && livereload("build/server/public"),

    !production && serve(),

    production && terser(),
  ],
  watch: {
    clearScreen: false,
  },
};

function serve() {
  let server;

  function toExit() {
    if (server) server.kill(0);
  }

  return {
    writeBundle() {
      if (server) return;
      server = require("child_process").spawn("npm", ["run", "dev:server"], {
        stdio: ["ignore", "inherit", "inherit"],
        shell: true,
      });

      process.on("SIGTERM", toExit);
      process.on("exit", toExit);
    },
  };
}

@Jacossaurus
Copy link

Bump. Any updates on this? I would like to be able to use Svelte 5 with runes, but I cannot find a way to resolve this error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants