Skip to content

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 :)

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
@SuperLlama88888
Copy link

@kangax Really sorry for the ping. Could you please take a look at this PR? Tests are included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for <script type="speculationrules"> Any plans to minify content within script tag of type="application/ld+json"?

3 participants