Difference between production and development build in ReactJS - reactjs

Recently I started learning react and I saw a tutorial where they used Webpack to create the production and development builds. But there was no explanation on what the difference between those two builds is and which one you have to use when. I searched the internet but didn't find anything that helped me. Does anyone have a tutorial or an explanation that I missed/didn't read?

The development build is used - as the name suggests - for development reasons. You have Source Maps, debugging and often times hot reloading ability in those builds.
The production build, on the other hand, runs in production mode which means this is the code running on your client's machine. The production build runs uglify and builds your source files into one or multiple minimized files. It also extracts CSS and images and of course any other sources you're loading with Webpack. There's also no hot reloading included. Source Maps might be included as separate files depending on your webpack devtool settings.
What specifically separates production from development is dependent on your preferences and requirements, which means it pretty much depends on what you write in your Webpack configuration.
The webpack-production documentation is very straight-forward.
Also, the article Webpack 3 + React — Production build tips describes the process of creating production builds for React with Webpack pretty good.

The very basic difference is that Production Build has ugly, minified(compressed) version of your javascript code, so this makes rendering of file on end user's browser very quick and performance enhancing.
You can also verify if production build is being used in the website by applying a google plugin extension, which when activated on your browser, will always tell you if the website is using react js on the front end and also tells whether the build type is production or development.
when react is development build,
production-ready versions of React and React DOM as single files are available as well,
<script src="https://unpkg.com/react#16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom#16/umd/react-dom.production.min.js"></script>
NOTE: Remember that only React files ending with .production.min.js are suitable for production.
The production and development build come into the picture just because of performance impact in real life deployed the application. Also, it happens that the location where the application is deployed is another continent altogether, so rendering development build js files on UI will take a hell of a time as compared to production version which is very crisp, compact, compressed, uglified for better user experience and loading on UI. for information CLICK HERE

react.development.js provides us extra features like debugging, hmr(Hot module reloading) and lots of other stuffs that you might
use while developing app with the help of bundlers like webpack, parcel, vite. This bundler bundles and minifies our code to be
deployed on production
These minified files will be deployed on production which removes lots of unnecessary files which will not be used by our app
for this we have react.production.js to make our much faster(as bundlers and lots of other files have done there work and are not required now)

Related

Which is better way to create a react app?

I have been working with reactjs from last 7-8 months, I have always used create-react-app to get started with any react application. but, by exploring more ways to get started with a react application I came to know there is a thing called vite which is I guess is providing a faster and leaner development experience for modern web projects.
I used it once till now, not in production yet as I am not very confident about it. So, which is a better way to get a simple template for react app. which is also better in production environment. Does using any one affects in production?
CRA uses webpack to handle its core functionalities. In the development webpack repeats the bundling process every time there is a change in the code. As a result, when your source code grows larger, everything becomes sluggish. The time it takes to serve a dev server and build your projects increases significantly.
Vite only needs to pre-bundle your dependencies using esbuild the first time you start development before it begins to serve your app.
Vite doesn’t need to bundle the entire app or transpile the modules and code before starting a dev server; transpiling is done on-demand, thus making it significantly faster than CRA.
https://blog.logrocket.com/vite-3-vs-create-react-app-comparison-migration-guide/#:~:text=Vite%20and%20CRA%20are%20not,and%20which%20modules%20are%20supported.

Install libraries with NPM or import from CDN?

I'm confused why all the React tutorials I'm using to learn React have me installing libraries through npm rather than importing through CDN's (React, Redux, Material UI, firebase, etc). I thought CDN's were more efficient.
Any guidance on this would be helpful (this is my first time using VS code and installing packages like this, the only other website I've programmed I just imported bootstrap and jquery through CDN and coded in notepad++ :/)
While you can of course use a CDN to import scripts into your application (with, for example a <script> tag) npm is useful in the fact that when you build your React app it will bundle all the required modules together.
This can be advantageous in the fact that your users don't have to download any scripts when your application loads (which can take some users with slower connections a long time) since everything is bundled within your React app. Some applications depend on a lot of dependencies, and forcing every new user to download each script individually can cause speed issues.
At the end of the day, I don't think there is a right or wrong answer as to whether you should use a CDN or NPM, just understand that when you use NPM it bundles the required scripts into your app when you run npm run build instead of the user having to download them when they visit your site.
CDN's will only get you so far. In the industry you're going to be expected to have some experience with npm/yarn. If you keeping going down the frontend rabbit hole, you'll eventually run into things like Webpack and Babble which will help make your Javascript a lot more efficient and backwards compatible. Installing things through npm/yarn will also often offer a lot in terms of providing you with a better development environment (things like react-scripts, eslint, prettier, etc).

SvelteKit split second unstyled html

I created an base app though the sveltekit cli commands. The options I chose are scss and typescript. At soon as I started the application for a split second I saw unstyled html. This happens every time and with every project i create. I did some testing and it seems like the css(app.scss) is loaded after the html(localhost). Another this that seems consistent is that is happens every time i reload the page, but not with navigating. This indicates that it is probably server-side.
In my mind the html and css should load in the same file, though SvelteKit might have a different approach.
I had the same issue with Sapper and solved that. But I forgot how I fixed it. Also with the new SvelteKit setup many things are different. Do you guys know how to fix this?
Thanks in advance
SvelteKit does not bundle everything together during development mode and injects support for HMR. This may result in flashes of unstyled content.
The flashes of unstyled content should go away in production mode, when you deploy with an adapter.
This stock SvelteKit template does not flash unstyled content. The only change was changing the adapter from the node adapter to the static adapter so it could be hosted on Netlify. (I have also confirmed with the Netlify adapter, so it's not a difference between static and dynamic.)
How to test with the stock SvelteKit template:
npm init svelte#next
yarn
yarn dev --open # Dev server: flashes of unstyled content.
yarn build
cd build
node index.js # Production server: no flashes of unstyled content.
Explanation from SvelteKit announcement:
Right now, we're seeing the rise of the unbundled development
workflow, which is radically simpler: instead of eagerly bundling your
app, a dev server can serve modules (converted to JavaScript, if
necessary) on-demand, meaning startup is essentially instantaneous
however large your app becomes.
...
That's not to say we're abandoning bundlers altogether. It's still
essential to optimise your app for production, and SvelteKit uses
Rollup to make your apps as fast and lean as they possibly can be
(which includes things like extracting styles into static .css files).

How can I know if everything actually optimized?

I built a simple react app using Gatsby. All that the app does create a calender and show it to the screen. When I checked the performance of the app using LightHouse, I got 90% on desktop perf and 31% on mobile perf.
This is a bare bone site. I thought Gatsby would convert my react code to HTML and that is supposed to load blazing fast...
So I checked the html of the page, </script></head><body><div id="___gatsby"></div><script src="/polyfill.js" nomodule=""></script><script src="/commons.js"></script></body></html>
It looks like the whole page just runs from JS script; nothing is actually converted to HTML.
What did I do wrong? I ran the site using npm run develop?
When you run gatsby develop all page requests are served the barebones HTML shell that loads a development-targeted version of Gatsby that will render everything client-side only and integrates hot-module reloading1. No minification is performed on your source files this way, React is in development mode, and there are streaming connections opened to the dev server to listen for changes.
When you run gatsby build, Gatsby will create static HTML file entry points to each of your pages that contain the pre-rendered page in an optimized format. Each of your plugins should also produce optimized output, such as inlining critical CSS or generating minified CSS classnames. JavaScript will be tree-shaken, minified, and bundled in chunks by webpack, and React will be loaded in production mode.
If you'd like to test the performance of your Gatsby site before deploying, you can get a rough indicator by running gatsby build and then gatsby serve, then running your Lighthouse audit on that page. Keep in mind that this won't test the network or server performance of your live site, so it isn't going to match precisely (but should at least catch content related issues).
1 This is actually set to change starting with v2.3.0, which enables SSR during development.

Gradle for AngularJS Application

I´m pretty new to gradle and currently there is question that bugs me. The situation appears as follows: Based on a bower technology stack I implemented an Angular App. The app as it is doesn't change nor has it to be built in any way since there are just static javascript and HTML pages. In my opinion the used versions of angular, bootstrap and other libraries should also stay the same due to compatibility of the single libraries so these files also shouldn't change. Is this a correct behavior or should I get at least the latest build of the used libraries version as I deploy the application?
Also less is used in the application. Is there a way to compile the CSS every time I run the gradle build file or should I deliver just the compiled and finished CSS file?
As a result I´m also not quite sure if it's recommended at all using gradle to deploy an "static" angular application.
I hope someone out there can help me to answer the questions above. As you can guess, I´m not very experienced at deploying of such angular applications since this is my first project with this kind of problem.
This goes for all package managers, not just gradle but npm, gem, nuget, maven, whatever.
Use static dependancy version numbers. Otherwise you will end up finding breaking dependencies in QA or Prod rather than production.
This means you need to be aware of security fixes in your dependencies.
When you need a feature or fix in a new version of your dependencies, unlock the versions, rebuild and test in dev. Re-lock the dependencies and send to QA for verification.

Resources