stencil icon indicating copy to clipboard operation
stencil copied to clipboard

Env Not Working

Open stephahart opened this issue 4 years ago • 3 comments

Stencil version:

 @stencil/[email protected]

I'm submitting a:

[x] bug report [ ] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior:

Not sure if this is feature just not available in the current release since there's not much documentation on it, but I'm using the Env variable exported by @stencil/core, and the environment variables I set in stencil.config.js are not available in the project.

Expected behavior: I expect environment variables to be made available at runtime via the Env variable from @stencil/core.

Steps to reproduce: See the related code below. Please let me know if you need more details.

Related code:

stencil.config.js

...

export const config: Config = {
  ...
  hashFileNames: true,
  plugins: [sass(), inlineSvg()],
  env: {
    'foo': 'bar'
  },
  buildEs5: true,
  extras: {
    safari10: true,
  },
};

MyComponent.tsx

import { Component, Prop, h, Element, State, Watch, Env } from '@stencil/core';

...

function getEnvVar(relPath: string) {
  console.log('Env.foo is ' + Env.foo);
}

Other information:

Related test cases: https://github.com/ionic-team/stencil/tree/master/test/end-to-end/src/env-data

stephahart avatar Apr 26 '21 20:04 stephahart

Hi! Pretty sure Env variables are compile time only. Try assigning to a class property (for example) then logging that

johnjenkins avatar Apr 28 '21 06:04 johnjenkins

Hi @stephahart - I'm going to mark this as needs investigation. We may also need to update the docs here if what John is saying is true as well... I have a hunch this may turn out to be a bug or even be a design decision that we may need to reconsider.

FWIW, for environment variables I've used the rollup-plugin-replace plugin to replace code in TypeScript, and even to replace JS files in the app like so

splitinfinities avatar Oct 11 '21 15:10 splitinfinities

Is this working for you? I am not able to find a solution for using env

camilolucena88 avatar May 30 '22 16:05 camilolucena88

~FWIW it seems to work for the dist output target but not for dist-custom-elements~

Actually that makes sense since dist-custom-elements is consumed by other bundlers (webpack/rollup ..) and so they won't read the lib stencil.config.ts. Need to actually replace code with rollup plugins i suppose

edolix avatar Aug 16 '22 15:08 edolix