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(env): setup env prefix and typescript for client-side #584

Merged
merged 6 commits into from
Feb 23, 2025

Conversation

jacobhq
Copy link
Member

@jacobhq jacobhq commented Feb 21, 2025

Checklist

Related issue

Fixes #587

Overview

Vite env prefix (TUONO_PUBLIC_), and set up support for env clientside (no extra work apart from fixing TS issues that exist at the moment), and write tests (fixing also the typescript errors)

  • Fix TS errors

@Valerioageno
Copy link
Member

@jacobhq can you write few lines about why you needed to export a type from tuono/client? It doesn't seem straightforward

@jacobhq
Copy link
Member Author

jacobhq commented Feb 22, 2025

@jacobhq can you write few lines about why you needed to export a type from tuono/client? It doesn't seem straightforward

Sure thing!

Vite provides some stuff (including env vars) on an object called import.meta. I hadn't encountered this object before, but after a brief read of MDN, I found it provides the module with context about itself like a URL etc.

Without this change, typescript had no idea about what was inside import.meta, and so treated it as you trying to get something that didn't exist (ts 2339: Property env does not exist on type ImportMeta.). After a quick google, I came across this stackoverflow question, which suggested creating a vite-env.d.ts with the following content (it also suggested another approach, I used that later on):

/// <reference types="vite/client" />

This works in vite projects, because vite exists as a dependency, but in tuono projects, vite is a dependency of tuono, so this doesn't work. I fixed this by importing client in packages/tuono/client.d.ts, exporting that as tuono/client, and then referencing that in tsconfig.json of new tuono projects. I took this approach because I hate all the files in the root of a project (look at how beautiful and simple it used to be in nextjs), using tsconfig.json so I didn't have to make a new root file in each project.

Hope that was useful. I had a long session last night trying to fix this, so it is fresh in my mind :)

@jacobhq jacobhq requested a review from marcalexiei February 22, 2025 18:51
@marcalexiei
Copy link
Member

TL;DR;

By adding tuono/client, which aliases vite/client, to tsconfig#types we include a file
which contains declarations about variables and files handled by vite:

Check vite/client content for more info: https://github.com/vitejs/vite/blob/main/packages/vite/client.d.ts


The major benefit of this is that we avoid users creating this kind of file inside their apps.
E.g.: I have done this myself in examples/tuono-tutorial/src/css-modules.d.ts


Kudos to @jacobhq for resolving this using this approach!

@Valerioageno
Copy link
Member

Thanks @jacobhq for the explanation. Great work with that!

@jacobhq
Copy link
Member Author

jacobhq commented Feb 22, 2025

No worries :)

@jacobhq jacobhq requested a review from marcalexiei February 23, 2025 11:20
Copy link
Member

@Valerioageno Valerioageno left a comment

Choose a reason for hiding this comment

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

LGTM

jacobhq

This comment was marked as outdated.

@Valerioageno Valerioageno merged commit d679982 into tuono-labs:main Feb 23, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Requires typescript knowledge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add TUONO_PUBLIC_ prefix to vite .env variables
3 participants