缩小嵌入PHP代码的JS文件 [英] Minify JS files embedded with PHP code

查看:124
本文介绍了缩小嵌入PHP代码的JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google和多个Github存储库中进行长时间搜索后,我找不到任何可靠的内容(我发现是HTML而不是JS)

例如,如果我有文件example.js.php

,则我用于JS文件的缩小器会弄乱我的PHP代码.

<?php header("Content-Type: application/javascript");
$var1 = $_GET['f'];
?>

//some JS and PHP code

例如,我使用的JS缩小器将$_GET['f']转换为$_GET.f;对于JS对象来说是可以的,但对于PHP而言则不是.

关于如何压缩JS代码并使PHP完好无损的任何想法?

解决方案

缩小器正试图做出几乎不可能的决定,将它们嵌入PHP确实可以增进友谊.

您应该分别缩小JavaScript,然后将其包含或通过PHP发送.

两种可能性是

<?php
    header('Content-Type: application/javascript');
    require_once 'script.min.js';
?>

如果合适的话,或者

<?php
    header('Content-Type: application/javascript');
    print file_get_contents('script.min.js');
?>

取决于您尝试混合它们的原因.

After long search in Google and on several Github repositories, I couldn't find anything solid (I found for HTML, not for JS)

The minifiers I use for JS files mess with my PHP code, for example, if I have the file example.js.php

<?php header("Content-Type: application/javascript");
$var1 = $_GET['f'];
?>

//some JS and PHP code

For example, the JS minifiers I use, convert $_GET['f'] to $_GET.f; which is OK for a JS object, but not for PHP.

Any ideas on how to compact the JS code leaving the PHP intact?

解决方案

Minifiers are trying to make nearly impossible decisions, and embedding them in PHP is really stretching the friendship.

You should minify the JavaScript separately, and then included it or send it through the PHP.

Two possibilities are:

<?php
    header('Content-Type: application/javascript');
    require_once 'script.min.js';
?>

if that is appropriate, or

<?php
    header('Content-Type: application/javascript');
    print file_get_contents('script.min.js');
?>

depending on why you’re trying to mix them.

这篇关于缩小嵌入PHP代码的JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆