code-8
code-8

Reputation: 58662

How can I minify my index.php using grunt?

I want to achieve this using grunt


Goal


index.php

<!DOCTYPE html>

<?php include '2015/layouts/ie.php'; ?>

<head>
  <?php include '2015/layouts/meta.php'; ?>
  <title>Title</title>
  <?php include '2015/layouts/link.php'; ?>
  <?php include '2015/layouts/style.php'; ?>
  <?php include '2015/layouts/ie9.php'; ?>
</head>


<body >

  <span id="web">
    <?php include '2015/layouts/nav-bar.php'; ?>
    <?php include '2015/layouts/welcome-sign.php'; ?>
    <?php include '2015/layouts/profile.php'; ?>
    <?php include '2015/layouts/skill.php'; ?>
    <?php include '2015/layouts/education.php'; ?>
    <?php include '2015/layouts/experience.php'; ?>
    <?php include '2015/layouts/portfolio.php'; ?>
    <?php include '2015/layouts/contact.php'; ?>
    <?php include '2015/layouts/script.php'; ?>
  </span>

  <span id="print" style="display: none;" ><img src="2015/img/image.png" width="90%"></span>

</body>


</html>

Lastly

I'm wondering what is the most efficient way to concatenate all my .php files into one php file, and minify it.

I prefer to achieve this using grunt, but if someone might have other suggestion on a better solution please feel free to suggest me.

Upvotes: 2

Views: 3258

Answers (2)

code-8
code-8

Reputation: 58662

I accomplished this in 2 simple steps:

  • concat all the php file into 1 long php file
  • minify that long php file

Step1

using : grunt-contrib-concat

concat: {

    php: {

        src: [

            '2015/layouts/ie.php',
            '2015/layouts/meta.php',
            '2015/layouts/link.php',
            '2015/layouts/style.php',
            '2015/layouts/ie9.php',
            '2015/layouts/nav-bar.php',
            '2015/layouts/welcome-sign.php',
            '2015/layouts/profile.php',
            '2015/layouts/skill.php',
            '2015/layouts/education.php',
            '2015/layouts/experience.php',
            '2015/layouts/portfolio.php',
            '2015/layouts/contact.php',
            '2015/layouts/script.php'

        ],

        dest: 'dist/php/concat.php'

    }
}

Step2

using : grunt-contrib-htmlmin

htmlmin: {

    dist: {
        options: {
            removeComments: true,
            collapseWhitespace: true
        },

        tasks: ['clean:php'],
        files: {
            'index.php': 'dist/php/concat.php',
        }
    }
}

Final grunt.initConfig() should look like

grunt.initConfig({

        concat: {

            php: {

                src: [

                    '2015/layouts/ie.php',
                    '2015/layouts/meta.php',
                    '2015/layouts/link.php',
                    '2015/layouts/style.php',
                    '2015/layouts/ie9.php',
                    '2015/layouts/nav-bar.php',
                    '2015/layouts/welcome-sign.php',
                    '2015/layouts/profile.php',
                    '2015/layouts/skill.php',
                    '2015/layouts/education.php',
                    '2015/layouts/experience.php',
                    '2015/layouts/portfolio.php',
                    '2015/layouts/contact.php',
                    '2015/layouts/script.php'

                ],

                dest: 'dist/php/concat.php'

            }
        },

htmlmin: {

    dist: {
        options: {
            removeComments: true,
            collapseWhitespace: true
        },

        tasks: ['clean:php'],
        files: {
            'index.php': 'dist/php/concat.php',
        }
    }
},

    });


    // Load NPM Tasks
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');

    // Default
    grunt.registerTask('default', ['concat','htmlmin']);

};

Result

It will not be fun, If I don't show you guys the result. Here is it.

enter image description here

Upvotes: 3

dyachenko
dyachenko

Reputation: 1212

if you want to create html file from php you can use PHP ob_start() function. So you create PHP file php2html.php

php2html.php

<?php
    ob_start();
    include 'index.php'; 
    file_put_contents('index.html', ob_get_clean());

then create exec task in GRUNT to call php2html.php script (read more about exec task https://github.com/jharding/grunt-exec )

Gruntfile.js

module.exports = function(grunt) {

    grunt.loadNpmTasks('grunt-exec');

    grunt.initConfig({
        exec: {
            php2html: {
                cmd: 'php php2html.php'
            }
        }
    });

    grunt.registerTask('default', ['exec:php2html']);
};

package.json

{
  "name": "test",
  "version": "0.0.0",
  "description": "",
  "main": "Gruntfile.js",
  "dependencies": {
    "grunt": "~0.4.5",
    "grunt-exec": "~0.4.6"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "BSD-2-Clause"
}

and at the last minify created index.html

Upvotes: 1

Related Questions