如何使用grunt缩小index.php?

时间:2015-05-12 12:43:06

标签: php html optimization gruntjs minify

  

我想用grunt实现这个目标

目标

  • 我的主要目标是在将index.php放到生产服务器上之前将其缩小。
  • 如果我有一个单index.html,那很简单,但我不会。
  • 相反,我的 index.php 已完整包含其他.php个文件。
  • 每个<?php ?>部分都是一段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>

最后

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

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

2 个答案:

答案 0 :(得分:2)

我通过两个简单步骤完成了这项工作:

  • 将所有php文件连接成1个长文件
  • 缩小那个长php文件

步骤1

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

    }
}

步骤2

使用: grunt-contrib-htmlmin

htmlmin: {

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

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

最终grunt.initConfig()应该看起来像

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']);

};

结果

这不会很有趣,如果我不向你们展示结果。就是这样。

enter image description here

答案 1 :(得分:1)

如果你想从php创建html文件,你可以使用PHP <?php ob_start(); include 'index.php'; file_put_contents('index.html', ob_get_clean()); 函数。 所以你创建PHP文件php2html.php

<强> php2html.php

module.exports = function(grunt) {

    grunt.loadNpmTasks('grunt-exec');

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

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

然后在GRUNT中创建 exec 任务来调用php2html.php脚本(了解有关exec任务https://github.com/jharding/grunt-exec的更多信息 )

<强> Gruntfile.js

{
  "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"
}

<强>的package.json

brew uninstall --force php56

brew update

brew tap --repair

find $(brew --cache) -mindepth 1 -print -delete

brew install -v php56

并在最后一次minify创建index.html