hard-source-webpack-plugin
hard-source-webpack-plugin copied to clipboard
Detect package.json changes without full cache reload
Hello,
I use new webpack.DefinePlugin
to share variables between webpack.config.js
and my TypeScript files. I shared also the version
from package.json
with the DefinePlugin.
If I change now the version in package.json
it will not be changed in the TypeScript files, because it is cached. If I use the environmentHash
to detect changes in the package.json
the full cache will be deleted and new initialized, what needs a lot of time.
Is it possible to detect changes in package.json
and reset the cache only for this file, and not for the whole project?
To get this kind of thing working you want to load the version without changing the configHash
or environmentHash
. The easiest option is to use yarn.lock
or package-lock.json
for the environment hash (hard-source defaults to those first before package.json
), and to require('package.json')
in your build instead of using DefinePlugin
. You could use loaders to get the smaller part of package.json
you want, like version.
Using DefinePlugin
for stuff like this with the Define
s changing because the version changes normally changes the configHash
leading hard-source to use a new separate cache. You could manage configHash
to exclude DefinePlugin
. If you don't want to require('./package.json')
directly in your source, you could use DefinePlugin
for that.
new DefinePlugin({
VERSION: `require('${__dirname}/package.json').version`,
})
Since DefinePlugin replaces defines with code you can use it this way so the config doesn't change.