dongseshu0698 2014-01-12 21:10
浏览 61
已采纳

如何指示grunt HTMLmin忽略PHP标记

I am using HTMLmin in my grunt.js workflow to minify my HTML.

I have a few files that also include PHP markup. For example:

<a href="<?php echo //code ?>">link</a>

HTMLmin doesn't parse this correctly, and instead throws an exception error:

Warning: [filename]
Parse Error: <a href="<?php echo //code ?>">link</a>
Aborted due to warnings.

Is there anyway to instruct HTMLmin to ignore PHP markup? I've looked through the docs but don't see any obvious answer.

  • 写回答

1条回答 默认 最新

  • dongwen7187 2014-06-30 17:16
    关注

    The world of modern JS doesn't take into account running in a subdir or templates that go through any kind of server side manipulation. The trick would be to inject the PHP after grunt minifies with some kind of data-* attribute. I haven't developed a solution I'm fully happy with. But, you can try some grunt regex tasks to do something like this

    <a data-phphref="<?= $templatePrefix;?>" data-phpclass="<?= $activeLink;?>" href="/foo/">Foo</a>
    

    after custom grunt task

    <a href="<?= $templatePrefix;?>/foo/" class="<?= $activeLink;?>">Foo</a>
    

    Or you can do full DOM manipulation, which would probably be the better long term choice.

    Update

    I guess there's a 3rd option, and that would be to do token replacement to make regex easier. You can watch your source template files in a directory like ui-src/ When a change happens, run a simple search/replace for tokens

    <a href="{{templatePrefix}}/foo/">Foo</a>
    

    Output to a template directory that PHP is configured to use. This should allow you to minify like

    <!-- build:js {{templatePrefix}}scripts/plugins.js -->
    <script type="application/javascript" src={{templatePrefix}}components/bootstrap/js/affix.js"></script>
    <!-- endbuild -->
    

    With a grunt task like:

          "string-replace": { 
            template: { 
                files: { "./": "<%= yeoman.dist %>/*.html.php"},
                options: { 
                  replacements:[ { 
                      pattern: /{{templatePrefix}}/,
                      replacement: "<?= $templatePrefix;?>"
                  }
                ]
                }
            },
    

    A bonus would be to put the PHP code snippets into a JS config file so your PHP code isn't littered throughout your Gruntfile.js

    2nd Update

    The preferred way to do this is have grunt-usemin inject the PHP code during the build. The recently released usemin 2.3.0 has this ability with blockReplacements.

    feature request: https://github.com/yeoman/grunt-usemin/pull/337 commit: https://github.com/yeoman/grunt-usemin/commit/83f6821a30020cbc9395d7257e0276cff142e219

    Basically, you can't make grunt ignore PHP, but you can make it work with PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料