-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
add support for [hash] in filename #257
Conversation
@@ -37,7 +37,7 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { | |||
|
|||
compiler.plugin('make', function (compilation, callback) { | |||
// Compile the template (queued) | |||
compilationPromise = childCompiler.compileTemplate(self.options.template, compiler.context, self.options.filename, compilation) | |||
compilationPromise = childCompiler.compileTemplate(self.options.template, compiler.context, 'template', compilation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filename was replaced here, because the child compiler would otherwise replace the [hash]
by itself, causing an error. The actual provided filename for this function seems to not matter and be a temporary thing anyway. Please correct me if my assumption is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Webpack will overwrite an existing file with the name 'template'. This could also conflict if you would have two instances of the webpack-html-plugin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm I guess that makes my approach rather pointless. We can not hand in the filename with the hash before we even have compiled the template.
Is this different from #249? |
Yes, this PR targets the output html file, #249 targets the included assets. new HtmlWebpackPlugin({
template: 'index.html.template',
filename: 'build/index-[hash].html',
}),
|
@@ -48,6 +48,7 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { | |||
// If the compilation change didnt change the cache is valid | |||
isCompilationCached = compilationResult.hash && self.childCompilerHash === compilationResult.hash; | |||
self.childCompilerHash = compilationResult.hash; | |||
self.options.filename = self.options.filename.replace(/\[hash\]/g, compilationResult.hash); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to change the user options here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could store the filename in a different property such as this.modifiedFilename
and leave the options untouched for clarity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just use self.childCompilerHash
?
I added a commit using |
Looks quite good - however I am still not sure about the hardcoded Maybe you could also add a test to the caching spec. |
Agree with both of your points. Will address them and document the functionality in the readme. |
f56103a
to
3287c5a
Compare
@jantimon Mind having another look at this? |
@dotch This PR is exactly what I need for an IFrame, but one thing is not clear to me: when the plugin generates a hash-based file name, how do I get it programmatically? I mean ideally, I could just |
Could you please revert the indentation change - it's really impossible to read otherwise. |
@jantimon |
Looks like it is still broken |
@@ -43,8 +43,12 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { | |||
} | |||
|
|||
compiler.plugin('make', function (compilation, callback) { | |||
// Remove occurences of '[hash]' in the filename so the child | |||
// compiler does not try to replace it. | |||
var escapedFilename = self.options.filename.replace('[hash]', ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldnt it be better to replace [
and ]
to reduce the chance of collisions and support also other webpack variables?
We could move the escaping to a small file in the library folder which does only the webpack filename escaping and unescaping.
What about [
-> {[{
and ]
-> }]}
?
Should still be a valid filename: https://en.wikipedia.org/wiki/Filename
After some research I tried to use the webpack way of handling the filename: #279. |
Looks good to me, I like that approach! |
Moved to #279 |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This PR adds support for replacing
[hash]
in the filename with the hash of the compiled template.This behavior was brought up in #121 and would be pretty helpful for me.