Cara Compress HTML, JS, CSS (Minify) - Codeigniter


Hallow temen" semua.. semoga semua sehat selalu dan wabah COVID-19 cepat berlalu

Buat yang pake codeigniter dan pengen output HTML nya bisa di compress agar biar load website lebih ringan ..

Saya mendapat referensi dari -> https://github.com/bcit-ci/CodeIgniter/wiki/Compress-HTML-output

Langkah pertama:

Kalian harus mengaktifkan hook -> application/config/config.php

$config['enable_hooks'] = TRUE;

Langkah kedua:

Buka file application/config/hooks.php kemudian tambahkan line berikut

$hook['display_override'][] = array(
  'class' => '',
  'function' => 'compress',
  'filename' => 'compress.php',
  'filepath' => 'hooks'
  );

Langkah ketiga:

Buat file di -> application/hooks/compress.php

kemudian tambahkan script berikut

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function compress()
{
    ini_set("pcre.recursion_limit", "16777");
    $CI =& get_instance();
    $s1= $CI->uri->segment(1);
    $s2= $CI->uri->segment(2);
    $buffer = $CI->output->get_output();

    if ($s1=='admin'){
        
        $CI->output->set_output($buffer);

    }else{

        $buffer = preg_replace('~<!--(?!<!)[^\[>].*?-->~s','',$buffer);

        if ($s1=='blog'&&!empty ($s2)){

            '';
            $script = '|script';

        }else{
            $buffer = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/','',$buffer);
            $script = '';
        }

        $re = '%# Collapse whitespace everywhere but in blacklisted elements.
        (?>             # Match all whitespans other than single space.
          [^\S ]\s*     # Either one [\t\r\n\f\v] and zero or more ws,
        | \s{2,}        # or two or more consecutive-any-whitespace.
        ) # Note: The remaining regex consumes no text at all...
        (?=             # Ensure we are not in a blacklist tag.
          [^<]*+        # Either zero or more non-"<" {normal*}
          (?:           # Begin {(special normal*)*} construct
            <           # or a < starting a non-blacklist tag.
            (?!/?(?:textarea|pre|ins|blockquote'.$script.')\b)
            [^<]*+      # more non-"<" {normal*}
          )*+           # Finish "unrolling-the-loop"
          (?:           # Begin alternation group.
            <           # Either a blacklist start tag.
            (?>textarea|pre|ins|blockquote'.$script.')\b
          | \z          # or end of file.
          )             # End alternation group.
        )  # If we made it here, we are not in a blacklist tag.
        %Six';

        $new_buffer = preg_replace($re, " ", $buffer);

        
        if ($new_buffer === null)
        {
                $new_buffer = $buffer;
        }

        $CI->output->set_output($new_buffer);
    }

    $CI->output->_display();
}

pada script tersebut kami sudah menambahkan script untuk remove html comment dan javascript comment

tetapi saya menemukan permasalahan pada script tersebut apabila kalian mempunya link seperti dibawah ini

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

dapat dirubah menjadi

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

silahkan mencoba.. apabisa ada permasalahan silakan comment di bawah yaa.. sebisa mungkin kami akan coba membantu untuk fix permasalahnya

terima kasih.. jangan lupa subscribe kreazy.id di browser kesayangan anda

 

#codeigniter #php #html #javascript