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)); $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($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']; } } throw new RuntimeException('Css not found'); } }