Code Monkey home page Code Monkey logo

Comments (8)

aaronchenwei avatar aaronchenwei commented on July 28, 2024 15

Here is what we tried to rewire process.env

const findWebpackPlugin = (plugins, pluginName) =>
  plugins.find(plugin => plugin.constructor.name === pluginName);

const overrideProcessEnv = value => config => {
  const plugin = findWebpackPlugin(config.plugins, 'DefinePlugin');
  const processEnv = plugin.definitions['process.env'] || {};

  plugin.definitions['process.env'] = {
    ...processEnv,
    ...value,
  };

  return config;
};

module.exports = override(
  overrideProcessEnv({
    VERSION: JSON.stringify(require('./package.json').version),
  })
);

from customize-cra.

FezVrasta avatar FezVrasta commented on July 28, 2024

Just wondering, does the variables set into the .env get consumed by webpack as well? If so maybe we can leverage dotenv to inject the variables in a clean way

from customize-cra.

aaronchenwei avatar aaronchenwei commented on July 28, 2024

dotenv in CRA requires environment variables starting with REACT_APP_ except NODE_ENV. That is why we want directly access DefinePlugin.

from customize-cra.

devals9 avatar devals9 commented on July 28, 2024

@aaronchenwei
I would like to be able to use DefinePlugin to define DEV and PROD variables. Any idea how I should try to achieve that ? I tried your solution but it does not work.

from customize-cra.

aaronchenwei avatar aaronchenwei commented on July 28, 2024

@devals9

Do you mean ?

proccess.env.DEV = {...}
process.env.PROD = {...}

My previous code snippet is just to append variables under process.env

from customize-cra.

huxianghe avatar huxianghe commented on July 28, 2024

Here is package.json/scritps config:

  "scripts": {
    "dev": "react-app-rewired start --open",
    "build:test": "react-app-rewired build --test",
    "build:pre": "react-app-rewired build --pre",
    "build:prod": "react-app-rewired build --prod",
    "eject": "react-scripts eject"
  }
const path = require('path')
const path = require('path')
const Dotenv = require('dotenv-webpack')
const version = JSON.stringify(require('../package.json').version)

const resolvePath = dir => path.join(__dirname, '..', dir)

const mode = process.argv[2].split('--')[1]
const isDev = process.env.NODE_ENV === 'development'

const envPath = isDev ? resolvePath('.env.development') : resolvePath(`.env.${mode}`)
const envVars = {}

const definitions = new Dotenv({
    path: envPath
}).definitions

Object.keys(definitions).forEach(key => {
  const [, api] = key.split('process.env.')
  envVars[api] = definitions[key]
})

function rewireDefinePlugin (config) {
  config.plugins.some(p => {
    if (
      p.definitions &&
      p.definitions['process.env']
    ) {
      p.definitions['process.env'] = { ...envVars, version}
      return true
    }
  })
  return config
}

module.exports = rewireDefinePlugin

from customize-cra.

with-heart avatar with-heart commented on July 28, 2024

Closing as the solution from @aaronchenwei is verified as working with [email protected]+. If there are further problems, please open a new issue.

from customize-cra.

foxundermoon avatar foxundermoon commented on July 28, 2024

https://github.com/foxundermoon/cra-define-override#readme

npm i -D cra-define-override
// config-overrides.js
const { overridePassedProcessEnv } = require("cra-define-override");

module.exports = override(
  overridePassedProcessEnv(["BASE_URL", "OTHER_ENV_NAME"])
);

then

BASE_URL=https://fox.mn   npm run build

from customize-cra.

Related Issues (20)

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.