html-minifier
html-minifier copied to clipboard
allow type module
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#Applying_the_module_to_your_HTML
Note that landing this PR as-is will break files using ES modules as this would make html-minifier
remove type="module"
from script tags making browsers think the modules are general JS files.
Hi. I've faced the same problem and reused this patch for minifying type="module" scripts. As I see, html-minifier doesn't remove this attribute from the script tag if 'removeScriptTypeAttributes: false', so this patch is kind of safe.
Perhaps, it is possible to make an exception for type="module" when removing attrs for
diff --git a/node_modules/html-minifier/src/htmlminifier.js b/node_modules/html-minifier/src/htmlminifier.js
index d7efa99..78350a3 100644
--- a/node_modules/html-minifier/src/htmlminifier.js
+++ b/node_modules/html-minifier/src/htmlminifier.js
@@ -155,6 +155,7 @@ function isAttributeRedundant(tag, attrName, attrValue, attrs) {
// https://mathiasbynens.be/demo/javascript-mime-type
// https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type
var executableScriptsMimetypes = utils.createMap([
+ 'module',
'text/javascript',
'text/ecmascript',
'text/jscript',
@@ -553,7 +554,7 @@ function normalizeAttr(attr, attrs, tag, options) {
if (options.removeRedundantAttributes &&
isAttributeRedundant(tag, attrName, attrValue, attrs) ||
options.removeScriptTypeAttributes && tag === 'script' &&
- attrName === 'type' && isScriptTypeAttribute(attrValue) ||
+ attrName === 'type' && isScriptTypeAttribute(attrValue) && attrValue !== 'module' ||
options.removeStyleLinkTypeAttributes && (tag === 'style' || tag === 'link') &&
attrName === 'type' && isStyleLinkTypeAttribute(attrValue)) {
return;