如何缩小和/或压缩 php 文件中的 JavaScript?
我正在研究优化选项,在检查了这些问题后,我不太明白我想要做的事情的答案。希望这并不表明我所做的事情是一种不好的做法!
我有一个 Intranet 应用程序,它通过 ajax 调用 php 文件来加载页面内容。许多 php 文件混合了 php、JavaScript,甚至一些 HTML,特定于它们加载到主界面中的界面功能。我想知道如何缩小或压缩这些文件。有没有办法做到这一点,或者我因为混合语言而陷入困境?
更新:关于接受的答案:
我已经接受了 Wildpeaks 的答案,因为我认为它最接近地回答了我原来的问题。然而,这是我希望能够接受两个答案的时候之一,因为我认为 Igor Zinov'yev 提供的答案可能给了我一个更重要的设计决策来考虑。因此,我对他的回答给予+1,我想其他人也会这么做。希望这是有道理的,并且符合 SO 规则。
I am looking at optimization options, and after checking SO questions, I don't quite see an answer for what I am trying to do. Hopefully that doesn't indicate that what I am doing is a bad practice!
I have an intranet application that loads page content via ajax calls to php files. A lot of the php files have a mixture of php, JavaScript, even some HTML, specific to the interface functionality that they load into the main interface. I was wondering about minifying or compressing these files. Is there a way to do it, or am I stuck because I have mixed languages?
Update: Concerning accepted answer:
I have accepted wildpeaks answer because I think it most closely answers my original question. However, this is one of those times when I wish I could accept two answers because I think the answer Igor Zinov'yev provided has given me perhaps a more important design decision to think about. For that reason I have given a +1 to his answer, as I imagine others will too. Hope that makes sense and is within the SO rules.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 PHP 脚本生成 Javascript 代码,因此它可以在输出之前缩小代码:在变量中生成代码,然后将该变量传递给缩小器,然后才输出到浏览器。
这是一个用于此目的的 PHP 库。
Your PHP script generates the Javascript code, so it can minify the code before outputting it: generate the code in a variable, then pass that variable to the minifier, and only then output to the browser.
Here's a PHP library for that.
您的优化是从错误的方向开始的。显然,如果您的 PHP 文件中有硬编码的 JavaScript、HTML 和其他内容,那么您确实需要重构代码。但即使不这样做,也不应该缩小代码,因为这样会更难维护。
把它从那里拉出来,从小步骤开始,你最终会到达那里。
更新:我想回复评论,但决定在这里详细说明为什么我这样回答你的问题。
我在这里谈论的是关注点分离。服务器端代码文件不适合放置客户端代码。到目前为止,我所见过的所有解决方案迟早都会变成无法维护的混乱。
如果您想返回一段 HTML 代码,请将其放入模板中,并为模板提供特定于当前情况的变量。您可以使用 Smarty 来做到这一点。通过这种方式,您可以获得以下好处:
稍后,当您将逻辑与表示分离时,您可能会发现不需要发送带有 HTML 片段的 JavaScript 代码。也许您将创建一个 JS 引擎(您将在构建时缩小)并且只需要在加载时触发某些事件。
You are starting your optimization off the wrong end. Obviously if you have hard-coded JavaScript, HTML and whatever else inside your PHP files, you seriously need to refactor the code. But even if you don't, you shouldn't minify the code in place because it would be even harder to maintain.
Pull it out of there, start with small steps, and you will get there eventually.
UPDATE: I thought of replying with a comment, but instead decided to elaborate on why I answered your question this way here.
I'm talking here about separation of concerns. Your server-side code files are no place for the client-side code. All solutions that do this that I have seen so far sooner or later turn into an unmaintainable mess.
If you want to return a piece of HTML code, put it into a template and supply the template with variables that are specific for this current situation. You can do that with Smarty. This way you get among others the following benefits:
Later on when you separate logic from presentation maybe you will find that you don't need to send JavaScript code with HTML snippets. Maybe you will create a single JS engine (that you will minify on build) and will only have to trigger certain events upon load.