diff options
author | Julius Härtl <jus@bitgrid.net> | 2023-03-22 09:17:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-22 09:17:28 +0100 |
commit | 8b4d49cf506d202c4b7ab58f453cdc84a30bca38 (patch) | |
tree | cfaaa6ee6ea356014349417788f0125dae3a4304 /lib | |
parent | 9c7d7139b6d7487275c5ad18645a0770c23761d9 (diff) | |
parent | d461da3b04dfb2d6b7e6bad8ebf9b592d3008f20 (diff) | |
download | nextcloud-server-8b4d49cf506d202c4b7ab58f453cdc84a30bca38.tar.gz nextcloud-server-8b4d49cf506d202c4b7ab58f453cdc84a30bca38.zip |
Merge pull request #36057 from nextcloud/feat/esm-js-scripts
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Template/JSResourceLocator.php | 93 | ||||
-rw-r--r-- | lib/private/legacy/template/functions.php | 16 |
2 files changed, 69 insertions, 40 deletions
diff --git a/lib/private/Template/JSResourceLocator.php b/lib/private/Template/JSResourceLocator.php index 7648c7953f3..120234146e1 100644 --- a/lib/private/Template/JSResourceLocator.php +++ b/lib/private/Template/JSResourceLocator.php @@ -27,16 +27,19 @@ */ namespace OC\Template; +use OCP\App\AppPathNotFoundException; +use OCP\App\IAppManager; use Psr\Log\LoggerInterface; class JSResourceLocator extends ResourceLocator { - /** @var JSCombiner */ - protected $jsCombiner; + protected JSCombiner $jsCombiner; + protected IAppManager $appManager; - public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner) { + public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner, IAppManager $appManager) { parent::__construct($logger); $this->jsCombiner = $JSCombiner; + $this->appManager = $appManager; } /** @@ -53,59 +56,63 @@ class JSResourceLocator extends ResourceLocator { // For language files we try to load them all, so themes can overwrite // single l10n strings without having to translate all of them. $found = 0; - $found += $this->appendIfExist($this->serverroot, 'core/'.$script.'.js'); - $found += $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js'); - $found += $this->appendIfExist($this->serverroot, $script.'.js'); - $found += $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js'); - $found += $this->appendIfExist($this->serverroot, 'apps/'.$script.'.js'); - $found += $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js'); + $found += $this->appendScriptIfExist($this->serverroot, 'core/'.$script); + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script); + $found += $this->appendScriptIfExist($this->serverroot, $script); + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$script); + $found += $this->appendScriptIfExist($this->serverroot, 'apps/'.$script); + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script); if ($found) { return; } - } elseif ($this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js') - || $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js') - || $this->appendIfExist($this->serverroot, $script.'.js') - || $this->appendIfExist($this->serverroot, $theme_dir . "dist/$app-$scriptName.js") - || $this->appendIfExist($this->serverroot, "dist/$app-$scriptName.js") - || $this->appendIfExist($this->serverroot, 'apps/'.$script.'.js') + } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script) + || $this->appendScriptIfExist($this->serverroot, $theme_dir.$script) + || $this->appendScriptIfExist($this->serverroot, $script) + || $this->appendScriptIfExist($this->serverroot, $theme_dir."dist/$app-$scriptName") + || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName") + || $this->appendScriptIfExist($this->serverroot, 'apps/'.$script) || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json') - || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js') - || $this->appendIfExist($this->serverroot, 'core/'.$script.'.js') - || (strpos($scriptName, '/') === -1 && ($this->appendIfExist($this->serverroot, $theme_dir . "dist/core-$scriptName.js") - || $this->appendIfExist($this->serverroot, "dist/core-$scriptName.js"))) + || $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script) + || $this->appendScriptIfExist($this->serverroot, 'core/'.$script) + || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir."dist/core-$scriptName") + || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName"))) || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json') ) { return; } $script = substr($script, strpos($script, '/') + 1); - $app_path = \OC_App::getAppPath($app); - $app_url = \OC_App::getAppWebPath($app); + $app_url = null; + + try { + $app_url = $this->appManager->getAppWebPath($app); + } catch (AppPathNotFoundException) { + // pass + } + + try { + $app_path = $this->appManager->getAppPath($app); - if ($app_path !== false) { // Account for the possibility of having symlinks in app path. Only // do this if $app_path is set, because an empty argument to realpath // gets turned into cwd. $app_path = realpath($app_path); - } - // missing translations files fill be ignored - if (strpos($script, 'l10n/') === 0) { - $this->appendIfExist($app_path, $script . '.js', $app_url); - return; - } + // missing translations files will be ignored + if (strpos($script, 'l10n/') === 0) { + $this->appendScriptIfExist($app_path, $script, $app_url); + return; + } - if ($app_path === false && $app_url === false) { + if (!$this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) { + $this->appendScriptIfExist($app_path, $script, $app_url); + } + } catch (AppPathNotFoundException) { $this->logger->error('Could not find resource {resource} to load', [ 'resource' => $app . '/' . $script . '.js', 'app' => 'jsresourceloader', ]); - return; - } - - if (!$this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) { - $this->append($app_path, $script . '.js', $app_url); } } @@ -115,6 +122,17 @@ class JSResourceLocator extends ResourceLocator { public function doFindTheme($script) { } + /** + * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`) + * @see appendIfExist() + */ + protected function appendScriptIfExist(string $root, string $file, string $webRoot = null) { + if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) { + return $this->appendIfExist($root, $file . '.js', $webRoot); + } + return true; + } + protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') { if (is_file($root.'/'.$file)) { if ($this->jsCombiner->process($root, $file, $app)) { @@ -122,7 +140,12 @@ class JSResourceLocator extends ResourceLocator { } else { // Add all the files from the json $files = $this->jsCombiner->getContent($root, $file); - $app_url = \OC_App::getAppWebPath($app); + $app_url = null; + try { + $app_url = $this->appManager->getAppWebPath($app); + } catch (AppPathNotFoundException) { + // pass + } foreach ($files as $jsFile) { $this->append($root, $jsFile, $app_url); diff --git a/lib/private/legacy/template/functions.php b/lib/private/legacy/template/functions.php index 56c488d5abe..7081bd4f743 100644 --- a/lib/private/legacy/template/functions.php +++ b/lib/private/legacy/template/functions.php @@ -72,14 +72,19 @@ function emit_css_loading_tags($obj) { * Prints a <script> tag with nonce and defer depending on config * @param string $src the source URL, ignored when empty * @param string $script_content the inline script content, ignored when empty + * @param string $content_type the type of the source (e.g. 'module') */ -function emit_script_tag($src, $script_content = '') { +function emit_script_tag(string $src, string $script_content = '', string $content_type = '') { + $nonceManager = \OC::$server->get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class); + $defer_str = ' defer'; - $s = '<script nonce="' . \OC::$server->getContentSecurityPolicyNonceManager()->getNonce() . '"'; + $type = $content_type !== '' ? ' type="' . $content_type . '"' : ''; + + $s = '<script nonce="' . $nonceManager->getNonce() . '"'; if (!empty($src)) { // emit script tag for deferred loading from $src - $s .= $defer_str.' src="' . $src .'">'; - } elseif (!empty($script_content)) { + $s .= $defer_str.' src="' . $src .'"' . $type . '>'; + } elseif ($script_content !== '') { // emit script tag for inline script from $script_content without defer (see MDN) $s .= ">\n".$script_content."\n"; } else { @@ -96,7 +101,8 @@ function emit_script_tag($src, $script_content = '') { */ function emit_script_loading_tags($obj) { foreach ($obj['jsfiles'] as $jsfile) { - emit_script_tag($jsfile, ''); + $type = str_ends_with($jsfile, '.mjs') ? 'module' : ''; + emit_script_tag($jsfile, '', $type); } if (!empty($obj['inline_ocjs'])) { emit_script_tag('', $obj['inline_ocjs']); |