jaspr icon indicating copy to clipboard operation
jaspr copied to clipboard

feat: Support --dart-define

Open nosmirck opened this issue 1 year ago • 2 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');

nosmirck avatar Oct 02 '24 19:10 nosmirck

Good idea. I would add it as --dart-define so its similar to flutter.

  1. Would you expect it to be available both on the server and client?
  2. Would you want to provide separate values for server and client? e.g. --server-define / --client-define?

schultek avatar Oct 06 '24 09:10 schultek

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.

nosmirck avatar Oct 06 '24 12:10 nosmirck

Added in v0.16.4: https://pub.dev/packages/jaspr/versions/0.16.4/changelog

schultek avatar Jan 03 '25 12:01 schultek

this is awesome! thank you!

nosmirck avatar Jan 03 '25 16:01 nosmirck