-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JavaScript: Enhance dependency optimization
- Loading branch information
Showing
5 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* No requirements */ | ||
define(function () { | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* Relative path, No extension */ | ||
define(["someThing/Else"], function (Else) { | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* Relative path outside the current directory, With extension */ | ||
define(["../someOther.js"], function (someOther) { | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Tests\Icinga\Web; | ||
|
||
use Icinga\Application\Icinga; | ||
use Icinga\Test\BaseTestCase; | ||
use Icinga\Web\JavaScript; | ||
use SplFileObject; | ||
|
||
class JavaScriptTest extends BaseTestCase | ||
{ | ||
protected $fileRoot; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->fileRoot = Icinga::app()->getBaseDir('test/config/JavaScriptTest'); | ||
} | ||
|
||
public function testLocalDefineOptimizations() | ||
{ | ||
$someThing = $this->getFile('someThing.js'); | ||
$this->assertSame(<<<'JS' | ||
/* Relative path, No extension */ | ||
define("JavaScriptTest/someThing", ["JavaScriptTest/someThing/Else"], function (Else) { | ||
}); | ||
|
||
JS, | ||
$this->optimizeFile($someThing) | ||
); | ||
|
||
$someThingElse = $this->getFile('someThing/Else.js'); | ||
$this->assertSame(<<<'JS' | ||
/* Relative path outside the current directory, With extension */ | ||
define("JavaScriptTest/someThing/Else", ["JavaScriptTest/someOther"], function (someOther) { | ||
}); | ||
|
||
JS, | ||
$this->optimizeFile($someThingElse) | ||
); | ||
} | ||
|
||
public function testNoRequirementsOptimization() | ||
{ | ||
$this->assertSame(<<<'JS' | ||
define("JavaScriptTest/noRequirements", [], function () { | ||
}); | ||
|
||
JS, | ||
JavaScript::optimizeDefine(<<<'JS' | ||
define(function () { | ||
}); | ||
|
||
JS, | ||
'JavaScriptTest/noRequirements', | ||
'JavaScriptTest', | ||
'JavaScriptTest' | ||
) | ||
); | ||
} | ||
|
||
public function testGlobalRequirementsOptimization() | ||
{ | ||
$this->assertSame(<<<'JS' | ||
define("JavaScriptTest/globalRequirements", ["SomeOtherTest/Anything"], function (Anything) { | ||
}); | ||
|
||
JS, | ||
JavaScript::optimizeDefine(<<<'JS' | ||
define(["SomeOtherTest/Anything"], function (Anything) { | ||
}); | ||
|
||
JS, | ||
'JavaScriptTest/globalRequirements', | ||
'JavaScriptTest', | ||
'JavaScriptTest' | ||
) | ||
); | ||
} | ||
|
||
protected function optimizeFile(SplFileObject $file): string | ||
{ | ||
return JavaScript::optimizeDefine( | ||
$file->fread($file->getSize()), | ||
$file->getRealPath(), | ||
$this->fileRoot, | ||
'JavaScriptTest' | ||
); | ||
} | ||
|
||
protected function getFile(string $file): SplFileObject | ||
{ | ||
return new SplFileObject(join(DIRECTORY_SEPARATOR, [$this->fileRoot, $file])); | ||
} | ||
} |