Skip to content

Commit 9b18541

Browse files
committed
JavaScript: Enhance dependency optimization
1 parent c4b6e4b commit 9b18541

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

library/Icinga/Web/JavaScript.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,26 @@ public static function optimizeDefine($js, $filePath, $basePath, $packageName)
243243
continue;
244244
}
245245

246-
if (preg_match('~^((?:\.\.?/)+)*(.*)~', $dependencyName, $natch)) {
246+
$fileExtension = '.js';
247+
$dirname = dirname($filePath);
248+
if (preg_match('~^((?:\.\.?/)+)*.*?(\.\w+)?$~', $dependencyName, $natch)) {
249+
if (! empty($natch[1])) {
250+
$dependencyName = substr($dependencyName, strlen($natch[1]));
251+
$dirname = realpath(join(DIRECTORY_SEPARATOR, [$dirname, $natch[1]]));
252+
}
253+
254+
if (! empty($natch[2])) {
255+
$dependencyName = substr($dependencyName, 0, -strlen($natch[2]));
256+
$fileExtension = $natch[2];
257+
}
258+
}
259+
260+
$dependencyPath = join(DIRECTORY_SEPARATOR, [$dirname, $dependencyName . $fileExtension]);
261+
if (file_exists($dependencyPath)) {
247262
$dependencyName = join(DIRECTORY_SEPARATOR, array_filter([
248263
$packageName,
249-
ltrim(substr(
250-
realpath(join(DIRECTORY_SEPARATOR, [dirname($filePath), $natch[1]])),
251-
strlen(realpath($basePath))
252-
), DIRECTORY_SEPARATOR),
253-
$natch[2]
264+
trim(substr($dirname, strlen(realpath($basePath))), DIRECTORY_SEPARATOR . ' '),
265+
$dependencyName
254266
]));
255267
}
256268
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* No requirements */
2+
define(function () {
3+
4+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Relative path, No extension */
2+
define(["someThing/Else"], function (Else) {
3+
4+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Relative path outside the current directory, With extension */
2+
define(["../someOther.js"], function (someOther) {
3+
4+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Tests\Icinga\Web;
4+
5+
use Icinga\Application\Icinga;
6+
use Icinga\Test\BaseTestCase;
7+
use Icinga\Web\JavaScript;
8+
use SplFileObject;
9+
10+
class JavaScriptTest extends BaseTestCase
11+
{
12+
protected $fileRoot;
13+
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->fileRoot = Icinga::app()->getBaseDir('test/config/JavaScriptTest');
19+
}
20+
21+
public function testLocalDefineOptimizations()
22+
{
23+
$someThing = $this->getFile('someThing.js');
24+
$this->assertSame(<<<'JS'
25+
/* Relative path, No extension */
26+
define("JavaScriptTest/someThing", ["JavaScriptTest/someThing/Else"], function (Else) {
27+
28+
});
29+
30+
JS,
31+
$this->optimizeFile($someThing)
32+
);
33+
34+
$someThingElse = $this->getFile('someThing/Else.js');
35+
$this->assertSame(<<<'JS'
36+
/* Relative path outside the current directory, With extension */
37+
define("JavaScriptTest/someThing/Else", ["JavaScriptTest/someOther"], function (someOther) {
38+
39+
});
40+
41+
JS,
42+
$this->optimizeFile($someThingElse)
43+
);
44+
}
45+
46+
public function testNoRequirementsOptimization()
47+
{
48+
$this->assertSame(<<<'JS'
49+
define("JavaScriptTest/noRequirements", [], function () {
50+
51+
});
52+
53+
JS,
54+
JavaScript::optimizeDefine(<<<'JS'
55+
define(function () {
56+
57+
});
58+
59+
JS,
60+
'JavaScriptTest/noRequirements',
61+
'JavaScriptTest',
62+
'JavaScriptTest'
63+
)
64+
);
65+
}
66+
67+
public function testGlobalRequirementsOptimization()
68+
{
69+
$this->assertSame(<<<'JS'
70+
define("JavaScriptTest/globalRequirements", ["SomeOtherTest/Anything"], function (Anything) {
71+
72+
});
73+
74+
JS,
75+
JavaScript::optimizeDefine(<<<'JS'
76+
define(["SomeOtherTest/Anything"], function (Anything) {
77+
78+
});
79+
80+
JS,
81+
'JavaScriptTest/globalRequirements',
82+
'JavaScriptTest',
83+
'JavaScriptTest'
84+
)
85+
);
86+
}
87+
88+
protected function optimizeFile(SplFileObject $file): string
89+
{
90+
return JavaScript::optimizeDefine(
91+
$file->fread($file->getSize()),
92+
$file->getRealPath(),
93+
$this->fileRoot,
94+
'JavaScriptTest'
95+
);
96+
}
97+
98+
protected function getFile(string $file): SplFileObject
99+
{
100+
return new SplFileObject(join(DIRECTORY_SEPARATOR, [$this->fileRoot, $file]));
101+
}
102+
}

0 commit comments

Comments
 (0)