Skip to content

Commit d6654a9

Browse files
committed
FEAT: JavaScriptBuilder will now minify it's output by default.
FEAT: JavaScriptBuilder will now minify it's output by default. This feature can be disabled by passing in a 'minify' parameter set to false. TESTS: Added a test to verify that the minify step is working and is enabled by default.
1 parent 1ebc500 commit d6654a9

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

JavascriptBuilder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
namespace fiftyone\pipeline\core;
2525

26+
use NodejsPhpFallback\Uglify;
27+
2628
/**
2729
* The JavaScriptBuilder aggregates JavaScript properties
2830
* from FlowElements in the Pipeline. This JavaScript also (when needed)
@@ -47,6 +49,7 @@ public function __construct($settings = array())
4749
"_enableCookies" => isset($settings["enableCookies"]) ? $settings["enableCookies"] : true
4850

4951
];
52+
$this->minify = isset($settings["minify"]) ? $settings["minify"] : true;
5053
}
5154

5255
public $dataKey = "javascriptbuilder";
@@ -161,6 +164,12 @@ public function processInternal($flowData)
161164
}
162165

163166
$output = $m->render(file_get_contents(__DIR__ . "/JavaScriptResource.mustache"), $vars);
167+
168+
if($this->minify) {
169+
// Minify the output
170+
$uglify = new Uglify(array($output));
171+
$output = $uglify->getMinifiedJs();
172+
}
164173

165174
$data = new ElementDataDictionary($this, ["javascript" => $output]);
166175

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"license": "EUPL-1.2",
2424
"type": "library",
2525
"require": {
26-
"mustache/mustache": "^2.13"
26+
"mustache/mustache": "^2.13",
27+
"nodejs-php-fallback/uglify": "^1.0"
2728
},
2829
"autoload": {
2930
"classmap": [

tests/javascriptBuilderTests.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,18 @@ public function processInternal($FlowData)
7070

7171
class TestPipeline
7272
{
73-
public function __construct()
73+
public function __construct($minify = NULL)
7474
{
75-
$this->Pipeline = (new PipelineBuilder())
75+
if(is_null($minify))
76+
{
77+
$pipelineSettings = array();
78+
}
79+
else
80+
{
81+
$jsSettings = array('minify'=>$minify);
82+
$pipelineSettings = array('javascriptBuilderSettings'=>$jsSettings);
83+
}
84+
$this->Pipeline = (new PipelineBuilder($pipelineSettings))
7685
->add(new TestEngine())
7786
->build();
7887
}
@@ -82,7 +91,7 @@ class JavaScriptBundlerTests extends TestCase
8291
{
8392
public function testJSONBundler()
8493
{
85-
$Pipeline = (new TestPipeline())->Pipeline;
94+
$Pipeline = (new TestPipeline(false))->Pipeline;
8695

8796
$FlowData = $Pipeline->createFlowData();
8897

@@ -106,9 +115,37 @@ public function testJSONBundler()
106115
$this->assertEquals($FlowData->jsonbundler->json, $expected);
107116
}
108117

109-
public function testSequence()
118+
public function testJavaScriptBuilder_Minify()
110119
{
120+
// Generate minified javascript
121+
$Pipeline = (new TestPipeline(true))->Pipeline;
122+
$FlowData = $Pipeline->createFlowData();
123+
$FlowData->process();
124+
$minified = $FlowData->javascriptbuilder->javascript;
125+
126+
// Generate non-minified javascript
127+
$Pipeline = (new TestPipeline(false))->Pipeline;
128+
$FlowData = $Pipeline->createFlowData();
129+
$FlowData->process();
130+
$nonminified = $FlowData->javascriptbuilder->javascript;
131+
132+
// Generate javascript with default settings
111133
$Pipeline = (new TestPipeline())->Pipeline;
134+
$FlowData = $Pipeline->createFlowData();
135+
$FlowData->process();
136+
$default = $FlowData->javascriptbuilder->javascript;
137+
138+
// We don't want to get too specific here. Just check that
139+
// the minified version is smaller to confirm that it's
140+
// done something.
141+
$this->assertGreaterThan(strlen($minified), strlen($nonminified));
142+
// Check that default is to minify the output
143+
$this->assertEquals(strlen($default), strlen($minified));
144+
}
145+
146+
public function testSequence()
147+
{
148+
$Pipeline = (new TestPipeline(false))->Pipeline;
112149

113150
$FlowData = $Pipeline->createFlowData();
114151

0 commit comments

Comments
 (0)