You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When cache is enabled with webpack 5, modifying the preprocessor options will not trigger a cache bust and thus will use the cached version of the last compiled file as if the options were not changed
It's recommended to set cache.buildDependencies.config: [__filename] in your webpack configuration to get the latest configuration and all dependencies.
BTW, I built a minimal example and found it actually works. Did i miss something?
./src/index.js
window.a=1;// #!debugexportconsta=1;// #!if a === 1exportconstb=2;// #!endif
It doesn't seem to be possible to use variables directly into the loader cache deps to calculate the cache only files, this seems like a good feature request for webpack
"scripts": {"test": "echo \"Error: no test specified\" && exit 1",// Write your env param to a temp file"build1": "echo 1>foo&& TEST=1 webpack","build2": "echo 2>foo&& TEST=2 webpack"},
webpack.config.js
cache: {type: 'filesystem',buildDependencies: {// Add the `foo` to the list. You get the dependency changes, and reuse the cache to its best.// Modify the `version` may break the actual cache control strategy.config: [__filename,'foo'],},},
That works with config: [__filename, './foo'], but it's so hackish 🤕 I'll keep the hash-object-version workaround in the meantime
It's technically possible to write a file inside the package and add this as dep of the loader until the feature comes along, except this would have the same problem, the loader function doesn't run again if it's cached... So a bit of a dead end (It's possible to hook into webpack when the loader is imported, but that always ends up in a lot of ugly hacks). This needs to be fixed upstream.
Activity
afterwind-io commentedon Apr 28, 2021
This might help maybe?
https://webpack.js.org/configuration/other-options/#cachebuilddependencies
BTW, I built a minimal example and found it actually works. Did i miss something?
./src/index.jswebpack.config.jspackage.jsonChange the config value
params: { a: 2 }toparams: { a: 1 }does update the packed result in./dist/main.jsbased upon the setup above.Tofandel commentedon Apr 28, 2021
Modifying
webpack.config.jsinvalidates the cache (as it is itself a build deps) try using an env variable and the filesystemTry this
Then run
Output will still be the one of 1
Tofandel commentedon Apr 28, 2021
It seems it's possible to use something of the like to get the correct behavior
It doesn't seem to be possible to use variables directly into the loader cache deps to calculate the cache only files, this seems like a good feature request for webpack
https://webpack.js.org/api/loaders/#thisadddependency
webpack/webpack#13249
Tofandel commentedon Apr 28, 2021
It seems it possible to hack this somehow!
https://github.com/webpack/webpack/blob/00f4a82903d99502ffa1eee5c0bfd2fa405d04b8/lib/NormalModule.js#L842
Just need to add the options to this buildMeta variable:( This doesn't rerun on rebuild if the file is already cached, so dead end
afterwind-io commentedon Apr 28, 2021
I see the pain here...
JUST FOR FUN, try this perhaps?
package.jsonwebpack.config.jsTofandel commentedon Apr 28, 2021
That works with
config: [__filename, './foo'],but it's so hackish 🤕 I'll keep the hash-object-version workaround in the meantimeIt's technically possible to write a file inside the package and add this as dep of the loader until the feature comes along, except this would have the same problem, the loader function doesn't run again if it's cached... So a bit of a dead end (It's possible to hook into webpack when the loader is imported, but that always ends up in a lot of ugly hacks). This needs to be fixed upstream.