【问题标题】:How can I minify my index.php using grunt?如何使用 grunt 缩小我的 index.php?
【发布时间】:2015-07-23 08:23:18
【问题描述】:

我想用 grunt 实现这个目标


目标

  • 我的主要目标是在将 index.php 放入生产服务器之前缩小它们。
  • 如果我有 1 个 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 来实现这一点,但如果有人对更好的解决方案有其他建议,请随时向我提出建议。

【问题讨论】:

  • 请不要标记我的帖子,他没有使用 grunt。我是。
  • 您可以将php输出写入模板并缩小它吗?
  • 你介意回答一下教我怎么做吗?
  • 你好,你可以像ob_start(); include 'index.php'; file_put_contents('index.html', ob_get_clean());这样使用php函数ob_start()然后使用index.html来缩小
  • 用上面的代码创建 php 文件,然后像grunt-tasks.com/grunt-exec 这样在调用 php 文件的地方添加 exec 任务来创建 index.html 文件

标签: php html optimization gruntjs minify


【解决方案1】:

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

  • 将所有 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'

    }
}

第二步

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

};

结果

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

【讨论】:

    【解决方案2】:

    如果你想从 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 中执行的操作,您能否添加一点但更多的展示,我们将如何在 grunt 配置中完成此操作。如果你能用它来更新你的答案,那就太好了。
    • 非常感谢您根据我的要求更新您的答案,我看到了。嘿 !你觉得我现在的回答怎么样?你喜欢吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2013-01-25
    • 2014-11-28
    • 1970-01-01
    相关资源
    最近更新 更多