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
Way of determining if an asset's filename contains a hash #9038
Comments
Yes, we have this problem, example we need regenerate |
Which of the proposed solutions would work best for the |
Store |
Apologies if I'm misunderstanding, but won't every asset have a I'd like to know whether any flavor of hash (it looks like the possible types might be |
I mean webpack/lib/TemplatedPathPlugin.js Lines 7 to 10 in 8a7597a
What we need in terser (and any minimizer):
|
I need to know whether an asset's filename contains some sort of hash value, regardless of what kind of hash value it is. You could, I guess, stick in two different kinds of hashes into the same filename, like Are you suggesting that there should be a new property on each asset, If that's what you're suggesting, what should the value of I don't want to minimize a use case that would benefit |
Yes
I think this solution will help you and me. |
Gotcha. Last(?) question: can I use a property name on the asset other than So for three hypothetical assets, here's how it could behave: Asset 1 Asset 2 Asset 3 For my use case, I could check |
Maybe better, i think name will be discussion in PR, it is not hard to change 😄
Not sure, maybe we should include hash as is
maybe Anyway /cc @sokra need your advice |
@developit pointed out that similar hash replacement logic also happens in https://github.com/webpack/loader-utils/blob/master/lib/interpolateName.js for assets that originate from a loader, so presumably I'll need to treat replacements that take place there similarly to ones that happen in webpack/lib/TemplatedPathPlugin.js Line 55 in 8a7597a
If anyone from the webpack team is aware of other places in the codebase that are also responsible for replacing |
We did considerably alter hashing in webpack 5 so I think we should consider how this works there as well because I don't want to implement a v5 API that obsoltes v4's need also. @jeffposnick Is the amount of cachebusting that large for devs running new builds every time currently? |
@TheLarkInn problem not in memory, problem what we need detect/change existing hashes |
I am definitely interesting in seeing something implemented that plays nicely with v5. If there are changes planned for v5 related to the As @evilebottnawi says, this is not related to optimizing resource consumption during the build process. It's more about needing some specific build metadata—specifically the hash values that contributed to an asset's final filename—exposed in a way that could be read by a plugin. Having that build metadata exposed would allow my |
/cc @jeffposnick can you look on #9687 |
That looks great—thanks @sokra & co.! |
Feature request
What is the expected behavior?
I would like a way of examining each asset that's generated as part of a webpack compilation and determining whether the corresponding filename contains some flavor of hash (e.g. content hash, chunk hash, etc.—not sure how many possible hashes there are).
One way of accomplishing this might be a new
boolean
flag that's added to each asset, calledhashInFilename
or something similar.Alternatively (less conveniently, but maybe more generally useful) it might be done by providing an property on each assets that reflects of the original
filename
template that was used to generate the final effective filename. For example, an asset whose final filename is'app.abcd1234.js'
might have a propertyfilenameTemplate: 'app.[hash:8].js'
. We could then infer whether a filename contains a hash or not by examining this value for one of the well-known hash substitution placeholders.What is motivation or use case for adding/changing the behavior?
Adding a hash to a filename results in a content-addressable URL that can be safely cached indefinitely, without having to worry about adding in cache-busting URL parametres, or otherwise implementing a means of bypassing a cache.
I work on the
workbox-webpack-plugin
, and being able to determine whether a given asset will have a content-addressable URL will make it easier for us to generate a service worker that knows whether it's necessary to cache-bust a request for that URL.How should this be implemented in your opinion?
Ideally, this would be some extra metadata that got unconditionally added in to each asset as part of the output of a compilation, as per the earlier suggestions.
Barring that, it would be possible to implement the same sort of thing if there were a new event that could be hooked into via a plugin, and exposed the
filename
template value, and the final effective name for each asset when filenames are assigned.I am not very clear on where all the logic for this is done inside of webpack, but I'm assuming the https://github.com/webpack/webpack/blob/9ededfa92da493e750cf7d573873dcc17bb43af4/lib/MainTemplate.js and https://github.com/webpack/webpack/blob/8a7597aa6eb2eef66a8f9db3a0c49bcb96022a94/lib/TemplatedPathPlugin.js modules are closely related to this suggestion
Are you willing to work on this yourself?
yes
The text was updated successfully, but these errors were encountered: