繁体   English   中英

如何使用grunt缩小index.php?

[英]How can I minify my index.php using grunt?

我想用咕unt声来实现


目标

  • 我的主要目标是在将index.php放到生产服务器上之前使其最小化。
  • 如果我只有1个index.html ,这很简单,但是我没有。
  • 相反,我有一个充满其他.php文件的index.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个简单的步骤完成了此操作:

  • 将所有php文件合并为1个长php文件
  • 缩小那个长的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']);

};

结果

如果我不告诉大家结果,那将不会很有趣。 就这个。

在此处输入图片说明

如果要从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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM