question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpack 4: access the mode flag from webpack.config.js file

See original GitHub issue

Do you want to request a feature or report a bug?

feature

If this is a feature request, what is motivation or use case for changing the behavior?

Since webpack 4 introduces an official --mode it’d be nice to be able to access it, otherwise I’m back to the old process.env.NODE_ENV === 'production' if I want to customize the production build, like for https://github.com/webpack/webpack/issues/6409#issuecomment-361636056

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

webpack 4.0.0-beta.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:48
  • Comments:40 (9 by maintainers)

github_iconTop GitHub Comments

297reactions
fregantecommented, Feb 8, 2018

This seems to work correctly with --mode production, -p and <no flag>. 🎉 for normalized argv

module.exports = (env, argv) => ({
	...etc,
	optimization: {
		minimize: false,
		minimizer: argv.mode === 'production' ? [
			new UglifyJsPlugin(...customConfig)
		] : []
	}
});

Are there any caveats to using a function? Like subtle changes of behavior?

133reactions
bengrunfeldcommented, May 7, 2018

Just to clarify for all those a little who need a little extra explanation (I fall into that camp).

webpack --mode development 

webpack.config.js

module.exports = (env, argv) => {
  console.log(argv.mode)        // outputs development
  return {
    // All your other custom config...
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mode | webpack
Its main purpose is to bundle JavaScript files for usage in a browser, ... Providing the mode configuration option tells webpack to use...
Read more >
Get current `--mode` in webpack.config.js - Stack Overflow
How can I get the current --mode specified in package.json inside webpack.config.js? (For instance, for pushing some plugins.)
Read more >
How to read mode in webpack.config.js - How to dev
Webpack works with many approaches to the configuration file. One of the allowed forms is configuration function. This approach is a bit more ......
Read more >
webpack 4 - How to have separate build outputs ... - Ed Spencer
In your webpack.config.js file, it's not possible to detect this mode flag, and to then conditionally add the plugin based on this flag....
Read more >
All You Need to Know about Webpack in Examples - Medium
js file. For specifying another config file that our server should use before start: npx webpack-dev-server --config webpack.common.js.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found