I'd like to serve static files, without webpack in my production build. However, there seems to be an instance of webpack, with source-maps to my source files being bundled and pushed to production. I've tested with both Heroku, and Google Cloud, with similar results. I've even tried serving the build directory exclusively, as suggested by this repos documentation, but the webpack with source files always persists on deploy to production. Is there a way to disable webpack/source-maps?
I figured it out. Looks like we have to eject to disable the source maps inside of our production webpack config. Steps are outlined below..
create-react-app
by running npm run eject
/config/webpack.config.prod.js
devtool: 'source-map',
The annotations inside of /config/webpack.config.prod.js
admit generating sourcemaps in production "is slow but gives good results". I'm not sure what "good results" mean, nor do I know why we would use a webpack devtool
for a production build. If anybody could shed light on why we push webpack source maps to production by default, that would be great.
You simply need to remove the .map files from the build directory. There's no need to eject.
A source map is a great way of debugging once in production, and it would be a crime to not generate them.
You can easily opt out, though!
You can modify your package.json file and append "&& rm build/*/.map" to your build command.
Hope this helps.
@Timer The map file is a huge file, does it affect performance on production?
should I remove the map before deploying it?
No, it has no effect on production. Sourcemaps are only downloaded if you open the browser DevTools.
@gaearon Thank you. I tested the production file with lite-server. And when I run on the terminal window does not load the source map file. Until I open the dev-tool and it loads the source map ... 馃憤 !
@gaearon Thanks for that explanation. Why isn't this mentioned here in README.md?
For those like me who arrive from Google, you can now disable sourcemaps at build time by setting the following environment variable:
export GENERATE_SOURCEMAP=false
yarn build
In my deployment script (on a CI server) I just added this export before the build & deploy to S3 process and it works like a charm :)
I note that webpack's documentation includes source map-related warnings:
Sourcemaps are only downloaded if you open the browser
Yup, but on what propose would you consider generating source-map for production? please take a look at Webpack's official docs.
In windows use:
SET GENERATE_SOURCEMAP=false
yarn build
For those like me who arrive from Google, you can now disable sourcemaps at build time by setting the following environment variable:
export GENERATE_SOURCEMAP=false yarn build
Most helpful comment
No, it has no effect on production. Sourcemaps are only downloaded if you open the browser DevTools.