Skip to content

Commit

Permalink
minifier & docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cbl committed Jun 24, 2020
1 parent 01cb263 commit 314bb1c
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 80 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Blade Style

A package to easily add styles to your blade views.
A package to easily add styles to your blade components.

```php
<button class="btn">{{ $slot }}</button>
Expand All @@ -22,13 +22,14 @@ This design pattern is a considerable simplification of the workflow in frontend
development.

With blade styles there is no need to run a compiler when working on your
styles. Also, only the styles of required blade components are loaded. This
styles. Also, only the styles of required blade components are included. This
saves you from loading large css files and the size can be reduced to a minimum.

## Compiler

Currently there is a `Sass` compiler for blade styles. If you want to build a
compiler for `Less` or `Stylus`, you can do so using the `Sass` example.
compiler for `Less` or `Stylus`, you can do so using the `Sass` package as an
example.

- [Blade Style Sass](https://github.com/cbl/blade-style-sass)

Expand All @@ -40,17 +41,17 @@ The package can be easily installed via composer.
composer requrie cbl/blade-style
```

Now you only have to save the storage where the styles are compiled and publish
the config.
now the necessary assets must be published. This includes the style.php config
and the storage folder where the compiled styles are stored.

```shell
php artisan vendor:publish --provider=BladeStyle\ServiceProvider
```

## Loading Styles
## Include Styles

The blade component `x-styles` is replaced with the required styles, so it may
be placed in the head.
The blade component `x-styles` includes all required styles, so it may be placed
in the head.

```php
<head>
Expand All @@ -76,7 +77,7 @@ then be written inside the wrapper like so.
</x-style>
```

You can build reusable blade components so easily:
You can build reusable blade components:

```php
<button class="btn">{{ $slot }}</button>
Expand All @@ -102,7 +103,7 @@ composer require cbl/blade-style-sass
The sass compiler uses [scssphp](https://github.com/scssphp/scssphp) that means
node is not needed.

Now you can use sass by specifying `sass` or `scss` as `lang` parameter like so.
Now you can use sass by specifying `sass` or `scss` as `lang` attribute like so.

```php
<button class="btn">{{ $slot }}</button>
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"email": "lennart.carbe@gmail.com"
}
],
"require": {},
"require": {
"matthiasmullie/minify": "^1.3"
},
"autoload": {
"psr-4": {
"BladeStyle\\": "src/"
Expand Down
16 changes: 16 additions & 0 deletions config/style.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

return [

/*
|--------------------------------------------------------------------------
| Minify Styles
|--------------------------------------------------------------------------
|
| This option determines wetther the compiled css string should be stored
| minified. It is highly recommended to do so. However you are free to
| disable minifying your styles.
|
| The php minifier from Matthias Mullie is used by default.
| https://github.com/matthiasmullie/minify
|
*/

'minify' => true,

/*
|--------------------------------------------------------------------------
| Compiled Style Path
Expand Down
36 changes: 31 additions & 5 deletions src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

namespace BladeStyle\Compiler;

use Illuminate\View\Compilers\Compiler as ViewCompiler;
use BladeStyle\Engines\MinifierEngine;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\CompilerInterface;
use Illuminate\View\Compilers\Compiler as ViewCompiler;

abstract class Compiler extends ViewCompiler implements CompilerInterface
{
/**
* Minifier engine.
*
* @var \BladeStyle\Engines\MinifierEngine
*/
protected $engine;

/**
* Compile style string.
*
Expand All @@ -15,6 +24,23 @@ abstract class Compiler extends ViewCompiler implements CompilerInterface
*/
abstract public function compileString($string);

/**
* Create a new compiler instance.
*
* @param \BladeStyle\Engines\MinifierEngine $engine
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $cachePath
* @return void
*
* @throws \InvalidArgumentException
*/
public function __construct(MinifierEngine $engine, Filesystem $files, $cachePath)
{
parent::__construct($files, $cachePath);

$this->engine = $engine;
}

/**
* Compile the style at the given path.
*
Expand All @@ -23,14 +49,14 @@ abstract public function compileString($string);
*/
public function compile($path)
{
$contents = $this->compileString(
$this->getRaw($path)
// Minify compiled css.
$css = $this->engine->minify(
$this->compileString($this->getRaw($path))
);


$this->files->put(
$this->getCompiledPath($path),
$contents
$css
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Components/StylesComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

class StylesComponent extends Component
{
const PLACEHOLDER = '<blade-styles></blade-styles>';
const PLACEHOLDER_OPEN = '<!-- START BLADE STYLES -->';
const PLACEHOLDER_CLOSE = '<!-- END BLADE STYLES -->';

/**
* Get the view / contents that represent the component.
Expand All @@ -15,6 +16,6 @@ class StylesComponent extends Component
*/
public function render()
{
return static::PLACEHOLDER;
return static::PLACEHOLDER_OPEN . static::PLACEHOLDER_CLOSE;
}
}
13 changes: 13 additions & 0 deletions src/Contracts/CssMinifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace BladeStyle\Contracts;

interface CssMinifier
{
/**
* Minify css string.
*
* @return string
*/
public function minify(string $css);
}
44 changes: 0 additions & 44 deletions src/Engines/BladeCompilerEngine.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Engines/CompilerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace BladeStyle\Engines;

use BladeStyle\StyleCompiler;
use Illuminate\Support\Facades\File;
use BladeStyle\Contracts\StyleEngine;
use Illuminate\View\Compilers\CompilerInterface;
Expand Down
46 changes: 46 additions & 0 deletions src/Engines/MinifierEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace BladeStyle\Engines;

use BladeStyle\Contracts\CssMinifier;

class MinifierEngine
{
/**
* Minifier.
*
* @var \BladeStyle\Contracts\CssMinifier
*/
protected $minifier;

/**
* Create new MinifierEngine instance.
*
* @param CssMinifier $minifier
*/
public function __construct(CssMinifier $minifier)
{
$this->minifier = $minifier;
}

/**
* Set minifier.
*
* @param CssMinifier $minifier
* @return void
*/
public function setMinifier(CssMinifier $minifier)
{
$this->minifier = $minifier;
}

/**
* Minify css string
*
* @return string
*/
public function minify(string $css)
{
return $this->minifier->minify($css);
}
}
25 changes: 25 additions & 0 deletions src/Engines/StyleCompilerEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace BladeStyle\Engines;

use BladeStyle\Components\StylesComponent;
use Illuminate\Support\Str;
use BladeStyle\Support\Style;
use Facade\Ignition\Views\Engines\CompilerEngine;

class StyleCompilerEngine extends CompilerEngine
{
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param array $data
* @return string
*/
public function get($path, array $data = [])
{
return Style::include(
parent::get($path, $data)
);
}
}
Loading

0 comments on commit 314bb1c

Please # to comment.