如何缩小和/或压缩 php 文件中的 JavaScript?

发布于 2024-12-06 16:14:19 字数 417 浏览 0 评论 0原文

我正在研究优化选项,在检查了这些问题后,我不太明白我想要做的事情的答案。希望这并不表明我所做的事情是一种不好的做法!

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2024-12-13 16:14:20

您的 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.

感悟人生的甜 2024-12-13 16:14:20

您的优化是从错误的方向开始的。显然,如果您的 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:

  • No repeating pieces of markup over and over - there are template loops for that
  • A possibility to re-use existing templates in several places
  • Developers working with templates do not need to get into your server-side code
  • And your server-side code gets cleaner, smells nice too!

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文