Laravel Elixir

Introduction

Laravel Elixir provides a clean, fluent API for defining basic Gulp tasks for your Laravel application. Elixir supports several common CSS and JavaScript pre-processors, and even testing tools.

If you've ever been confused about how to get started with Gulp and asset compilation, you will love Laravel Elixir!

Installation & Setup

Installing Node

Before triggering Elixir, you must first ensure that Node.js is installed on your machine.

  1. node -v

By default, Laravel Homestead includes everything you need; however, if you aren't using Vagrant, then you can easily install Node by visiting their download page. Don't worry, it's quick and easy!

Gulp

Next, you'll want to pull in Gulp as a global NPM package like so:

  1. npm install --global gulp

Laravel Elixir

The only remaining step is to install Elixir! With a new install of Laravel, you'll find a package.json file in the root. Think of this like your composer.json file, except it defines Node dependencies instead of PHP. You may install the dependencies it references by running:

  1. npm install

Usage

Now that you've installed Elixir, you'll be compiling and concatenating in no time! The gulpfile.js file in your project's root directory contains all of your Elixir tasks.

Compile Less

  1. elixir(function(mix) {
  2. mix.less("app.less");
  3. });

In the example above, Elixir assumes that your Less files are stored in resources/assets/less.

Compile Multiple Less Files

  1. elixir(function(mix) {
  2. mix.less([
  3. 'app.less',
  4. 'something-else.less'
  5. ]);
  6. });

Compile Sass

  1. elixir(function(mix) {
  2. mix.sass("app.scss");
  3. });

This assumes that your Sass files are stored in resources/assets/sass.

By default, Elixir, underneath the hood, uses the LibSass library for compilation. In some instances, it might prove advantageous to instead leverage the Ruby version, which, though slower, is more feature rich. Assuming that you have both Ruby and the Sass gem installed (gem install sass), you may enable Ruby-mode, like so:

  1. elixir(function(mix) {
  2. mix.rubySass("app.sass");
  3. });

Compile Without Source Maps

  1. elixir.config.sourcemaps = false;
  2. elixir(function(mix) {
  3. mix.sass("app.scss");
  4. });

Source maps are enabled out of the box. As such, for each file that is compiled, you'll find a companion *.css.map file in the same directory. This mapping allows you to, when debugging, trace your compiled stylesheet selectors back to your original Sass or Less partials! Should you need to disable this functionality, however, the code sample above will do the trick.

Compile CoffeeScript

  1. elixir(function(mix) {
  2. mix.coffee();
  3. });

This assumes that your CoffeeScript files are stored in resources/assets/coffee.

Compile All Less and CoffeeScript

  1. elixir(function(mix) {
  2. mix.less()
  3. .coffee();
  4. });

Trigger PHPUnit Tests

  1. elixir(function(mix) {
  2. mix.phpUnit();
  3. });

Trigger PHPSpec Tests

  1. elixir(function(mix) {
  2. mix.phpSpec();
  3. });

Combine Stylesheets

  1. elixir(function(mix) {
  2. mix.styles([
  3. "normalize.css",
  4. "main.css"
  5. ]);
  6. });

Paths passed to this method are relative to the resources/assets/css directory.

Combine Stylesheets and Save to a Custom Directory

  1. elixir(function(mix) {
  2. mix.styles([
  3. "normalize.css",
  4. "main.css"
  5. ], 'public/build/css/everything.css');
  6. });

Combine Stylesheets From A Custom Base Directory

  1. elixir(function(mix) {
  2. mix.styles([
  3. "normalize.css",
  4. "main.css"
  5. ], 'public/build/css/everything.css', 'public/css');
  6. });

The third argument to both the styles and scripts methods determines the relative directory for all paths passed to the methods.

Combine All Styles in a Directory

  1. elixir(function(mix) {
  2. mix.stylesIn("public/css");
  3. });

Combine Scripts

  1. elixir(function(mix) {
  2. mix.scripts([
  3. "jquery.js",
  4. "app.js"
  5. ]);
  6. });

Again, this assumes all paths are relative to the resources/assets/js directory.

Combine All Scripts in a Directory

  1. elixir(function(mix) {
  2. mix.scriptsIn("public/js/some/directory");
  3. });

Combine Multiple Sets of Scripts

  1. elixir(function(mix) {
  2. mix.scripts(['jquery.js', 'main.js'], 'public/js/main.js')
  3. .scripts(['forum.js', 'threads.js'], 'public/js/forum.js');
  4. });

Version / Hash A File

  1. elixir(function(mix) {
  2. mix.version("css/all.css");
  3. });

This will append a unique hash to the filename, allowing for cache-busting. For example, the generated file name will look something like: all-16d570a7.css.

Within your views, you may use the elixir() function to load the appropriately hashed asset. Here's an example:

  1. <link rel="stylesheet" href="{{ elixir("css/all.css") }}">

Behind the scenes, the elixir() function will determine the name of the hashed file that should be included. Don't you feel the weight lifting off your shoulders already?

You may also pass an array to the version method to version multiple files:

  1. elixir(function(mix) {
  2. mix.version(["css/all.css", "js/app.js"]);
  3. });
  1. <link rel="stylesheet" href="{{ elixir("css/all.css") }}">
  2. <script src="{{ elixir("js/app.js") }}"></script>

Copy a File to a New Location

  1. elixir(function(mix) {
  2. mix.copy('vendor/foo/bar.css', 'public/css/bar.css');
  3. });

Copy an Entire Directory to a New Location

  1. elixir(function(mix) {
  2. mix.copy('vendor/package/views', 'resources/views');
  3. });

Trigger Browserify

  1. elixir(function(mix) {
  2. mix.browserify('index.js');
  3. });

Want to require modules in the browser? Hoping to use EcmaScript 6 sooner than later? Need a built-in JSX transformer? If so, Browserify, along with the browserify Elixir task, will handle the job nicely.

This task assumes that your scripts are stored in resources/assets/js, though you're free to override the default.

Method Chaining

Of course, you may chain almost all of Elixir's methods together to build your recipe:

  1. elixir(function(mix) {
  2. mix.less("app.less")
  3. .coffee()
  4. .phpUnit()
  5. .version("css/bootstrap.css");
  6. });

Gulp

Now that you've told Elixir which tasks to execute, you only need to trigger Gulp from the command line.

Execute All Registered Tasks Once

  1. gulp

Watch Assets For Changes

  1. gulp watch

Only Compile Scripts

  1. gulp scripts

Only Compile Styles

  1. gulp styles

Watch Tests And PHP Classes for Changes

  1. gulp tdd

Note: All tasks will assume a development environment, and will exclude minification. For production, use gulp —production.

Custom Tasks and Extensions

Sometimes, you'll want to hook your own Gulp tasks into Elixir. Perhaps you have a special bit of functionality that you'd like Elixir to mix and watch for you. No problem!

As an example, imagine that you have a general task that simply speaks a bit of text when called.

  1. gulp.task("speak", function() {
  2. var message = "Tea...Earl Grey...Hot";
  3. gulp.src("").pipe(shell("say " + message));
  4. });

Easy enough. From the command line, you may, of course, call gulp speak to trigger the task. To add it to Elixir, however, use the mix.task() method:

  1. elixir(function(mix) {
  2. mix.task('speak');
  3. });

That's it! Now, each time you run Gulp, your custom "speak" task will be executed alongside any other Elixir tasks that you've mixed in. To additionally register a watcher, so that your custom tasks will be re-triggered each time one or more files are modified, you may pass a regular expression as the second argument.

  1. elixir(function(mix) {
  2. mix.task('speak', 'app/**/*.php');
  3. });

By adding this second argument, we've instructed Elixir to re-trigger the "speak" task each time a PHP file in the "app/" directory is saved.

For even more flexibility, you can create full Elixir extensions. Using the previous "speak" example, you may write an extension, like so:

  1. var gulp = require("gulp");
  2. var shell = require("gulp-shell");
  3. var elixir = require("laravel-elixir");
  4. elixir.extend("speak", function(message) {
  5. gulp.task("speak", function() {
  6. gulp.src("").pipe(shell("say " + message));
  7. });
  8. return this.queueTask("speak");
  9. });

Notice that we extend Elixir's API by passing the name that we will reference within our Gulpfile, as well as a callback function that will create the Gulp task.

As before, if you want your custom task to be monitored, then register a watcher.

  1. this.registerWatcher("speak", "app/**/*.php");

This lines designates that when any file that matches the regular expression, app/*/.php, is modified, we want to trigger the speak task.

That's it! You may either place this at the top of your Gulpfile, or instead extract it to a custom tasks file. If you choose the latter approach, simply require it into your Gulpfile, like so:

  1. require("./custom-tasks")

You're done! Now, you can mix it in.

  1. elixir(function(mix) {
  2. mix.speak("Tea, Earl Grey, Hot");
  3. });

With this addition, each time you trigger Gulp, Picard will request some tea.