diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-07-11 02:59:35 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-07-11 02:59:40 +0300 |
commit | 864e73cdc75a2fb0e4fad500f649dae2343c10a8 (patch) | |
tree | 6ce6762c6be72c98592a32fe0bed4f2ce751d544 /skin | |
parent | cb13ea239b9f1ca6aea43125d5694d5a55dcd287 (diff) |
rewrite css and js assets building
Diffstat (limited to 'skin')
-rw-r--r-- | skin/base.skin.php | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/skin/base.skin.php b/skin/base.skin.php index 2d71312..616e1b3 100644 --- a/skin/base.skin.php +++ b/skin/base.skin.php @@ -45,11 +45,20 @@ function renderScript($ctx, $unsafe_js, $unsafe_lang, $enable_dynlogo) { global $config; $styles = json_encode($ctx->styleNames); -$versions = !$config['is_dev'] ? json_encode($config['static']) : '{}'; +if ($config['is_dev']) + $versions = '{}'; +else { + $versions = []; + foreach ($config['static'] as $name => $v) { + list($type, $bname) = getStaticNameParts($name); + $versions[$type][$bname] = $v; + } + $versions = json_encode($versions); +} return <<<HTML <script type="text/javascript"> -StaticManager.setStyles({$styles}, {$versions}); +StaticManager.init({$styles}, {$versions}); {$ctx->if_true($unsafe_js, '(function(){'.$unsafe_js.'})();')} {$ctx->if_true($unsafe_lang, 'extend(__lang, '.$unsafe_lang.');')} {$ctx->if_true($enable_dynlogo, 'DynamicLogo.init();')} @@ -77,40 +86,46 @@ function renderStatic($ctx, $static, $theme) { $ctx->styleNames = []; foreach ($static as $name) { // javascript - if (str_ends_with($name, '.js')) + if (str_starts_with($name, 'js/')) $html[] = jsLink($name); - // cs - else if (str_ends_with($name, '.css')) { + // css + else if (str_starts_with($name, 'css/')) { $html[] = cssLink($name, 'light', $style_name); $ctx->styleNames[] = $style_name; if ($dark) $html[] = cssLink($name, 'dark', $style_name); else if (!$config['is_dev']) - $html[] = cssPrefetchLink(str_replace('.css', '_dark.css', $name)); + $html[] = cssPrefetchLink($style_name.'_dark'); } + else + logError(__FUNCTION__.': unexpected static entry: '.$name); } return implode("\n", $html); } function jsLink(string $name): string { - return '<script src="'.$name.'?'.getStaticVersion($name).'" type="text/javascript"></script>'; + global $config; + list (, $bname) = getStaticNameParts($name); + if ($config['is_dev']) { + $href = '/js.php?name='.urlencode($bname).'&v='.time(); + } else { + $href = '/dist-js/'.$bname.'.js?'.getStaticVersion($name); + } + return '<script src="'.$href.'" type="text/javascript"></script>'; } function cssLink(string $name, string $theme, &$bname = null): string { global $config; - $dname = dirname($name); - $bname = basename($name); - if (($pos = strrpos($bname, '.'))) - $bname = substr($bname, 0, $pos); + list(, $bname) = getStaticNameParts($name); if ($config['is_dev']) { $href = '/sass.php?name='.urlencode($bname).'&theme='.$theme.'&v='.time(); } else { $version = getStaticVersion('css/'.$bname.($theme == 'dark' ? '_dark' : '').'.css'); - $href = $dname.'/'.$bname.($theme == 'dark' ? '_dark' : '').'.css?'.$version; + $href = '/dist-css/'.$bname.($theme == 'dark' ? '_dark' : '').'.css?'.$version; } $id = 'style_'.$bname; @@ -121,17 +136,33 @@ function cssLink(string $name, string $theme, &$bname = null): string { } function cssPrefetchLink(string $name): string { - $url = $name.'?'.getStaticVersion($name); - return <<<HTML +$url = '/dist-css/'.$name.'.css?'.getStaticVersion('css/'.$name.'.css'); +return <<<HTML <link rel="prefetch" href="{$url}" /> HTML; } +function getStaticNameParts(string $name): array { + $dname = dirname($name); + $bname = basename($name); + if (($pos = strrpos($bname, '.'))) { + $ext = substr($bname, $pos+1); + $bname = substr($bname, 0, $pos); + } else { + $ext = ''; + } + return [$dname, $bname, $ext]; +} + function getStaticVersion(string $name): string { global $config; - if (str_starts_with($name, '/')) + if ($config['is_dev']) + return time(); + if (str_starts_with($name, '/')) { + logWarning(__FUNCTION__.': '.$name.' starts with /'); $name = substr($name, 1); - return $config['is_dev'] ? time() : $config['static'][$name] ?? 'notfound'; + } + return $config['static'][$name] ?? 'notfound'; } function renderHeader($ctx, $theme, $unsafe_logo_html) { |