使用压缩文件进行 Web 部署

发布于 2024-09-19 10:46:19 字数 837 浏览 8 评论 0原文

简短版本

当使用 YUI 压缩器等文件压缩器时,部署网站的正确步骤是什么,这样您就不必在开发期间弄乱压缩文件?发布过程中是否应该有压缩脚本?

长版本

我刚刚加入了一个项目,我们使用 YUI 压缩器来处理 JS 和 CSS 文件。我遇到了一些对我来说似乎很难闻的代码块。我想知道是否有更好的方法来做到这一点。

我认为在编写代码时,开发人员应该可以自由地使用未压缩的文件,并且让网站仍然可以工作并反映他们的更改。然后可以在发布时压缩更改后的 css 和 js 文件。这里的情况并非如此。

基本上,我们的核心 php 页面会对 PHP_SAPI 进行检查,如果是命令行,则运行压缩器。这包括在 git log 上运行 exec,然后使用一些 sed 魔法来获取当前修订号(用作 css 和 js)文件版本),然后 proc_open yuicompressor,在文件上运行它,再执行几次 exec 将新文件添加到 git...然后一些巫毒到达到 php 文件本身(是的,它是自我修改的)并将 $version 变量声明更改为新的修订号。此变量用于包含正确的 css 和 js 文件。

当我第一次从我们的存储库中检查代码时,该网站在本地运行,但没有 CSS。一位同事告诉我,我需要运行一个调用上面咒语的 shell 脚本。这花了大约半个小时调整文件权限才成功。

这味道有我想象的那么难闻吗?我们怎样才能摆脱这个开发步骤,并在开发过程中只包含常规的未压缩文件?现在我无法对上述文件进行更改并立即查看它们。在发布时执行此操作的问题是,在压缩文件时,包含内容必须更改为压缩文件。看起来这也需要修改代码的脚本。有什么想法吗?

Short Version

When using file compressors like YUI compressor, what is the proper procedure for deploying your website, so that you don't have to mess with compressed files during development time? Should there be a compression script as part of the release process?

Long Version

I just joined a project where we use YUI compressor for our JS and CSS files. I ran into some chunks of code that to me, seemed to smell very badly. I'm wondering if there is a better way to do this.

I think that while working on the code, developers should be free to work with the uncompressed files, and have the website still work and reflect their changes as they go. Then the altered css and js files can be compressed at release time. That's not quite how it goes here.

Basically, on of our core php pages does a check on PHP_SAPI, and if it's command line, runs the compressors. This consists of running an exec on git log and then some sed magic to get the current revision number (to be used as a css and js file version), then proc_opening the yuicompressor, run that on the files, a few more execs to add the new files to git... and then some voodoo reaches into the php file itself (yes, it's self-modifying) and changes a $version variable declaration to the new revision number. This variable is used to include the correct css and js files.

When I first checked the code out of our repo, the website ran locally, but there was no css. I was told by a colleague that I needed to run a shell script that invoked the incantation above. This took about half an hour of tweaking file permissions before it succeeded.

Does this smell as bad as I think it does? How can we get rid of this as a development step and have it possible to just include the regular uncompressed files during development? As it is now I can't make changes to said files and see them immediately. The problem with doing it at release time is that the includes would have to change to the compressed files at the time the files are compressed. It seems like that would require scripts that modify the code as well. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

云柯 2024-09-26 10:46:35

老兄 :) 我可以帮助你 - 我(主要)将 YUI Compressor 移植到 .NET,因为我需要确保.. 当我们部署我们的代码时.. 公共服务器已经缩小/组合了 css 和 js。 ..这可以适用于任何语言/部署场景,尽管我的场景是特定于.NET / TFS的。

诀窍是确保开发过程中源文件完全不被触及。绝不。只有当您将代码从您自己的开发(又名本地主机)计算机推送到其他服务器(即开发/测试/Uat/Live-Production)时,才应该缩小和/或组合 css/js。

在本地开发机器上工作时很难使用缩小和/或组合的 css/js .. 特别是如果你正在处理 UI 体验.. 即。执行 javascript/html/css。

那么我们该怎么办呢?首先,我们需要有一个部署解决方案。对于 .NET,我们使用 TFS 将代码部署到服务器。但是还有很多其他选项..就像我认为 Ruby On Rails 的人可以使用 Rake 并在其中添加 YUICompressor 任务。

有了 TFS,我们就可以进行持续集成.. 因此,每当签入某些代码时,我们的系统或多或少都会执行以下操作(这是本文的关键)。

.. 进入临时位置 ...

  • 获取最新代码
  • 编译最新代码
  • 缩小和/或组合任何 js 或 css .. 这些新创建的文件将进入编译代码的临时位置。
  • 将此代码/网站从此临时位置发布到所需的 Web 服务器(dev/test/uat/prod.. 等)。

该列表中最重要的是所有这一切都发生在临时位置。不超过您当前的开发文件夹。您不想弄乱您的主要源文件。

因此,您是否可以告诉我们

  • 您的代码存储在哪里(源代码)。我想你说的是GIT?
  • 什么语言(php?)
  • ,最重要的是,如何将代码从源代码管理获取到实时服务器?

Dude :) I can help you with this - I (mainly) ported YUI Compressor to .NET because I needed to make sure that .. when we deploy our code .. the public servers had minified/combined css and js. .. This could apply to any language/deployment scenario, despite my scenarios being .NET / TFS specific.

The trick is to make sure that the source files during your development are not touched at all. Never. It's only when you push the code from your own development (aka Localhost) machines to others servers (ie. Dev/Test/Uat/Live-Production) should the css/js be minified and/or combined.

It's too hard to work with minified and/or combined css/js while working on your localhost dev machines .. ESPECIALLY if you're working on the UI experience .. ie. doing javascript/html/css.

So what do we do? First of all, we need to have a deployment solution. For .NET we use TFS to deploy our code to servers. But there's plenty of other options ..like i think Ruby On Rails peeps can use Rake and add a YUICompressor task in there.

With TFS, we have Continuous Integration going on .. so whenever some code is checked in, our system more or less does the following (which is the crux of this post).

.. into a temp location ...

  • Grab latest code
  • Compile latest code
  • Minify and/or combine any js or css .. and these newly created files go into the temp location where the code was compiled.
  • Publish this code/website from this temp location to the desired web server (dev/test/uat/prod.. etc).

What is crucial in that list is the fact that all this is happening in a temp location. Not over your current development folder. You don't want to mess with your main source files.

So if you can tell us

  • where your code is stored (source code). I think you said GIT?
  • what language (php?)
  • and most importantly, how do you get the code from your source control to the live server?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文