Skip to content

Commit

Permalink
Merge pull request #29 from kylekatarnls/minify-assets
Browse files Browse the repository at this point in the history
Minify assets
  • Loading branch information
kylekatarnls committed Sep 4, 2017
2 parents c3d044e + 1d87202 commit 1f7ac28
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 8 deletions.
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ finder:
- "src/config"

enabled:
- long_array_syntax
- concat_with_spaces

disabled:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"php": ">=5.4.0",
"illuminate/support": "4 - 5",
"illuminate/view": "4 - 5",
"pug-php/pug": "1 - 2",
"pug-php/pug-assets": "^1.0.1",
"pug/installer": "^0.1.3"
},
"require-dev": {
Expand Down
50 changes: 44 additions & 6 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@

// Dependencies
use Illuminate\View\Engines\CompilerEngine;
use Pug\Assets;
use Pug\Pug;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
protected function setDefaultOption(Pug $pug, $name, $value)
{
try {
$pug->getOption($name);
} catch (\InvalidArgumentException $exception) {
$pug->setCustomOption($name, call_user_func($value));
}
}

/**
* Get the major Laravel version number.
*
Expand Down Expand Up @@ -36,11 +46,39 @@ public function register()
$this->app->singleton('laravel-pug.pug', function () {
$config = $this->getConfig();
$pug = new Pug($config);
$assets = new Assets($pug);
$getEnv = array('App', 'environment');
$assets->setEnvironment(is_callable($getEnv) ? call_user_func($getEnv) : 'production');

$this->app->singleton('laravel-pug.pug-assets', function () use ($assets) {
return $assets;
});

// Determine the cache dir if not configured
$pug->setCustomOption(
'defaultCache',
storage_path($this->version() >= 5 ? '/framework/views' : '/views')
);
$this->setDefaultOption($pug, 'defaultCache', function () {
return storage_path($this->version() >= 5 ? '/framework/views' : '/views');
});

// Determine assets input directory
$this->setDefaultOption($pug, 'assetDirectory', function () {
return array_map(function ($params) {
list($function, $arg) = $params;

return function_exists($function) ? call_user_func($function, $arg) : null;
}, array(
array('resource_path', 'assets'),
array('app_path', 'views/assets'),
array('app_path', 'assets'),
array('app_path', 'views'),
array('app_path', ''),
array('base_path', ''),
));
});

// Determine assets output directory
$this->setDefaultOption($pug, 'outputDirectory', function () {
return function_exists('public_path') ? public_path() : null;
});

return $pug;
});
Expand Down Expand Up @@ -104,9 +142,9 @@ public function bootLaravel4()
*/
public function bootLaravel5()
{
$this->publishes([
$this->publishes(array(
__DIR__ . '/../config/config.php' => config_path('laravel-pug.php'),
], 'laravel-pug');
), 'laravel-pug');
}

/**
Expand Down
101 changes: 100 additions & 1 deletion tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Facade;
use Illuminate\View\Engines\CompilerEngine;
use Pug\Assets;

include_once __DIR__ . '/helpers.php';

class Config implements ArrayAccess
{
protected $useSysTempDir = false;

protected $data = array();

public function __construct($source = null)
{
$this->data['source'] = $source;
}

public function setUseSysTempDir($useSysTempDir)
{
$this->useSysTempDir = $useSysTempDir;
}

public function get($input)
{
if ($this->useSysTempDir && in_array($input, ['laravel-pug', 'laravel-pug::config'])) {
return [
'assetDirectory' => __DIR__ . '/assets',
'outputDirectory' => sys_get_temp_dir(),
'defaultCache' => sys_get_temp_dir(),
];
}

return isset($this->data[$input]) ? $this->data[$input] :array(
'input' => $input,
);
Expand Down Expand Up @@ -55,6 +72,8 @@ public function __toString()

class LaravelTestApp implements Application, ArrayAccess
{
protected $useSysTempDir = false;

protected $singletons = array();

const VERSION = '4.0.0';
Expand All @@ -64,6 +83,11 @@ public function version()
return static::VERSION;
}

public function setUseSysTempDir($useSysTempDir)
{
$this->useSysTempDir = $useSysTempDir;
}

public function basePath()
{
return __DIR__;
Expand Down Expand Up @@ -180,7 +204,10 @@ public function factory($abstract)

public function make($abstract, array $parameters = [])
{
return new Config($abstract);
$config = new Config($abstract);
$config->setUseSysTempDir($this->useSysTempDir);

return $config;
}

public function call($callback, array $parameters = [], $defaultMethod = null)
Expand Down Expand Up @@ -353,6 +380,7 @@ public function testVersion()

/**
* @covers ::register
* @covers ::setDefaultOption
*/
public function testRegister()
{
Expand Down Expand Up @@ -513,4 +541,75 @@ public function testBootUnsupportedLaravel()
$provider = new ServiceProvider($app);
$provider->boot();
}

/**
* @covers ::register
* @covers ::setDefaultOption
*/
public function testView()
{
$this->app->setUseSysTempDir(true);
$view = new View();
$resolver = new Resolver();
$this->app['view.engine.resolver'] = $resolver;
$this->app['view'] = $view;
$this->provider->register();
$this->provider->boot();
$path = __DIR__ . '/assets.pug';

/** @var CompilerEngine $pug */
$pug = $resolver->get('pug');

self::assertSame(
'<head><script src="js/app.min.js"></script></head>',
preg_replace(
'/\s{2,}/',
'',
$this->app['view.engine.resolver']->get('pug')->get($path)
)
);

$contents = file_get_contents(sys_get_temp_dir() . '/js/app.min.js');

self::assertSame('a();b();', trim($contents));

unlink(sys_get_temp_dir() . '/js/app.min.js');
unlink($pug->getCompiler()->getCompiledPath($path));

/** @var Assets $assets */
$assets = $this->app->getSingleton('laravel-pug.pug-assets');
$assets->setEnvironment('dev');

self::assertSame(
'<head><minify>app<script src="foo.js"></script><script src="bar.js"></script></minify></head>',
preg_replace('/\s{2,}/', '', $pug->get($path))
);

unlink($pug->getCompiler()->getCompiledPath($path));

$assets->setEnvironment('production');

self::assertSame(
'<head><script src="js/app.min.js"></script></head>',
preg_replace(
'/\s{2,}/',
'',
$this->app['view.engine.resolver']->get('pug')->get($path)
)
);

self::assertSame('a();b();', trim($contents));

unlink(sys_get_temp_dir() . '/js/app.min.js');
unlink($pug->getCompiler()->getCompiledPath($path));

$assets->unsetMinify();

self::assertSame(
'<head><minify>app<script src="foo.js"></script><script src="bar.js"></script></minify></head>',
preg_replace('/\s{2,}/', '', $pug->get($path))
);

unlink($pug->getCompiler()->getCompiledPath($path));
}
}
4 changes: 4 additions & 0 deletions tests/assets.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
head
minify app
script(src="foo.js")
script(src="bar.js")
1 change: 1 addition & 0 deletions tests/assets/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b();
1 change: 1 addition & 0 deletions tests/assets/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a();

0 comments on commit 1f7ac28

Please sign in to comment.