2017 © Pedro Peláez
 

craft-plugin mix

Helper plugin for Laravel Mix in Craft CMS templates.

image

delaneymethod/mix

Helper plugin for Laravel Mix in Craft CMS templates.

  • Friday, July 20, 2018
  • by hello@delaneymethod.com
  • Repository
  • 1 Watchers
  • 4 Stars
  • 77 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 24 % Grown

The README.md

Craft Mix Logo, (*1)

Helper plugin for Laravel Mix in Craft CMS templates. , (*2)

Total Downloads Latest Stable Version License , (*3)

Requirements

  • Craft CMS 3+
  • PHP 7 PHP 7+
  • Node.js 6+

Installation

To install the plugin, follow these instructions., (*4)

  1. Open your terminal and go to your Craft project:
cd /path/to/project
  1. Then tell Composer to load the plugin:
composer require delaneymethod/mix
  1. In the Craft Control Panel, go to Settings → Plugins and click the "Install" button for Craft Mix., (*5)

  2. Create a package.json file with the following contents to install Laravel Mix dependencies and configure asset build tasks., (*6)

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "cross-env": "^3.2.3",
        "laravel-mix": "^0.10.0",
        "node-sass": "^4.5.3"
    }
}

Install the Node.js dependencies using npm or yarn., (*7)

npm install # OR yarn install

Configuration

To configure Craft Mix go to Settings → Plugins → Craft Mix in the Craft Control Panel., (*8)

The available settings are:, (*9)

  • Public Path - The path of the public directory containing the index.php
  • Asset Path - The path of the asset directory where Laravel Mix stores the compiled files

To demonstrate usage of the plugin, let's imagine a project with the following directory structure., (*10)

...
assets/
  js/
    global.js
  scss/
    global.scss
web/
  assets/
    js/
    css/
...

Create a webpack.mix.js file at the root of your project to configure Laravel Mix for building your assets. See the Laravel Mix documentation for configuration details and more options. Be sure to configure the publicPath option to point at the directory from which you will serve static assets (images, fonts, javascript and CSS). Here's an example configuration as a starting point that would work with the previously described project structure:, (*11)

const del = require('del');
const { mix } = require('laravel-mix');

del(['web/assets/**', '!web/assets']);

mix.setPublicPath('web/assets');

if (mix.inProduction) {
    mix.disableNotifications();
}

if (!mix.inProduction) {
    mix.sourceMaps();
}

mix.options({
    processCssUrls: false
});

mix.sass('assets/sass/global.scss', 'web/assets/css/global.css');

mix.js('assets/js/global.js', 'web/assets/js/global.js');

mix.copy('assets/fonts', 'web/assets/fonts');

mix.copy('assets/img', 'web/assets/img');

mix.version();

Usage

The primary purpose of this plugin is to provide template helpers that translate between a known path to your build assets and the real path to the assets after they have been built (which varies depending on the build mode). There are three main ways you can use Mix from Twig templates in CraftCMS:, (*12)

{# Twig Filter #}


<link rel="stylesheet" href="{{ 'css/global.css' | mix }}">

{# Twig Function #}


<link rel="stylesheet" href="{{ mix('css/global.cs') }}">

{# CraftCMS Variable #}


{{ craft.mix.withTag(\'css/global.css\') | raw }}

// include the content of a versioned file inline.
{{ craft.mix.withTag(\'css/global.css\', true) | raw }}

There are a handful of different modes in which you can run Mix and the plugin will work differently in each mode, as described in the following sections., (*13)

Dev Mode

Dev mode will build your assets to target a development environment. Depending on how you've configured Mix, this may bypass certain build instructions intended only for the production environment. In the example webpack.mix.js file, we are only versioning assets in production mode for cache busting or similar use cases. You can build the assets for developer mode by using the npm script we added in our package.json file:, (*14)

npm run dev

Watch Mode

Functions just like Dev Mode except Mix will continue running as a foreground process through NodeJS and building assets as changes to the source files are detected., (*15)

npm run watch

Hot Module Replacement Mode

Builds your assets and runs the Webpack dev server to allow Hot Module Replacement. It works very similarly to what is described in the Laravel Mix documentation. To run in HMR mode, run the following command:, (*16)

npm run hot

Production Mode

or bundle your assets for production, (*17)

npm run production

Credits

About DelaneyMethod

DelaneyMethod are a Full-Stack Web Development Agency with a no-nonsense, get it done attitude with a proven track record for delivering their part of a project. Learn more about us on our website., (*18)

License

The MIT License (MIT). Please see License File for more information., (*19)

The Versions

20/07 2018

dev-master

9999999-dev

Helper plugin for Laravel Mix in Craft CMS templates.

  Sources   Download

MIT

The Requires

 

laravel yii2 cms craftcms craft-plugin craft mix craft3 delaneymethod

05/09 2017

1.0.3

1.0.3.0

Helper plugin for Laravel Mix in Craft CMS templates.

  Sources   Download

MIT

The Requires

 

laravel yii2 cms craftcms craft-plugin craft mix craft3 delaneymethod

05/09 2017

1.0.2

1.0.2.0

Helper plugin for Laravel Mix in Craft CMS templates.

  Sources   Download

MIT

The Requires

 

laravel yii2 cms craftcms craft-plugin craft mix craft3 delaneymethod

05/09 2017

1.0.1

1.0.1.0

Helper plugin for Laravel Mix in Craft CMS templates.

  Sources   Download

MIT

The Requires

 

laravel yii2 cms craftcms craft-plugin craft mix craft3 delaneymethod

05/09 2017

1.0.0

1.0.0.0

Helper plugin for Laravel Mix in Craft CMS templates.

  Sources   Download

MIT

The Requires

 

laravel yii2 cms craftcms craft-plugin craft mix craft3 delaneymethod