Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i manage frontend assets with laravel mix and git?

I'm using Mix (this applies to Elixir as well i guess) to manage my frontend assets. I have Mix configured to version files in production but not in dev. As such, when i update assets, i get updated app.js and app.css files in dev and app.{hash}.js and app.{hash}.css files in production.

What is your advice on how to leverage this with version control?

  1. Put the files in version control? If so how do i manage the constant changes during development? Keep adding new hashed files? Do i leave app.js and app.css on the production server?
  2. .gitignore the js and css files and run npm on the production server to compile the assets locally?
  3. Some other solution?

What are your thoughts on this?

Thanks in advance.

like image 366
Gonçalo Marrafa Avatar asked Mar 16 '17 14:03

Gonçalo Marrafa


People also ask

Which command will compile frontend assets in laravel?

mix. js('resources/js/app. js', 'public/js');

What is laravel mix and how do you use it?

Laravel Mix is a tool for compiling and optimizing assets in a Laravel app. It's similar to a build tool like gulp, Grunt and such like. it's specific to Laravel but can also be used externally as an npm package. Laravel Mix covered 80% of Webpack's use case to make compiling assets easier.

What is webpack mix JS in laravel?

Introduction. Laravel Mix, a package developed by Laracasts creator Jeffrey Way, provides a fluent API for defining webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors. . postCss('resources/css/app.


1 Answers

At my workplace we've .gitignore'd the css/js folders for almost a year for different projects with no issue just letting the server handle npm as part of it's build step.

And if you use yarn, it'll create a lock file which ensures you'll get the same exact js dependencies on production.

When using versioning just use the mix() helper on your script/css tags and it'll handle pointing to the correct file via the mix-manifest.json file in the public directory.

For example:

layout/app.blade.php has

<script src="{{ mix('js/app.js') }}"></script>

when you run npm mix creates the the public/mix-manifest.json file like so

{ "/js/app.js": "/js/app.{hash-example}.js" }

The only issue I've seen is with mix and using the mix() helper it appears to only work while npm watch is being run, which is something elixir handled fine before but mix is relatively new so it might get addressed, I myself haven't been annoyed enough to look into it since during development I always run npm run hot which does hot reloading on any js file changes. -- Edit: this appears to be fixed now

like image 189
Justin Reasoner Avatar answered Oct 13 '22 21:10

Justin Reasoner