如何使用 grunt 缩小我的 index.php?

How can I minify my index.php using grunt?

I want to achieve this using grunt


目标


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 来实现这一点,但如果有人对更好的解决方案有其他建议,请随时向我提出建议。

如果您想从 php 创建 html 文件,您可以使用 PHP ob_start() 函数。 所以你创建 PHP 文件 php2html.php

php2html.php

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

然后在 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"
}

并在最后一次缩小时创建 index.html

我通过两个简单的步骤完成了这个:

  • 将所有 php 文件合并成 1 个长 php 文件
  • 缩小这么长的 php 文件

第一步

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

    }
}

第二步

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

};

结果

如果我不给你们看结果,那就没意思了。在这里。