Skip to content

Instantly share code, notes, and snippets.

@abdumu
Created June 29, 2019 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdumu/ec16e768073a0a1e0566d5522bbce2ad to your computer and use it in GitHub Desktop.
Save abdumu/ec16e768073a0a1e0566d5522bbce2ad to your computer and use it in GitHub Desktop.
production mix versioning ...
<?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]}");
});
}
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