stackblitz / webcontainer-core

Dev environments. In your web app.
https://webcontainers.io
MIT License
3.93k stars 172 forks source link

[Suggestion] Export an env. variable, that signifies a StackBlitz development environment #1551

Open makrigia opened 4 weeks ago

makrigia commented 4 weeks ago

Is your feature request related to a problem? Please describe: I want to be able to know when I'm running on StackBlitz, instead of a local environment, so that I can apply some conditional logic. (load a different .env file for example)

Describe the solution you'd like: Make an ENV variable available, that denotes that we're running on StackBlitz environment. For example, STACKBLITZ_EDITOR=1

Nemikolh commented 4 weeks ago

Hey! You can know whether or not your code is running in WebContainer with the @webcontainer/env package. It exposes a function isWebContainer() that only returns true if the environment is WebContainer:

import { isWebContainer } from '@webcontainer/env';

if (isWebContainer()) {
  console.log('Running in StackBlitz' WebContainer!')
}

Would that work well enough for you?

makrigia commented 4 weeks ago

It would work indeed, but it means that I'd need to add a dependency on @webcontainer/env, which feels analogous to adding a dependency to is-odd, to find odd numbers! 😅

It's also a JS-specific solution, meaning you can't use it with the (experimental) Python environment of StackBlitz for example.

It'd be simpler to expose an env. variable, that can then be checked in a dependency-free and language-agnostic way:

if (process.env.STACKBLITZ_EDITOR) {
  console.log('Running JS in StackBlitz WebContainer!')
}

or

import os

if "STACKBLITZ_EDITOR" in os.environ:
  print('Running Python in StackBlitz WebContainer!')
makrigia commented 4 weeks ago

On a second thought, I think this suggestion should be moved to stackblitz/core, since I'm talking about the StackBlitz editor specifically, and not webcontainers in general.
Sorry for the misunderstanding.