hard-source-webpack-plugin
Hard cache the source of modules in webpack.
Last updated 7 years ago by mzgoddard .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ npm install hard-source-webpack-plugin 
SYNC missed versions from official npm registry.

HardSourceWebpackPlugin

Build Status Build status

HardSourceWebpackPlugin is a plugin for webpack to provide an intermediate caching step for modules. In order to see results, you'll need to run webpack twice with this plugin: the first build will take the normal amount of time. The second build will be signficantly faster.

Install with npm install --save hard-source-webpack-plugin or yarn. And include the plugin in your webpack's plugins configuration.

// webpack.config.js
var HardSourceWebpackPlugin = require('hard-source-webpack-plugin');

module.exports = {
  context: // ...
  entry: // ...
  output: // ...
  plugins: [
    new HardSourceWebpackPlugin()
  ]
}

You can optionally set where HardSource writes and reads its cache to and from, and the hash values that determine when it creates new caches.

new HardSourceWebpackPlugin({
  // Either an absolute path or relative to webpack's options.context.
  cacheDirectory: 'node_modules/.cache/hard-source/[confighash]',
  // Either a string of object hash function given a webpack config.
  configHash: function(webpackConfig) {
    // node-object-hash on npm can be used to build this.
    return require('node-object-hash')({sort: false}).hash(webpackConfig);
  },
  // Either false, a string, an object, or a project hashing function.
  environmentHash: {
    root: process.cwd(),
    directories: [],
    files: ['package-lock.json', 'yarn.lock'],
  },
  // An object.
  info: {
    // 'none' or 'test'.
    mode: 'none',
    // 'debug', 'log', 'info', 'warn', or 'error'.
    level: 'debug',
  },
}),

Options

cacheDirectory

The cacheDirectory is where the cache is written to. The default stores the cache in a directory under node_modules so if node_modules is cleared so is the cache.

The cacheDirectory has a field in it [confighash] that is replaced by the configHash option when webpack is started. The [confighash] field is here to help with changes to the configuration by the developer or by a script. For example if the same webpack configuration is used for the webpack cli tool and then the webpack-dev-server cli tool, they will generate different configuration hashes. webpack-dev-server adds plugins for its reloading features, and the default hash function produces a different value with those plugins added.

configHash

configHash turns a webpack configuration when a webpack instance is started and is used by cacheDirectory to build different caches for different webpack configurations.

Configurations may change how modules are rendered and so change how they appear in the disk cache hard-source writes. It is important to use a different cache per webpack configuration or webpack cli tool. webpack and webpack-dev-server for example needed separate caches, configHash and [confighash] in the cacheDirectory will create separate caches due to the plugins and configuration changes webpack-dev-server makes.

The default value for configHash is

configHash: function(webpackConfig) {
  return require('node-object-hash')({sort: false}).hash(webpackConfig);
}

This uses the npm node-object-hash module with sort set to false to hash the object. node-object-hash hashes as much as it can but may have issue with some plugins or plugins and loaders that load an additional configuration file like a babel rc file or postcss config. In those cases you can depend on node-object-hash and extend what it hashes to best cover those changes.

configHash can also be set to a string or it can be a function that generates a value based on other parts of the environment.

configHash: function() {
  return process.env.NODE_ENV + '-' process.env.BABEL_ENV;
}

environmentHash

When loaders, plugins, other build time scripts, or other dynamic dependencies change, hard-source needs to replace the cache to make sure the output is correct. The environmentHash is used to determine this. If the hash is different than a previous build, a fresh cache will be used.

The default object

environmentHash: {
  root: process.cwd(),
  directories: [],
  files: ['package-lock.json', 'yarn.lock']
}

hashes the lock files for npm and yarn. They will both be used if they both exist, or just one if only one exists. If neither file is found, the default will hash package.json and the package.json under node_modules.

You can disable the environmentHash by setting it to false. By doing this you will manually need to delete the cache when there is any dependency environment change.

info

Control the amount of messages from hard-source.

mode

Sets other defaults for info. Defaults to 'test' when NODE_ENV==='test'.

level

The level of log messages to report down to. Defaults to 'debug' when mode is 'none'. Defaults to 'warn' when mode is 'test'.

For example 'debug' reports all messages while 'warn' reports warn and error level messages.

Troubleshooting

Configuration changes are not being detected

hard-source needs a different cache for each different webpack configuration. The default configHash may not detect all of your options to plugins or other configuration files like .babelrc or postcss.config.js. In those cases a custom configHash is needed hashing the webpack config and those other values that it cannot normally reach.

Hot reloading is not working

webpack-dev-server needs a different cache than webpack or other webpack cli tools. Make sure your cacheDirectory and configHash options are hashing the changes webpack-dev-server makes to your webpack config. The default hard-source values should do this.

Multiple webpack processes at the same time are getting bad results

If you are using multiple webpack instances in separate processes make sure each has its own cache by changing cacheDirectory or configHash.

Rebuilds are slower than the first build during dev-server

This is can be due to module context dependencies. require.context or loaders that watch folders use this webpack feature so webpack rebuilds when files or folders are added or removed from these watched directories. Be careful about using require.context or context aware loaders on folders that contain a lot of items. Both require.context and context loaders depend on those folders recursively. hard-source hashes every file under a require.context or context loader folder to detect when the context has changed and dependent modules are rebuilt.

webpack-dev-server build loops continuously

Make sure you don't have a require.context or context loader on the root of your project. Such a context module means webpack is watching the hard source cache and when the cache is written after a build, webpack will start a new build for that module. This normally does not happen with webpack-dev-server because it writes the built files into memory instead of the disk. hard-source cannot do that since that would defeat its purpose as a disk caching plugin.

Please contribute!

If you encounter any issues or have an idea for hard-source-webpack-plugin could be better, please let us know.

Change Log

Current Tags

  • 0.13.1                                ...           latest (6 years ago)

164 Versions

  • 0.13.1                                ...           6 years ago
  • 0.13.0                                ...           6 years ago
  • 0.12.0                                ...           6 years ago
  • 0.11.2                                ...           6 years ago
  • 0.11.1                                ...           6 years ago
  • 0.11.0                                ...           6 years ago
  • 0.10.2                                ...           6 years ago
  • 0.10.1                                ...           6 years ago
  • 0.10.0                                ...           6 years ago
  • 0.9.0                                ...           7 years ago
  • 0.8.1                                ...           7 years ago
  • 0.8.0                                ...           7 years ago
  • 0.7.6                                ...           7 years ago
  • 0.7.5                                ...           7 years ago
  • 0.7.4                                ...           7 years ago
  • 0.7.3                                ...           7 years ago
  • 0.7.2                                ...           7 years ago
  • 0.7.1                                ...           7 years ago
  • 0.7.0                                ...           7 years ago
  • 0.6.12                                ...           7 years ago
  • 0.7.0-beta.4                                ...           7 years ago
  • 0.6.11                                ...           7 years ago
  • 0.7.0-beta.3                                ...           7 years ago
  • 0.7.0-beta.2                                ...           7 years ago
  • 0.6.10                                ...           7 years ago
  • 0.7.0-beta.1                                ...           7 years ago
  • 0.7.0-beta.0                                ...           7 years ago
  • 0.7.0-alpha.4                                ...           7 years ago
  • 0.7.0-alpha.3                                ...           7 years ago
  • 0.6.9                                ...           7 years ago
  • 0.7.0-alpha.2                                ...           7 years ago
  • 0.6.8                                ...           7 years ago
  • 0.7.0-alpha.1                                ...           7 years ago
  • 0.7.0-alpha.0                                ...           7 years ago
  • 0.6.7                                ...           7 years ago
  • 0.6.6                                ...           7 years ago
  • 0.6.5                                ...           7 years ago
  • 0.6.4                                ...           7 years ago
  • 0.6.3                                ...           7 years ago
  • 0.6.2                                ...           7 years ago
  • 0.6.1                                ...           7 years ago
  • 0.6.0                                ...           7 years ago
  • 0.5.18                                ...           7 years ago
  • 0.6.0-alpha.8                                ...           7 years ago
  • 0.6.0-alpha.7                                ...           7 years ago
  • 0.5.17                                ...           7 years ago
  • 0.6.0-alpha.6                                ...           7 years ago
  • 0.6.0-alpha.5                                ...           7 years ago
  • 0.6.0-alpha.4                                ...           7 years ago
  • 0.6.0-alpha.3                                ...           7 years ago
  • 0.6.0-alpha.2                                ...           7 years ago
  • 0.6.0-alpha.0                                ...           7 years ago
  • 0.5.16                                ...           7 years ago
  • 0.5.15                                ...           7 years ago
  • 0.5.14                                ...           7 years ago
  • 0.5.13                                ...           7 years ago
  • 0.5.12                                ...           7 years ago
  • 0.5.11                                ...           7 years ago
  • 0.5.10                                ...           7 years ago
  • 0.5.9                                ...           7 years ago
  • 0.5.8                                ...           7 years ago
  • 0.5.7                                ...           7 years ago
  • 0.5.6                                ...           7 years ago
  • 0.5.5                                ...           7 years ago
  • 0.5.4                                ...           7 years ago
  • 0.5.3                                ...           7 years ago
  • 0.5.2                                ...           7 years ago
  • 0.5.1                                ...           7 years ago
  • 0.5.0                                ...           7 years ago
  • 0.5.0-rc.8                                ...           7 years ago
  • 0.5.0-rc.7                                ...           7 years ago
  • 0.4.15                                ...           7 years ago
  • 0.5.0-rc.6                                ...           7 years ago
  • 0.4.14                                ...           7 years ago
  • 0.5.0-rc.5                                ...           7 years ago
  • 0.4.13                                ...           7 years ago
  • 0.5.0-rc.4                                ...           7 years ago
  • 0.4.12                                ...           7 years ago
  • 0.5.0-rc.3                                ...           7 years ago
  • 0.4.11                                ...           7 years ago
  • 0.4.10                                ...           7 years ago
  • 0.5.0-rc.2                                ...           7 years ago
  • 0.5.0-rc.1                                ...           7 years ago
  • 0.4.9                                ...           7 years ago
  • 0.5.0-rc.0                                ...           7 years ago
  • 0.4.8                                ...           7 years ago
  • 0.4.7                                ...           7 years ago
  • 0.4.6                                ...           8 years ago
  • 0.4.5                                ...           8 years ago
  • 0.4.4                                ...           8 years ago
  • 0.4.3                                ...           8 years ago
  • 0.4.2                                ...           8 years ago
  • 0.4.1                                ...           8 years ago
  • 0.4.0                                ...           8 years ago
  • 0.3.13                                ...           8 years ago
  • 0.3.12                                ...           8 years ago
  • 0.3.11                                ...           8 years ago
  • 0.3.10                                ...           8 years ago
  • 0.3.9                                ...           8 years ago
  • 0.3.8                                ...           8 years ago
  • 0.3.7                                ...           8 years ago
  • 0.3.6                                ...           8 years ago
  • 0.3.5                                ...           8 years ago
  • 0.3.4                                ...           8 years ago
  • 0.3.3                                ...           8 years ago
  • 0.3.2                                ...           8 years ago
  • 0.3.1                                ...           8 years ago
  • 0.3.0                                ...           8 years ago
  • 0.2.7                                ...           8 years ago
  • 0.2.6                                ...           8 years ago
  • 0.2.5                                ...           8 years ago
  • 0.2.4                                ...           8 years ago
  • 0.2.3                                ...           8 years ago
  • 0.2.2                                ...           8 years ago
  • 0.2.1                                ...           8 years ago
  • 0.2.0                                ...           8 years ago
  • 0.1.4                                ...           8 years ago
  • 0.1.3                                ...           8 years ago
  • 0.1.2                                ...           8 years ago
  • 0.1.0                                ...           8 years ago
  • 0.0.44                                ...           8 years ago
  • 0.0.43                                ...           8 years ago
  • 0.0.42                                ...           8 years ago
  • 0.0.41                                ...           8 years ago
  • 0.0.40                                ...           8 years ago
  • 0.0.39                                ...           8 years ago
  • 0.0.38                                ...           8 years ago
  • 0.0.37                                ...           8 years ago
  • 0.0.36                                ...           8 years ago
  • 0.0.35                                ...           8 years ago
  • 0.0.34                                ...           8 years ago
  • 0.0.33                                ...           8 years ago
  • 0.0.32                                ...           8 years ago
  • 0.0.31                                ...           8 years ago
  • 0.0.30                                ...           8 years ago
  • 0.0.29                                ...           8 years ago
  • 0.0.28                                ...           8 years ago
  • 0.0.27                                ...           8 years ago
  • 0.0.26                                ...           8 years ago
  • 0.0.25                                ...           8 years ago
  • 0.0.24                                ...           8 years ago
  • 0.0.23                                ...           8 years ago
  • 0.0.22                                ...           8 years ago
  • 0.0.21                                ...           8 years ago
  • 0.0.20                                ...           8 years ago
  • 0.0.19                                ...           8 years ago
  • 0.0.18                                ...           8 years ago
  • 0.0.17                                ...           8 years ago
  • 0.0.16                                ...           8 years ago
  • 0.0.15                                ...           8 years ago
  • 0.0.14                                ...           8 years ago
  • 0.0.13                                ...           8 years ago
  • 0.0.12                                ...           8 years ago
  • 0.0.11                                ...           8 years ago
  • 0.0.10                                ...           8 years ago
  • 0.0.9                                ...           8 years ago
  • 0.0.8                                ...           8 years ago
  • 0.0.7                                ...           8 years ago
  • 0.0.6                                ...           8 years ago
  • 0.0.5                                ...           8 years ago
  • 0.0.4                                ...           8 years ago
  • 0.0.3                                ...           8 years ago
  • 0.0.2                                ...           8 years ago
  • 0.0.1                                ...           8 years ago
Maintainers (1)
Downloads
Total 6
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (9)
Dev Dependencies (19)
Dependents (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE