npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

manual-chunk-plugin

v2.0.0

Published

Webpack 2, 3 plugin for splitting chunk file via manual assignment

Downloads

15

Readme

Manual chunk plugin

This is a Webpack 2 plugin for splitting chunk file via manual assignment. (Which I believe is more flexible for multi-entry application)

Unlike Webpack's CommonChunkPlugin

  • It is not limited for only creating app, common, manifest chunks it can create unlimited number of chunks.
  • For multi-entry project or 'vendor' + 'app' like project, you don't need to put mutiple "CommonChunkPlugin" in config file.

Each module can only be assigned to 1 chunk file during that compilation, you get to wisely decide what chunk name that module should be assigned to. Plugin will maintain correct relationship (parent, child, async) between these chunks of entire compilation. Totally support splitloading chunk as what require.ensure() creates.

Assume you have a project with 3 entries app1, app2, app3, while app1, app2 depends on lib and node_modules/module1, app3 only depends on node_modules/module1

process.cwd()
├─── app1/
│    ├─── index.js
│    └─── stylesheets
│          └─── style1.css
├─── app2/
│    ├─── index.js
│    └─── stylesheets
│          └─── style2.css
├─── app3/
│    ├─── index.js
│    └─── stylesheets
│          └─── style3.css
├─── lib/
│    └─── index.js
│
└─── node_modules/
      └─── module1/index.js, package.json, ...

e.g. In webpack.config.js, assume I use top level directory name as chunk name for those modules.

plugins: [
	new ManualChunkPlugin({
		// The chunk name for manifest
		manifest: 'runtime',
		// The default chunk name if getChunkName(file) returns null for that module
		defaultChunkName: 'vendor-lib'), 
		// A required callback function that returns chunk name of each "module",
		// Returning same chunk name for different "module" leads to put then into same chunk file
		getChunkName: (file, originalChunk) => {
			var relativePath = path.relative(process.cwd(), file);
			var dirName = relativePath.split(path.sep)[0];
			return dirName === 'node_modules' ? 'vendor-lib' : dirName;
		},
		// An optional callback function that returns chunk name of split-load "module":
		// function(file: string, originalChunk: Chunk) => string
		// default behaviour is appending ".split" to the string returned by `opts.getChunkName()`.
		// By default split-load chunk's name is null, when this plugin kicks in
		//  1) If chunk is a initial chunk, will name that chunk with `opts.getChunkName(file)`
		//  2) If chunk is an async(split-load) chunk, will name that chunk with `opts.getAsyncChunkName(file)`
		//     or `opts.getChunkName(file) + ".split"`
		getAsyncChunkName: (file, originalChunk) => this.opts.getChunkName(file, originalChunk) + '.split'
	}),
	...
]

Eventually, compilation comes up with 5 chunk assets

  • app1.js
  • app2.js
  • app3.js
  • lib.js
  • vendor-lib.js
  • runtime.js

Their relationship is like (you can hack chunks property from Webpack compilation object to oversee parent-child relationship):

  • app1.js ───> lib.js ───> vendor-lib.js ───> runtime.js
  • app2.js ───> lib.js ───> vendor-lib.js ───> runtime.js
  • app3.js ───> vendor-lib.js ───> runtime.js

Chunk runtime is the small Webpack manifest chunk which changes every time, you'd better use some plugin to inline it into all your entry HTML files.

So I create 2 common chunks for 3 entries so that if a client will access all 3 tree of these entries, all chunks can be cached in browser, but on duplicate modules will be downloaded.

Or I can also combine lib and vendor-lib into a single chunk, every combination is under my explicit controll.

The good part of this plugin in contrast with CommonChunkPlugin is that, it is very streightforward to understand how chunks are actually splitted, but as for CommonChunkPlugin you may need to manually put "vendor" chunk in config file it you want to split more than 1 common chunk, which is not the part I want it to be manual.

It plugin doesn't come up with unit test file, because it is just seperated from my company's common architecture project https://github.com/dr-web-house (Document) as a part and covered by its integration test.