5
Google has two tools for compilar/minificar
optimizing the files JS
and CSS
, Closure Compiler JS
and Closure Compiler CSS.
To perform the compilation process I can do as follows:
java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
Currently I use a file .bat
with 40 lines that were created manually for minificar
the files, and as the system grows and new files arise sometimes I forget to include new files in that bat, so I thought to make a process to create these paths.
The first thing I did was go through the whole directory where the JS
and save the files in an array:
function dirToArray($dir) { $result = array(); $cdir = scandir($dir); $raiz = ''; foreach ($cdir as $key => $value) { if (!in_array($value, array(".", ".."))) { if (is_dir($dir . DIRECTORY_SEPARATOR . $value) && ($value !== "lib")) { $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); $raiz = $value; } elseif (strrchr($value, '.') === ".js") { $result[] = $value; } } } return $result; } $dir = '../../controller/js'; $files = dirToArray($dir);
This way return me an array:
array 0 => string 'agenda.js' (length=9) 1 => string 'agendaNegoc.js' (length=14) 2 => string 'atribCobrador.js' (length=16) 3 => string 'atualizaSistema.js' (length=18) 'cadastro' => array 0 => string 'agencia.js' (length=10) 1 => string 'avisos.js' (length=9) 2 => string 'bancoEmpresa.js' (length=15) 3 => string 'cidade.js' (length=9) 4 => string 'cliente.js' (length=10) 5 => string 'cobradores.js' (length=13) 6 => string 'codEventos.js' (length=13) 7 => string 'remessa.js' (length=10) 4 => string 'comercial.js' (length=15) 5 => string 'controleCobranca.js' (length=22) 6 => string 'classCliente.js' (length=15) 7 => string 'cliente.js' (length=10) 8 => string 'codigosEventos.js' (length=17) 9 => string 'consultaWebservice.js' (length=27) 10 => string 'coresSistema.js' (length=15) 11 => string 'dadosCliente.js' (length=15) 12 => string 'detalhesOperacao.js' (length=19) 13 => string 'emissaoboleto.js' (length=16) 14 => string 'enviarEmail.js' (length=14) 15 => string 'exportador.js' (length=13) 16 => string 'fases.js' (length=14) 17 => string 'filtroNivelUsuario.js' (length=21) 18 => string 'funcoes.js' (length=10) 'importador' => array 0 => string 'importadorAS1.js' (length=18) 20 => string 'importador.js' (length=13) 21 => string 'importador_exportador.js' (length=24) 22 => string 'inserirAgendamento.js' (length=21) 23 => string 'inserirEventos.js' (length=17) 24 => string 'listaUsuarios.js' (length=16) 25 => string 'login.js' (length=8) 26 => string 'mensagemInterna.js' (length=24) 27 => string 'menu.js' (length=7) 28 => string 'motivosPausa.js' (length=15) 'negociacao' => array 0 => string 'cobrancaA.js' (length=19) 1 => string 'cobrancaB.js' (length=22) 2 => string 'cobrancaB2.js' (length=28) 3 => string 'cobrancaC3.js' (length=25) 4 => string 'cobrancaM.js' (length=18) 5 => string 'codEvento.js' (length=12) 6 => string 'propostaAnexos.js' (length=22) 29 => string 'notificador.js' (length=14) 30 => string 'origemdb.js' (length=11) 31 => string 'permissoes.js' (length=13) 32 => string 'pesquisaCliente.js' (length=18) 'portal' => array 0 => string 'negociacao.js' (length=13) 33 => string 'referenciasPessoais.js' (length=22) 'relatorio' => array 0 => string 'filtroDemonstrativo.js' (length=28) 1 => string 'filtroRelBol.js' (length=18) 2 => string 'filtroRelEventos.js' (length=19) 3 => string 'filtroRelPrestContas.js' (length=23) 4 => string 'relDemonsDebito.js' (length=18) 5 => string 'relEventos.js' (length=13) 34 => string 'supervisorLista.js' (length=18) 35 => string 'usuarioBanco.js' (length=15)
So I created a function to generate the full path:
function geraPatch($files, $param, $devPatch, $prodPatch) { $result = array(); foreach ($files['cadastro'] as $key => $value) { $result['cadastro'][$key] = $param . $devPatch . 'cadastro/' . $value . $prodPatch . 'cadastro/' . $value; } foreach ($files['importador'] as $key => $value) { $result['importador'][$key] = $param . $devPatch . 'importador/' . $value . $prodPatch . 'importador/' . $value; } foreach ($files['negociacao'] as $key => $value) { $result['negociacao'][$key] = $param . $devPatch . 'negociacao/' . $value . $prodPatch . 'negociacao/' . $value; } foreach ($files['portal'] as $key => $value) { $result['portal'][$key] = $param . $devPatch . 'portal/' . $value . $prodPatch . 'portal/' . $value; } foreach ($files['relatorio'] as $key => $value) { $result['relatorio'][$key] = $param . $devPatch . 'relatorio/' . $value . $prodPatch . 'relatorio/' . $value; } return $result; } $dir = '../../controller/js'; $files = dirToArray($dir); $param = 'java -jar compiler.jar --js '; $devPatch = 'desenvolvimento/'; $prodPatch = ' --js_output_file producao/'; var_dump(geraPatch($files, $param, $devPatch, $prodPatch));
And the result comes out as expected
0 => string 'java -jar compiler.jar --js desenvolvimento/cadastro/agencia.js --js_output_file producao/cadastro/agencia.js' (length=109) 1 => string 'java -jar compiler.jar --js desenvolvimento/cadastro/avisos.js --js_output_file producao/cadastro/avisos.js' (length=107) 2 => string 'java -jar compiler.jar --js desenvolvimento/cadastro/bancoEmpresa.js --js_output_file producao/cadastro/bancoEmpresa.js' (length=119) 3 => string 'java -jar compiler.jar --js desenvolvimento/cadastro/cidade.js --js_output_file producao/cadastro/cidade.js' (length=107) 4 => string 'java -jar compiler.jar --js desenvolvimento/cadastro/cliente.js --js_output_file producao/cadastro/cliente.js' (length=109)
What are my doubts?
1° to make a foreach
from the root without leaving the subArrays
and how ? if I make one foreach
the result comes out like this:
java -jar compiler.jar --js desenvolvimento/Array --js_output_file producao/Array
2° How can I get all this array configured and create a file in php .bat
and save all that text to run it ?
3° Is there any tool that simplifies this whole process, my need is precisely to be able to select a directory that contains the files in which I want to minify.