aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--main/app/sprinkles/core/src/Controller/CoreController.php71
1 files changed, 70 insertions, 1 deletions
diff --git a/main/app/sprinkles/core/src/Controller/CoreController.php b/main/app/sprinkles/core/src/Controller/CoreController.php
index 4bacef2..1076a94 100644
--- a/main/app/sprinkles/core/src/Controller/CoreController.php
+++ b/main/app/sprinkles/core/src/Controller/CoreController.php
@@ -95,9 +95,78 @@ class CoreController extends SimpleController
throw new NotFoundException($request, $response);
}
+ $content = $assetLoader->getContent();
+
+ switch ($assetLoader->getType()) {
+ case "text/css":
+ if (trim($content) === "") return $content;
+ $content = preg_replace(
+ array(
+ // Remove comment(s)
+ '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)|^\s*|\s*$#s',
+ // Remove unused white-space(s)
+ '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
+ // Replace `0(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)` with `0`
+ '#(?<=[\s:])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si',
+ // Replace `:0 0 0 0` with `:0`
+ '#:(0\s+0|0\s+0\s+0\s+0)(?=[;\}]|\!important)#i',
+ // Replace `background-position:0` with `background-position:0 0`
+ '#(background-position):0(?=[;\}])#si',
+ // Replace `0.6` with `.6`, but only when preceded by `:`, `,`, `-` or a white-space
+ '#(?<=[\s:,\-])0+\.(\d+)#s',
+ // Minify string value
+ '#(\/\*(?>.*?\*\/))|(?<!content\:)([\'"])([a-z_][a-z0-9\-_]*?)\2(?=[\s\{\}\];,])#si',
+ '#(\/\*(?>.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si',
+ // Minify HEX color code
+ '#(?<=[\s:,\-]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i',
+ // Replace `(border|outline):none` with `(border|outline):0`
+ '#(?<=[\{;])(border|outline):none(?=[;\}\!])#',
+ // Remove empty selector(s)
+ '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#s'
+ ),
+ array(
+ '$1',
+ '$1$2$3$4$5$6$7',
+ '$1',
+ ':0',
+ '$1:0 0',
+ '.$1',
+ '$1$3',
+ '$1$2$4$5',
+ '$1$2$3',
+ '$1:0',
+ '$1$2'
+ ),
+ $content);
+ break;
+ case "text/javascript":
+ if(trim($content) === "") return $content;
+ $content = preg_replace(
+ array(
+ // Remove comment(s)
+ '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#',
+ // Remove white-space(s) outside the string and regex
+ '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s',
+ // Remove the last semicolon
+ '#;+\}#',
+ // Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}`
+ '#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i',
+ // --ibid. From `foo['bar']` to `foo.bar`
+ '#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i'
+ ),
+ array(
+ '$1',
+ '$1$2',
+ '}',
+ '$1$3',
+ '$1.$3'
+ ),
+ $content);
+ }
+
return $response
->withHeader('Content-Type', $assetLoader->getType())
->withHeader('Content-Length', $assetLoader->getLength())
- ->write($assetLoader->getContent());
+ ->write($content);
}
}