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
feat: omit html tag attribute with null/undefined/false value #1598
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1198,38 +1198,42 @@ describe('HtmlWebpackPlugin', () => { | |
null, done, false, false); | ||
}); | ||
|
||
it('allows events to remove an attribute by setting it to false', done => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep the old test and just add 2 more - 1 for null 1 for undefined.. that way we get better error messages if 1 test fails and to guarantee that the old tests are not modified and still work |
||
const examplePlugin = { | ||
apply: function (compiler) { | ||
compiler.hooks.compilation.tap('HtmlWebpackPlugin', compilation => { | ||
HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync('HtmlWebpackPluginTest', (pluginArgs, callback) => { | ||
pluginArgs.assetTags.scripts = pluginArgs.assetTags.scripts.map(tag => { | ||
if (tag.tagName === 'script') { | ||
tag.attributes.async = false; | ||
} | ||
return tag; | ||
it('allows events to remove an attribute by setting it to null/undefined/false', done => { | ||
const types = [null, undefined, false]; | ||
|
||
types.forEach(function (type) { | ||
const examplePlugin = { | ||
apply: function (compiler) { | ||
compiler.hooks.compilation.tap('HtmlWebpackPlugin', compilation => { | ||
HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync('HtmlWebpackPluginTest', (pluginArgs, callback) => { | ||
pluginArgs.assetTags.scripts = pluginArgs.assetTags.scripts.map(tag => { | ||
if (tag.tagName === 'script') { | ||
tag.attributes.async = type; | ||
} | ||
return tag; | ||
}); | ||
callback(null, pluginArgs); | ||
}); | ||
callback(null, pluginArgs); | ||
}); | ||
}); | ||
} | ||
}; | ||
testHtmlPlugin({ | ||
mode: 'production', | ||
entry: { | ||
app: path.join(__dirname, 'fixtures/index.js') | ||
}, | ||
output: { | ||
path: OUTPUT_DIR, | ||
filename: '[name]_bundle.js' | ||
} | ||
}; | ||
testHtmlPlugin({ | ||
mode: 'production', | ||
entry: { | ||
app: path.join(__dirname, 'fixtures/index.js') | ||
}, | ||
output: { | ||
path: OUTPUT_DIR, | ||
filename: '[name]_bundle.js' | ||
}, | ||
plugins: [ | ||
new HtmlWebpackPlugin(), | ||
examplePlugin | ||
] | ||
}, | ||
plugins: [ | ||
new HtmlWebpackPlugin(), | ||
examplePlugin | ||
] | ||
}, | ||
[/<script defer="defer" src="app_bundle.js"><\/script>[\s]*<\/head>/], | ||
null, done, false, false); | ||
[/<script defer="defer" src="app_bundle.js"><\/script>[\s]*<\/head>/], | ||
null, done, false, false); | ||
}); | ||
}); | ||
|
||
it('provides the options to the afterEmit event', done => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ideally, this assertion could be rewritten as the following:
It has better readability, but due to lint constraints that was not possible.
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.
What about
tagDefinition.attributes[attributeName] === '' || tagDefinition.attributes[attributeName]
?That should be equal to your code
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.
This would introduce a bug in the plugin due to the nature of
truthy
values, which excludesfalse
,0
,""
,null
,undefined
andNaN
values.As you can see,
0
as value might be needed.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.
but in your typings you didn't allow numbers - should we add them too?
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.
that's actually a good point! I'm not sure about the broad use case of it, but if we're going to proceed in this way, then we can simply refactor it to:
This would satisfy my use case, although I would appreciate your feedback regarding it, as you might have a better sense of the community usage.
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.
e.g.:
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.
Or we just keep your initial proposal :)
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 idea to have this
hasValue
is interesting, but if the usage is restricted to this single case, have the assessment inline provides better understand about the code, so I would go for that suggestion of yours:This fits nicely
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.
Okay cool so we have two valid options I guess:
tagDefinition.attributes[attributeName] === '' || tagDefinition.attributes[attributeName]
which does not allow numbersI am fine with both ways :)
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.
I have applied the suggestion you made, as you have a better understanding about its usage.
Thank you for the nice brainstorming