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

Importing TS files from component is not working when script tag spans multiple lines #14384

Closed
fehnomenal opened this issue Nov 20, 2024 · 3 comments

Comments

@fehnomenal
Copy link

Describe the bug

If the <script lang="ts"> has a long generics attribute that causes prettier to break it into multiple lines, imports with a .js extension that should resolve to a TS file are no longer working.

<script
  lang="ts"
  generics="SomethingLongToTriggerReformat extends Record<string, unknown>, MoreStrings extends keyof SomethingLongToTriggerReformat"
>
  import { test } from './lib/test.js';
</script>

{test}

If you change the import to './lib/test.ts' or './lib/test' it works again.
If you add a space after <script it also works again.

Reproduction

Repo: https://github.com/fehnomenal/svelte-ts-import

This is the vite@latest svelte template stripped of all unneeded stuff.

The second commit (fehnomenal/svelte-ts-import@91e042f) gives the diff as an example.

Logs

❯ pnpm build

> [email protected] build /tmp/svelte-ts-import
> vite build

vite v5.4.11 building for production...
✓ 15 modules transformed.
x Build failed in 70ms
error during build:
Could not resolve "./lib/test.js" from "src/App.svelte"
file: /tmp/svelte-ts-import/src/App.svelte
    at getRollupError (file:///tmp/svelte-ts-import/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:396:41)
    at error (file:///tmp/svelte-ts-import/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:392:42)
    at ModuleLoader.handleInvalidResolvedId (file:///tmp/svelte-ts-import/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:20085:24)
    at file:///tmp/svelte-ts-import/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:20045:26
 ELIFECYCLE  Command failed with exit code 1.

System Info

System:
    OS: Linux 6.6 cpe:/o:nixos:nixos:24.11 24.11 (Vicuna)
    CPU: (16) x64 AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
    Memory: 9.77 GB / 60.64 GB
    Container: Yes
    Shell: 0.99.1 - /run/current-system/sw/bin/nu
  Binaries:
    Node: 23.1.0 - /etc/profiles/per-user/andi/bin/node
    npm: 10.9.0 - /etc/profiles/per-user/andi/bin/npm
    pnpm: 9.14.1 - /etc/profiles/per-user/andi/bin/pnpm
    bun: 1.1.31 - /etc/profiles/per-user/andi/bin/bun
  Browsers:
    Chromium: 130.0.6723.91
  npmPackages:
    svelte: 5.2.7 => 5.2.7

Severity

annoyance

@fehnomenal
Copy link
Author

OK, I found the problem is caused by vite-plugin-svelte. It uses a similar regex as the compiler to extract the script tag's language. But it has this as an edge case.
Fix:

 const scriptLangRE =
-	/<!--[^]*?-->|<script (?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=["']?([^"' >]+)["']?[^>]*>/g;
+	/<!--[^]*?-->|<script\s+(?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=["']?([^"' >]+)["']?[^>]*>/g;

@fehnomenal
Copy link
Author

I submitted a PR at the repo: sveltejs/vite-plugin-svelte#1028

@dummdidumm
Copy link
Member

Closing since this was fixed in v-p-s

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

2 participants