Last active
February 29, 2020 08:28
-
-
Save ulcuber/7e8ddf27e94eef62fc4776157dd36bcb to your computer and use it in GitHub Desktop.
Revisions
-
ulcuber revised this gist
Feb 29, 2020 . 1 changed file with 16 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,20 +10,27 @@ /** * Warning: chrome coverage skips @keyframes, @font-face, @media * So, need to bundle auto.css with media.css after this command */ class OptimizeCssCommand extends Command { protected $signature = 'css:optimize'; protected $description = 'Optimize css using chrome coverage'; public function handle() { $this->optimize('public/css/app.css', 'resources/css/app-desktop-auto.css', 'Coverage-desktop.json'); $this->optimize('public/css/app.css', 'resources/css/app-mobile-auto.css', 'Coverage-mobile.json'); $this->info('OK'); } public function optimize(string $from, string $to, string $coverage) { $disk = Storage::disk('root'); $file = $disk->get($from); $ranges = $this->getCoverageRanges($coverage); $bar = $this->output->createProgressBar(count($ranges)); @@ -45,22 +52,20 @@ public function handle() $bar->finish(); $this->info(''); $disk->put($to, $minified); } protected function getCoverageRanges(string $file): array { $coverageFile = Storage::disk('local')->get($file); $coverage = json_decode($coverageFile, true); if (!is_array($coverage)) { throw new RuntimeException('Coverage not found'); } foreach ($coverage as $item) { if (Str::contains($item['url'], '/css/app.css')) { return $item['ranges']; } } -
ulcuber created this gist
Feb 24, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ <?php namespace App\Console\Commands; use RuntimeException; use Illuminate\Support\Str; use Illuminate\Console\Command; use Illuminate\Support\Facades\Storage; /** * Warning: chrome coverage skips @keyframes, @font-face, @media */ class OptimizeCssCommand extends Command { protected $signature = 'css:optimize'; protected $description = 'Optimize css using chrome coverage'; protected const CSS_PATH = 'css/app.css'; public function handle() { $disk = Storage::disk('real-public'); $file = $disk->get(static::CSS_PATH); $ranges = $this->getCoverageRanges(); $bar = $this->output->createProgressBar(count($ranges)); $bar->start(); $minified = ''; $last = ''; $start = 0; while ($range = array_shift($ranges)) { if ($range['start'] !== $start) { $minified .= $last; } $start = $range['start']; $length = $range['end'] - $start; $last = mb_substr($file, $start, $length); $bar->advance(); } $bar->finish(); $this->info(''); $disk->put(static::CSS_PATH, $minified); $this->info('OK'); } protected function getCoverageRanges(): array { $coverageFile = Storage::disk('local')->get('Coverage.json'); $coverage = json_decode($coverageFile, true); if (!is_array($coverage)) { throw new RuntimeException('Coverage not found'); } foreach ($coverage as $item) { if (Str::contains($item['url'], static::CSS_PATH)) { return $item['ranges']; } } throw new RuntimeException('Css not found'); } }