Skip to content

set NODE_ENV to production#393

Merged
theimowski merged 8 commits intoSAFE-Stack:masterfrom
CallumVass:master
Sep 7, 2020
Merged

set NODE_ENV to production#393
theimowski merged 8 commits intoSAFE-Stack:masterfrom
CallumVass:master

Conversation

@CallumVass
Copy link
Copy Markdown
Contributor

Fix #392

Comment thread Content/package.minimal.json Outdated
"scripts": {
"start": "webpack-dev-server",
"build": "webpack -p"
"build": "cross-env NODE_ENV=production webpack -p"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is "best practice" I'm wondering whether we should force it for minimal template - maybe just use the best practice for default template and let minimal users handle that themselves if necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind, I was just being consistent in applying it to both versions of the template as they both issue the same command but I can see your point. I'm happy to take it out of the minimal template? Let me know.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please - let's keep the minimal minimal :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, that's done. Thanks

@kulshekhar
Copy link
Copy Markdown
Contributor

Doesn't webpack -p automatically set this?

Running webpack --help shows:

  -p           shortcut for --optimize-minimize --define process.env.NODE_ENV="production"

@olivercoad
Copy link
Copy Markdown
Contributor

@kulshekhar Only in the bundle for runtime, it doesn't set it for build time.

@CallumVass is there any reason not to just override it in webpack.config.js?

process.env.NODE_ENV = isProduction ? "production" : ""

Or how about setting isProduction = process.env.NODE_ENV === 'production' instead?

I can just envision creating problems from having two sources of truth for the mode used in different places.

@kulshekhar
Copy link
Copy Markdown
Contributor

kulshekhar commented Aug 30, 2020

The following statement from the webpack docs make me think -p would be sufficient. I'm no webpack expert though so if you're sure, please ignore my post

If you're using a library like react, you should actually see a significant drop in bundle size after adding DefinePlugin

In this case, adding DefinePlugin and using the -p flag are equivalent.

@olivercoad
Copy link
Copy Markdown
Contributor

Just above that it says

Contrary to expectations, process.env.NODE_ENV is not set to 'production' within the build script

@kulshekhar
Copy link
Copy Markdown
Contributor

kulshekhar commented Aug 30, 2020

My understanding of that statement (based on what follows) is that process.env.NODE_ENV won't be set to production as far as the code within the webpack configuration file is concerned but will build everything in production mode. In other words, if you have code in the webpack file that depends on NODE_ENV being set to production then you'd need to set the env variable when invoking webpack.

Edit: a quick glance at https://github.com/SAFE-Stack/SAFE-template/blob/master/Content/webpack.default.config.js#L52 indicates that the config file itself doesn't need the NODE_ENV variable and uses a different mechanishm to determine whether it should be building for production or not

@CallumVass
Copy link
Copy Markdown
Contributor Author

Hi,

Setting -p flag when running webpack doesn't set NODE_ENV to production from what I saw with my issue. If you wanted me to put up a sample repo then I am more then happy to. I'm also not a front-end expert so setting it as part of the npm build script seemed the best place to me? @Zaid-Ajaj recommended it here too: #392 (comment)

@kulshekhar
Copy link
Copy Markdown
Contributor

kulshekhar commented Aug 31, 2020

is there any reason not to just override it in webpack.config.js?

process.env.NODE_ENV = isProduction ? "production" : ""

I can just envision creating problems from having two sources of truth for the mode used in different places.

This seems like a good solution which

  • won't require new dependencies
  • will have one source of truth

If an externally provided env variable is needed anyway, it might be better to use the --env flag that webpack has. That doesn't set NODE_ENV but the value from there can be used to set both isProduction and NODE_ENV somewhere at the top of the script.

@CallumVass
Copy link
Copy Markdown
Contributor Author

Hi,

I've set NODE_ENV inside the webpack.config now. Removed the cross-env dependency and reverted all my other changes.

@isaacabraham
Copy link
Copy Markdown
Member

LGTM

Copy link
Copy Markdown
Member

@theimowski theimowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also apply same changes to Content/webpack.tests.config.js to keep consistent

@CallumVass
Copy link
Copy Markdown
Contributor Author

Please also apply same changes to Content/webpack.tests.config.js to keep consistent

That's done. Thanks for reviewing.

@theimowski theimowski merged commit 712a9b7 into SAFE-Stack:master Sep 7, 2020
@theimowski
Copy link
Copy Markdown
Member

thanks!

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

Successfully merging this pull request may close these issues.

Not setting NODE_ENV when bundling for Production

5 participants