Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Conversation

@jamesopstad
Copy link
Contributor

@jamesopstad jamesopstad commented Jan 14, 2025

resolves #29

This adds support for the CLOUDFLARE_ENV environment variable to determine the environment that is used in the Worker config.

When updating the versions of Wrangler and Miniflare, I had to make changes to the Workflow tests to reflect changes in the implementation. I have also updated all the compatibility dates to the most recent.

Note that the environment variable will not work if provided in a .env file. Vite only automatically provides values from .env (prefixed with VITE_) to code rather than config. The loadEnv function (https://vite.dev/guide/api-javascript.html#loadenv) needs to be used to access variables from .env elsewhere (by default this only loads env variables prefixed with VITE_).

Do you think we should call loadEnv in the plugin in so that the value can be read from .env files?

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 14, 2025

Open in Stackblitz

npm i https://pkg.pr.new/flarelabs-net/vite-plugin-cloudflare/@flarelabs-net/vite-plugin-cloudflare@127

commit: fbe0fb3

Comment on lines 4 to 6
test('returns the correct custom-env var when CLOUDFLARE_ENV=custom-env', async () => {
expect(await getTextResponse()).toEqual('Custom env var');
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to use vi.stubEnv (https://vitest.dev/api/vi.html#vi-stubenv) here instead of changing process.env in the Vite config. It didn't work though (I think because the Vite config has already run by the time we run the test).

@jamesopstad jamesopstad requested review from dario-piotrowicz and petebacondarwin and removed request for petebacondarwin January 14, 2025 15:37
Copy link
Contributor

@dario-piotrowicz dario-piotrowicz left a comment

Choose a reason for hiding this comment

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

Looks good to me, I just left some minor comments 🙂

@dario-piotrowicz
Copy link
Contributor

dario-piotrowicz commented Jan 15, 2025

Do you think we should call loadEnv in the plugin in so that the value can be read from .env files?

That sounds like a good idea, wrangler does read .env files for config purposes so this would be consistent with that

But I don't know if loadEnv picks up more files than wrangler (I don't think it loads .env.local files for example: wrangler code) ideally if this were implemented I think it should work as closely as possible to the way wrangler does? 🤔

@jamesopstad
Copy link
Contributor Author

That sounds like a good idea, wrangler does read .env files for config purposes so this would be consistent with that

But I don't know if loadEnv picks up more files than wrangler (I don't think it loads .env.local files for example: wrangler code) ideally if this were implemented I think it should work as closely as possible to the way wrangler does? 🤔

This is only for use within the plugin itself though so we would only use it to get the CLOUDFLARE_ENV value. Vite already provides .env variables to user code (if they're prefixed with VITE_).

@dario-piotrowicz
Copy link
Contributor

This is only for use within the plugin itself though so we would only use it to get the CLOUDFLARE_ENV value. Vite already provides .env variables to user code (if they're prefixed with VITE_).

yeah that's a good point, yes I would be in favour of calling loadEnv then 👍

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

Approved with some nits.

@jamesopstad jamesopstad force-pushed the james/support-wrangler-environments branch from 595081b to 8b35fbf Compare January 15, 2025 17:20
@jamesopstad jamesopstad merged commit 917395c into main Jan 15, 2025
5 checks passed
@jamesopstad jamesopstad deleted the james/support-wrangler-environments branch January 15, 2025 19:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Respect environments when reading Wrangler config files

4 participants