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

feat(cli-utils): add experimental support for .svelte files #241

Merged
merged 15 commits into from
Apr 22, 2024

Conversation

kitten
Copy link
Member

@kitten kitten commented Apr 21, 2024

Related: #232
Related: #236

Summary

This abstracts our usage of volar’s utilities down to VirtualCode wrappers and abstracts it to have parity with the Svelte file support.
The Svelte2TSX compiler has been added to compile Svelte files to TSX as well.

Notably, this changes the approach slightly. To risk confusing external files with virtual files, we now create a SourceFile of the project file (svelte/vue file) outside of our TypeScript program and don't add it to the project.

Redirections to the virtual file and such still function as before. But the biggest change is that all commands will now iterate over the virtual files (e.g. .vue.ts and .vue.svelte) instead of the non-TS project files (.vue and .svelte), since those aren't loaded into TypeScript anymore. The output of the logger is hence mapped and getFilePosition now uses the position mapper as needed.

Set of changes

  • Squash usage of Volar helpers down to just VirtualCode
  • Add async-imports for external virtual code creators
  • Add file location mapper
    • This will either map from project -> virtual or virtual -> project file, so it can be used for both, the GraphQLSP plugin info methods, and the logger outputs
  • Add svelte example

Copy link

changeset-bot bot commented Apr 21, 2024

🦋 Changeset detected

Latest commit: a128090

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines +66 to +81
const virtualFileId = file.fileId + VIRTUAL_EXT;
const virtualSourceFile = project.createSourceFile(
virtualFileId,
virtualSnapshot.getText(0, virtualSnapshot.getLength()),
{ overwrite: true, scriptKind: ts.ScriptKind.TSX }
);

if (virtualSourceFile._markAsInProject) virtualSourceFile._markAsInProject();

projectToVirtual.set(file.fileId, virtualSourceFile.compilerNode);
virtualToProject.set(virtualFileId, projectSourceFile);
sourceMaps.set(file.fileId, sourceMap);
sourceMaps.set(virtualFileId, sourceMap);
}

return ((fileId, position) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still unable to override the original filename with the output here, which would allow us to clean that up. Since TypeScript does support arbitrary file extensions, I'm not quite sure where that issue may be coming from, but for now, this workaround is still needed

Comment on lines +8 to +9
const VueVirtualCode =
vue.VueGeneratedCode || ((vue as any).VirtualCode as typeof vue.VueGeneratedCode);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This export was renamed on the main branch of @vuejs/language-tools, so both names will have to be checked here to anticipate their next release: vuejs/language-tools@f46634c

@kitten kitten changed the title feat(cli-utils): add experimental support for .svelte files feat(cli-utils): add experimental support for .svelte files Apr 21, 2024
@kitten kitten merged commit b67d40e into main Apr 22, 2024
2 checks passed
@kitten kitten deleted the feat/refactor-volar-integration branch April 22, 2024 10:39
@github-actions github-actions bot mentioned this pull request Apr 22, 2024
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

Successfully merging this pull request may close these issues.

None yet

2 participants