简体   繁体   English

版本控制:如何在环境之间控制css和js压缩/缩小版本

[英]version control: how to control css and js compressed/minified versions between environments

I am using git (via GitHub) for version control on my projects. 我正在使用git(通过GitHub)对我的项目进行版本控制。 I'm still new to this but I'd like to know best practice for how to keep my css and js files synchronized between environments. 我还是新手,但我想了解如何让我的cssjs文件在环境之间保持同步的最佳实践。

Example: Let's say I write a js script on dev. 示例:假设我在dev上编写了一个js脚本。 I'm happy with my work and I push to testing. 我很满意我的工作,我推动测试。 Well on testing I would want a minified/compressed version. 在测试时,我想要一个缩小/压缩版本。 How would I accomplish that without a lot of overhead tasking? 如果没有大量的开销任务,我将如何实现这一目标? What do you guys do? 你们做什么的? I'm assuming it's part of some sort of deploy script that would compress the code and push it to whatever environment I specify. 我假设它是某种部署脚本的一部分,它会压缩代码并将其推送到我指定的任何环境。

This brings up another question: What about my header (and/or footer ) file(s) in my project? 这提出了另一个问题:我的项目中的header (和/或footer )文件怎么样? If my dev has: 如果我的dev有:

<link rel="stylesheet" href="<?php echo base_url(); ?>css/main.css">

and my testing has: 我的testing有:

<link rel="stylesheet" href="<?php echo base_url(); ?>css/main.min.css">

That's all fine, but what if I need to make changes to my header? 这一切都很好,但如果我需要更改标题怎么办? How would I separate all these things from each other? 我如何将所有这些东西彼此分开? If I make changes to my header and push to testing or production I would lose the .min from that include line. 如果我对标题进行更改并推送到测试或生产,我将从该包含行中丢失.min

Currently what I do to deploy updates is just a simple git pull origin [branch] from the command line inside the environment I want to update. 目前,我所做的部署更新只是一个简单的git pull origin [branch]来自我想要更新的环境中的命令行。

Again, I'm looking for best practice, whatever learning it requires. 再一次,我正在寻找最佳实践,无论学习它需要什么。 Thanks! 谢谢!

You might want to check out preprocessor tools, such as LESS or Sass. 您可能想要查看预处理器工具,例如LESS或Sass。 These tools allow you to write CSS (I believe they may be able to handle JS, too, for purposes of minifying), and set up scripts that handle how they compile the code, based on the environment. 这些工具允许您编写CSS(我相信他们也可以为了缩小目的而处理JS),并根据环境设置脚本来处理编译代码的方式。

What you'd do, then, is write your code in "source" files, and set up the preprocesser to compile the code according to settings laid out in a settings file (for Sass, this is easily done with the Compass framework), based on the environment you're in. You'd then keep only the source files in the repository (set Git to ignore the compiled versions), and set up post-receive hooks to compile the source files on the server. 那么,你要做的就是在“源”文件中编写你的代码,并设置preprocesser来根据设置文件中的设置编译代码(对于Sass,这可以通过Compass框架轻松完成),根据你所处的环境。然后你只保留存储库中的源文件(设置Git忽略已编译的版本),并设置post-receive钩子来编译服务器上的源文件。 Your HTML can then be written to access the compiled files (which should have the same name across environments), so you don't have to write logic that determines on the fly, every time, what environment the code is running in. 然后可以编写HTML来访问已编译的文件(这些文件应该在不同的环境中具有相同的名称),因此您不必编写即时确定代码运行环境的逻辑。

Typically a minified file is generated by your CMS on page load. 通常,CMS会在页面加载时生成缩小文件。 So from a code standpoint you don't need to track the minified version as all the code is tracked in your actual js and css files. 因此,从代码的角度来看,您不需要跟踪缩小版本,因为所有代码都在实际的js和css文件中进行跟踪。 So minified copies can just be ignored using the .gitignore file. 因此,使用.gitignore文件可以忽略缩小的副本。

My .gitignore file typically looks like: 我的.gitignore文件通常如下所示:

css-min            #directory to store generated minified css files
js-min             #directory to store generated minified js files
tmp                #directory to store temporary files
files/images/cache #directory for storing generated images such as thumbnails
settings.php       #File that stores system variables.

The settings file is used to set global variables such as your platform like "dev", "staging", "production". 设置文件用于设置全局变量,例如您的平台,如“dev”,“staging”,“production”。 Then in your other files you can check the platform as to which css/js files to use. 然后在您的其他文件中,您可以检查平台上要使用的css / js文件。 Since that file is ignored by your repository you can make the settings specific to each platform. 由于存储库会忽略该文件,因此您可以针对每个平台进行特定设置。

if ($GLOBAL['platform'] = PLATFORM_DEV) {
  $path = 'css/main.css';
}
elseif ($GLOBAL['platform'] = PLATFORM_STAGE) {
  $path = 'css-min/main.min.css';
}

<link rel="stylesheet" href="<?php print base_url(); print $path; ?>">
  1. Don't put minified version of CSS, JS into version control. 不要将缩小版的CSS,JS放入版本控制中。 That's duplicate. 那是重复的。

  2. Git can be used on delopy but its purpose is not deploy. Git可以在delopy上使用,但其目的不是部署。

  3. For the including CSS tags, that's easy. 对于包含CSS标签,这很容易。 A quick roundup is use your framework's env vairable. 快速综述是使用您的框架的env vairable。 As I know CodeIgniter has this function. 据我所知CodeIgniter有这个功能。 If env == test, include minified version, if not, include raw versions. 如果env == test,则包括缩小版本,如果不包括,则包括原始版本。

Besides you need a build script or framework plugin to generate minified versions automatically. 此外,您需要一个构建脚本或框架插件来自动生成缩小版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM