-
Notifications
You must be signed in to change notification settings - Fork 579
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
Minify content within script tag of type JSON (importmap, manifest, JSON-LD, etc.) #1157
base: gh-pages
Are you sure you want to change the base?
Conversation
src/htmlminifier.js
Outdated
// Try parsing and re-stringifying the JSON | ||
return JSON.stringify(JSON.parse(text)); | ||
} | ||
catch (e) { |
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.
Is there a way to log the error instead of suppressing it? Similar to what is done here: https://github.com/kangax/html-minifier/blob/ab4c9d6e3e11ab207f545728c918895d5cb4c5f8/src/htmlminifier.js#L740C11-L740C28
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.
Done! In f63fdbd :)
src/htmlminifier.js
Outdated
@@ -387,6 +387,11 @@ function cleanConditionalComment(comment, options) { | |||
|
|||
function processScript(text, options, currentAttrs) { | |||
for (var i = 0, len = currentAttrs.length; i < len; i++) { | |||
if (currentAttrs[i].name.toLowerCase() === 'type' && | |||
currentAttrs[i].value === 'application/ld+json') { |
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.
Import maps (type="importmap"
) are also often used and should be minified. Could we use an array of acceptable types?
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.
Great shout! I've implemented that in 25e77e8. It adds support for import maps and makes it much simpler to implement a new format (it will just be a matter of adding it to the array of acceptable types). I've added more types to the list as well, let me know if you think there are any others I've missed!
Resolves #1132, resolves #1152, adds support for import maps and other script tags containing JSON.