html-minifier icon indicating copy to clipboard operation
html-minifier copied to clipboard

allow type module

Open vitaliytv opened this issue 5 years ago • 3 comments

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#Applying_the_module_to_your_HTML

vitaliytv avatar Sep 27 '19 13:09 vitaliytv

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.

R4356th avatar Oct 13 '21 08:10 R4356th

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

dmitrysmagin avatar Aug 01 '22 09:08 dmitrysmagin

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; 

dmitrysmagin avatar Aug 01 '22 10:08 dmitrysmagin