|
| 1 | +# Laravel Mix for Statamic |
| 2 | + |
| 3 | +This is a Stamic add-on which allows you to build CSS and JavaScript files for your website using |
| 4 | +[Laravel Mix](https://github.com/JeffreyWay/laravel-mix). Mix differs from Laravel Elixir in several ways - the one we're |
| 5 | +interested in here is the `mix-manifest.json` file, if you're familiar with Elixir then you'll notice the filename has changed. |
| 6 | +This makes it incompatible with Statamic's built in support, hence needing a separate add-on. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +The easiest way of installing this add-on is to download as a zip bundle straigt from GitHub and placing it in your |
| 11 | +`site/addons` directory. |
| 12 | + |
| 13 | +An alternative way, if you're a Git user, would be do add it as a Git submodule. This gives you the avantage of being able |
| 14 | +to pull in any updates which may be published in the future. The following command will install this add-on as a submodule if |
| 15 | +run from the root of your project: |
| 16 | + |
| 17 | +```bash |
| 18 | +$ git submodule add https://github.com/edcs/laravel-mix-statamic site/addons/LaravelMix |
| 19 | +``` |
| 20 | + |
| 21 | +## Setting Up Mix |
| 22 | + |
| 23 | +This is an example `webpack.mix.js` file - it sets the public path where the built JavaScript and CSS should end up (I like to |
| 24 | +keep my Statamic application files out of my public directory), then it transpiles my JavaScript modules into a single file |
| 25 | +and finally it converts my SASS into CSS: |
| 26 | + |
| 27 | +```javascript |
| 28 | +const laravelMix = require('laravel-mix'); |
| 29 | + |
| 30 | +laravelMix |
| 31 | + .setPublicPath('../../../public/site/themes/theme-name') |
| 32 | + .js('js/app.js', '/js/theme-name.js') |
| 33 | + .sass('scss/app.scss', '/css/theme-name.css'); |
| 34 | +``` |
| 35 | + |
| 36 | +One of the useful things Mix does is tell Webpack to version the built files so that the cache can be busted when new changes |
| 37 | +are published to a website. To implement this, the following change is required: |
| 38 | + |
| 39 | +```javascript |
| 40 | +const laravelMix = require('laravel-mix'); |
| 41 | + |
| 42 | +laravelMix |
| 43 | + .setPublicPath('../../../public/site/themes/theme-name') |
| 44 | + .js('js/app.js', '/js/theme-name.js') |
| 45 | + .sass('scss/app.scss', '/css/theme-name.css') |
| 46 | + .version(); |
| 47 | +``` |
| 48 | + |
| 49 | +This will generate files with a filename similar to this, `theme-name.a22b8a115a8da98d0a70.css`. This file can now no longer be |
| 50 | +picked up by Statamic's built in theme CSS and JS tags as they no longer follow convention. Don't worry though, the next step |
| 51 | +solves that problem! |
| 52 | + |
| 53 | +## Setting Up Theme Templates |
| 54 | + |
| 55 | +You can now update your theme template tags to use those provided by this add-on: |
| 56 | + |
| 57 | +``` |
| 58 | +<!-- the default CSS template tag: --> |
| 59 | +{{ theme:css }} |
| 60 | +
|
| 61 | +<!-- should be changed to: --> |
| 62 | +{{ laravel_mix:css }} |
| 63 | +
|
| 64 | +<!-- the default JavaScript template tag: --> |
| 65 | +{{ theme:js }} |
| 66 | +
|
| 67 | +<!-- should be changed to: --> |
| 68 | +{{ laravel_mix:js }} |
| 69 | +``` |
| 70 | + |
| 71 | +This add-on supports some of the parameters as the default Statamic tags, these are: |
| 72 | + |
| 73 | +| Parameter | Default | Description | |
| 74 | +|:----------:|:---------------:|:----------------------------------------:| |
| 75 | +| `tag` | `boolean` false | Enable this to output the full HTML tag. | |
| 76 | +| `absolute` | `boolean` false | Output an absolute or relative URL. | |
| 77 | + |
| 78 | +## And Finally... |
| 79 | + |
| 80 | +Now you're ready to build your CSS and JavaScript assets using Laravel Mix - once complete, there should be a |
| 81 | +`mix-manifest.json` in the build directory. |
0 commit comments