Skip to content

Commit da7f0e7

Browse files
committed
JSS Compiler
1 parent 3caf09a commit da7f0e7

File tree

2 files changed

+107
-6
lines changed

2 files changed

+107
-6
lines changed

Cli/Optimizer.php

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ function run()
7979
$this->cmdAll();
8080
break;
8181

82+
case 'jss':
83+
$this->cmdJss();
84+
break;
85+
8286
default:
8387
echo "\n\n Command \"optimize:".$this->cmd."\" not exists!";
8488
exit(Main::help());
@@ -155,6 +159,89 @@ private function cmdAll()
155159
{
156160
$this->cmdCss();
157161
$this->cmdJs();
162+
$this->cmdJss();
163+
}
164+
165+
/**
166+
* Jointer command
167+
* @return void Configurated files are saved
168+
*/
169+
private function cmdJss()
170+
{
171+
if ($jss = $this->config->get('jss')) {
172+
echo "\n - Compiled JS+CSS -\n";
173+
174+
if (!$this->subCmd) {
175+
foreach ($jss as $k => $v) {
176+
echo "\n - $k:\n";
177+
$this->jssCompiler($v);
178+
}
179+
return;
180+
}
181+
182+
if (isset($jss->{$this->subCmd})) {
183+
echo "\n - ".$this->subCmd."\n";
184+
185+
$this->jssCompiler($jss->{$this->subCmd});
186+
return;
187+
} else {
188+
echo "\n Err: Not found!\n";
189+
return;
190+
}
191+
}
192+
}
193+
194+
/**
195+
* Jointer JS & CSS
196+
* @param object $config configuration data
197+
* @return void save file
198+
*/
199+
private function jssCompiler($config)
200+
{
201+
if (!isset($config->filename)) {
202+
echo "ERROR!!\n";
203+
return false;
204+
}
205+
206+
$content = "var JSS = Array();\n";
207+
$width = 4096;
208+
209+
//CSS
210+
if (isset($config->css)) {
211+
$tmp = $this->minify(false,
212+
$config->css,
213+
$this->yuicompressor,
214+
$this->config->baseDir);
215+
216+
$tmp = explode("\n", str_replace("'", "\'", $tmp));
217+
218+
foreach ($tmp as $k => $v) {
219+
$v = trim($v);
220+
if ($v == '') {
221+
continue;
222+
}
223+
224+
if (substr($v, -1) == '\\') {
225+
$v .= '\\';
226+
}
227+
228+
$content .= 'JSS['.$k.'] = \''.$v."';\n";
229+
}
230+
}
231+
232+
//Função para montagem do STYLE
233+
$content .= 'for(var i in JSS){var etmp=document.createElement("STYLE");etmp.type="text/css";etmp.innerHTML=JSS[i];document.head.appendChild(etmp);}document.getElementsByClassName("container")[0].style.display="block";document.getElementById("loader").style.display="none";'."\n";
234+
235+
//Javascripts
236+
if (isset($config->js)) {
237+
$content .= $this->minify(false,
238+
$config->js,
239+
$this->yuicompressor,
240+
$this->config->baseDir);
241+
}
242+
243+
file_put_contents($config->filename, $content);
244+
echo "\n\n Saving: ".$config->filename."\n";
158245
}
159246

160247
// ----------------------------------------------------------------------
@@ -167,22 +254,31 @@ private function cmdAll()
167254
* @param string $baseDir Path of base directory
168255
* @return void void
169256
*/
170-
private function minify($filename, $files, $yuicompressor, $baseDir)
257+
private function minify($filename = false, $files, $yuicompressor, $baseDir, $width = null)
171258
{
172259
$content = '';
260+
if ($width === null) {
261+
$extra = '';
262+
} else {
263+
$extra = '--line-break '.intval($width);
264+
}
173265

174266
foreach ($files as $file) {
175267
if (file_exists($baseDir.$file)) {
176-
$result = exec("java -jar $yuicompressor $baseDir$file");
177-
$content .= "/* $file */$result\n";
268+
$result = exec("java -jar $yuicompressor $extra $baseDir$file ");
269+
$content .= (defined('_MODE') && _MODE == 1 ? "/* $file */" : '')."$result\n";
178270
echo "\n Add: $file";
179271
} else {
180272
echo "\n Err: $file - not found.";
181273
}
182274
}
183-
echo "\n Saving: $filename\n";
275+
//return minyfied data
276+
if ($filename === false) {
277+
return $content;
278+
}
184279

185-
file_put_contents($baseDir.$filename, $content);
186-
echo "\n Minified ..\n";
280+
echo "\n Saving: $filename\n";
281+
file_put_contents($baseDir.$filename, $content);
282+
echo "\n Minified ..\n";
187283
}
188284
}

Config/Lib/Cli/Optimizer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Optimizer
2828
public $baseDir = null;
2929
private $css = null;
3030
private $js = null;
31+
private $jss = null;
3132

3233
private static $configFile = false;
3334
private static $node = false;
@@ -144,6 +145,10 @@ function load(string $file = null)
144145
$this->js = $a->js;
145146
}
146147

148+
if (isset($a->jss)) {
149+
$this->jss = $a->jss;
150+
}
151+
147152
return true;
148153
}
149154
}

0 commit comments

Comments
 (0)