Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/vendor-bundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @webpack-blocks/vendor-bundle - Changelog

## 1.0.0

Initial release.
60 changes: 60 additions & 0 deletions packages/vendor-bundle/README.md
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webpack-blocks/webpack re-exports createConfig from @webpack-blocks/core.

const vendorBundle = require('@webpack-blocks/vendorBundle')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change @webpack-blocks/vendorBundle to @webpack-blocks/vendor-bundle. NPM doesn't support capital letters in the package names.


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.
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Owner

Choose a reason for hiding this comment

The 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 😉

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that’s exactly what is says! ;-)

Copy link
Owner

Choose a reason for hiding this comment

The 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 😜

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
36 changes: 36 additions & 0 deletions packages/vendor-bundle/__tests__/vendorBundle.test.js
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))
})
36 changes: 36 additions & 0 deletions packages/vendor-bundle/index.js
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)
}
}
Loading