用于开发和生产的(非)缩小的 js/css 文件的工作流程

发布于 2024-11-06 19:58:08 字数 205 浏览 2 评论 0原文

我正在寻找一种方法来构建我的工作流程,以便在使用“未压缩”的 js/css 文件进行开发和使用缩小的文件进行生产时不会感到困惑/陷入麻烦。

我不想拥有同一源的两个 html 版本(一个用于开发,一个用于缩小的 js/css 文件)。或者我必须这样做?

另外,自动化实际缩小过程的最佳方法是什么?

注意:我正在寻找本地解决方案。服务器端不是一个选项。

I'm looking for a way to structure my workflow so I don't get confused/into trouble when working with "uncompressed" js/css files for development and minified ones for production.

I don't want to have two html versions (one with development and one with minified js/css files) of the same source. Or do I have to?

Also whats the best way to automate the actual minify process?

NOTE: I'm looking for a local solution. Server side is not an option.

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

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

发布评论

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

评论(3

沧桑㈠ 2024-11-13 19:58:08

我一直在 PHP 中使用它 - 你可能会用它来获得灵感:

<?
$test_server = $_SERVER['SERVER_NAME'] == "127.0.0.1" || $_SERVER['SERVER_NAME'] == "localhost" || substr($_SERVER['SERVER_NAME'],0,3) == "192";

function caching_headers ($timestamp) {
global $test_server;    
    if (!$test_server) {
        $gmt_mtime = gmdate('r', $timestamp);

        if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
            if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $gmt_mtime) {
                header('HTTP/1.1 304 Not Modified');
                exit();
            }
        }

        header('Last-Modified: '.$gmt_mtime);       
    }
}


header ("Content-Type: application/javascript; charset=utf-8");

include ($_SERVER['DOCUMENT_ROOT']."/media/js/jsmin.php");

$libs = explode("|",$_GET['libs']);

$uniq_string = "";

foreach ($libs as $lib) {   
    $uniq_string .= filemtime($_SERVER['DOCUMENT_ROOT']."/media/js/$lib.js");
}

$hash = md5($uniq_string);

$cachefile = $_SERVER['DOCUMENT_ROOT']."/cache/".$hash.".js";

if(file_exists($cachefile)) {
    $last_mod = filemtime($cachefile);

    caching_headers ($last_mod);
    include($cachefile);
    echo "//Cached on ".gmdate('r', $last_mod)." to ".$hash;
    exit;
} else {
    $combined = "";

    foreach ($libs as $lib) {   
        if (substr($lib, strlen($lib)-3, 3) == "min") {
            $combined .= file_get_contents($_SERVER['DOCUMENT_ROOT']."/media/js/$lib.js")."\n";
        } else {
            $combined .= JSMin::minify(file_get_contents($_SERVER['DOCUMENT_ROOT']."/media/js/$lib.js"))."\n";          
        }
    }

    $fp = fopen($cachefile, 'w'); 
    fwrite($fp, $combined);
    fclose($fp);

    $last_mod = filemtime($cachefile);

    caching_headers ($last_mod);    
    include($cachefile);
    echo "//Cached on ".gmdate('r', $last_mod)." to ".$hash;
}

?>

JSMin-php 一起使用。

使用:

<script src="/media/js/combined.php?libs=jquery-1.5.1.min|behaviour|jquery.form"></script>

然后我在我的页面中

。它将缓存的缩小文件存储在 /cache/ 中,因此如果您尝试执行此操作,请确保该文件夹存在。

I've been using this in PHP – you might use it for inspiration:

<?
$test_server = $_SERVER['SERVER_NAME'] == "127.0.0.1" || $_SERVER['SERVER_NAME'] == "localhost" || substr($_SERVER['SERVER_NAME'],0,3) == "192";

function caching_headers ($timestamp) {
global $test_server;    
    if (!$test_server) {
        $gmt_mtime = gmdate('r', $timestamp);

        if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
            if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $gmt_mtime) {
                header('HTTP/1.1 304 Not Modified');
                exit();
            }
        }

        header('Last-Modified: '.$gmt_mtime);       
    }
}


header ("Content-Type: application/javascript; charset=utf-8");

include ($_SERVER['DOCUMENT_ROOT']."/media/js/jsmin.php");

$libs = explode("|",$_GET['libs']);

$uniq_string = "";

foreach ($libs as $lib) {   
    $uniq_string .= filemtime($_SERVER['DOCUMENT_ROOT']."/media/js/$lib.js");
}

$hash = md5($uniq_string);

$cachefile = $_SERVER['DOCUMENT_ROOT']."/cache/".$hash.".js";

if(file_exists($cachefile)) {
    $last_mod = filemtime($cachefile);

    caching_headers ($last_mod);
    include($cachefile);
    echo "//Cached on ".gmdate('r', $last_mod)." to ".$hash;
    exit;
} else {
    $combined = "";

    foreach ($libs as $lib) {   
        if (substr($lib, strlen($lib)-3, 3) == "min") {
            $combined .= file_get_contents($_SERVER['DOCUMENT_ROOT']."/media/js/$lib.js")."\n";
        } else {
            $combined .= JSMin::minify(file_get_contents($_SERVER['DOCUMENT_ROOT']."/media/js/$lib.js"))."\n";          
        }
    }

    $fp = fopen($cachefile, 'w'); 
    fwrite($fp, $combined);
    fclose($fp);

    $last_mod = filemtime($cachefile);

    caching_headers ($last_mod);    
    include($cachefile);
    echo "//Cached on ".gmdate('r', $last_mod)." to ".$hash;
}

?>

alongside JSMin-php.

I then use:

<script src="/media/js/combined.php?libs=jquery-1.5.1.min|behaviour|jquery.form"></script>

in my pages.

It stores the cached minified file at /cache/, so make sure that folder exists if you are trying this.

或十年 2024-11-13 19:58:08

目前最好的解决方案是 HTML5 样板构建脚本

请注意,在能够使用完整的功能之前有一个学习曲线。

另外值得一提的是,构建脚本针对网站进行了优化,每个页面都使用相同的 JavaScript 和 CSS 文件。因此,如果您有某些页面,其中包含您想要优化/缩小的其他 CSS 和 JavaScript 文件,您可能需要单独执行此操作。

该脚本还压缩 HTML 并(可选)保持 PHP 内容不变。

HTML5 样板构建脚本非常棒。它是开源的,请贡献!

注意:我的大部分信息都是 3 个多月前的。让我了解新的进展。

Currently the best solution is the HTML5 boilerplate build script.

Beware that there is a learning curve before being able to use the complete power.

Also it's worth mentioning, that the build script optimized for websites, where every page uses the same JavaScript and CSS files. So if you have certain pages, that have additional CSS and JavaScript files that you want to have optimized/minified you might need to do this separately.

The script also compresses HTML and (optionally) leaves PHP stuff untouched.

HTML5 boilerplate build script is awesome. It's open source, please contribute!

Note: Most of my infos are 3+ month old. Let me know about new developments.

烟─花易冷 2024-11-13 19:58:08

您可以根据 URL 动态注入适当的 js include。本质上,您检查它是否是生产 URL,以及是否包含缩小版本。然后使用 else 分支来处理非生产 URL 并注入开发版本(这样别人就看不到您的暂存 URL)。

You could dynamically inject the appropriate js include depending on the URL. In essence, you check to see if it's the production URL, and if it is include the minified version. Then use an else branch to handle non-production URLs and inject the development version (this way someone can't see your staging URL).

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