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

"react-hot-loader/babel" includes file path in production mode #789

Closed
TheAifam5 opened this issue Jan 14, 2018 · 19 comments
Closed

"react-hot-loader/babel" includes file path in production mode #789

TheAifam5 opened this issue Jan 14, 2018 · 19 comments

Comments

@TheAifam5
Copy link

TheAifam5 commented Jan 14, 2018

Description

I found that "react-hot-loader/babel" includes files path in production mode, also when "process.env.NODE_ENV" is defined to "production".
After removing from webpack configuration and babelrc, everything looks fine.

Expected behavior

There shouldn't be any paths in production mode.

Actual behavior

"react-hot-loader/babel" includes files path, ignoring the environment type.

Environment

React Hot Loader version: next (today refreshed)

node -v: v9.4.0
npm -v: 5.6.0
Operating system: Windows 10
Browser and version: Firefox Nightly 59.0a1

My webpack config to prevent including file paths inside the compiled scripts.

{
  loader: 'babel-loader',
  options: {
    babelrc: true,
    ... env.name === 'development' ? {
      plugins: ['react-hot-loader/babel'],
    } : {},
  },
},
@theKashey
Copy link
Collaborator

Duplicates #692, please check your configuration, ie does dead code elimination works.

@opr
Copy link

opr commented Feb 1, 2018

@theKashey
Copy link
Collaborator

You have proven, that webpack could remove "dead" branches, and by "dead" I mean with const conditions.
Let's try to search inside your bundle next string

if (process.env.NODE_ENV === 'production') {

It does exist, thus it is not constant, thus could not be evaluated during the complication, and next removed as a dead code.

PS: You also got 2 version of React. Hooray!

Try replace

new webpack.DefinePlugin({
            "process.env": {
                NODE_ENV: JSON.stringify("production")
            }
        }),

Into.

new webpack.DefinePlugin({
'process.env.NODE_ENV': environment,
});

Now we have exact variable name, and webpack will do the rest of magic.

@opr
Copy link

opr commented Feb 1, 2018

Hi @theKashey thanks for your response

I made the change you suggested to the webpack.DefinePlugin.
https://github.com/opr/spooky/blob/109ccb46ee3a316be471b0cc68acb4c9cfb1423a/webpack.production.config.js

I looked inside the uglified bundle and could not find that string - this is the uglified bundle again for reference: https://github.com/opr/spooky/blob/109ccb46ee3a316be471b0cc68acb4c9cfb1423a/assets/js/dist/spooky.min.js

If you observe line 11 here:

https://github.com/opr/spooky/blob/109ccb46ee3a316be471b0cc68acb4c9cfb1423a/assets/js/react/TestInput/TestInput.jsx

you will see I have tested the environment is being set correctly. Inside the uglified bundle I can still see paths and I can also see my "BANANAS" test.

(Please don't worry about the non-uglified bundle as I am not using the DefinePlugin there)

I hope this provides more information.

@opr
Copy link

opr commented Feb 1, 2018

I have discovered that my problem was the same as #602 (comment)

Thank you for the help and apologies for missing this!

(PS what do you mean that I have 2 versions of react?)

@theKashey
Copy link
Collaborator

PS: React also has dev and prod variant, and in modern versions this is 2 big flat(single file) bundles.

@lili21
Copy link

lili21 commented Jul 16, 2018

still have the issue.
I'm using react-hot-loader@4.3.3

@lili21
Copy link

lili21 commented Jul 16, 2018

@theKashey
Copy link
Collaborator

@lili21 - sorry, but RTFM

You have to

new webpack.DefinePlugin({
     'process.env.NODE_ENV': JSON.stringify('production')
 })

@lili21
Copy link

lili21 commented Jul 16, 2018

I think mode: production already did that for me.

@lili21
Copy link

lili21 commented Jul 16, 2018

new webpack.DefinePlugin({
     'process.env.NODE_ENV': JSON.stringify('production')
 })

doesn't work for me.

NODE_ENV=production webpack --config webpack.prod.js works.

@theKashey
Copy link
Collaborator

That's why I linked an issue about "webpack is not doing it". And providing NODE_ENV is super important.

@pats
Copy link

pats commented Jul 20, 2018

Still, providing NODE_ENV=production doesn't work,

@theKashey
Copy link
Collaborator

You shall not only set NODE_ENV, but also "define" that env for webpack. 2 comments above.

@Sakots2499
Copy link

Only

@martinzachariassen
Copy link

I can confirm that what @theKashey 's saying resolves the issue. Setting --env.mode itself won't do the trick and you have to set the webpack.DefinePlugin (env) to production.

// Npm script
"webpack --env.mode production"

// Webpack config
module.exports = (env) => {}

// In Webpack config plugins
new webpack.DefinePlugin({
  'process.env.NODE_ENV': JSON.stringify(env.mode),
}),

It seems that process.env.NODE_ENV must be set exactly like it's typed, as process.env won't suffice. Also important that the env.mode is stringifyed.

Thanks @theKashey.

@rflmyk
Copy link

rflmyk commented Nov 29, 2018

I'm have the same problem, I try use workaround @m7dev but... dont work for me, i'm using:

    ...
    "webpack": "4.26.0",
    "webpack-dev-server": "^3.1.10",
    "react-hot-loader": "^4.3.12"

Someone has a workaround or fix for that?

@davincho
Copy link

My issue was that https://github.com/gaearon/react-hot-loader/blob/master/babel.js was using the dev version even when building for production.

So it was not about setting up webpack.DefinePlugin correctly, but making sure babel is aware as well, like NODE_ENV=production webpack --config webpack.prod.js

@rflmyk
Copy link

rflmyk commented Nov 29, 2018

Just it, work for me, but I using a solution a litle diferent, take a look:

on my webpack.config.js I have

const IS_DEV = (argv.mode === 'development');

to check mode.

I create a:

        ...
	const BABEL_PLUGINS = [
		'lodash',
		'@babel/plugin-syntax-dynamic-import',
		'@babel/plugin-transform-react-jsx',
		'transform-class-properties',
		'@babel/plugin-proposal-object-rest-spread',
		'@babel/plugin-transform-runtime',
		'@babel/plugin-syntax-async-generators',
		'@babel/plugin-transform-regenerator',
	];

	IS_DEV && BABEL_PLUGINS.push('react-hot-loader/babel');
        ...

And below I do it:

        ...
        {
            loader: 'babel-loader',
            options: {
                compact: true,
                presets: [
                    '@babel/preset-env',
                    '@babel/preset-react'
                ],
                plugins: BABEL_PLUGINS
            }
        }
      ....

Now work very well ;) tnks @davincho

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

10 participants