Giter VIP home page Giter VIP logo

Comments (9)

saostad avatar saostad commented on September 1, 2024 2

Correct!
it's working well!
Thanks.

from rescripts.

saostad avatar saostad commented on September 1, 2024 1

I have created a repo in case anybody wants to see the solution

from rescripts.

harrysolovay avatar harrysolovay commented on September 1, 2024

Hi @saostad –– absolutely. You can install react-app-rewire-define-plugin and use it in conjunction with @rescripts/rescript-use-rewire:

.rescriptsrc.js

module.exports = [
  [
    'use-rewire',
    'react-app-rewire-define-plugin',
    process.env.NODE_ENV,
    {
      'process.env.VERSION': JSON.stringify(require('./package.json').version)
    }
  ]
]

Although I'd recommend doing this instead:

.rescriptsrc.js

const {appendWebpackPlugin} = require('@rescripts/utilities')
const {DefinePlugin} = require('webpack')

module.exports = config =>
  appendWebpackPlugin(new DefinePlugin({
    'process.env.VERSION': JSON.stringify(require('./package.json').version)
  }), config)

^ that should do the trick.

Please let me know if you encounter any problems. If this solves your problem, please close the issue 👍

& thanks for using Rescripts! Let me know anything I can do to make it a better developer experience for you!

from rescripts.

saostad avatar saostad commented on September 1, 2024

thanks for quick response.
how can I check if I am in prod or dev environment?
I need something like this:

const { appendWebpackPlugin } = require("@rescripts/utilities");
const { DefinePlugin } = require("webpack");

require("dotenv").config();

// if in dev (npm start script)
const API_URL = process.env.API_DEV_URL

// if in prod (npm run build script)
const API_URL = process.env.API_PROD_URL

module.exports = config =>
  appendWebpackPlugin(
    new DefinePlugin({
      "process.env.API_URL": JSON.stringify(API_URL)
    }),
    config
  );

from rescripts.

harrysolovay avatar harrysolovay commented on September 1, 2024

@saostad it depends where you need the reference. I don't think you'd need the dotenv dependency. You should be able to reference env variables directly from process.env. My understanding is that DefinePlugin is meant to make variables globally accessible to the app after it has been built. hope this has been helpful!

from rescripts.

saostad avatar saostad commented on September 1, 2024

what I wanna do is just run build or start command but because I have 2 different API url for dev and prod environments I wanna define one global variable (API_URL) and in build time set it based on the environment.
technically if I run
npm start set API_URL to http://dev.mydomain.com
and
npm run build set API_URL to http://prod.mydomain.com
and in my react app just use process.env.API_URL

from rescripts.

harrysolovay avatar harrysolovay commented on September 1, 2024

@saostad then you'll probably want to do something similar to what you had above:

const { appendWebpackPlugin } = require("@rescripts/utilities");
const { DefinePlugin } = require("webpack");

const { NODE_ENV, API_PROD_URL, API_DEV_URL } = process.env
const API_URL = NODE_ENV === 'production' ? API_PROD_URL : API_DEV_URL

module.exports = config =>
  appendWebpackPlugin(
    new DefinePlugin({ API_URL }),
    config,
  );

Also, @rescripts/utilities has built-in support for currying, so you could do this instead:

const { appendWebpackPlugin } = require("@rescripts/utilities");
const { DefinePlugin } = require("webpack");

const { NODE_ENV, API_PROD_URL, API_DEV_URL } = process.env
const API_URL = NODE_ENV === 'production' ? API_PROD_URL : API_DEV_URL

module.exports = appendWebpackPlugin(
  new DefinePlugin({ API_URL }),
);

Please let me know if this was helpful (and if so, please close the issue) 👍

from rescripts.

saostad avatar saostad commented on September 1, 2024

this is the only way it works for me:

const { appendWebpackPlugin } = require("@rescripts/utilities");
const { DefinePlugin } = require("webpack");
require("dotenv").config();

const { NODE_ENV, API_PROD_URL, API_DEV_URL } = process.env;
const API_URL = NODE_ENV === "production" ? API_PROD_URL : API_DEV_URL;

module.exports = config =>
  appendWebpackPlugin(
    new DefinePlugin({
      "process.env.API_URL": JSON.stringify(API_URL)
    }),
    config
  );

for some reason I still need to do require("dotenv").config();
thank you so much.

from rescripts.

harrysolovay avatar harrysolovay commented on September 1, 2024

@saostad ohhh––you're passing the environment vars through a .env file, not as script params... in that case, using dotenv should do the trick. Please let me know if you have any other problems :)

from rescripts.

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.