Skip to content

Commit e2c6b39

Browse files
committed
JavaScript: Enhance dependency optimization
1 parent e12574f commit e2c6b39

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

library/Icinga/Web/JavaScript.php

+18-6
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
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* No requirements */
2+
define(function () {
3+
4+
});
+4
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+
});
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+
});
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+
$expected = <<<'JS'
24+
/* Relative path, No extension */
25+
define("JavaScriptTest/someThing", ["JavaScriptTest/someThing/Else"], function (Else) {
26+
27+
});
28+
29+
JS;
30+
$someThing = $this->getFile('someThing.js');
31+
$this->assertSame($expected, $this->optimizeFile($someThing));
32+
33+
$expected = <<<'JS'
34+
/* Relative path outside the current directory, With extension */
35+
define("JavaScriptTest/someThing/Else", ["JavaScriptTest/someOther"], function (someOther) {
36+
37+
});
38+
39+
JS;
40+
$someThingElse = $this->getFile('someThing/Else.js');
41+
$this->assertSame($expected, $this->optimizeFile($someThingElse));
42+
}
43+
44+
public function testNoRequirementsOptimization()
45+
{
46+
$expected = <<<'JS'
47+
define("JavaScriptTest/noRequirements", [], function () {
48+
49+
});
50+
51+
JS;
52+
$source = <<<'JS'
53+
define(function () {
54+
55+
});
56+
57+
JS;
58+
$this->assertSame($expected, JavaScript::optimizeDefine(
59+
$source,
60+
'JavaScriptTest/noRequirements',
61+
'JavaScriptTest',
62+
'JavaScriptTest'
63+
));
64+
}
65+
66+
public function testGlobalRequirementsOptimization()
67+
{
68+
$expected = <<<'JS'
69+
define("JavaScriptTest/globalRequirements", ["SomeOtherTest/Anything"], function (Anything) {
70+
71+
});
72+
73+
JS;
74+
$source = <<<'JS'
75+
define(["SomeOtherTest/Anything"], function (Anything) {
76+
77+
});
78+
79+
JS;
80+
$this->assertSame($expected, JavaScript::optimizeDefine(
81+
$source,
82+
'JavaScriptTest/globalRequirements',
83+
'JavaScriptTest',
84+
'JavaScriptTest'
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)