{"id":89,"date":"2008-02-15T12:49:53","date_gmt":"2008-02-15T11:49:53","guid":{"rendered":"http:\/\/vince.tikasz.hu\/2008\/02\/15\/gyorsabb-js-es-css-ii\/"},"modified":"2011-07-24T15:15:09","modified_gmt":"2011-07-24T13:15:09","slug":"gyorsabb-js-es-css-ii","status":"publish","type":"post","link":"https:\/\/vince.tikasz.hu\/2008\/02\/15\/gyorsabb-js-es-css-ii\/","title":{"rendered":"Gyorsabb JS \u00e9s CSS II."},"content":{"rendered":"
Most, hogy m\u00e1r csak egy-egy \u00e1llom\u00e1nyt kell kiszolg\u00e1lnunk<\/a>, \u00e9s a klienseknek let\u00f6lteni, ak\u00e1r tov\u00e1bb is l\u00e9phet\u00fcnk. T\u00f6m\u00f6r\u00edts\u00fcnk az \u00f6sszef\u0171z\u00f6tt k\u00f3don. M\u00e1sodik l\u00e9p\u00e9s teh\u00e1t a JavaScript k\u00f3d t\u00f6m\u00f6r\u00edt\u00e9se. \u00c9n erre a feladatra a JavaScriptPacker<\/a>-t aj\u00e1nlom. Haszn\u00e1lata pofon egyszer\u0171:<\/p>\n Felhaszn\u00e1lva ezt a pici k\u00f3dot kicsit \u00e1talak\u00edtjuk a m\u00e1r megl\u00e9v\u0151 f\u00fcggv\u00e9ny\u00fcnket<\/p>\n A f\u00fcggv\u00e9ny\u00fcnk, kapott egy plusz param\u00e9tert, amivel be\u00e1ll\u00edthatjuk a t\u00f6m\u00f6r\u00edt\u00e9s m\u00e9rt\u00e9k\u00e9t. Ugyan\u00fagy \u00f6sszeszedj\u00fck, \u00e9s \u00f6sszef\u0171zz\u00fck az \u00e1llom\u00e1nyok tartalm\u00e1t, de ha Most, hogy m\u00e1r csak egy-egy \u00e1llom\u00e1nyt kell kiszolg\u00e1lnunk, \u00e9s a klienseknek let\u00f6lteni, ak\u00e1r tov\u00e1bb is l\u00e9phet\u00fcnk. T\u00f6m\u00f6r\u00edts\u00fcnk az \u00f6sszef\u0171z\u00f6tt k\u00f3don. M\u00e1sodik l\u00e9p\u00e9s teh\u00e1t a JavaScript k\u00f3d t\u00f6m\u00f6r\u00edt\u00e9se. \u00c9n erre a feladatra a JavaScriptPacker-t aj\u00e1nlom. Haszn\u00e1lata pofon egyszer\u0171: $in = file_get_contents( … Continue reading $in = file_get_contents( 'my.js' );\r\n\r\nrequire_once( 'class.JavaScriptPacker.php' );\r\n$packer = new JavaScriptPacker($in, 62, true, false);\r\n\r\n$out = $packer->pack();\r\nfile_put_contents( 'my.compressed.js', $out );<\/pre>\n
function headerFiles( array $files, $mode, $fileType, $prefix, $compress = 62 \u00a0) {\r\n if ( count( $files ) > 0) {\r\n if ( $mode == 'list' ) {\r\n foreach ( $files as $f ) {\r\n if ( $fileType == 'css' ) {\r\n echo '<link rel=\"stylesheet\" type=\"text\/css\" href=\"'. $prefix.$f.'\" \/>';\r\n }\r\n elseif ( $fileType == 'script' ) {\r\n echo '<script type=\"text\/javascript\" src=\"'. $prefix.$f.'\"><\/script>';\r\n }\r\n }\r\n }\r\n elseif ( $mode == 'one' ) {\r\n $dirname = dirname($_SERVER['SCRIPT_FILENAME']);\r\n $extension = $fileType == 'script' ? 'js' : 'css';\r\n $Hashes = array();\r\n foreach ( $files as $f ) {\r\n $Hashes[] = sha1_file( $dirname.$f );\r\n }\r\n $name = sha1( join(';#%#;', $Hashes) );\r\n $fileName = \"{$dirname}\/public\/cache\/{$name}.{$extension}\";\r\n if ( !is_file( $fileName ) ) {\r\n $Content = '';\r\n foreach ( $files as $f ) {\r\n if ( is_file( $dirname.$f ) ) {\r\n $Content .= \"\/*START file: {$f}*\/\\n\";\r\n $Content .= file_get_contents( $dirname.$f );\u00ed\r\n $Content .= \"\\n\/*\/\/END file: {$f}*\/\\n\";\r\n }\r\n }\r\n if ( $fileType == 'script' && $compress > 0 ) {\r\n require_once( 'class.JavaScriptPacker.php' );\r\n $packer = new JavaScriptPacker($Content, $compress, true, false);\r\n $Content = $packer->pack();\r\n }\r\n file_put_contents( $fileName, $Content );\r\n }\r\n headerFiles( array( \"\/public\/cache\/{$name}.{$extension}\" ), 'list', $fileType, $prefix );\r\n }\r\n }\r\n}<\/pre>\n
script<\/code>-ekkel dolgozunk, \u00e9s a t\u00f6m\u00f6r\u00edt\u00e9s m\u00e9rt\u00e9ke nagyobb mint nulla, akkor a k\u00edrand\u00f3 tartalmat t\u00f6m\u00f6r\u00edtj\u00fck.<\/p>\n","protected":false},"excerpt":{"rendered":"