Skip to content
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

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from

Conversation

simonewebdesign
Copy link

@simonewebdesign simonewebdesign commented Dec 1, 2024

Resolves #1132, resolves #1152, adds support for import maps and other script tags containing JSON.

// Try parsing and re-stringifying the JSON
return JSON.stringify(JSON.parse(text));
}
catch (e) {

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! In f63fdbd :)

@@ -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') {

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?

Copy link
Author

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!

@simonewebdesign simonewebdesign changed the title Minify content within script tag of type="application/ld+json" Minify content within script tag of type JSON (importmap, manifest, JSON-LD, etc.) Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants