Skip to content

Instantly share code, notes, and snippets.

@ulcuber
Last active February 29, 2020 08:28
Show Gist options
  • Save ulcuber/7e8ddf27e94eef62fc4776157dd36bcb to your computer and use it in GitHub Desktop.
Save ulcuber/7e8ddf27e94eef62fc4776157dd36bcb to your computer and use it in GitHub Desktop.

Revisions

  1. ulcuber revised this gist Feb 29, 2020. 1 changed file with 16 additions and 11 deletions.
    27 changes: 16 additions & 11 deletions OptimizeCssCommand.php
    Original 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';

    protected const CSS_PATH = 'css/app.css';

    public function handle()
    {
    $disk = Storage::disk('real-public');
    $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(static::CSS_PATH);
    $ranges = $this->getCoverageRanges();
    $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(static::CSS_PATH, $minified);

    $this->info('OK');
    $disk->put($to, $minified);
    }

    protected function getCoverageRanges(): array
    protected function getCoverageRanges(string $file): array
    {
    $coverageFile = Storage::disk('local')->get('Coverage.json');
    $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'], static::CSS_PATH)) {
    if (Str::contains($item['url'], '/css/app.css')) {
    return $item['ranges'];
    }
    }
  2. ulcuber created this gist Feb 24, 2020.
    69 changes: 69 additions & 0 deletions OptimizeCssCommand.php
    Original 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');
    }
    }