Skip to content

Commit

Permalink
#149 fix javascript/module/nomodule loading
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Sep 8, 2021
1 parent e510d9e commit 4163e67
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 39 deletions.
16 changes: 8 additions & 8 deletions gzip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
<option value="1">JYes</option>
<option value="0">JNo</option>
</field>
<field name="imageignore" label="PLG_GZIP_FIELD_IMAGEIGNORE_LABEL" description="PLG_GZIP_FIELD_IMAGEIGNORE_DESCRIPTION" hint="PLG_GZIP_FIELD_IMAGEIGNORE_HINT" type="textarea" rows="15" cols="45" />
<field name="imageignore" label="PLG_GZIP_FIELD_IMAGEIGNORE_LABEL" description="PLG_GZIP_FIELD_IMAGEIGNORE_DESCRIPTION" hint="PLG_GZIP_FIELD_IMAGEIGNORE_HINT" type="textarea" rows="5" cols="45" />
<field type="spacer" hr="true" />
<field name="imageconvert" label="PLG_GZIP_FIELD_IMAGE_CONVERT_LABEL" description="PLG_GZIP_FIELD_IMAGE_CONVERT_DESCRIPTION" hint="PLG_GZIP_FIELD_IMAGE_CONVERT_HINT" type="radio" class="btn-group" default="0">
<option value="1">JYes</option>
Expand Down Expand Up @@ -277,8 +277,8 @@
<option value="1">JYes</option>
<option value="0">JNo</option>
</field>
<field name="jsignore" label="PLG_GZIP_FIELD_JSIGNORE_LABEL" description="PLG_GZIP_FIELD_JSIGNORE_DESCRIPTION" hint="PLG_GZIP_FIELD_JSIGNORE_HINT" type="textarea" rows="15" cols="45" />
<field name="jsremove" label="PLG_GZIP_FIELD_JSREMOVE_LABEL" description="PLG_GZIP_FIELD_JSREMOVE_DESCRIPTION" type="textarea" rows="15" cols="45" />
<field name="jsignore" label="PLG_GZIP_FIELD_JSIGNORE_LABEL" description="PLG_GZIP_FIELD_JSIGNORE_DESCRIPTION" hint="PLG_GZIP_FIELD_JSIGNORE_HINT" type="textarea" rows="5" cols="45" />
<field name="jsremove" label="PLG_GZIP_FIELD_JSREMOVE_LABEL" description="PLG_GZIP_FIELD_JSREMOVE_DESCRIPTION" type="textarea" rows="5" cols="45" />
</fields>
</fieldset>
<fieldset name="css" label="CSS">
Expand Down Expand Up @@ -361,8 +361,8 @@
<option value="auto">PLG_GZIP_FIELD_FONTDISPLAY_OPTION_AUTO</option>
</field>
<field type="spacer" hr="true" />
<field name="cssignore" label="PLG_GZIP_FIELD_CSSIGNORE_LABEL" description="PLG_GZIP_FIELD_CSSIGNORE_DESCRIPTION" hint="PLG_GZIP_FIELD_CSSIGNORE_HINT" type="textarea" rows="15" cols="45" />
<field name="cssremove" label="PLG_GZIP_FIELD_CSSREMOVE_LABEL" description="PLG_GZIP_FIELD_CSSREMOVE_DESCRIPTION" type="textarea" rows="15" cols="45" />
<field name="cssignore" label="PLG_GZIP_FIELD_CSSIGNORE_LABEL" description="PLG_GZIP_FIELD_CSSIGNORE_DESCRIPTION" hint="PLG_GZIP_FIELD_CSSIGNORE_HINT" type="textarea" rows="5" cols="45" />
<field name="cssremove" label="PLG_GZIP_FIELD_CSSREMOVE_LABEL" description="PLG_GZIP_FIELD_CSSREMOVE_DESCRIPTION" type="textarea" rows="5" cols="45" />
</fields>
</fieldset>
<fieldset name="critical_path" label="Critical Css">
Expand Down Expand Up @@ -405,7 +405,7 @@
<option value="html">JOPTION_PWA_OFFLINE_PREF_HTML</option>
</field>
<field name="pwa_offline_page" label="PLG_GZIP_FIELD_PWA_OFFLINE_PAGE_LABEL" description="PLG_GZIP_FIELD_PWA_OFFLINE_PAGE_DESCRIPTION" hint="PLG_GZIP_FIELD_PWA_OFFLINE_PAGE_HINT" type="text" default="" />
<field name="pwa_offline_html_page" label="PLG_GZIP_FIELD_PWA_OFFLINE_HTML_PAGE_LABEL" description="PLG_GZIP_FIELD_PWA_OFFLINE_HTML_PAGE_DESCRIPTION" hint="PLG_GZIP_FIELD_PWA_OFFLINE_HTML_PAGE_HINT" type="textarea" rows="15" cols="80" filter="raw" default="&lt;!doctype html&gt;
<field name="pwa_offline_html_page" label="PLG_GZIP_FIELD_PWA_OFFLINE_HTML_PAGE_LABEL" description="PLG_GZIP_FIELD_PWA_OFFLINE_HTML_PAGE_DESCRIPTION" hint="PLG_GZIP_FIELD_PWA_OFFLINE_HTML_PAGE_HINT" type="textarea" rows="5" cols="80" filter="raw" default="&lt;!doctype html&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;Site Unreachable&lt;/title&gt;
&lt;style&gt;
Expand Down Expand Up @@ -507,7 +507,7 @@
<field name="title" label="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_TITLE_LABEL" description="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_TITLE_DESCRIPTION" type="text" default="" hint="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_TITLE_HINT" />
<field name="text" label="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_TEXT_LABEL" description="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_TEXT_DESCRIPTION" type="text" default="" hint="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_TEXT_HINT" />
<field name="url" label="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_URL_LABEL" description="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_URL_DESCRIPTION" type="text" default="" hint="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_URL_HINT" />
<field name="files" label="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_FILES_LABEL" description="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_FILES_DESCRIPTION" type="textarea" default="" cols="15" rows="15" hint="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_FILES_HINT" />
<field name="files" label="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_FILES_LABEL" description="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_FILES_DESCRIPTION" type="textarea" default="" cols="15" rows="5" hint="PLG_GZIP_FIELD_PWA_SHARE_TARGET_FIELDS_FILES_HINT" />
</fields>
</fields>
</fieldset>
Expand Down Expand Up @@ -623,7 +623,7 @@
<option value="GET">JOPTION_PWA_SYNC_METHOD_OPTION_GET</option>
<option value="POST">JOPTION_PWA_SYNC_METHOD_OPTION_POST</option>
</field>
<field name="pwa_sync_patterns" label="PLG_GZIP_FIELD_PWA_SYNC_PATTERN_LABEL" description="PLG_GZIP_FIELD_PWA_SYNC_PATTERN_DESCRIPTION" hint="PLG_GZIP_FIELD_PWA_SYNC_PATTERN_HINT" type="textarea" default="" rows="15" />
<field name="pwa_sync_patterns" label="PLG_GZIP_FIELD_PWA_SYNC_PATTERN_LABEL" description="PLG_GZIP_FIELD_PWA_SYNC_PATTERN_DESCRIPTION" hint="PLG_GZIP_FIELD_PWA_SYNC_PATTERN_HINT" type="textarea" default="" rows="5" />
</fields>
</fieldset>
<fieldset name="webpush" label="Web Push">
Expand Down
2 changes: 1 addition & 1 deletion helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 33 additions & 3 deletions helpers/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('#<script([^>]*)>(.*?)</script>#si', function ($matches) use(&$sources, $path, $fetch_remote, $ignore, $remove) {
$html = preg_replace_callback('#<script([^>]*)>(.*?)</script>#si', function ($matches) use(&$sources, &$modules, $path, $fetch_remote, $ignore, $remove) {

$attributes = [];

Expand All @@ -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];
}

Expand Down Expand Up @@ -290,11 +307,11 @@ public function processHTML ($html, array $options = []) {
$async = true;
}

$script[$position] .= "\n".'<script data-async async defer src="' . array_shift($fileList) . '"'.$attr.'></script>';
$script[$position] .= "\n".'<script data-async defer src="' . array_shift($fileList) . '"'.$attr.'></script>';

if ($hasScript) {

$script[$position] .= "\n".'<script type="text/foo">' . trim(implode(';', $sources['inline'][$position]), ';') . '</script>';
$script[$position] .= "\n".'<script type="text/script">' . trim(implode(';', $sources['inline'][$position]), ';') . '</script>';
unset($sources['inline'][$position]);
}
}
Expand Down Expand Up @@ -329,6 +346,19 @@ public function processHTML ($html, array $options = []) {
}
}

if (!empty($modules)) {

if (isset($script['body'])) {

$script['body'] .= '<template data-type="module">'.implode("\n", $modules).'</template>';
}

else {

$script['body'] = '<template data-type="module">'.implode("\n", $modules).'</template>';
}
}

foreach ($script as $position => $content) {

if (empty($content)) {
Expand Down
81 changes: 55 additions & 26 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
);
Expand All @@ -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()
}
}
})
})
}
}

});
2 changes: 1 addition & 1 deletion loader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4163e67

Please sign in to comment.