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

The using keyword cannot be used within .svelte files #953

Open
kran6a opened this issue Jul 29, 2024 · 5 comments
Open

The using keyword cannot be used within .svelte files #953

kran6a opened this issue Jul 29, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@kran6a
Copy link

kran6a commented Jul 29, 2024

Describe the bug

The using keyword seems to be unsupported within .svelte files.

Reproduction URL

https://stackblitz.com/edit/vitejs-vite-jnn3rq

Reproduction

No response

Logs

No response

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 AMD Ryzen 5 3600XT 6-Core Processor            
    Memory: 16.13 GB / 63.93 GB
  Binaries:
    Node: 22.5.1 - C:\Program Files\nodejs\node.EXE
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.6.0 - C:\Program Files\nodejs\pnpm.CMD
    bun: 1.1.21 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @sveltejs/adapter-static: ^3.0.2 => 3.0.2
    @sveltejs/kit: ^2.5.18 => 2.5.18
    @sveltejs/vite-plugin-svelte: ^3.1.1 => 3.1.1
    svelte: 5.0.0-next.201 => 5.0.0-next.201
    vite: ^5.3.5 => 5.3.5
@kran6a kran6a added bug Something isn't working triage Awaiting triage by a project member labels Jul 29, 2024
@bluwy
Copy link
Member

bluwy commented Jul 29, 2024

Typically in Vite if you want to use very new syntax like this, you need to configure this Vite config:

  esbuild: {
    // either this
    target: 'es2023',
    // or this config also works
    supported: {
      using: false
    }
  }

However our preprocessor runs its own setup so that config doesn't influence how it runs in Svelte files:

const { code, map } = await transformWithEsbuild(content, filename, {
loader: /** @type {import('vite').ESBuildOptions['loader']} */ (lang),
target: 'esnext',
tsconfigRaw: {
compilerOptions: {
// svelte typescript needs this flag to work with type imports
importsNotUsedAsValues: 'preserve',
preserveValueImports: true
}
}
});

The target: 'esnext' prevents any down-transpilation (in this case using) so it errors in parsing by Svelte next. Seems like the script preprocessor may also need to be influenced by the resolved Vite config like the style preprocessor to support this ootb 🤔

@dominikg
Copy link
Member

in the interim, you might be able to use svelte-preprocess instead, that uses typescript under the hood.

@dominikg
Copy link
Member

The reasoning for using target esnext was that the resulting js output is going through vite anyways, so any downleveling would happen there. But obviously this only covers js features, not ts

@dominikg dominikg removed the triage Awaiting triage by a project member label Jul 29, 2024
@kran6a
Copy link
Author

kran6a commented Aug 6, 2024

supported: {
      using: false
    }

Tried forking vite-plugin-svelte and using these configs but neither of them work.
The using keyword gets transpiled but then the $props() call end up inside a try block causing $props() can only be used at the top level of components as a variable declaration initializer.

@dominikg
Copy link
Member

dominikg commented Aug 7, 2024

your reproduction does not contain use of $props and you did not share your fork, so we can't tell whats going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants