|
| 1 | +# Laravel Elixir |
| 2 | + |
| 3 | +- [Introduction](#introduction) |
| 4 | +- [Installation & Setup](#installation) |
| 5 | +- [Usage](#usage) |
| 6 | +- [Gulp](#gulp) |
| 7 | +- [Directory Defaults](#defaults) |
| 8 | +- [Extensions](#extensions) |
| 9 | + |
| 10 | +<a name="introduction"></a> |
| 11 | +## Introduction |
| 12 | + |
| 13 | +Laravel Elixir provides a clean, fluent API for defining basic [Gulp](http://gulpjs.com) tasks for your |
| 14 | +Laravel application. Elixir supports several common CSS and JavaScript pre-processors, and even testing tools. |
| 15 | + |
| 16 | +If you've ever been confused about how to get started with Gulp and asset compliation, you will love Laravel Elixir! |
| 17 | + |
| 18 | +<a name="installation"></a> |
| 19 | +## Installation & Setup |
| 20 | + |
| 21 | +### Installing Node |
| 22 | + |
| 23 | +Before triggering Elixir, you must first ensure that Node.js is installed on your machine. |
| 24 | + |
| 25 | + node -v |
| 26 | + |
| 27 | +By default, Laravel Homestead includes everything you need; however, if you aren't using Vagrant, then you |
| 28 | +can easily install Node by visiting [their download page](http://nodejs.org/download/). Don't worry, it's |
| 29 | +quick and easy! |
| 30 | + |
| 31 | +### Gulp |
| 32 | + |
| 33 | +Next, you'll want to pull in [Gulp](http://gulpjs.com) as a global NPM package like so: |
| 34 | + |
| 35 | + npm install --global gulp |
| 36 | + |
| 37 | +### Laravel Elixir |
| 38 | + |
| 39 | +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: |
| 40 | + |
| 41 | + npm install |
| 42 | + |
| 43 | +<a name="usage"></a> |
| 44 | +## Usage |
| 45 | + |
| 46 | +Now that you've installed Elixir, you'll be compiling and concatenating in no time! |
| 47 | + |
| 48 | +#### Compile Less |
| 49 | + |
| 50 | +```javascript |
| 51 | +elixir(function(mix) { |
| 52 | + mix.less("app.less"); |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +In the example above, Elixir assumes that your Less files are stored in `resources/assets/less`. |
| 57 | + |
| 58 | +#### Compile Sass |
| 59 | + |
| 60 | +```javascript |
| 61 | +elixir(function(mix) { |
| 62 | + mix.sass("app.scss"); |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +This assumes that your Sass files are stored in `resources/assets/sass`. |
| 67 | + |
| 68 | +#### Compile CoffeeScript |
| 69 | + |
| 70 | +```javascript |
| 71 | +elixir(function(mix) { |
| 72 | + mix.coffee(); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +This assumes that your CoffeeScript files are stored in `resources/assets/coffee`. |
| 77 | + |
| 78 | +#### Compile All Less and CoffeeScript |
| 79 | + |
| 80 | +```javascript |
| 81 | +elixir(function(mix) { |
| 82 | + mix.less() |
| 83 | + .coffee(); |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +#### Trigger PHPUnit Tests |
| 88 | + |
| 89 | +```javascript |
| 90 | +elixir(function(mix) { |
| 91 | + mix.phpUnit(); |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +#### Trigger PHPSpec Tests |
| 96 | + |
| 97 | +```javascript |
| 98 | +elixir(function(mix) { |
| 99 | + mix.phpSpec(); |
| 100 | +}); |
| 101 | +``` |
| 102 | + |
| 103 | +#### Combine Stylesheets |
| 104 | + |
| 105 | +```javascript |
| 106 | +elixir(function(mix) { |
| 107 | + mix.styles([ |
| 108 | + "normalize.css", |
| 109 | + "main.css" |
| 110 | + ]); |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +Paths passed to this method are relative to the `resources/css` directory. |
| 115 | + |
| 116 | +#### Combine Stylesheets and Save to a Custom Directory |
| 117 | + |
| 118 | +```javascript |
| 119 | +elixir(function(mix) { |
| 120 | + mix.styles([ |
| 121 | + "normalize.css", |
| 122 | + "main.css" |
| 123 | + ], 'public/build/css/everything.css'); |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +#### Combine Stylesheets From A Custom Base Directory |
| 128 | + |
| 129 | +```javascript |
| 130 | +elixir(function(mix) { |
| 131 | + mix.styles([ |
| 132 | + "normalize.css", |
| 133 | + "main.css" |
| 134 | + ], 'public/build/css/everything.css', 'public/js'); |
| 135 | +}); |
| 136 | +``` |
| 137 | + |
| 138 | +The third argument to both the `styles` and `scripts` methods determines the relative directory for all paths passed to the methods. |
| 139 | + |
| 140 | +#### Combine All Styles in a Directory |
| 141 | + |
| 142 | +```javascript |
| 143 | +elixir(function(mix) { |
| 144 | + mix.stylesIn("public/css"); |
| 145 | +}); |
| 146 | +``` |
| 147 | + |
| 148 | +#### Combine Scripts |
| 149 | + |
| 150 | +```javascript |
| 151 | +elixir(function(mix) { |
| 152 | + mix.scripts([ |
| 153 | + "jquery.js", |
| 154 | + "app.js" |
| 155 | + ]); |
| 156 | +}); |
| 157 | +``` |
| 158 | + |
| 159 | +Again, this assumes all paths are relative to the `resources/js` directory. |
| 160 | + |
| 161 | +#### Combine All Scripts in a Directory |
| 162 | + |
| 163 | +```javascript |
| 164 | +elixir(function(mix) { |
| 165 | + mix.scriptsIn("public/js/some/directory"); |
| 166 | +}); |
| 167 | +``` |
| 168 | + |
| 169 | +#### Combine Multiple Sets of Scripts |
| 170 | + |
| 171 | +```javascript |
| 172 | +elixir(function(mix) { |
| 173 | + mix.scripts(['jquery.js', 'main.js'], 'public/js/main.js') |
| 174 | + .scripts(['forum.js', 'threads.js'], 'public/js/forum.js'); |
| 175 | +}); |
| 176 | +``` |
| 177 | + |
| 178 | +#### Version / Hash A File |
| 179 | + |
| 180 | +```javascript |
| 181 | +elixir(function(mix) { |
| 182 | + mix.version("css/all.css"); |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +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`. |
| 187 | + |
| 188 | +Within your views, you may use the `elixir()` function to load the appropriately hashed asset. Here's an example: |
| 189 | + |
| 190 | +```html |
| 191 | +<link rel="stylesheet" href="{{ elixir("css/all.css") }}"> |
| 192 | +``` |
| 193 | + |
| 194 | +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? |
| 195 | + |
| 196 | +#### Copy a File to a New Location |
| 197 | + |
| 198 | +```js |
| 199 | +elixir(function(mix) { |
| 200 | + mix.copy('vendor/foo/bar.css', 'public/css/bar.css'); |
| 201 | +}); |
| 202 | +``` |
| 203 | + |
| 204 | +#### Copy an Entire Directory to a New Location |
| 205 | + |
| 206 | +```js |
| 207 | +elixir(function(mix) { |
| 208 | + mix.copy('vendor/package/views', 'resources/views'); |
| 209 | +}); |
| 210 | +``` |
| 211 | + |
| 212 | +#### Method Chaining |
| 213 | + |
| 214 | +Of course, you may chain almost all of Elixir's methods together to build your recipe: |
| 215 | + |
| 216 | +```javascript |
| 217 | +elixir(function(mix) { |
| 218 | + mix.less("app.less") |
| 219 | + .coffee() |
| 220 | + .phpUnit() |
| 221 | + .version("css/bootstrap.css"); |
| 222 | +}); |
| 223 | +``` |
| 224 | + |
| 225 | +<a name="gulp"></a> |
| 226 | +## Gulp |
| 227 | + |
| 228 | +Now that you've told Elixir which tasks to execute, you only need to trigger Gulp from the command line. |
| 229 | + |
| 230 | +#### Execute All Registered Tasks Once |
| 231 | + |
| 232 | + gulp |
| 233 | + |
| 234 | +#### Watch Assets For Changes |
| 235 | + |
| 236 | + gulp watch |
| 237 | + |
| 238 | +#### Watch Tests And PHP Classes for Changes |
| 239 | + |
| 240 | + gulp tdd |
| 241 | + |
| 242 | +> **Note:** All tasks will assume a development environment, and will exclude minification. For production, use `gulp --production`. |
| 243 | +
|
| 244 | +<a name="extensions"></a> |
| 245 | +## Extensions |
| 246 | + |
| 247 | +You can even create your own Gulp tasks, and hook them into Elixir. Imagine that you want to add a fun task that |
| 248 | + uses the Terminal to verbally notify you with some message. Here's what that might look like: |
| 249 | + |
| 250 | +```javascript |
| 251 | + var gulp = require("gulp"); |
| 252 | + var shell = require("gulp-shell"); |
| 253 | + var elixir = require("laravel-elixir"); |
| 254 | + |
| 255 | + elixir.extend("message", function(message) { |
| 256 | + |
| 257 | + gulp.task("say", function() { |
| 258 | + gulp.src("").pipe(shell("say " + message)); |
| 259 | + }); |
| 260 | + |
| 261 | + return this.queueTask("say"); |
| 262 | + |
| 263 | + }); |
| 264 | +``` |
| 265 | + |
| 266 | +Notice that we `extend` Elixir's API by passing the key that we will use within our Gulpfile, as well as a callback function that will create the Gulp task. |
| 267 | + |
| 268 | +If you want your custom task to be monitored, then register a watcher as well. |
| 269 | + |
| 270 | +```javascript |
| 271 | +this.registerWatcher("message", "**/*.php"); |
| 272 | +``` |
| 273 | + |
| 274 | +This lines designates that when any file that matches the regex, `**/*.php` is modified, we want to trigger the `message` task. |
| 275 | + |
| 276 | +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: |
| 277 | + |
| 278 | +```javascript |
| 279 | +require("./custom-tasks") |
| 280 | +``` |
| 281 | + |
| 282 | +You're done! Now, you can mix it in. |
| 283 | + |
| 284 | +```javascript |
| 285 | +elixir(function(mix) { |
| 286 | + mix.message("Tea, Earl Grey, Hot"); |
| 287 | +}); |
| 288 | +``` |
| 289 | + |
| 290 | +With this addition, each time you trigger Gulp, Picard will request some tea. |
0 commit comments