Skip to content

Support for .civet files outputting .svelte.ts/.svelte.js for Svelte Runes reactive compatibility #1733

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

adam2am
Copy link

@adam2am adam2am commented Apr 27, 2025

Summary

  • Expand handling of .civet files to support output extensions like .svelte.ts or .svelte.js.
  • Update isCivetTranspiled regex to match .civet.svelte.ts and .civet.svelte.js.
  • Update cleanCivetId() to correctly strip new suffixes (.svelte.ts, .svelte.js).
  • This enables .civet files to output TypeScript-compatible .svelte files, which are needed for Svelte's new Runes API.
  • No breaking changes - .civet.jsx / .civet.tsx are still supported as before.

Motivation

  • Svelte's new Runes system.
  • Civet users can now fully integrate with workflows by outputting .svelte.ts from .civet files.
    (even tho for now IDE will not recognize syntax of it out of the box, have to manually do .d.ts)

Thanks for reading! 🚀 Happy to adjust anything if needed

Copy link
Collaborator

@edemaine edemaine left a comment

Choose a reason for hiding this comment

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

Thanks for this PR, and sorry for the delay. Do you think it would be possible to use outputExtension instead of hard-coding Svelte support? That would be better, I think, but it may require a bit more restructuring.

@adam2am
Copy link
Author

adam2am commented Apr 29, 2025

Thanks for this PR, and sorry for the delay. Do you think it would be possible to use outputExtension instead of hard-coding Svelte support? That would be better, I think, but it may require a bit more restructuring.

You mean something like this?

  outExt :=
    options.outputExtension ?? (ts is "preserve" ? ".tsx" : ".jsx")
  cleanOutExt := outExt.replace(/\./g, '\\.')
  cleanOutputExtRE := new RegExp(`${cleanOutExt}$`)
  isCivetTranspiled := new RegExp(`(\\.civet)(?:${cleanOutExt})?([?#].*)?$`)

  function cleanCivetId(id: string): {id: string, postfix: string}
    let postfix = ''
    id = id
    .replace postfixRE, (match) =>
      postfix = match
      ''
    .replace cleanOutputExtRE, ''
    {id, postfix}

Also while experimenting I noticed it's adding it to the end, so it's file.civet.ts / file.civet.svelte.ts without stripping
Do you think would it be a cool idea to optionally add something like

   stripCivet  := options.stripCivetExtension ?? false

and some extra logic to make it possible > file.ts / file.svelte.ts / file.tsx or there is no point for doing that?

@edemaine
Copy link
Collaborator

If we stick to regular expressions, you'll need to escape all active regexp characters, not just .. (Can't wait for RegExp.escape...) Something like

cleanOutExt := outExt.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')

But I wonder if we could instead just remove do the postfixRE removal, then detect whether the filename endsWIth(outExt), strip that, and then check for /\.civet$/, instead of building a big regular expression.

Do you think would it be a cool idea to optionally add something like

   stripCivet  := options.stripCivetExtension ?? false

I don't think this can work in the unplugin, because we wouldn't be able to detect transformed files, because they no longer have .civet in them. But I'm also not quite sure why it really matters: I don't think the unplugin ever emits a file with the output extension in it, does it? (The CLI already supports writing .ts files instead of .civet.ts files.) I think the unplugin's filenames are all for internal processing purposes. The point of this PR is to allow postprocessing triggered by extensions other than .tsx, including .svelte.ts.

1 - no more dynamic regex for validation
2 - splitIdPostfix - only removes ? or # part
3 - validateAndExtractCivetBase -
    - removes postfix
    - checks if id ends with outputextension
    - checks if id before ends with .civet
        true > returns base .civet path,
        otherwise null
4 - resolveId now uses splitIdPostfix to only remove postfix = path without striping outext, then it appends everything
5 - loadinclude = direct, clear validation (is not null)
6 - load - uses validateAndExtractCivetBase to both validate and extract civet file
@adam2am
Copy link
Author

adam2am commented May 1, 2025

instead of building a big regular expression.

Hey, sorry for the delay, could you review my code in latest commit is it overcoooked or a nice one

Basically
1 - helper functions instead of regex for validation
2 - splitIdPostfix - only removes ? or # part
3 - validateAndExtractCivetBase -

    - removes postfix
    - checks if id ends with outputextension
    - checks if id before ends with .civet
        true > returns base .civet path, 
        otherwise null

4 - resolveId now uses splitIdPostfix to only remove postfix = path without striping outext, then it appends everything
5 - loadinclude = direct, clear validation (is not null)
6 - load - uses validateAndExtractCivetBase to both validate and extract civet file

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.

2 participants