-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] New vendor bundle block #188
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @webpack-blocks/vendor-bundle - Changelog | ||
|
||
## 1.0.0 | ||
|
||
Initial release. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Webpack blocks - vendor bundle | ||
|
||
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
[![NPM Version](https://img.shields.io/npm/v/@webpack-blocks/vendor-bundle.svg)](https://www.npmjs.com/package/@webpack-blocks/vendor-bundle) | ||
|
||
This block for [webpack-blocks](https://github.com/andywer/webpack-blocks) extracts a vendor bundle that includes the most used dependencies of your app. | ||
|
||
## Usage | ||
|
||
```js | ||
const { createConfig } = require('@webpack-blocks/webpack') | ||
const vendorBundle = require('@webpack-blocks/vendorBundle') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change |
||
|
||
module.exports = createConfig([ | ||
vendorBundle(/* options */) | ||
]) | ||
``` | ||
|
||
## Options | ||
|
||
#### name *(default: main)* | ||
|
||
Name of the main bundle of your app. | ||
|
||
#### filename *(default: vendor.[chunkhash].js)* | ||
|
||
Output bundle file name. | ||
|
||
#### minChunks *(default: 5)* | ||
|
||
Minimal number of chunks to include dependency into the bundle. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why wouldn't you want to include a dependency with few chunks into the vendor bundle? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because you may have some big dependencies that you’re using only on several pages, like some fancy chart library. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So my idea was to include dependencies that are used on most pages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhh, it's not about the size of the dependency, but how many chunks are using this dependency. I think the documentation should highlight that 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that’s exactly what is says! ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least I didn't get that it means "number of chunks using the dependency" and I am maintainer 😜 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I’ll update it ;-) |
||
|
||
## Generated webpack config | ||
|
||
By default generates this configuration: | ||
|
||
```js | ||
{ | ||
plugins: [ | ||
new webpack.optimize.CommonsChunkPlugin({ | ||
name: 'main', | ||
filename: 'vendor.[chunkhash].js', | ||
children: true, | ||
minChunks (module, count) { | ||
return module.context && module.context.includes('node_modules') && | ||
module.request.endsWith('.js') && | ||
count >= 5 | ||
} | ||
}) | ||
] | ||
} | ||
``` | ||
|
||
## Webpack blocks | ||
|
||
Check out the | ||
|
||
👉 [Main Documentation](https://github.com/andywer/webpack-blocks) | ||
|
||
Released under the terms of the MIT license. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import test from 'ava' | ||
const webpack = require('webpack') | ||
import { createConfig } from '@webpack-blocks/core' | ||
import vendorBundle from '../index' | ||
|
||
const moduleInNode = { | ||
context: '/Users/gendalf/moria/node_modules/decko/dist', | ||
request: '/Users/gendalf/moria/node_modules/decko/dist/decko.js' | ||
} | ||
const moduleNotInNode = { | ||
context: '/Users/gendalf/moria/webpack/assets/javascripts/components/main/components', | ||
request: '/Users/gendalf/moria/node_modules/babel-loader/lib/index.js??ref--5-0!/Users/gendalf/moria/src/components/Button.js' | ||
} | ||
|
||
test('Vendor bundle default options work', t => { | ||
const config = createConfig({}, [ | ||
vendorBundle() | ||
]) | ||
|
||
t.true(config.plugins[0] instanceof webpack.optimize.CommonsChunkPlugin) | ||
t.false(config.plugins[0].minChunks(moduleNotInNode, 5)) | ||
t.false(config.plugins[0].minChunks(moduleInNode, 2)) | ||
t.true(config.plugins[0].minChunks(moduleInNode, 5)) | ||
}) | ||
|
||
test('Vendo bundle options work', t => { | ||
const config = createConfig({}, [ | ||
vendorBundle({ | ||
minChunks: 42 | ||
}) | ||
]) | ||
|
||
t.false(config.plugins[0].minChunks(moduleNotInNode, 50)) | ||
t.false(config.plugins[0].minChunks(moduleInNode, 20)) | ||
t.true(config.plugins[0].minChunks(moduleInNode, 50)) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Vendor bundle webpack block. | ||
*/ | ||
|
||
const webpack = require('webpack') | ||
|
||
module.exports = vendorBundle | ||
|
||
/** | ||
* @param {object} [options] | ||
* @param {object} [options.name=main] Name of the main bundle of your app. | ||
* @param {object} [options.filename=vendor.\[chunkhash\].js] Output bundle file name. | ||
* @param {object} [options.minChunks=5] Minimal number of chunks to include dependency into the bundle. | ||
* @return {Function} | ||
*/ | ||
function vendorBundle (options = {}) { | ||
options = Object.assign({ | ||
name: 'main', | ||
filename: 'vendor.[chunkhash].js', | ||
minChunks: 5 | ||
}, options) | ||
|
||
return (context, util) => { | ||
const plugin = new webpack.optimize.CommonsChunkPlugin({ | ||
name: options.main, | ||
filename: options.filename, | ||
children: true, | ||
minChunks (module, count) { | ||
return module.context && module.context.includes('node_modules') && | ||
module.request.endsWith('.js') && | ||
count >= options.minChunks | ||
} | ||
}) | ||
return util.addPlugin(plugin) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webpack-blocks/webpack
re-exportscreateConfig
from@webpack-blocks/core
.