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

[Feature Request] Set os environment variable during dev #65

Closed
maraisr opened this issue Oct 15, 2021 · 6 comments
Closed

[Feature Request] Set os environment variable during dev #65

maraisr opened this issue Oct 15, 2021 · 6 comments
Labels
enhancement New feature or request fixed-in-next Fixed in next release

Comments

@maraisr
Copy link

maraisr commented Oct 15, 2021

When running miniflare from cli (or perhaps through node api) set a process.env.MINIFLARE = true header.

Why?

When using build tools like esbuild or like id like to run slightly different code during my local dev time, than the worker running on the real platform.

Currently im env MINIFLARE=1 miniflare — but be awesome to have this first-class.

@mrbbot
Copy link
Contributor

mrbbot commented Oct 16, 2021

Hey! 👋 This definitely sounds useful for build scripts. Wondering if it's also worth setting a MINIFLARE global to true in the actual worker context too... 🤔

@mrbbot mrbbot added the enhancement New feature or request label Oct 16, 2021
@maraisr
Copy link
Author

maraisr commented Oct 16, 2021

Honestly think both would be great. Adding it to the worker context, would only work in some cases. So probably well worth documenting where you can use it.

If you can point me in the right direction, I can surely help here with a PR 🚀

@mrbbot
Copy link
Contributor

mrbbot commented Oct 27, 2021

Hey! 👋 The first pre-release of Miniflare 2 has just been released, including both a MINIFLARE=1 environment variable for builds, and a MINIFLARE=true global within the sandbox. You can find the full changelog here and install it with npm i miniflare@next -D. Please let me know if you have any other issues, and feel free to ask questions in the #miniflare channel of the Cloudflare Workers Discord server.

@mrbbot mrbbot closed this as completed Oct 27, 2021
@ptim
Copy link

ptim commented Nov 25, 2021

Loving miniflare, tx!

This was a helpful thread for me… What is your recommended usage? I was confused by:

including both a MINIFLARE=1 environment variable for builds...

…as when I run wrangler publish, I was seeing:

webpack 5.64.2 compiled successfully in 827 ms
✨  Build completed successfully!
Error: Something went wrong with the request to Cloudflare...
Uncaught ReferenceError: MINIFLARE is not defined
  at line 435
  at line 449
  at line 451
 [API code: 10021]

My solution is importing the following DEV wherever I want to use it:

declare const MINIFLARE: boolean | number
/**
 * DEV is true if we're running inside miniflare.
 *
 * Miniflare sets MINIFLARE=true (or 1?), but it's not available in to `wrangler publish`
 *
 * See https://github.com/cloudflare/miniflare/issues/65#issuecomment-952828243
 */
export const DEV = typeof MINIFLARE !== 'undefined'
export const PROD = !DEV

I'm on "miniflare": "^2.0.0-rc.2". Tx again 👍

@mrbbot
Copy link
Contributor

mrbbot commented Nov 25, 2021

Hey! 👋 Depends on where you're using it. If it's in your actual worker code...

if (globalThis.MINIFLARE) {
  // Do something when running in Miniflare
} else {
  // Do something when running in production
}

Note the globalThis. to avoid the ReferenceError.

If it's in a custom Node.js build script, say build.js...

if(process.env.MINIFLARE) {
  // Do something when building for Miniflare
} else {
  // Do something when building for production
}

@ptim
Copy link

ptim commented Nov 25, 2021

Heaps cleaner, thanks! 🙏

If it's in your actual worker code

That's me - I'm loading different data from staging 👍

I'm using https://github.com/cloudflare/worker-typescript-template, and when I referenced globalThis.MINIFLARE, I got:

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

…which I resolved by added the following to my src/global.d.ts:

declare global {
  /** globalThis.MINIFLARE is true if we're in running in miniflare in dev */
  var MINIFLARE: boolean | undefined
}

export {}

Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed-in-next Fixed in next release
Projects
None yet
Development

No branches or pull requests

3 participants