Coder Social home page Coder Social logo

jeantimex / react-webpack-code-splitting Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 3.0 263 KB

:trident: A demo of how to split the code for a React Webpack application.

License: MIT License

HTML 2.31% JavaScript 97.69%
webpack2 code-splitting react-router-v3 react webpack-dev-server

react-webpack-code-splitting's Introduction

How to Code Split with React Router and Webpack

rocket

About

Webpack is an awesome tool for bundling front end assets including JavaScript, CSS etc. The output bundle size can be big and that will cause loading slowness and poor user experiences, we should only load the assets on demand for each route.

Luckily React Router allows us to load the components asynchronously and Webpack can bundle the components into chunks. For example, there are three routes, and Each route is associate with a React component:

  • /home (Home.js)
  • /about (About.js)
  • /users (Users.js)

You don't want to bundle them all into one giant App.js. In React Router, we can use System.import (or requre.ensure) to load each component for each route, like so:

getComponent(location, cb) {
  require.ensure([], (require) => {
    cb(null, require('./components/Home').default)
  }, 'Home'); // Output Home.chunk.js
}

or

getComponent(location, cb) {
  System.import('./components/Home' /* webpackChunkName:'Home' */)
    .then(loadRoute(cb, false))
    .catch(errorLoading);
},

Then all we need to do in the Webpack config is to specify the chunk filename:

output: {
  ...
  chunkFilename: '[name].[chunkhash].chunk.js',
  ...
},

Now when you navigate to each route, only the necessary component will be loaded.

Quick start

  1. Clone this repo using git clone https://github.com/jeantimex/react-webpack-code-splitting.git
  2. Run yarn or npm install to install the dependencies
  3. Run yarn start and see the example app at http://localhost:3000

License

This project is licensed under the MIT license, Copyright (c) 2017 Yong Su. For more information see LICENSE.md.

react-webpack-code-splitting's People

Contributors

jeantimex avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.