Skip to content

Commit

Permalink
Add async and defer
Browse files Browse the repository at this point in the history
  • Loading branch information
Olicek committed Apr 29, 2016
1 parent b233e2d commit 3af2c0b
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 2 deletions.
42 changes: 42 additions & 0 deletions WebLoader/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class Compiler
/** @var bool */
private $debugging = FALSE;

/** @var bool */
private $async = FALSE;

/** @var bool */
private $defer = FALSE;

public function __construct(IFileCollection $files, IOutputNamingConvention $convention, $outputDir)
{
$this->collection = $files;
Expand Down Expand Up @@ -117,6 +123,42 @@ public function setJoinFiles($joinFiles)
$this->joinFiles = (bool) $joinFiles;
}

/**
* @return boolean
*/
public function isAsync()
{
return $this->async;
}

/**
* @param boolean $async
* @return Compiler
*/
public function setAsync($async)
{
$this->async = (bool) $async;
return $this;
}

/**
* @return boolean
*/
public function isDefer()
{
return $this->defer;
}

/**
* @param boolean $defer
* @return Compiler
*/
public function setDefer($defer)
{
$this->defer = $defer;
return $this;
}

/**
* Set check last modified
* @param bool $checkLastModified
Expand Down
8 changes: 7 additions & 1 deletion WebLoader/Nette/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function getDefaultConfig()
'filters' => array(),
'fileFilters' => array(),
'joinFiles' => TRUE,
'async' => FALSE,
'defer' => FALSE,
'namingConvention' => '@' . $this->prefix('jsNamingConvention'),
),
'cssDefaults' => array(
Expand All @@ -51,6 +53,8 @@ public function getDefaultConfig()
'filters' => array(),
'fileFilters' => array(),
'joinFiles' => TRUE,
'async' => FALSE,
'defer' => FALSE,
'namingConvention' => '@' . $this->prefix('cssNamingConvention'),
),
'js' => array(
Expand Down Expand Up @@ -126,7 +130,9 @@ private function addWebLoader(ContainerBuilder $builder, $name, $config)
$config['tempDir'],
));

$compiler->addSetup('setJoinFiles', array($config['joinFiles']));
$compiler->addSetup('setJoinFiles', array($config['joinFiles']))
->addSetup('setAsync', array($config['async']))
->addSetup('setDefer', array($config['defer']));

if ($builder->parameters['webloader']['debugger']) {
$compiler->addSetup('@' . $this->prefix('tracyPanel') . '::addLoader', array(
Expand Down
5 changes: 4 additions & 1 deletion WebLoader/Nette/JavaScriptLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class JavaScriptLoader extends WebLoader
*/
public function getElement($source)
{
return Html::el("script")->type("text/javascript")->src($source);
$el = Html::el("script");
$this->getCompiler()->isAsync() ? $el = $el->addAttributes(['async' => TRUE]) : NULL;
$this->getCompiler()->isDefer() ? $el = $el->addAttributes(['defer' => TRUE]) : NULL;
return $el->type("text/javascript")->src($source);
}

}
36 changes: 36 additions & 0 deletions tests/Nette/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,42 @@ public function testJoinFilesOffInOneService()
$this->assertFalse($this->container->getService('webloader.cssJoinOffCompiler')->getJoinFiles());
}

public function testAsyncOn()
{
$this->prepareContainer(array(
__DIR__ . '/../fixtures/extension.neon',
__DIR__ . '/../fixtures/extensionAsyncTrue.neon',
));
$this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->isAsync());
}

public function testAsyncOff()
{
$this->prepareContainer(array(
__DIR__ . '/../fixtures/extension.neon',
__DIR__ . '/../fixtures/extensionAsyncFalse.neon',
));
$this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->isAsync());
}

public function testDeferOn()
{
$this->prepareContainer(array(
__DIR__ . '/../fixtures/extension.neon',
__DIR__ . '/../fixtures/extensionDeferTrue.neon',
));
$this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->isDefer());
}

public function testDeferOff()
{
$this->prepareContainer(array(
__DIR__ . '/../fixtures/extension.neon',
__DIR__ . '/../fixtures/extensionDeferFalse.neon',
));
$this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->isDefer());
}

public function testExtensionName()
{
$tempDir = __DIR__ . '/../temp';
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/extensionAsyncFalse.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
webloader:
jsDefaults:
async: false
3 changes: 3 additions & 0 deletions tests/fixtures/extensionAsyncTrue.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
webloader:
jsDefaults:
async: true
3 changes: 3 additions & 0 deletions tests/fixtures/extensionDeferFalse.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
webloader:
jsDefaults:
defer: false
3 changes: 3 additions & 0 deletions tests/fixtures/extensionDeferTrue.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
webloader:
jsDefaults:
defer: true

0 comments on commit 3af2c0b

Please sign in to comment.