Giter VIP home page Giter VIP logo

laravel-mix-kirby's Introduction

Laravel Mix Helper for Kirby

Use the mix helper function to get the correct path to your versioned CSS and JS files generated by Laravel Mix.

Installation

Download

Download and copy this repository to site/plugins/laravel-mix-kirby.

Git submodule

git submodule add https://github.com/Diverently/laravel-mix-kirby.git site/plugins/laravel-mix-kirby

Composer

composer require diverently/laravel-mix-kirby

Laravel Mix

You should also install Laravel Mix, otherwise this plugin is pretty useless ;-) See the installation guide for further information, though it's actually pretty straight forward:

Setup

In your site/config/config.php you can set two options to make this helper work with your specific setup:

diverently.laravel-mix-kirby.manifestPath

This is where the helper function will look for the manifest created by Laravel Mix.

Default: assets/mix-manifest.json

diverently.laravel-mix-kirby.assetsDirectory

This will be prepended to the individual file paths given to the mix() function when creating the final HTML tags.

Default: assets

Options

mix() helper function

The mix() helper function reads the mix-manifest.json file and returns the right HTML tag with the correct path to the requested file. In our example we would call it like so:

<html>
<head>
    <!-- ... -->
    <?php echo mix('/main.css') ?>
    <?php echo mix([
      '/additional.css',
      '@autocss'
    ]) ?>
</head>
<body>
    <!-- ... -->
    <?php echo mix('/main.js') ?>
    <?php echo mix([
      '/additional.js',
      '@autojs'
    ]) ?>
</body>
</html>

And that's it, actually.

Development

  1. If you haven't already, create a package.json inside your project root: npm init -y
  2. Install Laravel Mix: npm install laravel-mix --save-dev
  3. Copy the webpack.mix.js file into your root: cp node_modules/laravel-mix/setup/webpack.mix.js ./

After that you can start using Laravel Mix in your project.

webpack.mix.js

See the official documentation for more information.

let mix = require("laravel-mix")
mix.setPublicPath("assets")
mix.browserSync("my-website.dev")
if (mix.inProduction()) {
  mix.version()
}
mix
  .sourceMaps()
  .js("src/js/main.js", "assets")
  .sass("src/css/main.scss", "assets")

NPM scripts

Add the following NPM scripts to your package.json:

"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": "npm run development -- --watch",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --disable-host-check --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"
}

License

MIT

Credits

Robert Cordes

Diverently

The idea behind this originally came from the mix Blade helper created for the Laravel framework. This is merely a "translation" for the Kirby CMS, only that it also generates the correct HTML tag depending on what file type you request.

laravel-mix-kirby's People

Contributors

arnoson avatar iv-agatha avatar robertcordes avatar therephil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-mix-kirby's Issues

Error handling

Unfortunately I don’t know the Kirby way of handling errors. I've marked the lines in laravel-mix-kirby.php with @TODO's. I would love to get some help with that πŸ˜ƒ

Manifest path on custom folder setup

Hi there.

I am trying to use the Mix helper plugin on a Kirby installation with custom folder setup.
I am assigning the assets root where the mix-manifest.json can be found in the index.php like so:

$kirby = new Kirby([
  'roots' => [
    'assets'   => dirname(__DIR__, 2) . '/assets'
  ],
]);

Then setting the manifestPath of the plugin in the config.php like so:

return [
  'diverently.laravel-mix-kirby.manifestPath' => kirby()->root('assets') . '/mix-manifest.json',
];

This returns an absolute URL which during local development has a leading /. The /is removed by the plugin here, and I therefor get the error that The Mix manifest does not exist..

Is there any way to work around this? Why does the / have to be removed?

Any help would be appreciated. I love the plugin and have used it many times, only now running into difficulties with the custom folder setup.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.