Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 36a413c

Browse files
authored
Merge pull request #1 from MattiasMediaempire/master
Support for Laravel Blade.
2 parents 6eece1e + 112ece8 commit 36a413c

File tree

4 files changed

+116
-57
lines changed

4 files changed

+116
-57
lines changed

LaravelMixModifier.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Statamic\Addons\LaravelMix;
4+
5+
use Statamic\Extend\Modifier;
6+
7+
class LaravelMixModifier extends Modifier
8+
{
9+
use LaravelMixTrait;
10+
11+
/**
12+
* Modifies the input into an asset path.
13+
*
14+
* @param string $value In this case either 'js' or 'css'.
15+
* @return string
16+
*/
17+
public function index($value)
18+
{
19+
return $this->getAssetPath($value);
20+
}
21+
}

LaravelMixTags.php

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22

33
namespace Statamic\Addons\LaravelMix;
44

5-
use Statamic\API\Config;
6-
use Statamic\API\File;
7-
use Statamic\API\Str;
8-
use Statamic\API\URL;
95
use Statamic\Extend\Tags;
106

117
class LaravelMixTags extends Tags
128
{
13-
/**
14-
* The name of Mix's revision manifest file.
15-
*/
16-
const MANIFEST = 'mix-manifest.json';
9+
use LaravelMixTrait;
1710

1811
/**
1912
* The {{ laravel_mix }} tag - outputs the revision manifest file as JSON.
@@ -32,9 +25,7 @@ public function index()
3225
*/
3326
public function css()
3427
{
35-
$src = $this->get('src', Config::get('theming.theme'));
36-
$path = '/css/' . Str::ensureRight($src, '.css');
37-
$url = $this->themeUrl($this->getManifest()->get($path, $path));
28+
$url = $this->getAssetPath('css');
3829

3930
if ($this->getBool('tag')) {
4031
return '<link rel="stylesheet" href="' . $url . '" />';
@@ -50,53 +41,12 @@ public function css()
5041
*/
5142
public function js()
5243
{
53-
$src = $this->get('src', Config::get('theming.theme'));
54-
$path = '/js/' . Str::ensureRight($src, '.js');
55-
$url = $this->themeUrl($this->getManifest()->get($path, $path));
44+
$url = $this->getAssetPath('js');
5645

5746
if ($this->getBool('tag')) {
5847
return '<script src="' . $url . '"></script>';
5948
}
6049

6150
return $url;
6251
}
63-
64-
/**
65-
* Returns the revision manifest contained in a Collection.
66-
*
67-
* @return \Illuminate\Support\Collection
68-
*/
69-
private function getManifest()
70-
{
71-
$path = webroot_path('site/themes/' . Config::get('theming.theme') . '/' . static::MANIFEST);
72-
73-
return collect(json_decode(File::get($path), true));
74-
}
75-
76-
/**
77-
* Transforms the asset directory into a relative or absolute URL for use in the front-end.
78-
*
79-
* @param string $path
80-
* @return string
81-
*/
82-
private function themeUrl($path)
83-
{
84-
$url = URL::assemble(
85-
Config::get('system.filesystems.themes.url'),
86-
Config::get('theming.theme'),
87-
$path
88-
);
89-
90-
$url = URL::prependSiteUrl(
91-
$url,
92-
$this->get('locale', default_locale()),
93-
false
94-
);
95-
96-
if (!$this->getBool('absolute')) {
97-
$url = URL::makeRelative($url);
98-
}
99-
100-
return $url;
101-
}
10252
}

LaravelMixTrait.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Statamic\Addons\LaravelMix;
4+
5+
use Statamic\API\Config;
6+
use Statamic\API\File;
7+
use Statamic\API\Str;
8+
use Statamic\API\URL;
9+
use Statamic\Extend\HasParameters;
10+
11+
trait LaravelMixTrait
12+
{
13+
/**
14+
* Provides access to methods for retrieving parameters
15+
*/
16+
use HasParameters;
17+
18+
/**
19+
* The name of Mix's revision manifest file.
20+
*/
21+
protected $manifest = 'mix-manifest.json';
22+
23+
/**
24+
* Returns the path of given asset type.
25+
26+
* @param string $type
27+
* @return string
28+
*/
29+
public function getAssetPath($type)
30+
{
31+
$theme = Config::get('theming.theme');
32+
$path = '/' . $type . '/' . Str::ensureRight($theme, '.' . $type);
33+
$manifest = $this->getManifest()->get($path);
34+
35+
return $this->themeUrl($manifest);
36+
}
37+
38+
/**
39+
* Transforms the asset directory into a relative or absolute URL for use in the front-end.
40+
*
41+
* @param string $path
42+
* @return string
43+
*/
44+
private function themeUrl($path)
45+
{
46+
$url = URL::assemble(
47+
Config::get('system.filesystems.themes.url'),
48+
Config::get('theming.theme'),
49+
$path
50+
);
51+
52+
$url = URL::prependSiteUrl(
53+
$url,
54+
$this->get('locale', default_locale()),
55+
false
56+
);
57+
58+
if (!$this->getBool('absolute')) {
59+
$url = URL::makeRelative($url);
60+
}
61+
62+
return $url;
63+
}
64+
65+
/**
66+
* Returns the revision manifest contained in a Collection.
67+
*
68+
* @return \Illuminate\Support\Collection
69+
*/
70+
private function getManifest()
71+
{
72+
$path = webroot_path('site/themes/' . Config::get('theming.theme') . '/' . $this->manifest);
73+
74+
return collect(json_decode(File::get($path), true));
75+
}
76+
}

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Laravel Mix for Statamic
22

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
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
55
interested in here is the `mix-manifest.json` file, if you're familiar with Elixir then you'll notice the filename has changed.
66
This makes it incompatible with Statamic's built in support, hence needing a separate add-on.
77

@@ -10,7 +10,7 @@ This makes it incompatible with Statamic's built in support, hence needing a sep
1010
The easiest way of installing this add-on is to download as a zip bundle straigt from GitHub and placing it in your
1111
`site/addons` directory.
1212

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
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
1414
to pull in any updates which may be published in the future. The following command will install this add-on as a submodule if
1515
run from the root of your project:
1616

@@ -21,7 +21,7 @@ $ git submodule add https://github.com/edcs/laravel-mix-statamic site/addons/Lar
2121
## Setting Up Mix
2222

2323
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
24+
keep my Statamic application files out of my public directory), then it transpiles my JavaScript modules into a single file
2525
and finally it converts my SASS into CSS:
2626

2727
```javascript
@@ -75,6 +75,18 @@ This add-on supports some of the parameters as the default Statamic tags, these
7575
| `tag` | `boolean` false | Enable this to output the full HTML tag. |
7676
| `absolute` | `boolean` false | Output an absolute or relative URL. |
7777

78+
## Using with Laravel Blade instead of Antlers.
79+
80+
When using Laravel Blade the add-on tags aren't available. Instead, use the modifier:
81+
82+
```
83+
<!-- For CSS. -->
84+
<link rel="stylesheet" href="{{ modify('css')->laravelMix() }}">
85+
86+
<!-- For JS. -->
87+
<script src="{{ modify('js')->laravelMix() }}"></script>
88+
```
89+
7890
## And Finally...
7991

8092
Now you're ready to build your CSS and JavaScript assets using Laravel Mix - once complete, there should be a

0 commit comments

Comments
 (0)