如何使用grunt缩小index.php?

时间:2022-01-19 11:19:17

I want to achieve this using grunt

我想用grunt实现这个目标


Goal

  • My main goal is to minify my index.php before placing them on my production server.
  • 我的主要目标是在将index.php放入生产服务器之前将其缩小。

  • It's simple if I have 1 single index.html, but I don't.
  • 如果我有一个单独的index.html,这很简单,但我没有。

  • Instead I have an index.php full with other .php files.
  • 相反,我有一个index.php与其他.php文件完整。

  • Each <?php ?> section is a block of HTML code.
  • 每个 部分都是一段HTML代码。


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.

我想知道将所有.php文件连接成一个php文件的最有效方法是什么,并将其缩小。

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

我更喜欢使用grunt实现这一点,但如果有人可能对更好的解决方案有其他建议,请随时建议我。

2 个解决方案

#1


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

如果你想从php创建html文件,你可以使用PHP ob_start()函数。所以你创建PHP文件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 )

然后在GRUNT中创建exec任务来调用php2html.php脚本(阅读更多关于exec任务的信息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

并在最后minify创建index.html

#2


I accomplished this in 2 simple steps:

  • concat all the php file into 1 long php file
  • 将所有php文件汇总到1个长php文件中

  • minify that long php file
  • 缩小那个长的php文件


Step1

using : grunt-contrib-concat

使用: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

使用: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.

它不会很有趣,如果我没有向你们展示结果。就这个。

如何使用grunt缩小index.php?

#1


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

如果你想从php创建html文件,你可以使用PHP ob_start()函数。所以你创建PHP文件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 )

然后在GRUNT中创建exec任务来调用php2html.php脚本(阅读更多关于exec任务的信息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

并在最后minify创建index.html

#2


I accomplished this in 2 simple steps:

  • concat all the php file into 1 long php file
  • 将所有php文件汇总到1个长php文件中

  • minify that long php file
  • 缩小那个长的php文件


Step1

using : grunt-contrib-concat

使用: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

使用: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.

它不会很有趣,如果我没有向你们展示结果。就这个。

如何使用grunt缩小index.php?