Skip to content

Commit

Permalink
#84 x60 faster critical css extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Dec 24, 2021
1 parent b37168b commit aa56b9c
Show file tree
Hide file tree
Showing 73 changed files with 2,139 additions and 2,040 deletions.
28 changes: 13 additions & 15 deletions gzip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@
</fields>
</fieldset>
<fieldset name="css" label="CSS">
<field type="spacer" label="Configure Css settings" />
<field type="spacer" hr="true" />
<fields name="gzip">
<field type="spacer" label="General settings" />
<field name="cssenabled" label="PLG_GZIP_FIELD_CSS_ENABLED_LABEL" description="PLG_GZIP_FIELD_CSS_ENABLED_DESCRIPTION" type="radio" class="btn-group" default="1">
<option value="1">JYes</option>
<option value="0">JNo</option>
Expand All @@ -309,6 +308,13 @@
<option value="1">JYes</option>
<option value="0">JNo</option>
</field>
<field type="spacer" hr="true" />
<field type="spacer" label="Critical CSS path" />
<field name="criticalcssenabled" label="PLG_GZIP_FIELD_CRITICALCSS_ENABLED_LABEL" description="PLG_GZIP_FIELD_CRITICALCSS_ENABLED_DESCRIPTION" type="radio" class="btn-group" default="0">
<option value="1">JYes</option>
<option value="0">JNo</option>
</field>
<field name="criticalcssviewports" label="PLG_GZIP_FIELD_CRITICALCSS_VIEWPORT_LABEL" description="PLG_GZIP_FIELD_CRITICALCSS_VIEWPORT_DESCRIPTION" hint="PLG_GZIP_FIELD_CRITICALCSS_VIEWPORT_HINT" type="textarea" rows="5" cols="15" default="800x600" />
<fields name="css_parser">
<field type="spacer" hr="true" />
<field type="spacer" label="Parser Options" />
Expand All @@ -324,6 +330,10 @@
<option value="1">JYes</option>
<option value="0">JNo</option>
</field>
<field name="capture_errors" label="PLG_GZIP_FIELD_CSS_PARSER_CAPTURE_ERRORS_LABEL" description="PLG_GZIP_FIELD_CSS_PARSER_ALLOW_CAPTURE_ERRORS_DESCRIPTION" default="1" type="list" class="btn-group">
<option value="1">JOPTION_CSS_PARSER_IGNORE</option>
<option value="0">JOPTION_CSS_PARSER_EXCEPTION</option>
</field>
<field type="spacer" hr="true" />
</fields>
<fields name="css_renderer">
Expand Down Expand Up @@ -361,23 +371,11 @@
<option value="auto">PLG_GZIP_FIELD_FONTDISPLAY_OPTION_AUTO</option>
</field>
<field type="spacer" hr="true" />
<field type="spacer" label="File Matching" />
<field name="cssignore" label="PLG_GZIP_FIELD_CSSIGNORE_LABEL" description="PLG_GZIP_FIELD_CSSIGNORE_DESCRIPTION" hint="PLG_GZIP_FIELD_CSSIGNORE_HINT" type="textarea" rows="5" cols="45" />
<field name="cssremove" label="PLG_GZIP_FIELD_CSSREMOVE_LABEL" description="PLG_GZIP_FIELD_CSSREMOVE_DESCRIPTION" type="textarea" rows="5" cols="45" />
</fields>
</fieldset>
<fieldset name="critical_path" label="Critical Css">
<field type="spacer" label="Configure Critical CSS path" />
<field type="spacer" hr="true" />
<fields name="gzip">
<field name="criticalcssenabled" label="PLG_GZIP_FIELD_CRITICALCSS_ENABLED_LABEL" description="PLG_GZIP_FIELD_CRITICALCSS_ENABLED_DESCRIPTION" type="radio" class="btn-group" default="0">
<option value="1">JYes</option>
<option value="0">JNo</option>
</field>
<field name="criticalcssviewports" label="PLG_GZIP_FIELD_CRITICALCSS_VIEWPORT_LABEL" description="PLG_GZIP_FIELD_CRITICALCSS_VIEWPORT_DESCRIPTION" hint="PLG_GZIP_FIELD_CRITICALCSS_VIEWPORT_HINT" type="textarea" rows="5" cols="15" default="800x600" />
<!-- <field name="criticalcssclass" label="PLG_GZIP_FIELD_CRITICALCSS_CLASS_LABEL" description="PLG_GZIP_FIELD_CRITICALCSS_CLASS_DESCRIPTION" hint="PLG_GZIP_FIELD_CRITICALCSS_CLASS_HINT" type="textarea" rows="5" cols="15" default="" />-->
<!-- <field name="criticalcss" label="PLG_GZIP_FIELD_CRITICALCSS_LABEL" description="PLG_GZIP_FIELD_CRITICALCSS_DESCRIPTION" hint="PLG_GZIP_FIELD_CRITICALCSS_HINT" type="textarea" rows="5" cols="15" default="" />-->
</fields>
</fieldset>
<fieldset name="pwa_app" label="Service Worker">
<field type="spacer" label="Configure progressive web application settings" />
<field type="spacer" hr="true" />
Expand Down
28 changes: 15 additions & 13 deletions helpers/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,15 @@ public function processHTML(array $options, $html)

$async = !empty($options['asynccss']) || !empty($options['criticalcssenabled']);

$css_options = isset($options['css_options']) ? $options['css_options'] : [
$css_renderer_options = isset($options['css_renderer']) ? $options['css_renderer'] : [
'compress' => !empty($options['minifycss'])
];

$css_parser_options = isset($options['css_parser']) ? $options['css_parser'] : [];

$hashFile = GZipHelper::getHashMethod($options);
$cssParser = new Parser();
$cssRenderer = new Renderer($css_options);
$cssParser = new Parser('', $css_parser_options);
$cssRenderer = new Renderer($css_renderer_options);
$headStyle = new Stylesheet();

$fetchFonts = function ($node) use ($options, $headStyle) {
Expand Down Expand Up @@ -326,7 +328,7 @@ public function processHTML(array $options, $html)
$profiler = \JProfiler::getInstance('Application');
$profiler->mark('CssInit');

$html = preg_replace_callback('#<(' . (empty($options['parseinlinecss']) ? 'link' : '[^\s>]+') . ')([^>]*)>#', function ($matches) use ($css_options, $cssRenderer, $parseUrls, &$links, $ignore, $remove, $cssParser, $path, $fetch_remote, $options) {
$html = preg_replace_callback('#<(' . (empty($options['parseinlinecss']) ? 'link' : '[^\s>]+') . ')([^>]*)>#', function ($matches) use ($css_renderer_options, $cssRenderer, $parseUrls, &$links, $ignore, $remove, $cssParser, $path, $fetch_remote, $options) {

$attributes = [];

Expand Down Expand Up @@ -359,7 +361,7 @@ public function processHTML(array $options, $html)

return 'url(' . GZipHelper::url($name) . ')';

}, $attributes['style']) . ' }', $css_options))->parse()))));
}, $attributes['style']) . ' }', $css_renderer_options))->parse()))));

$result = '<' . $matches[1] . ' ';

Expand Down Expand Up @@ -455,13 +457,13 @@ public function processHTML(array $options, $html)
$hash .= $media;
}

$file = $path . GZipHelper::shorten(crc32($hash)) . '-main' . (!empty($css_options['compress']) ? '.min' : '') . '.css';
$file = $path . GZipHelper::shorten(crc32($hash)) . '-main' . (!empty($css_renderer_options['compress']) ? '.min' : '') . '.css';

if (!is_file($file)) {

foreach ($blob['links'] as $key => $attr) {

$cssParser->append($attr['href']);
$cssParser->append($attr['href'], isset($attr['media']) && $attr['media'] != 'all' && $attr['media'] !== '' ? $attr['media'] : '');
}

file_put_contents($file, $parseUrls($cssRenderer->render($this->parseBackgroundImages($cssParser->parse()->traverse($fetchFonts, 'enter'), $options))));
Expand All @@ -479,7 +481,7 @@ public function processHTML(array $options, $html)

$profiler->mark('CssMergeFile');

if (empty($options['mergecss']) && !empty($css_options['compress'])) {
if (empty($options['mergecss']) && !empty($css_renderer_options['compress'])) {

foreach ($links as $position => $blob) {

Expand All @@ -493,11 +495,11 @@ public function processHTML(array $options, $html)

$file = $path . GZipHelper::shorten(crc32($hash)) . '-' .
GZipHelper::sanitizeFileName(pathinfo($attr['href'], PATHINFO_BASENAME)) .
(!empty($css_options['compress']) ? '.min' : '') . '.css';
(!empty($css_renderer_options['compress']) ? '.min' : '') . '.css';

if (!is_file($file)) {

$cssParser->load($attr['href']);
$cssParser->load($attr['href'], isset($attr['media']) && $attr['media'] != 'all' && $attr['media'] !== '' ? $attr['media'] : '');
file_put_contents($file, $parseUrls($cssRenderer->render($this->parseBackgroundImages($cssParser->parse()->traverse($fetchFonts, 'enter'), $options))));
}
}
Expand Down Expand Up @@ -530,7 +532,7 @@ public function processHTML(array $options, $html)
}
}

$html = preg_replace_callback('#(<style[^>]*>)(.*?)</style>#si', function ($matches) use (&$links, $css_options, $parseUrls) {
$html = preg_replace_callback('#(<style[^>]*>)(.*?)</style>#si', function ($matches) use (&$links, $css_renderer_options, $parseUrls) {

$attributes = [];

Expand Down Expand Up @@ -564,7 +566,7 @@ public function processHTML(array $options, $html)

$position = isset($attributes['data-position']) && $attributes['data-position'] == 'head' ? 'head' : 'body';
$matches[2] = $parseUrls($matches[2]);
$links[$position]['style'][] = !empty($css_options['compress']) ? (new Renderer($css_options))->renderAst(new Parser($matches[2])) : $parseUrls($matches[2]);
$links[$position]['style'][] = !empty($css_renderer_options['compress']) ? (new Renderer($css_renderer_options))->renderAst(new Parser($matches[2])) : $parseUrls($matches[2]);

return '';
}, $html);
Expand Down Expand Up @@ -625,7 +627,7 @@ public function processHTML(array $options, $html)

if ($style !== '') {

$headStyle->append($this->parseBackgroundImages((new Parser($style, $css_options))->parse(), $options));
$headStyle->append($this->parseBackgroundImages((new Parser($style, $css_renderer_options))->parse(), $options));

if ($headStyle->hasChildren()) {

Expand Down
6 changes: 6 additions & 0 deletions language/en-GB/en-GB.plg_system_gzip.ini
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ PLG_GZIP_FIELD_CSS_PARSER_ALLOW_DUPLICATE_DESCRIPTION="Allow duplicate rules or
PLG_GZIP_FIELD_CSS_PARSER_ALLOW_DUPLICATE_DECLARATIONS_LABEL="Allow Duplicate Declarations"
PLG_GZIP_FIELD_CSS_PARSER_ALLOW_DUPLICATE_DECLARATIONS_DESCRIPTION=""

PLG_GZIP_FIELD_CSS_PARSER_CAPTURE_ERRORS_LABEL="Parse Error"
PLG_GZIP_FIELD_CSS_PARSER_ALLOW_CAPTURE_ERRORS_DESCRIPTION="Parser error behavior"

JOPTION_CSS_PARSER_IGNORE="Ignore exceptions"
JOPTION_CSS_PARSER_EXCEPTION="Throw an exception"

PLG_GZIP_FIELD_CSS_RENDERING_CSS_LEVEL_LABEL="Css Level"
PLG_GZIP_FIELD_CSS_RENDERING_CSS_LEVEL_DESCRIPTION="Render Colors Using Css4 Syntax"

Expand Down
16 changes: 6 additions & 10 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ LIB.ready(function () {

link = links[i];

link.addEventListener('load', () => {

if (link.hasAttribute('data-media')) {
link.media = link.dataset.media;
link.removeAttribute("data-media");
} else {
link.removeAttribute("media");
}
})
if (link.hasAttribute('data-media')) {
link.media = link.dataset.media;
link.removeAttribute("data-media");
} else {
link.removeAttribute("media");
}
}

const scripts = document.querySelectorAll(
Expand Down Expand Up @@ -103,5 +100,4 @@ LIB.ready(function () {
})
}
}

});
2 changes: 1 addition & 1 deletion loader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,17 @@
},
{
"name": "mck89/peast",
"version": "v1.13.8",
"version_normalized": "1.13.8.0",
"version": "v1.13.9",
"version_normalized": "1.13.9.0",
"source": {
"type": "git",
"url": "https://github.com/mck89/peast.git",
"reference": "4f0423441ec557f3935b056d10987f2e1c7a3e76"
"reference": "5329e997fb50e0b82ca8f6e4164f92941f689b47"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mck89/peast/zipball/4f0423441ec557f3935b056d10987f2e1c7a3e76",
"reference": "4f0423441ec557f3935b056d10987f2e1c7a3e76",
"url": "https://api.github.com/repos/mck89/peast/zipball/5329e997fb50e0b82ca8f6e4164f92941f689b47",
"reference": "5329e997fb50e0b82ca8f6e4164f92941f689b47",
"shasum": ""
},
"require": {
Expand All @@ -282,11 +282,11 @@
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
},
"time": "2021-09-11T10:28:18+00:00",
"time": "2021-11-12T13:44:49+00:00",
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.13.8-dev"
"dev-master": "1.13.9-dev"
}
},
"installation-source": "dist",
Expand All @@ -309,7 +309,7 @@
"description": "Peast is PHP library that generates AST for JavaScript code",
"support": {
"issues": "https://github.com/mck89/peast/issues",
"source": "https://github.com/mck89/peast/tree/v1.13.8"
"source": "https://github.com/mck89/peast/tree/v1.13.9"
},
"install-path": "../mck89/peast"
},
Expand All @@ -320,12 +320,12 @@
"source": {
"type": "git",
"url": "https://github.com/tbela99/css.git",
"reference": "557993fb7cd0d0c4a00bb8b914d0ac5e0ba19f9f"
"reference": "28e0623998e4ded3d18f8f1dd99586cddc9d0d50"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/tbela99/css/zipball/557993fb7cd0d0c4a00bb8b914d0ac5e0ba19f9f",
"reference": "557993fb7cd0d0c4a00bb8b914d0ac5e0ba19f9f",
"url": "https://api.github.com/repos/tbela99/css/zipball/28e0623998e4ded3d18f8f1dd99586cddc9d0d50",
"reference": "28e0623998e4ded3d18f8f1dd99586cddc9d0d50",
"shasum": ""
},
"require": {
Expand All @@ -334,7 +334,7 @@
"ext-json": "*",
"php": ">=5.6"
},
"time": "2021-11-09T16:35:28+00:00",
"time": "2021-12-24T02:50:23+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
Expand Down
12 changes: 6 additions & 6 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => '25d4b6d8ecf8d8f973163b0665191a5482307d59',
'reference' => 'b37168bdd8a25c89f72e2857fc390ef2c764d55d',
'name' => '__root__',
'dev' => true,
),
Expand All @@ -16,7 +16,7 @@
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => '25d4b6d8ecf8d8f973163b0665191a5482307d59',
'reference' => 'b37168bdd8a25c89f72e2857fc390ef2c764d55d',
'dev_requirement' => false,
),
'axy/backtrace' => array(
Expand Down Expand Up @@ -65,12 +65,12 @@
'dev_requirement' => false,
),
'mck89/peast' => array(
'pretty_version' => 'v1.13.8',
'version' => '1.13.8.0',
'pretty_version' => 'v1.13.9',
'version' => '1.13.9.0',
'type' => 'library',
'install_path' => __DIR__ . '/../mck89/peast',
'aliases' => array(),
'reference' => '4f0423441ec557f3935b056d10987f2e1c7a3e76',
'reference' => '5329e997fb50e0b82ca8f6e4164f92941f689b47',
'dev_requirement' => false,
),
'tbela99/css' => array(
Expand All @@ -79,7 +79,7 @@
'type' => 'library',
'install_path' => __DIR__ . '/../tbela99/css',
'aliases' => array(),
'reference' => '557993fb7cd0d0c4a00bb8b914d0ac5e0ba19f9f',
'reference' => '28e0623998e4ded3d18f8f1dd99586cddc9d0d50',
'dev_requirement' => false,
),
),
Expand Down
2 changes: 1 addition & 1 deletion vendor/mck89/peast/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.13.8-dev"
"dev-master": "1.13.9-dev"
}
}
}
3 changes: 3 additions & 0 deletions vendor/mck89/peast/doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
==========

#### 1.13.9
* Implemented parsing of arbitrary module namespace identifier names

#### 1.13.8
* Fixed identifiers bug introduced in the last release

Expand Down
7 changes: 7 additions & 0 deletions vendor/mck89/peast/lib/Peast/Syntax/ES2022/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ class Features extends \Peast\Syntax\ES2021\Features
* @var bool
*/
public $classStaticBlock = true;

/**
* Arbitrary module namespace identifier names
*
* @var bool
*/
public $arbitraryModuleNSNames = true;
}
7 changes: 7 additions & 0 deletions vendor/mck89/peast/lib/Peast/Syntax/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,11 @@ class Features
* @var bool
*/
public $classStaticBlock = false;

/**
* Arbitrary module namespace identifier names
*
* @var bool
*/
public $arbitraryModuleNSNames = false;
}
Loading

0 comments on commit aa56b9c

Please sign in to comment.