feat: Support --dart-define
Closed this issue · 4 comments
As the title says, we need support for --dart-define (or maybe --jaspr-define or just --define like pure dart)
jaspr serve --dart-define=ENV=dev --dart-define=API_URL=https://api.example.com
Now, you'll be able to access these values from code at compile time like this:
const env = String.fromEnvironment('ENV', defaultValue: 'dev');
const apiUrl = String.fromEnvironment('API_URL', defaultValue: 'https://api.example.com');
Good idea. I would add it as --dart-define
so its similar to flutter.
- Would you expect it to be available both on the server and client?
- Would you want to provide separate values for server and client? e.g.
--server-define
/--client-define
?
I was testing this as just client side rendering, didn't think of the SSR part yet, but now that you mention it I think that having a way to define environment values separately is a better approach. Maybe '--dart-define' can be used as a global environment and '--server-define'/'--client-define' is used for SSR to separate environment values used by the server vs the client. It adds a lot more complexity because the user would need to handle this somehow (like trying to access a value defined for the server from a client side route and vice versa) but you'll be able to keep secret server environment values separate from the client.
In any case, I think that at least with support for a global '--dart-define' should be enough as a minimum requirement, people would need to handle secrets in a different way for the server side of they need to but that's ok.
Added in v0.16.4
: https://pub.dev/packages/jaspr/versions/0.16.4/changelog
this is awesome! thank you!