Created
June 29, 2019 16:04
-
-
Save abdumu/ec16e768073a0a1e0566d5522bbce2ad to your computer and use it in GitHub Desktop.
production mix versioning ...
This file contains 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 characters
<?php | |
// .... | |
public function boot() | |
{ | |
Blade::directive('mix', function ($path) { | |
if(! app()->environment('production')) { | |
return "<?=e(mix({$path}))?>"; | |
} | |
$path = trim($path, '"\''); | |
if (! Str::startsWith($path, '/')) { | |
$path = "/{$path}"; | |
} | |
$manifestPath = public_path('/mix-manifest.json'); | |
if (! file_exists($manifestPath)) { | |
throw new \Exception('The Mix manifest does not exist.'); | |
} | |
$manifest = json_decode(file_get_contents($manifestPath), true); | |
if (! isset($manifest[$path])) { | |
$exception = new \Exception("Unable to locate Mix file: {$path}."); | |
if (! app('config')->get('app.debug')) { | |
report($exception); | |
return $path; | |
} else { | |
throw $exception; | |
} | |
} | |
preg_match('/(.*?)\/([a-z0-9._-]+)\.([a-z]+)\?id=([\w\d]+)/', $manifest[$path], $matches); | |
return url("{$matches[1]}/{$matches[2]}.{$matches[4]}.{$matches[3]}"); | |
}); | |
} |
This file contains 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 characters
mix.then(function() { | |
if (mix.inProduction()) { | |
let jsFileVersion = new File('public/js/app.js').version() | |
new File('public/js/app.js').rename('app.' + jsFileVersion + '.js') | |
let cssFileVersion = new File('public/css/app.css').version() | |
new File('public/css/app.css').rename('app.' + cssFileVersion + '.css') | |
console.log("[production] renamed app.js -> app." + jsFileVersion + ".js") | |
console.log("[production] renamed app.css -> app." + cssFileVersion + ".css") | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment