Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use with webpack.DefinePlugin #55

Closed
suddjian opened this issue Dec 13, 2018 · 9 comments
Closed

Use with webpack.DefinePlugin #55

suddjian opened this issue Dec 13, 2018 · 9 comments

Comments

@suddjian
Copy link

suddjian commented Dec 13, 2018

I'm trying to inject some constants into my app at build time under a global object called config, but no matter where I put my plugin, when I run the app I get 'config' is not defined. What am I doing wrong? Here's my craco.config.js:

const webpack = require('webpack')
const reactHotReloadPlugin = require('craco-plugin-react-hot-reload')

const appConfig = require('./config')

module.exports = {
  webpack: {
    plugins: [
      new webpack.DefinePlugin({
        config: JSON.stringify(appConfig),
      })
    ]
  },
  plugins: [
    { plugin: reactHotReloadPlugin }
  ]
}
@saostad
Copy link

saostad commented Dec 20, 2018

same problem here!
what I want to do is having 1 variable with different value in prod and dev environment.
is there any way to define environment variable in webpack config?
something like :

const rewireDefinePlugin = require('react-app-rewire-define-plugin')

// Use `webpack.DefinePlugin` to add the version number from package.json
config = rewireDefinePlugin(config, env, {
  'process.env.VERSION': JSON.stringify(require('./package.json').version)
})

@Atlaswimming
Copy link

It works in my project.
craco.config.js :

  const appConfig = { a: 1 };
  const config = {
    webpack: {
      plugins: [
        new webpack.DefinePlugin({
          PROCESS_ENV_BETA: JSON.stringify(process.env.ENV === 'beta'),
          PROCESS_ENV_DEV: JSON.stringify(process.env.ENV === 'dev'),
          config: JSON.stringify(appConfig),
        }),
      ],
      configure: (webpackConfig, { env, paths }) => {
        ...
        return webpackConfig;
      },
   };
...

index.js:

  console.log(config); // eslint-disable-line

then run build results in:

  console.log({a:1})

I was trying to find a way to use DefinePlugin today, and just edit my craco file with your code in this issue, then it just works...
But I cannot find any different between our code.

@d0ruk
Copy link

d0ruk commented Jan 9, 2019

You might be confused how the DefinePlugin works.

It sets global variables during your build process.

Thus, to have window.config in your app;

craco.config.js

new webpack.DefinePlugin({
  "process.env": { config: JSON.stringify(appConfig) }
})

index.js

window.config = process.env.config;

@patricklafrance
Copy link
Contributor

@ChemicalRocketeer @saostad do you still have the issue?

@saostad
Copy link

saostad commented Jan 18, 2019

@ChemicalRocketeer @saostad do you still have the issue?

Hi, I fix my problem with another package. rescripts

@saostad
Copy link

saostad commented Apr 18, 2019

Here is the solution in case anybody wants to use it:

const { DefinePlugin } = require("webpack");

module.exports = {
  webpack: {
    plugins: [
      new DefinePlugin({
        "process.env.VERSION": JSON.stringify(
          require("./package.json").version
        ),
        "process.env.YEAR": new Date().getFullYear()
      })
    ]
  }
}

and here is another example I added ant design to config, it's good if you need mix and match webpack plugins and babel plugins!

const { DefinePlugin } = require("webpack");

module.exports = {
  webpack: {
    plugins: [
      new DefinePlugin({
        "process.env.VERSION": JSON.stringify(
          require("./package.json").version
        ),
        "process.env.YEAR": new Date().getFullYear()
      })
    ]
  },
  plugins: [
    {
      plugin: require("craco-antd"),
      options: {
        customizeTheme: {
          "@primary-color": "#1DA57A"
        },
        lessLoaderOptions: {
          noIeCompat: true
        }
      }
    }
  ]
};
 

@soriyath
Copy link

Does not work for me.

1 similar comment
@gauravmarsoniya
Copy link

Does not work for me.

@hacking-robot
Copy link

hacking-robot commented Oct 30, 2020

If you want it to work you have to do this:

  • First try console.log in your code because in chrome dev tools it will return undefined.
  • Second, put everything under process.env otherwise you will get a compilation error.
  • Or use dotenv-webpack instead
const { DefinePlugin } = require("webpack")
const Dotenv = require('dotenv-webpack')
const CracoAntDesignPlugin = require('craco-antd')

module.exports = {
  plugins: [
    {
      plugin: CracoAntDesignPlugin,
      options: {
        customizeTheme: {
        }
      }
    }
  ],
  webpack: {
    plugins: [
      new Dotenv(),
      new DefinePlugin({
        "process.env.VERSION": JSON.stringify(
          require("./package.json").version
        )
      })
    ]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants