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

add webpack-env-define-plugin package #1030

Closed
wants to merge 5 commits into from

Conversation

meldiner
Copy link

@meldiner meldiner commented Nov 9, 2016

I found this code useful for our project configuration solution.
I didn't want to duplicate it so thought it could be wrapped in a handy webpack plugin.

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks!

If you are contributing on behalf of someone else (eg your employer): the individual CLA is not sufficient - use https://developers.facebook.com/opensource/cla?type=company instead. Contact cla@fb.com if you have any questions.

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Copy link

@camwest camwest left a comment

Choose a reason for hiding this comment

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

Overall looks good. The show stopper is really the way that react-scripts is referencing the webpack plugin.

@@ -6,3 +6,4 @@ my-app*
template/src/__tests__/__snapshots__/
lerna-debug.log
npm-debug.log
.idea/
Copy link

Choose a reason for hiding this comment

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

Add .idea to your global .gitignore not this one. See https://help.github.com/articles/ignoring-files/ for details

// Assert this just to be safe.
// Development builds of React are slow and not intended for production.
if (env['process.env'].NODE_ENV !== '"production"') {
throw new Error('Production builds must have NODE_ENV=production.');
Copy link

Choose a reason for hiding this comment

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

Why did you remove this throw?

Copy link
Author

Choose a reason for hiding this comment

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

I changed the default for NODE_ENV in webpack.config.prod.js to be production so the risk of running a production build with NODE_ENV !== 'production' is low.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't say it's low. People try to run NODE_ENV=my-server npm run build all the time and get non-prod builds by mistake.

@@ -64,6 +64,7 @@
"url-loader": "0.5.7",
"webpack": "1.13.2",
"webpack-dev-server": "1.16.2",
"webpack-env-define-plugin": "../webpack-env-define-plugin",
Copy link

Choose a reason for hiding this comment

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

Don't reference a local path. Refer to the package the way it would appear on npmjs.com

See https://github.com/lerna/lerna for more details on how these packages link together.

@@ -0,0 +1,14 @@
{
Copy link

Choose a reason for hiding this comment

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

You should add yourself as a contributor to the package.json. See http://browsenpm.org/package.json#contributors for details.

Copy link
Author

Choose a reason for hiding this comment

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

done

@gaearon
Copy link
Contributor

gaearon commented Nov 20, 2016

Ooh, I like this. But can we move this into react-dev-utils as a separate export, just like all the other custom plugins we have? This helps reduce devDependency bloat after ejecting.

Copy link
Contributor

@gaearon gaearon left a comment

Choose a reason for hiding this comment

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

Let's move it into react-dev-utils.

@meldiner
Copy link
Author

@gaearon done!

@@ -28,6 +29,7 @@
"html-entities": "1.2.0",
"opn": "4.0.2",
"sockjs-client": "1.0.3",
"strip-ansi": "3.0.1"
"strip-ansi": "3.0.1",
"webpack": "^1.13.3"
Copy link
Contributor

Choose a reason for hiding this comment

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

There isn't an explicit webpack dependency to support both 1.x and 2.x.

See #963 and #950 (comment) for more info.

Copy link
Author

Choose a reason for hiding this comment

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

@gaearon what do you want to do regarding the webpack dependency?

Copy link
Contributor

Choose a reason for hiding this comment

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

You can remove it.

@@ -201,3 +201,35 @@ module.exports = {
// ...
}
```

#### `new webpackEnvDefinePlugin({regex: string, customVariables: Object.<string, string>})`
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove webpack prefix.

regex: /^MY_PREFIX_/i,
customVariables: {
// Useful for determining whether we’re running in production mode.
// Most importantly, it switches React into the correct mode.
Copy link
Contributor

Choose a reason for hiding this comment

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

Both of these comments are a bit out of context (and the second one is actually misleading).
Can we come up with better realistic examples?

// Assert this just to be safe.
// Development builds of React are slow and not intended for production.
if (env['process.env'].NODE_ENV !== '"production"') {
throw new Error('Production builds must have NODE_ENV=production.');
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't say it's low. People try to run NODE_ENV=my-server npm run build all the time and get non-prod builds by mistake.

// Useful for determining whether we’re running in production mode.
// Most importantly, it switches React into the correct mode.
'NODE_ENV': JSON.stringify(
process.env.NODE_ENV || 'development'
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need to read process.env.NODE_ENV here at all. We don't allow overriding it. So we can just pass 'development'.

// Useful for determining whether we’re running in production mode.
// Most importantly, it switches React into the correct mode.
'NODE_ENV': JSON.stringify(
process.env.NODE_ENV || 'production'
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. Just pass 'production'?

Copy link
Contributor

Choose a reason for hiding this comment

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

You'd need to update comments to make sense after the changes.

@meldiner
Copy link
Author

meldiner commented Dec 7, 2016

@gaearon I fixed the issues you referred to but removing the webpack dependency caused a build error.

Should I add webpack as a peer dependency to react-dev-utils? - doesn't fix the build

Should I remove the webpack dependency from the EnvDefinePlugin code altogether?
By making EnvDefinePluginreturn a DefinePlugin json configuration and wrapping it with a DefinePlugin instance only in the react-scripts package (that already includes webpack as a dependency) I can remove webpack from being a react-dev-utils dependency.

@meldiner
Copy link
Author

@gaearon, removed the webpack.DefinePlugin dependency

@gaearon
Copy link
Contributor

gaearon commented Feb 24, 2017

Hey, sorry for the trouble. I think we won't go ahead with this after all. 😞

We've been changing how env variables work recently (since they're also useful in index.html) and we might change that code later again. So it's premature to abstract this away.

I'm sorry I didn't realize this earlier.

@gaearon gaearon closed this Feb 24, 2017
@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants