I need to minify my code on server.
It tells me that npm run build
do all the minifying stuff
but on my server (of course I access it not via localhost
) it shows all the code.
I am doing npm run build
in pom.xml
in maven.
My package json contains actual ver of react-scripts.
All code is served with nginx.
Ok, I’ve seen this
When does create-react-app obfuscate or minify code?
and this GENERATE_SOURCEMAP=false Issue
and read this https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-howgenerate
where it tells me that Developer tools (currently WebKit nightly builds, Google Chrome, or Firefox 23+) can parse the source map automatically and make it appear as though you're running unminified and uncombined files.
So it tells me that source maps is the reason I see my code as is.
And there is a Firefox Browser without devTools where I turned off source maps
and…
It shows me the code as is!
Why it still shows me the code despite "npm run build
makes build as minified"?
Please tell me where I’m bad and how could I fix it? And BTW, everywhere it is written that *.env should be in the root directory. Root directory of whole project or only frontend folder?
2
Answers
Well,
We should go on server and in sources type js like file extension. If they are plain to see - minimizing (obfuscating and uglifying) is not working (that was my case)
doesn't work for me. I've created permission.env and added
into it and then run
I've attached screenshot where I searched in generated build for *.map extension files. They are here even we created the *.env file.
So I've added
in my
package.json
and runnpm run build
once again. We can see there are no map files - so that command helped us.I get the sense that most of your question is actually related to confusion over what minifying code actually means.
Definition
Source: Why Minify JavaScript Code?
Applied to Create React App
In your case, the
npm run build
command will executereact-scripts build
which is provided by Create React App. The documentation explains what this does:Source: Create React App: npm run build
When
npm run build
is run, it will create abuild
folder. Inside./build/static
you’ll typically find your minified code (javascript, css, media, …).In
./build/static/js
you’ll likely find the following files:Note: the exact output will vary on your version of Create React App.
What are Source Maps?
As you’ll have noticed above, the build folder contains a
*.js.map
file for each.js
file, these are source maps.The reason these are generated is for debugging purposes.
If you were to try and debug your code using the developer tools of your browser, you’d have a hard time understanding your minified (and possibly uglified code). By adding these
.map
-files, your browser can load these (by using an HTTP header) and represent your code as if you were looking at the original code.You can find more information here: MDN – Use a source map
Developer tools "Inspector"
Every major browser has a way of viewing or debugging your application using "developer tools". These developer tools usually show a version of your code that is modified to be easier to read as a developer.
If you wanted to verify if your HTML code is minified or not, there are 2 things you could check:
./build/index.html
file and ensure that only thebuild
folder is being hosted by your web server