Skip to content

Commit 3af2c0b

Browse files
committed
Add async and defer
1 parent b233e2d commit 3af2c0b

File tree

8 files changed

+101
-2
lines changed

8 files changed

+101
-2
lines changed

WebLoader/Compiler.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class Compiler
3434
/** @var bool */
3535
private $debugging = FALSE;
3636

37+
/** @var bool */
38+
private $async = FALSE;
39+
40+
/** @var bool */
41+
private $defer = FALSE;
42+
3743
public function __construct(IFileCollection $files, IOutputNamingConvention $convention, $outputDir)
3844
{
3945
$this->collection = $files;
@@ -117,6 +123,42 @@ public function setJoinFiles($joinFiles)
117123
$this->joinFiles = (bool) $joinFiles;
118124
}
119125

126+
/**
127+
* @return boolean
128+
*/
129+
public function isAsync()
130+
{
131+
return $this->async;
132+
}
133+
134+
/**
135+
* @param boolean $async
136+
* @return Compiler
137+
*/
138+
public function setAsync($async)
139+
{
140+
$this->async = (bool) $async;
141+
return $this;
142+
}
143+
144+
/**
145+
* @return boolean
146+
*/
147+
public function isDefer()
148+
{
149+
return $this->defer;
150+
}
151+
152+
/**
153+
* @param boolean $defer
154+
* @return Compiler
155+
*/
156+
public function setDefer($defer)
157+
{
158+
$this->defer = $defer;
159+
return $this;
160+
}
161+
120162
/**
121163
* Set check last modified
122164
* @param bool $checkLastModified

WebLoader/Nette/Extension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public function getDefaultConfig()
3737
'filters' => array(),
3838
'fileFilters' => array(),
3939
'joinFiles' => TRUE,
40+
'async' => FALSE,
41+
'defer' => FALSE,
4042
'namingConvention' => '@' . $this->prefix('jsNamingConvention'),
4143
),
4244
'cssDefaults' => array(
@@ -51,6 +53,8 @@ public function getDefaultConfig()
5153
'filters' => array(),
5254
'fileFilters' => array(),
5355
'joinFiles' => TRUE,
56+
'async' => FALSE,
57+
'defer' => FALSE,
5458
'namingConvention' => '@' . $this->prefix('cssNamingConvention'),
5559
),
5660
'js' => array(
@@ -126,7 +130,9 @@ private function addWebLoader(ContainerBuilder $builder, $name, $config)
126130
$config['tempDir'],
127131
));
128132

129-
$compiler->addSetup('setJoinFiles', array($config['joinFiles']));
133+
$compiler->addSetup('setJoinFiles', array($config['joinFiles']))
134+
->addSetup('setAsync', array($config['async']))
135+
->addSetup('setDefer', array($config['defer']));
130136

131137
if ($builder->parameters['webloader']['debugger']) {
132138
$compiler->addSetup('@' . $this->prefix('tracyPanel') . '::addLoader', array(

WebLoader/Nette/JavaScriptLoader.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ class JavaScriptLoader extends WebLoader
2020
*/
2121
public function getElement($source)
2222
{
23-
return Html::el("script")->type("text/javascript")->src($source);
23+
$el = Html::el("script");
24+
$this->getCompiler()->isAsync() ? $el = $el->addAttributes(['async' => TRUE]) : NULL;
25+
$this->getCompiler()->isDefer() ? $el = $el->addAttributes(['defer' => TRUE]) : NULL;
26+
return $el->type("text/javascript")->src($source);
2427
}
2528

2629
}

tests/Nette/ExtensionTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,42 @@ public function testJoinFilesOffInOneService()
7777
$this->assertFalse($this->container->getService('webloader.cssJoinOffCompiler')->getJoinFiles());
7878
}
7979

80+
public function testAsyncOn()
81+
{
82+
$this->prepareContainer(array(
83+
__DIR__ . '/../fixtures/extension.neon',
84+
__DIR__ . '/../fixtures/extensionAsyncTrue.neon',
85+
));
86+
$this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->isAsync());
87+
}
88+
89+
public function testAsyncOff()
90+
{
91+
$this->prepareContainer(array(
92+
__DIR__ . '/../fixtures/extension.neon',
93+
__DIR__ . '/../fixtures/extensionAsyncFalse.neon',
94+
));
95+
$this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->isAsync());
96+
}
97+
98+
public function testDeferOn()
99+
{
100+
$this->prepareContainer(array(
101+
__DIR__ . '/../fixtures/extension.neon',
102+
__DIR__ . '/../fixtures/extensionDeferTrue.neon',
103+
));
104+
$this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->isDefer());
105+
}
106+
107+
public function testDeferOff()
108+
{
109+
$this->prepareContainer(array(
110+
__DIR__ . '/../fixtures/extension.neon',
111+
__DIR__ . '/../fixtures/extensionDeferFalse.neon',
112+
));
113+
$this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->isDefer());
114+
}
115+
80116
public function testExtensionName()
81117
{
82118
$tempDir = __DIR__ . '/../temp';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
webloader:
2+
jsDefaults:
3+
async: false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
webloader:
2+
jsDefaults:
3+
async: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
webloader:
2+
jsDefaults:
3+
defer: false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
webloader:
2+
jsDefaults:
3+
defer: true

0 commit comments

Comments
 (0)