Skip to content

Commit 2a3edcf

Browse files
committed
Get ready for Kirby 3
1 parent 5eea238 commit 2a3edcf

File tree

8 files changed

+220
-130
lines changed

8 files changed

+220
-130
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.php]
13+
indent_size = 4
14+
15+
[*.md,*.txt]
16+
trim_trailing_whitespace = false
17+
insert_final_newline = false
18+
19+
[composer.json]
20+
indent_size = 4

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# OS files
2+
.DS_Store
3+
4+
# npm modules
5+
/node_modules
6+
7+
# Composer files
8+
/vendor

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
# Change log
2+
23
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/).
34

5+
## [1.1.0] - 2019-02-19
6+
7+
### Added
8+
9+
- Throw exceptions when in debug mode, otherwise just fail silently
10+
- Make it possible to install via composer
11+
12+
### Changed
13+
14+
- Change config identifiers to keep them inline with the new [Kirby convention](https://getkirby.com/docs/guide/plugins/plugin-basics#plugin-options)
15+
416
## [1.0.1] - 2017-01-30
17+
518
### Changed
19+
620
- Change config identifiers to keep them inline with the Kirby convention (thanks [fvsch](https://github.com/fvsch))

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2019
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,96 +4,106 @@ Use the `mix` helper function to get the correct path to your versioned CSS and
44

55
## Installation
66

7-
Copy or link the `laravel-mix-kirby` directory to `site/plugins` or use the [Kirby CLI](https://github.com/getkirby/cli): `kirby plugin:install diverently/laravel-mix-kirby`.
7+
### Download
88

9-
You should also install [Laravel Mix](https://github.com/JeffreyWay/laravel-mix), otherwise this plugin is pretty useless ;-) See the [installation guide](https://github.com/JeffreyWay/laravel-mix/blob/master/docs/installation.md) for further information, though it's actually pretty straight forward:
9+
Download and copy this repository to `site/plugins/laravel-mix-kirby`.
1010

11-
1. If you haven't already, create a `package.json` inside your project root: `npm init -y`
12-
2. Install Laravel Mix: `npm install laravel-mix --save-dev`
13-
3. Move the `webpack.config.js` and `webpack.mix.js` files into your root: `cp -r node_modules/laravel-mix/setup/** ./`
14-
15-
After that you can start using Laravel Mix in your project.
11+
### Git submodule
1612

17-
### NPM scripts
13+
```
14+
git submodule add https://github.com/Diverently/laravel-mix-kirby.git site/plugins/laravel-mix-kirby
15+
```
1816

19-
You should add the following NPM scripts to your `package.json` to speed up your workflow:
17+
### Composer
2018

21-
```json
22-
"scripts": {
23-
"dev": "cross-env NODE_ENV=development webpack --progress --hide-modules",
24-
"watch": "cross-env NODE_ENV=development webpack --watch --progress --hide-modules",
25-
"production": "cross-env NODE_ENV=production webpack --progress --hide-modules"
26-
}
19+
```
20+
composer require diverently/laravel-mix-kirby
2721
```
2822

23+
### Laravel Mix
2924

25+
You should also install **Laravel Mix**, otherwise this plugin is pretty useless ;-) See the [installation guide](https://laravel-mix.com/docs/4.0/installation) for further information, though it's actually pretty straight forward:
3026

31-
## Usage
27+
## Setup
3228

33-
### Initial setup
34-
Depending on your preferred project structure you have to make some changes to your `webpack.mix.js`. You should set the paths where the `mix-manifest.json` as well as the CSS and JS files will be stored.
35-
I keep my source files outside the `kirby` (or `dist`) directory but you may have a different setup. Mine looks as follows:
29+
In your `site/config/config.php` you can set two options to make this helper work with your specific setup:
3630

37-
```
38-
- kirby (or dist)
39-
|- assets
40-
|- css
41-
|- js
42-
|- ...
43-
- src
44-
|- css
45-
|- main.scss
46-
|- ...
47-
|- js
48-
|- main.js
49-
|- ...
50-
|- ...
51-
```
31+
### `diverently.laravel-mix-kirby.manifestPath`
5232

53-
With that setup I start with the following `webpack.mix.js`:
33+
This is where the helper function will look for the manifest created by Laravel Mix.
5434

55-
```js
56-
mix.setPublicPath('kirby/assets/')
57-
mix.browserSync('my-website.dev')
58-
mix.sourceMaps()
59-
.js('src/js/main.js', 'kirby/assets/js/')
60-
.sass('src/css/main.scss', 'kirby/assets/css')
61-
.version();
62-
```
35+
Default: `assets/mix-manifest.json`
6336

64-
When you run `npm run dev` your `main.js` and `main.css` files will be created. They will also be versioned which makes referencing them via the standard `css()` and `js()` methods pretty tedious. That’s where the helper function comes in (and thus this entire plugin).
37+
### `diverently.laravel-mix-kirby.assetsDirectory`
38+
39+
This will be prepended to the individual file paths given to the `mix()` function when creating the final HTML tags.
40+
41+
Default: `assets`
42+
43+
## Options
6544

6645
### `mix()` helper function
67-
The `mix()` helper function reads the `mix-manifest.json` file and returns the right HTML tag with the correct (versioned) path to the requested file. In our example we would call it like so:
46+
47+
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:
6848

6949
```php
70-
// snippets/header.php
50+
<html>
7151
<head>
72-
// ...
73-
74-
<?= mix('/css/main.css') ?>
52+
// ...
53+
<?php echo mix('/main.css') ?>
7554
</head>
76-
77-
// snippets/footer.php
78-
<?= mix('/js/main.js') ?>
55+
<body>
56+
// ...
57+
<?php echo mix('/main.js') ?>
7958
</body>
8059
</html>
8160
```
8261

8362
And that's it, actually.
8463

85-
### Options
64+
## Development
8665

87-
In your `site/config/config.php` you can set two options to make this helper work with your specific setup:
66+
1. If you haven't already, create a `package.json` inside your project root: `npm init -y`
67+
2. Install Laravel Mix: `npm install laravel-mix --save-dev`
68+
3. Copy the `webpack.mix.js` file into your root: `cp node_modules/laravel-mix/setup/webpack.mix.js ./`
8869

89-
#### `mix.manifest`
90-
This is where the helper function will look for the manifest created by Laravel Mix.
70+
After that you can start using Laravel Mix in your project.
9171

92-
Default: `assets/mix-manifest.json`
72+
### `webpack.mix.js`
9373

94-
#### `mix.assets`
95-
This will be prepended to the individual file paths given to the `mix()` function when creating the final HTML tags.
74+
See the [official document](https://laravel-mix.com/docs/4.0/basic-example) for more information.
9675

97-
Default: `assets`
76+
```js
77+
let mix = require('laravel-mix)
78+
mix.setPublicPath('assets')
79+
mix.browserSync('my-website.dev')
80+
mix.sourceMaps()
81+
.js('src/js/main.js', 'assets')
82+
.sass('src/css/main.scss', 'assets')
83+
.version();
84+
```
85+
86+
### NPM scripts
87+
88+
Add the following NPM scripts to your `package.json`:
89+
90+
```json
91+
"scripts": {
92+
"dev": "npm run development",
93+
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
94+
"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",
95+
"prod": "npm run production",
96+
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
97+
}
98+
```
99+
100+
## License
101+
102+
MIT
103+
104+
## Credits
105+
106+
[Robert Cordes](https://github.com/RobertCordes)
107+
[Diverently](https://diverently.com)
98108
99-
> The idea behind this originally came from the `mix` Blade helper created for the Laravel framework. This is merely just a "translation" for the Kirby CMS, only that it also generates the correct HTML tag depending on what file type you request.
109+
> 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.

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "diverently/laravel-mix-kirby",
3+
"description": "Laravel Mix helper for the Kirby CMS",
4+
"type": "kirby-plugin",
5+
"license": "MIT",
6+
"authors": [{
7+
"name": "Robert Cordes",
8+
"email": "[email protected]"
9+
}],
10+
"require": {
11+
"getkirby/composer-installer": "^1.1"
12+
}
13+
}

index.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Laravel Mix helper for the Kirby CMS
4+
*/
5+
6+
if (! function_exists('mix')) {
7+
/**
8+
* Get the appropriate HTML tag with the right path for the (versioned) Mix file.
9+
*
10+
* @param string $path Path as it appears in the mix-manifest.json
11+
*
12+
* @return string Either a <link> or a <script> tag, depending on the $path
13+
*
14+
* @throws \Exception
15+
*/
16+
function mix($path)
17+
{
18+
static $manifest = [];
19+
20+
// Get the correct $path
21+
if (!Str::startsWith($path, '/')) {
22+
$path = "/{$path}";
23+
}
24+
25+
// Get the correct $manifestPath
26+
$manifestPath = option('diverently.laravel-mix-kirby.manifestPath', 'assets/mix-manifest.json');
27+
28+
if (Str::startsWith($manifestPath, '/')) {
29+
$manifestPath = Str::substr($manifestPath, 1);
30+
}
31+
32+
// Get the correct $$assetsDirectory
33+
$assetsDirectory = option('diverently.laravel-mix-kirby.assetsDirectory', '/assets/');
34+
35+
if (! Str::startsWith($assetsDirectory, '/')) {
36+
$assetsDirectory = "/{$assetsDirectory}";
37+
}
38+
39+
if (! Str::endsWith($assetsDirectory, '/')) {
40+
$assetsDirectory = "{$assetsDirectory}/";
41+
}
42+
43+
// Get the manifest contents
44+
if (!$manifest) {
45+
if (! F::exists($manifestPath)) {
46+
if (option('debug')) {
47+
throw new Exception('The Mix manifest does not exists.');
48+
} else {
49+
return false;
50+
}
51+
}
52+
53+
$manifest = json_decode(F::read($manifestPath), 'json');
54+
}
55+
56+
// Check if the manifest contains the given $path
57+
if (! array_key_exists($path, $manifest)) {
58+
if (option('debug')) {
59+
throw new Exception("Unable to locate Mix file: {$path}.");
60+
} else {
61+
return false;
62+
}
63+
}
64+
65+
// Get the actual file path for the given $path
66+
$mixFilePath = $assetsDirectory . $manifest[$path];
67+
68+
// Use the appropriate Kirby helper method to get the correct HTML tag
69+
$pathExtension = F::extension($mixFilePath);
70+
71+
if (Str::contains($pathExtension, 'css')) {
72+
$mixFileLink = css($mixFilePath);
73+
} elseif (Str::contains($pathExtension, 'js')) {
74+
$mixFileLink = js($mixFilePath);
75+
} else {
76+
if (option('debug')) {
77+
throw new Exception("File type not recognized");
78+
} else {
79+
return false;
80+
}
81+
}
82+
83+
return $mixFileLink;
84+
}
85+
}

laravel-mix-kirby.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)