Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow panel plugins to load index.dev.mjs instead of index.js (needed by kirbyup) #4541

Merged
merged 22 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a23e357
feat: allow panel plugins to load index.mjs
jonaskuske Jul 30, 2022
5e32bc8
feat: update mjs plugin loading strategy
jonaskuske Aug 2, 2022
782be53
chore: run php-cs-fixer
jonaskuske Aug 3, 2022
0ace76e
feat: new mjs plugin loading implementation
jonaskuske Aug 3, 2022
2edfbca
refactor: minor cleanup
jonaskuske Aug 3, 2022
e8a05f5
style: fix HTML indentation
jonaskuske Aug 4, 2022
cfb87af
refactor: <script> instead of Html::tag()
jonaskuske Aug 4, 2022
81209de
style: php-cs-fixer
jonaskuske Aug 4, 2022
3c58a17
test: update MimeTest and PluginsTest for mjs
jonaskuske Aug 4, 2022
7489d89
style: run php-cs-fixer
jonaskuske Aug 4, 2022
102a0ed
fix: correctly output nonce in panel view
jonaskuske Aug 8, 2022
1446f03
Revert "chore: run php-cs-fixer"
jonaskuske Aug 9, 2022
8f30e0f
fix: remove EOL from mjs snapshot in PluginsTest
jonaskuske Aug 9, 2022
9aef72a
fix: add octet-stream→mjs to mime fixing map
jonaskuske Aug 9, 2022
c2dd5e1
fix: append PHP_EOL to mjs snapshot in PluginsTest
jonaskuske Aug 9, 2022
d155826
refactor: make filter function more readable
jonaskuske Aug 12, 2022
fcbab89
test: add test case for empty mjs modules
jonaskuske Aug 12, 2022
6c7f189
docs: add code comments for index.dev.mjs feature
jonaskuske Aug 12, 2022
ee56289
docs: add comment on use of data URIs
jonaskuske Aug 12, 2022
15bb6f8
refactor: adjust coding style, add type
jonaskuske Aug 12, 2022
b68e0a4
refactor: index.js filter fn as concise oneliner
jonaskuske Aug 12, 2022
53b1e19
Additional logic cleanup
lukasbestle Aug 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ protected function file(string $key): string
case '/':
break;

// backslashes get their own marker in the path
// to differentiate the cache key from one with forward slashes
// backslashes get their own marker in the path
// to differentiate the cache key from one with forward slashes
jonaskuske marked this conversation as resolved.
Show resolved Hide resolved
case '\\':
$keyParts[] = '_backslash';
break;

// empty part means two slashes in a row;
// special marker like for backslashes
// empty part means two slashes in a row;
// special marker like for backslashes
case '':
$keyParts[] = '_empty';
break;

// an actual path segment
// an actual path segment
default:
// check if the segment only contains safe characters;
// underscores are *not* safe to guarantee uniqueness
Expand Down
2 changes: 1 addition & 1 deletion src/Cms/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function template(): void
$this->props['body']['text'] = $text->render($data);
}

// fallback to single email text template
// fallback to single email text template
} elseif ($text->exists()) {
$this->props['body'] = $text->render($data);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,11 @@ public function splitIdentifier($table, $identifier): array
case 1:
return [$table, $this->unquoteIdentifier($parts[0])];

// qualified identifier
// qualified identifier
case 2:
return [$this->unquoteIdentifier($parts[0]), $this->unquoteIdentifier($parts[1])];

// every other number is an error
// every other number is an error
default:
throw new InvalidArgumentException('Invalid identifier ' . $identifier);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Filesystem/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Mime
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mif' => 'application/vnd.mif',
'mjs' => 'text/javascript',
'mov' => 'video/quicktime',
'movie' => 'video/x-sgi-movie',
'mp2' => 'audio/mpeg',
Expand Down Expand Up @@ -133,11 +134,15 @@ public static function fix(string $file, string $mime = null, string $extension
'text/plain' => [
'css' => 'text/css',
'json' => 'application/json',
'mjs' => 'text/javascript',
'svg' => ['Kirby\Filesystem\Mime', 'fromSvg'],
],
'text/x-asm' => [
'css' => 'text/css'
],
'text/x-java' => [
'mjs' => 'text/javascript',
],
'image/svg' => [
'svg' => 'image/svg+xml'
]
Expand Down
1 change: 1 addition & 0 deletions src/Panel/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static function assets(): array
'custom' => static::customAsset('panel.css'),
],
'icons' => static::favicon($url),
'imports' => $plugins->read('mjs'),
'js' => [
'vendor' => [
'nonce' => $nonce,
Expand Down
32 changes: 31 additions & 1 deletion src/Panel/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Kirby\Panel;

use Kirby\Cms\App;
use Kirby\Data\Json;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;

/**
Expand Down Expand Up @@ -42,6 +44,8 @@ public function files(): array
foreach (App::instance()->plugins() as $plugin) {
$this->files[] = $plugin->root() . '/index.css';
$this->files[] = $plugin->root() . '/index.js';
// used by kirbyup during plugin development
$this->files[] = $plugin->root() . '/index.dev.mjs';
}

return $this->files;
Expand Down Expand Up @@ -75,9 +79,25 @@ public function read(string $type): string
{
$dist = [];

foreach ($this->files() as $file) {
$files = $this->files();

// filter out index.js files that have an index.dev.mjs counterpart (which takes precedence)
if ($type === 'js') {
$files = A::filter(
$files,
fn ($f) => !(
Str::endsWith($f, 'index.js') && F::exists(preg_replace('/\.js$/', '.dev.mjs', $f))
)
);
}
lukasbestle marked this conversation as resolved.
Show resolved Hide resolved

foreach ($files as $file) {
if (F::extension($file) === $type) {
if ($content = F::read($file)) {
if ($type === 'mjs') {
$content = F::uri($file);
}

if ($type === 'js') {
$content = trim($content);

Expand All @@ -92,6 +112,16 @@ public function read(string $type): string
}
}

if ($type === 'mjs') {
if (empty($dist)) {
return '';
}

$modules = Json::encode($dist);
$modulePromise = "Promise.all($modules.map(url => import(url)))";
return "try { await $modulePromise } catch (e) { console.error(e) }" . PHP_EOL;
}

return implode(PHP_EOL . PHP_EOL, $dist);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Toolkit/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public static function youtube(string $url, array $options = [], array $attr = [

break;

// regular video URLs
// regular video URLs
case 'watch':
if ($isYoutubeId($query->v) === true) {
$src = $host . '/' . $query->v;
Expand Down
14 changes: 12 additions & 2 deletions views/panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<link nonce="<?= $nonce ?>" rel="<?= $rel ?>" href="<?= Url::to($icon['url']) ?>" type="<?= $icon['type'] ?>">
<?php endforeach ?>

<?php foreach ($assets['js'] as $js): ?>
<?php if (($js['type'] ?? null) === 'module'): ?>
<link rel="modulepreload" href="<?= $js['src'] ?>">
<?php endif ?>
<?php endforeach ?>
jonaskuske marked this conversation as resolved.
Show resolved Hide resolved

<base href="<?= $panelUrl ?>">
</head>
<body>
Expand All @@ -59,8 +65,12 @@
window.fiber = json;
</script>

<?php foreach ($assets['js'] as $js): ?>
<?= Html::tag('script', '', $js) . PHP_EOL ?>
<?php foreach ($assets['js'] as $key => $js): ?>
<?php if ($key === 'index'): ?>
<?= Html::tag('script', [$assets['imports'], "import('{$js['src']}')"], A::without($js, 'src')) . PHP_EOL ?>
jonaskuske marked this conversation as resolved.
Show resolved Hide resolved
<?php else: ?>
<?= Html::tag('script', '', $js) . PHP_EOL ?>
<?php endif ?>
<?php endforeach ?>

</body>
Expand Down