Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
320 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Webpack Options | ||
|
||
## DLL Plugin | ||
|
||
[Webpack Wiki](https://github.com/webpack/docs/wiki/list-of-plugins#dllplugin) | ||
|
||
The DLL Plugin is a way of caching the module dependency graph that Webpack builds every time it loads our project. It doesn't execute any of the code, but simply makes it available to any modules which depend on it. It is perfect for boilerplates due to the fact that many of the dependency decisions are made up front and won't need to change that frequently. | ||
|
||
Using the DLL Plugin allows Webpack to prioritize building your application's code, while relying on caching to handle the dependency code. The end result is a significant boost in both server startup time and in hot-module reload times. | ||
|
||
The DLL plugin is enabled by default for development, and depends on a configuration property `dllPlugin` being found in the project's `package.json`. There is also a one-time build step that will take place before the webpack development configuration can be used. | ||
|
||
### DLL Plugin Configuration Options | ||
|
||
#### Automatic Configuration | ||
|
||
By default, a DLL bundle is generated for all of the modules listed as dependencies in the project's `package.json` file. | ||
|
||
``` | ||
{ | ||
"dependencies": { | ||
"babel-polyfill": "^6.7.4", | ||
"chalk": "^1.1.3", | ||
"compression": "^1.6.1", | ||
"express": "^4.13.4", | ||
"file-loader": "^0.8.5", | ||
"fontfaceobserver": "^1.7.1", | ||
"history": "^2.1.0", | ||
"immutable": "^3.8.1", | ||
"react": "^15.0.1", | ||
... | ||
} | ||
} | ||
``` | ||
|
||
Certain production dependencies which don't belong in the browser bundle can be excluded by naming them in the `exclude` list. | ||
|
||
Other dependencies which may not be listed in the project's package.json, can be explicitly included by naming them in the `include` list. For example using `babel-polyfill` will include `core-js` and hot module reloading dependencies may also include `lodash`. | ||
|
||
**Example:** | ||
|
||
``` | ||
{ | ||
"dllPlugin": { | ||
"exclude": ["express","chalk","compression"], | ||
"include": ["core-js", "eventsource-polyfill", "lodash"] | ||
} | ||
} | ||
``` | ||
|
||
Using the `npm run analyse` script will give you a good idea of what exactly is part of your bundle. | ||
|
||
### Manual Configuration | ||
|
||
There may be situations where you want fine grained control over the DLL Plugin. In that case you can specify which dependencies belong to which DLL using the following: | ||
|
||
``` | ||
{ | ||
"dllPlugin": { | ||
"dlls": { | ||
"reactCore": [ | ||
"react", | ||
"react-dom", | ||
"react-router", | ||
"redux", | ||
"react-redux", | ||
"redux-think", | ||
"redux-saga" | ||
], | ||
"reactBootstrap": [ | ||
"jquery", | ||
"bootstrap", | ||
"react-bootstrap" | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Generating DLL Files | ||
|
||
The cost of the performance boost given to us by the DLL Plugin is that we will need to occasionally update the module manifest and dependency bundle. | ||
|
||
This can be accomplished by running the `npm run build:dll` script. This will create a dependency bundle javascript file, a JSON manifest, in the `node_modules/react-boilerplate-dlls` folder in your project. | ||
|
||
For convenience, this script will automatically be run after an `npm install` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/*eslint-disable*/ | ||
|
||
// No need to build the DLL in production | ||
if (process.env.NODE_ENV === 'production') { | ||
process.exit(0) | ||
} | ||
|
||
require('shelljs/global'); | ||
|
||
rm('-rf', 'node_modules/react-boilerplate-dlls') | ||
mkdir('node_modules/react-boilerplate-dlls') | ||
|
||
echo('Building the Webpack DLL...') | ||
exec('cross-env BUILDING_DLL=true webpack --display-chunks --color --config internals/webpack/webpack.dll.babel.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* WEBPACK DLL GENERATOR | ||
* | ||
* This profile is used to cache webpack's module | ||
* contexts for external library and framework type | ||
* dependencies which will usually not change often enough | ||
* to warrant building them from scratch every time we use | ||
* the webpack process. | ||
* | ||
* It makes sense to run this after npm install | ||
* or dependency changes. | ||
*/ | ||
|
||
const { keys } = Object; | ||
const { resolve } = require('path'); | ||
const pkg = require(resolve(process.cwd(), 'package.json')); | ||
|
||
if (!pkg.dllPlugin) { | ||
throw new Error('Usage of the Webpack DLL plugin depends on a dllPlugin key being present in your package.json'); | ||
} | ||
|
||
const pullAll = require('lodash/pullAll'); | ||
const uniq = require('lodash/uniq'); | ||
const defaults = require('lodash/defaultsDeep'); | ||
const webpack = require('webpack'); | ||
|
||
const dllPlugin = defaults(pkg.dllPlugin, { | ||
/** | ||
* Not all dependencies can be bundled | ||
*/ | ||
exclude: [ | ||
'express', | ||
'chalk', | ||
'compression', | ||
'sanitize.css', | ||
'cross-env', | ||
'ip', | ||
], | ||
|
||
/** | ||
* Some additional dependencies which aren't | ||
* in the production dependencies need to be bundled. | ||
*/ | ||
include: [ | ||
'babel-polyfill', | ||
'eventsource-polyfill', | ||
'core -js', | ||
], | ||
|
||
/** | ||
* folder where the generated dlls get stored. | ||
*/ | ||
path: 'node_modules/react-boilerplate-dlls', | ||
}); | ||
|
||
if (dllPlugin.dlls && typeof dllPlugin.dlls !== 'object') { | ||
throw new Error('The Webpack DLL Plugin configuration in your package.json must contain a dlls property.'); | ||
} | ||
|
||
const outputPath = resolve(process.cwd(), dllPlugin.path); | ||
const reactBoilerplateDeps = pullAll(uniq(keys(pkg.dependencies).concat(dllPlugin.include)), dllPlugin.exclude); | ||
|
||
module.exports = { | ||
context: process.cwd(), | ||
entry: (typeof dllPlugin.dlls === 'undefined' ? { reactBoilerplateDeps } : dllPlugin.dlls), | ||
devtool: 'eval', | ||
output: { | ||
filename: '[name].js', | ||
library: '[name]', | ||
path: outputPath, | ||
}, | ||
plugins: [ | ||
new webpack.DllPlugin({ name: '[name]', path: resolve(outputPath, '[name].json') }), // eslint-disable-line no-new | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.