Giter Site home page Giter Site logo

kamleshchandnani / react-chunkable Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 1.0 236 KB

:boom: Simplest way to code split and load async chunks using Webpack and React Router V4

License: MIT License

JavaScript 100.00%
codesplitting react webpack react-router-v4 async-chunks magic-comments

react-chunkable's Introduction


react-chunkable ๐Ÿ•

Simplest way to code split and load async chunks

Prerequisite

Webpack 2.x.x/3.x.x
React Router 4.x.x

Installation

yarn add react-chunkable

or

npm install react-chunkable --save

Usage

Load as you go. In a traditional SPA, Instead of downloading the entire bundle on the client side, split your code which makes your user to download the piece of code as and when required. Code splitting is a Webpack feature that enables a JS bundle within a single build to be split up and loaded on-demand in smaller parts.

Without Code Splitting

The entire app is bundled in a single js file i.e HomePage and ProfilePage are loaded at the first start itself, though the ProfilePage is not required at the first start.

import HomePage from "./pages/home";
import ProfilePage from "./pages/profile";

class Routes extends PureComponent {
  render() {
    return (
      <Router>
        <Switch>
          <Route exact={true} path="/" component={props => <HomePage {...props} />} />
          <Route path="/profile/:username" component={props => <ProfilePage {...props} />} />
        </Switch>
      </Router>
    );
  }
}

With Code Splitting

With Code Splitting only the HomePage is loaded at the first load. When the user visits ProfilePage at that time the chunk for ProfilePage will be loaded.

import ComponentChunk from "react-chunkable";

const HomePage = props =>
  (<ComponentChunk
    componentProps={props}
    loadChunk={import(/*  webpackMode: "lazy",webpackChunkName: "home" */ "./pages/home")}
  />);

const ProfilePage = props =>
  (<ComponentChunk
    componentProps={props}
    loadChunk={import(/*  webpackMode: "lazy",webpackChunkName: "profile" */ "./pages/profile")}
  />);

class Routes extends PureComponent {
  render() {
    return (
      <Router>
        <Switch>
          <Route exact={true} path="/" component={props => <HomePage {...props} />} />
          <Route path="/profile/:username" component={props => <ProfilePage {...props} />} />
        </Switch>
      </Router>
    );
  }
}

Magic Comments

Webpack 2.4.0 introduced one feature called "magic comments". Now you can name the chunks corresponding to the modules/components you import.

import(/* webpackMode: "lazy",webpackChunkName: "profile" */ "./pages/profile") this statement indicates webpack to consider this as a split point and load it in a separate bundle.

webpackMode: "lazy" indicates webpack to lazily load this chunk.

webpackChunkName: "profile" allows you to name your chunk.

More Info on other available options

Production

In the project directory, you can run: yarn build Builds the library for production to the dist folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

Example

Check out the example applications to see how simple this is.

SSR?

Well, really do you need it? Check this

Support

Please open an issue for support.

Like it?

โญ this repo

License

MIT ยฉ Kamlesh Chandnani

react-chunkable's People

Contributors

kamleshchandnani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mozinrat

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.