L
L
Leonid Rozhentsev2021-12-03 10:50:22
webpack
Leonid Rozhentsev, 2021-12-03 10:50:22

How to use condition in webpack config?

Hello. I'm trying to use the cross-env module to only run the purgecss plugin on production builds. Found such a solution;
Here is the command with cross-env

"scripts": {
    "start": "cross-env NODE_ENV=development webpack serve --config webpack/webpack.config.babel.js --mode development"
  }

And in webpack.config.js you need to write this:
const isDev = process.env.NODE_ENV === 'development';
const isProd = !isDev;

if (isProd) {
// Используем PurgeCSS.
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2021-12-03
@yarkov

And in webpack.config.js you need to write this

So write it down. What's stopping you? Or already show the config and the place where it does not work.

S
Sergey delphinpro, 2021-12-03
@delphinpro

The webpack config is a regular js file. Can you write in js?

const config = {
  entry: './src/index.js',
  output: {
    // ...
  },
  module: {
    // ...
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'assets/css/style.css'
    }),
    // ...
  ]
};

if (isDev) {
  config.plugins.push( new PurgecssPlugin() );
}

module.exports = config;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question