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

JavaScript::optimizeDefine(): Take multiple define() calls in same file into account #5255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
90 changes: 50 additions & 40 deletions library/Icinga/Web/JavaScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,57 +213,67 @@ public static function send($minified = false)
*/
public static function optimizeDefine($js, $filePath, $basePath, $packageName)
{
if (! preg_match(self::DEFINE_RE, $js, $match) || strpos($js, 'define.amd') !== false) {
if (! preg_match_all(self::DEFINE_RE, $js, $matches, PREG_SET_ORDER) || strpos($js, 'define.amd') !== false) {
return $js;
}

try {
$assetName = $match[1] ? Json::decode($match[1]) : '';
if (! $assetName) {
$assetName = explode('.', basename($filePath))[0];
}
foreach ($matches as $match) {
try {
$assetName = $match[1] ? Json::decode($match[1]) : '';
if (! $assetName) {
$assetName = explode('.', basename($filePath))[0];
}

$assetName = join(DIRECTORY_SEPARATOR, array_filter([
$packageName,
ltrim(substr(dirname($filePath), strlen($basePath)), DIRECTORY_SEPARATOR),
$assetName
]));
$assetName = join(DIRECTORY_SEPARATOR, array_filter([
$packageName,
ltrim(substr(dirname($filePath), strlen($basePath)), DIRECTORY_SEPARATOR),
$assetName
]));

$assetName = Json::encode($assetName, JSON_UNESCAPED_SLASHES);
} catch (JsonDecodeException $_) {
$assetName = $match[1];
Logger::debug(
'Can\'t optimize name of "%s". Are single quotes used instead of double quotes?',
$filePath
);
}

$assetName = Json::encode($assetName, JSON_UNESCAPED_SLASHES);
} catch (JsonDecodeException $_) {
$assetName = $match[1];
Logger::debug('Can\'t optimize name of "%s". Are single quotes used instead of double quotes?', $filePath);
}
try {
$dependencies = $match[2] ? Json::decode($match[2]) : [];
foreach ($dependencies as &$dependencyName) {
if ($dependencyName === 'exports') {
// exports is a special keyword and doesn't need optimization
continue;
}

try {
$dependencies = $match[2] ? Json::decode($match[2]) : [];
foreach ($dependencies as &$dependencyName) {
if ($dependencyName === 'exports') {
// exports is a special keyword and doesn't need optimization
continue;
if (preg_match('~^((?:\.\.?/)+)*(.*)~', $dependencyName, $natch)) {
$dependencyName = join(DIRECTORY_SEPARATOR, array_filter([
$packageName,
ltrim(
substr(
realpath(join(DIRECTORY_SEPARATOR, [dirname($filePath), $natch[1]])),
strlen(realpath($basePath))
),
DIRECTORY_SEPARATOR
),
$natch[2]
]));
}
}

if (preg_match('~^((?:\.\.?/)+)*(.*)~', $dependencyName, $natch)) {
$dependencyName = join(DIRECTORY_SEPARATOR, array_filter([
$packageName,
ltrim(substr(
realpath(join(DIRECTORY_SEPARATOR, [dirname($filePath), $natch[1]])),
strlen(realpath($basePath))
), DIRECTORY_SEPARATOR),
$natch[2]
]));
}
$dependencies = Json::encode($dependencies, JSON_UNESCAPED_SLASHES);
} catch (JsonDecodeException $_) {
$dependencies = $match[2];
Logger::debug(
'Can\'t optimize dependencies of "%s". Are single quotes used instead of double quotes?',
$filePath
);
}

$dependencies = Json::encode($dependencies, JSON_UNESCAPED_SLASHES);
} catch (JsonDecodeException $_) {
$dependencies = $match[2];
Logger::debug(
'Can\'t optimize dependencies of "%s". Are single quotes used instead of double quotes?',
$filePath
);
$js = str_replace($match[0], sprintf("define(%s, %s, %s", $assetName, $dependencies, $match[3]), $js);
}

return str_replace($match[0], sprintf("define(%s, %s, %s", $assetName, $dependencies, $match[3]), $js);
return $js;
}
}
Loading