Skip to content

Commit

Permalink
Version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdekruijk committed Nov 18, 2019
0 parents commit 0f26a83
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Nick de Kruijk

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:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

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.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Minify

A simple package to minify CSS/SCSS and Javascript on the fly without the need of tools like Laravel Mix or Webpack.
It combines all stylesheet files or javascript files into a single, minified file with simpel but effective cachebusting with filemtime()

## Installation

Begin by installing this package through Composer.

```composer require nickdekruijk/minify```

### Laravel installation

Publish the config file if the defaults doesn't suite your needs:

```php artisan vendor:publish --provider="NickDeKruijk\Minify\MinifyServiceProvider"```

### Stylesheet

```php
// app/views/hello.blade.php
<html>
<head>
...
{!! Minify::stylesheet(['lightbox.css', 'fonts.css', 'styles.css']) !!}
</head>
...
</html>

```

### Javascript

```php
// app/views/hello.blade.php

<html>
<body>
...
{!! Minify::javascript(['lazyload.min.js', 'scripts.js']) !!}
</body>
</html>
```

### Config
See the config file at `/config/minify.php`
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "nickdekruijk/minify",
"keywords": ["minify", "laravel5", "css", "sass", "scss", "javascript", "js"],
"description": "A package for automaticaly minifying CSS, SCSS and Javascript for Laravel",
"license": "MIT",
"authors": [{
"name": "Nick de Kruijk",
"email": "[email protected]"
}],
"require": {
"php": ">=7.2.0",
"scssphp/scssphp": "^1.0",
"tedivm/jshrink": "^1.3"
},
"autoload": {
"psr-4": {
"NickDeKruijk\\Minify\\": "src/"
}
},
"minimum-stability": "stable",
"extra": {
"laravel": {
"providers": [
"NickDeKruijk\\Minify\\ServiceProvider"
],
"aliases": {
"Minify": "NickDeKruijk\\Minify\\Facade"
}
}
}
}
16 changes: 16 additions & 0 deletions src/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NickDeKruijk\Minify;

class Facade extends \Illuminate\Support\Facades\Facade
{
/**
* Name of the binding in the IoC container
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'minify';
}
}
97 changes: 97 additions & 0 deletions src/Minify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace NickDeKruijk\Minify;

use ScssPhp\ScssPhp\Compiler;
use JShrink\Minifier;
use Exception;

class Minify
{
private static function minifyRequired($files, $output, $paths)
{
if (in_array(app()->environment(), config('minify.skip_environment'))) {
return false;
}
if (!file_exists(public_path($output))) {
return true;
}
$filemtime = filemtime(public_path($output));
foreach((array)$files as $file) {
if (filemtime(self::findFile($file, $paths)) > $filemtime) {
return true;
}
}
return false;
}

private static function findFile($file, $paths)
{
if (file_exists($file)) {
return $file;
} else {
foreach($paths as $path) {
$filename = rtrim($path, '/') . '/' . $file;
if (file_exists($filename)) {
return $filename;
}
}
throw new Exception($file . ' not found within scssImportPaths');
}
}

private static function findCssFile($file)
{
return self::findFile($file, config('minify.scssImportPaths'));
}

private static function findJsFile($file)
{
return self::findFile($file, config('minify.jsImportPaths'));
}

private static function outputFile($file, $content)
{
$directory = pathinfo($file)['dirname'];
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
file_put_contents($file, $content);
}

public static function stylesheet($files = ['../resources/sass/app.scss'], $output = null)
{
$output = $output ?: config('minify.output.css');

if (self::minifyRequired($files, $output, config('minify.scssImportPaths'))) {
$scss = new Compiler();
$scss->setImportPaths(config('minify.scssImportPaths'));
$formatter = config('minify.scssFormatter');
$scss->setFormatter(new $formatter);

$css = '';
foreach((array)$files as $file) {
$css .= file_get_contents(self::findCssFile($file));
}
$css = $scss->compile($css);

self::outputFile(public_path($output), $css);
}
return '<link rel="stylesheet" type="text/css" href="' . asset($output) . '?' . filemtime(public_path($output)) . '">';
}

public static function javascript($files = ['../resources/js/app.js'], $output = null)
{
$output = $output ?: config('minify.output.js');

if (self::minifyRequired($files, $output, config('minify.jsImportPaths'))) {
$js = '';
foreach((array)$files as $file) {
$js .= file_get_contents(self::findJsFile($file));
}
$js = Minifier::minify($js, ['flaggedComments' => config('minify.jsFlaggedComments')]);
self::outputFile(public_path($output), $js);
}
return '<script src="' . asset($output) . '?' . filemtime(public_path($output)) . '"></script>';
}
}
34 changes: 34 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace NickDeKruijk\Minify;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/config.php' => config_path('minify.php'),
], 'config');
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/config.php', 'minify');

// Register the main class to use with the facade
$this->app->singleton('minify', function () {
return new Minify;
});
}
}
87 changes: 87 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| output
|--------------------------------------------------------------------------
|
| Filenames for the minified js and css files relative to public_path()
|
*/

'output' => [
'css' => 'css/build/app.css',
'js' => 'js/build/app.js',
],

/*
|--------------------------------------------------------------------------
| skip_environment
|--------------------------------------------------------------------------
|
| Don't minify in these environments
| You shouldn't run live minification in production environments but run it
| localy and push the minified output files to the production server
|
*/

'skip_environment' => [
'production',
],

/*
|--------------------------------------------------------------------------
| scssImportPaths
|--------------------------------------------------------------------------
|
| Paths to search for CSS / SCSS @import
|
*/

'scssImportPaths' => [
'../resources/sass/',
'../resources/scss/',
'../resources/css/',
'../public/css/',
],

/*
|--------------------------------------------------------------------------
| jsImportPaths
|--------------------------------------------------------------------------
|
| Paths to search for javascript files
|
*/

'jsImportPaths' => [
'../resources/js/',
'../public/js/',
],

/*
|--------------------------------------------------------------------------
| scssFormatter
|--------------------------------------------------------------------------
|
| Default ScssPhp Formatter to use
| Options: Expanded Nested Compact Compressed Crunched
|
*/

'scssFormatter' => '\ScssPhp\ScssPhp\Formatter\Crunched',

/*
|--------------------------------------------------------------------------
| jsFlaggedComments
|--------------------------------------------------------------------------
|
| Disable YUI style comment preservation.
|
*/

'jsFlaggedComments' => false,

];

0 comments on commit 0f26a83

Please sign in to comment.