Giter Club home page Giter Club logo

separate-env-injection-playground's Introduction

This project is the playground for

https://github.com/simbathesailor/envvarprep-loader

This project uses envvarprep-loader (webnpack loader to replace process.env strings)

Run

yarn run build

Run

node replaceStringInBundle.js

This kind of code also fail even without this laoder:

import * as React from "react"; import logo from "./logo.svg"; import "./App.css"; import { SOMEPARAM, V1 } from "./consts"; console.log("๐Ÿš€ ~ file: App.js ~ line 5 ~ V1", V1);

console.log("SOMEPARAM outside", SOMEPARAM);

const { PUBLIC_URL_TWO } = process.env; console.log("๐Ÿš€ ~ file: App.js ~ line 10 ~ PUBLIC_URL_TWO", PUBLIC_URL_TWO);

const { REACT_APP_PARAM_TWO: hello } = process.env; console.log("๐Ÿš€ ~ file: App.js ~ line 12 ~ hello", hello);

const { env } = process;

const { REACT_APP_PARAM_THREE: threeVar } = env; console.log("๐Ÿš€ ~ file: App.js ~ line 18 ~ threeVar", threeVar);

so yes writing const { env } =process is inherentluy not supported as it is available only on node. So putting an effort to support following syntax is not worth it.

const { env } = process
const { variable } = env

I think there is one more way which also support this additional syntax which is used sometimes

const { PARAM } = process.env

To support that i can make use of define plugin and envvar preploader. Just have this define plugin defined like this in webpack:

 new webpack.DefinePlugin({
        ...env.stringified,
        MY_SECRET_VALUE: JSON.stringify("HOLA My SECRET VALUE"),
        "process.env": (() => {
          return Object.keys(env.raw).reduce((acc, key) => {
            if (
              ["NODE_ENV", "PUBLIC_URL"].includes(key) &&
              ![
                "WDS_SOCKET_HOST",
                "WDS_SOCKET_PATH",
                "WDS_SOCKET_PORT",
              ].includes(key)
            ) {
              acc[key] = JSON.stringify(env.raw[key]);
            } else if (
              ![
                "WDS_SOCKET_HOST",
                "WDS_SOCKET_PATH",
                "WDS_SOCKET_PORT",
              ].includes(key)
            ) {
              console.log(
                "๐Ÿš€ ~ file: webpack.config.js ~ line 625 ~ returnObject.keys ~ key",
                key
              );

              // "$$_INTERNAL__process.env.REACT_APP_PARAM_TWOMATH_RANDOM_START".concat(
              //   Math.random(),
              //   "MATH_RANDOM_END$$_INTERNAL__"

              // "$$_INTERNAL__process.env.REACT_APP_PARAM_TWOMATH_RANDOM_START".concat(Math.random(),"MATH_RANDOM_END$$_INTERNAL__"
              // '$$_INTERNAL__process.env.REACT_APP_PARAM_ONEMATH_RANDOM_START".concat(Math.random(),"MATH_RANDOM_END$$_INTERNAL__'
              acc[
                key
              ] = `"$$_INTERNAL__process.env.${key}MATH_RANDOM_START".concat(Math.random(),"MATH_RANDOM_END$$_INTERNAL__")`;
            }
            return acc;
          }, {});
        })(),
      }),

And then once this is done just run node replaceStringInBundle.js

and then starts the server exactly same.

But this may requuire changes in the way you will plan the docker.

Now you need to know atleast the keys , before hand at the compile time and then you can build the bundle .

and at the run time , you can inject the right values.

For now, i can set a disclaimer that only use procee.env values as process.env.PARAMNAME.

So will change that if needed.

Note:I am making use of npm link for the development and testing of envvar-prep loader . So if cloning in a new machine make sure that you do npm link. If you want to make use of one from the npm, just npm unlink envvarprep-loader.

In the best case i will be needing define plugin and envvarprep-loader injectEnv script.

Define plugin does the inplace code replacement , before processing the code. It is as simple as string replacement.

separate-env-injection-playground's People

Contributors

simbathesailor avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.