Sitemap
gURLmeetsCode

gURLmeetsCode

Full Time Programmer && Part Time Unicorn

Webpack 4 && React Loadable

4 min readJul 30, 2019

A tale of code splitting and react lazy loading

Press enter or click to view image in full size

Straight from the website, here is what Webpack does:

webpack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset.

There are so many new wonderful features in Webpack 4 but I only wanted to highlight the two I have worked with so far.

  1. It’s amazingly fast, build times decreased from 60 to 98%s
  2. Overridable API called optimization.splitChunks

So as a new developer, you may ask why does this matter? Well, being able to code split / lazy load your webpage does amazing things for user experience. Imagine waiting more than 5 seconds for your page to load, that would totally suck, wouldn't it? Webpack split chunks feature paired with React Loadable will take your loading speed to the next level.

What is React Loadable? React Loadable is Component-centric code splitting and loading in React. So in plain english, it provides a higher order component that can be set up to dynamically import specific components at specific times. React Loadable makes code splitting possible in a React application. Feel free to read more from the creator here.

What is code splitting? Code splitting is a technique where an application only loads the code it needs at the moment, and nothing more. You only load what you need in that moment in time. Prior to this, most applications would load everything all at once. Again, try to imagine the loading times for these types of applications…

Ok, I know we have covered alot of ground but hopefully, you are still with me. So let’s see this in action. Check out the code:

Here is how we would implement react loadable’s HOC (Higher Order Component). In this example, index.js is at the highest part of the component tree.

Press enter or click to view image in full size

In this example, we are using the route-based splitting. In this secenario components are loaded according to the current route. So let’s go over what this code is doing, we pass a function as a value to loader which returns the component, and we make use of import() to dynamically import it. The fallback UI we want to render before the component is imported is returned by loading(). In this example, we are returning a div element, though we can also put a component in there instead if we want.

Get gURLmeetsCode’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s look at how we would add webpack to the mix

Press enter or click to view image in full size

If you have used webpack before alot o this may look familar so I will not go into detail of every line of code. Let’s specifically look at output, plugins and optimization.

On line 15, we added the chuckFIlename for dynamic importing. This option determines the name of non-entry chunk files.

As for the plugins, we are leveraging CleanWebpackPlugin which allows us to start off with a fresh build pack every time you run a webpack script. We are also using the HtmlWebpackPlugin. The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation. You can either let the plugin generate an HTML file for you, supply your own template using lodash templates, or use your own loader.

In this scenario, the HTML rendered is coming from template.html and then is being loaded into our public folder. The reason you would use this is so you can properly link your webpack bundle(s) to the HTML.

As for the optimization, this prevents duplication. Prior to ‘SplitChunksPlugin’ we were using the CommonChunkPlugin. The SplitChunksPlugin allows us to extract common dependencies into an existing entry chunk or an entirely new chunk. Cache groups can inherit and/or override any options from splitChunks.*; but test, priorityand reuseExistingChunk can only be configured on cache group level.

In this example, we created a vendors chunk, which includes the initial code from node_modules in the whole application. This method allows us to only include our core frameworks and utilities and dynamically load the rest of the dependencies.

Here is what you get when you run your bundling script

Press enter or click to view image in full size

Now we have an application that will run lightening fast!

Want more information, check out this cool youtube video!

Happy Coding, folks!!!

--

--

gURLmeetsCode
gURLmeetsCode

Written by gURLmeetsCode

Full Time Programmer && Part Time Unicorn