diff --git a/gzip.xml b/gzip.xml index 33021ae..639ce62 100644 --- a/gzip.xml +++ b/gzip.xml @@ -211,7 +211,7 @@ - + @@ -277,8 +277,8 @@ - - + +
@@ -361,8 +361,8 @@ - - + +
@@ -405,7 +405,7 @@ - - +
@@ -623,7 +623,7 @@
- +
diff --git a/helper.php b/helper.php index 0cdb0de..a184dfc 100644 --- a/helper.php +++ b/helper.php @@ -424,7 +424,7 @@ public static function js($file, $remote_service = true) if (is_null($parser)) { - $parser = new Renderer(static::$options['minifyjs'] ? new Compact() : new PrettyPrint()); + $parser = (new Renderer)->setFormatter(static::$options['minifyjs'] ? new Compact() : new PrettyPrint()); } try { diff --git a/helpers/Script.php b/helpers/Script.php index 6c4648d..0ed31d2 100644 --- a/helpers/Script.php +++ b/helpers/Script.php @@ -48,8 +48,13 @@ public function processHTML ($html, array $options = []) { $fetch_remote = !empty($options['fetchjs']); $remote_service = !empty($options['minifyjsservice']); + /** + * capture scripts marked as nomodule/module + */ + $modules = []; + // parse scripts - $html = preg_replace_callback('#]*)>(.*?)#si', function ($matches) use(&$sources, $path, $fetch_remote, $ignore, $remove) { + $html = preg_replace_callback('#]*)>(.*?)#si', function ($matches) use(&$sources, &$modules, $path, $fetch_remote, $ignore, $remove) { $attributes = []; @@ -63,10 +68,22 @@ public function processHTML ($html, array $options = []) { $position = isset($attributes['data-position']) && $attributes['data-position'] == 'head' ? 'head' : 'body'; + if (array_key_exists('nomodule', $attributes)) { + + $modules[] = $matches[0]; + return ''; + } + // ignore custom type // preg_match('#\btype=(["\'])(.*?)\1#', $matches[1], $match); if (isset($attributes['type']) && stripos($attributes['type'], 'javascript') === false) { + if($attributes['type'] == 'module') { + + $modules[] = $matches[0]; + return ''; + } + return $matches[0]; } @@ -290,11 +307,11 @@ public function processHTML ($html, array $options = []) { $async = true; } - $script[$position] .= "\n".''; + $script[$position] .= "\n".''; if ($hasScript) { - $script[$position] .= "\n".''; + $script[$position] .= "\n".''; unset($sources['inline'][$position]); } } @@ -329,6 +346,19 @@ public function processHTML ($html, array $options = []) { } } + if (!empty($modules)) { + + if (isset($script['body'])) { + + $script['body'] .= ''; + } + + else { + + $script['body'] = ''; + } + } + foreach ($script as $position => $content) { if (empty($content)) { diff --git a/loader.js b/loader.js index 27f278e..4e9e274 100644 --- a/loader.js +++ b/loader.js @@ -13,6 +13,48 @@ LIB.ready(function () { + function runScripts() { + const scripts = document.querySelectorAll('script[type="text/script"],script[type="text/module"]'); + const j = scripts.length; + let i = 0; + + for (; i < j; i++) { + + setTimeout( + (function (oldScript, script) { + return function () { + const parent = oldScript.parentElement; + script.text = oldScript.text; + + if (oldScript.type == 'text/module') { + + script.type = 'module'; + } + + try { + parent.insertBefore(script, oldScript); + parent.removeChild(oldScript); + } catch (e) { + console.error(e); + } + }; + }(scripts[i], document.createElement("script"))), + 0 + ); + } + + const module = document.querySelector('template[data-type=module]'); + + if (module) { + + setTimeout(function () { + + module.parentElement.insertBefore(module.content, module); + module.remove(); + }, 0) + } + } + const links = document.querySelectorAll( 'link[data-media]' ); @@ -33,43 +75,30 @@ LIB.ready(function () { } const scripts = document.querySelectorAll( - 'script[data-async][async]' + 'script[data-async]' ); let count = scripts.length; - for (i = 0; i < count; i++) { + if (count == 0) { - scripts[i].addEventListener('load', function () { + runScripts(); + } - count--; + else { - if (count == 0) { + for (i = 0; i < count; i++) { - const scripts = document.querySelectorAll('script[type="text/foo"]'); - const j = scripts.length; - let i = 0; + scripts[i].addEventListener('load', function () { - for (; i < j; i++) { + count--; - setTimeout( - (function (oldScript, script) { - return function () { - const parent = oldScript.parentElement; - script.text = oldScript.text; + if (count == 0) { - try { - parent.insertBefore(script, oldScript); - parent.removeChild(oldScript); - } catch (e) { - console.error(e); - } - }; - }(scripts[i], document.createElement("script"))), - 0 - ); + runScripts() } - } - }) + }) + } } + }); \ No newline at end of file diff --git a/loader.min.js b/loader.min.js index 4f16619..185dea0 100644 --- a/loader.min.js +++ b/loader.min.js @@ -1 +1 @@ -LIB.ready((function(){const e=document.querySelectorAll("link[data-media]"),t=e.length;let n,r=0;for(;r