All Projects → niieani → chunk-splitting-plugin

niieani / chunk-splitting-plugin

Licence: MIT License
Arbitrarily split your Webpack chunks and bundles into smaller pieces

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to chunk-splitting-plugin

chunk-progress-webpack-plugin
Provides runtime progress events by replacing default webpack chunk loading with XHR
Stars: ✭ 17 (+13.33%)
Mutual labels:  webpack-plugin, chunk
React Refresh Webpack Plugin
A Webpack plugin to enable "Fast Refresh" (also previously known as Hot Reloading) for React components.
Stars: ✭ 2,413 (+15986.67%)
Mutual labels:  webpack-plugin, hot-reload
Html Res Webpack Plugin
plugin for generating html in webpack
Stars: ✭ 170 (+1033.33%)
Mutual labels:  webpack-plugin, chunk
amd-cmd-hot-update-hmr
esl-hot-update: Hot update esl modules(AMD、CMD) when modifed. JS, LESS, tpl, component is all supported!
Stars: ✭ 25 (+66.67%)
Mutual labels:  hot-reload
license-info-webpack-plugin
Making a list of package's LICENSE information for webpack
Stars: ✭ 20 (+33.33%)
Mutual labels:  webpack-plugin
flutter dynamic
The flutter_dynamic is a library that create flutter application dynamic.
Stars: ✭ 66 (+340%)
Mutual labels:  hot-reload
add-module-exports-webpack-plugin
Add `module.exports` for Babel and TypeScript compiled code
Stars: ✭ 36 (+140%)
Mutual labels:  webpack-plugin
rbfx
Game engine with (optional) C# support and WYSIWYG editor.
Stars: ✭ 511 (+3306.67%)
Mutual labels:  hot-reload
hotswap
Hotswap provides a solution for reloading your go code without restarting your server, interrupting or blocking any ongoing procedure.
Stars: ✭ 71 (+373.33%)
Mutual labels:  hot-reload
asset-graph-webpack-plugin
Webpack plugin to easily get assets dependency graph based on entry point
Stars: ✭ 13 (-13.33%)
Mutual labels:  webpack-plugin
robotstxt-webpack-plugin
A webpack plugin to generate a robots.txt file
Stars: ✭ 31 (+106.67%)
Mutual labels:  webpack-plugin
base-href-webpack-plugin
Webpack plugin for inserting base href tag in head block
Stars: ✭ 23 (+53.33%)
Mutual labels:  webpack-plugin
prettier-webpack-plugin
Process your Webpack dependencies with Prettier
Stars: ✭ 47 (+213.33%)
Mutual labels:  webpack-plugin
LightCycle
React + Redux + TypeScript + Webpack + HotReload Boilerplate
Stars: ✭ 16 (+6.67%)
Mutual labels:  hot-reload
react16-seed-with-apollo-graphql-scss-router4-ssr-tests-eslint-prettier-docker-webpack3-hot
Seed to create your own project using React with Apollo GraphQL client
Stars: ✭ 19 (+26.67%)
Mutual labels:  hot-reload
webpack-ext-reloader
Add hot reloading to your webpack WebExtension! 🔥
Stars: ✭ 31 (+106.67%)
Mutual labels:  webpack-plugin
dva-typescript-antd-starter-kit
A admin dashboard application demo based on antd by typescript and dva
Stars: ✭ 61 (+306.67%)
Mutual labels:  webpack-plugin
react-ssr-spa
Server side rendered single page app using reactjs official libraries.
Stars: ✭ 30 (+100%)
Mutual labels:  hot-reload
tinyimg-webpack-plugin
A webpack plugin for compressing image
Stars: ✭ 61 (+306.67%)
Mutual labels:  webpack-plugin
copy-modules-webpack-plugin
A Webpack plugin which copies module sources to a separate directory
Stars: ✭ 17 (+13.33%)
Mutual labels:  webpack-plugin

DEPRECATED AND NO LONGER MAINTAINED. FUNCTIONALITY INCLUDED NATIVELY IN WEBPACK. PLEASE DO NOT USE.

ChunkSplittingPlugin for Webpack

Greenkeeper badge

Arbitrarily split your Webpack chunks and bundles into smaller pieces.

asciicast

Uses

Watch and HMR

One of the use cases is optimizing development-time performance when using watch, live-reload or HMR. If you have huge chunks (few MB each), Webpack needs to combine all the modules of the affected chunks and their entry points each time a change happens. If you split your chunks into smaller pieces, Webpack will only need to re-glue those smaller pieces.

Splitting for production

You could also use this as a production-time plugin in place of the (currently buggy) AggressiveSplittingPlugin. The main difference is that the default ChunkSplittingPlugin's segregator allows you to configure the maximum number of modules per file, not the maximum and minimum size of each chunk.

However, the splitting behavior is entirely configurable, via the segregator parameter, which must be a function when provided. See Customizing new chunks for details.

Installation

Install it with npm

npm install chunk-splitting-plugin --save-dev

or with yarn

yarn add chunk-splitting-plugin --dev

Usage

The simplest way to configure the plugin is to set maxModulesPerChunk and maxModulesPerEntry.

// webpack.config.js
const ChunkSplittingPlugin = require('chunk-splitting-plugin')

module.exports = {
  // (the rest of your config...)
  plugins: [
    new ChunkSplittingPlugin({
      maxModulesPerChunk: 10,
      maxModulesPerEntry: 1,
    })
  ]
}

The minimal numbers for respectful options are:

  • maxModulesPerChunk: 1 one chunk per each module
  • maxModulesPerEntry: 0 entry will only contain the Webpack manifest

The correct order of loading chunks

If you'd like to manually load chunks (i.e. hand-craft the index.html), you need to load all the parts first, and finally the entry, which will execute the code. Like this:

  • chunk-part-1.bundle.js
  • chunk-part-2.bundle.js
  • chunk-part-3.bundle.js
  • chunk-part-4.bundle.js
  • chunk.bundle.js

Configuring generated chunk names

By default, new chunks will be named {CHUNK_NAME}-part-{#NEW_CHUNK_NUMBER}.

If the chunk does not have a name, the parts will likewise remain unnamed.

You can configure this by passing a getPartName function, like this:

new ChunkSplittingPlugin({
  maxModulesPerChunk: 5,
  maxModulesPerEntry: 0,
  getPartName: (sourceChunk, index) => sourceChunk.name && `${sourceChunk.name}-part-${index + 1}`,
})

You could, for example use maxModulesPerChunk: 1 and name each chunk like the module it contains to simulate an unbundled environment, similar to JSPM or SystemJS.

Customizing the contents of new chunks

You can customize the logic by which the plugin decides which modules end up in which chunk by passing a segregator function instead of the maxModulesPerChunk and maxModulesPerEntry options.

new ChunkSplittingPlugin({
  segregator: (chunk, isEntry) => {
    // Source modules are in the chunk.modulesIterable Set.
    // You must return an Array of Sets that contain modules from the chunk.
    // New chunks will be created, based on each group of modules returned.
    // If 'isEntry' is true, the first returned group
    // will become the new entry chunk.

    // Any modules that aren't returned here
    // will remain in the original chunk.
    const modules = Array.from(chunk.modulesIterable)
    // For example:
    return [new Set(modules.slice(3, 2)), new Set(modules.slice(5))]
    // will cause:
    // - the original chunk to contain the first 3 modules
    // - create two new chunks:
    //     1. containing 2 modules
    //     2. containing the remaining modules (if any)
  }
})

Acknowledgements

This module's code is heavily inspired by @sokra's CommonsChunkPlugin, native to Webpack.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].