我怎样才能融为一体preSS / GZIP我发布到AWS S3之前mimified的.js和.css文件? [英] How can I compress / gzip my mimified .js and .css files before publishing to AWS S3?

查看:223
本文介绍了我怎样才能融为一体preSS / GZIP我发布到AWS S3之前mimified的.js和.css文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑了谷歌pagespeed并提出COM pressing我的.js和的CSS

I ran Google pagespeed and it suggests compressing my .js and .css

消除渲染拦截JavaScript和CSS在上面的倍量 显示如何修复

Eliminate render-blocking JavaScript and CSS in above-the-fold content Show how to fix

Enable compression
Compressing resources with gzip or deflate can reduce the number of bytes sent over the network.
Enable compression for the following resources to reduce their transfer size by 210.9KiB (68% reduction).
Compressing http://xx.com/content/bundles/js.min.js could save 157.3KiB (65% reduction).
Compressing http://xx.com/content/bundles/css.min.css could save 35.5KiB (79% reduction).
Compressing http://xx.com/ could save 18.1KiB (79% reduction).

在我发表我有一个使用Windows PowerShell来移动一个.js和的CSS mimified捆绑到S3,这去CloudFront的一个步骤。

During my publish I have a step that uses Windows Powershell to move a .js and .css mimified bundle to S3 and this goes to cloudfront.

有一些步骤,我可以添加在PowerShell脚本是将COM preSS的的.js和.css文件?

Is there some step I could add in the PowerShell script that would compress the .js and .css files?

此外,一旦文件COM pressed然后做我必须做什么比更改名称,以便告诉我的浏览器,这将需要尝试和接受gzip文件?

Also once the files are compressed then do I have to do anything more than change the name so as to tell my browser that it will need to try and accept a gzip file?

推荐答案

您可以添加到您上传脚本所需的code为gzip COM preSS的文件。

You can add to your upload script the needed code to gzip compress the files.

一些例如code可能是这样的:

Some example code could be this:

function Gzip-FileSimple
{
    param
    (
        [String]$inFile = $(throw "Gzip-File: No filename specified"),
        [String]$outFile = $($inFile + ".gz"),
        [switch]$delete # Delete the original file
    )

    trap
    {
        Write-Host "Received an exception: $_.  Exiting."
        break
    }

    if (! (Test-Path $inFile))
    {
        "Input file $inFile does not exist."
        exit 1
    }

    Write-Host "Compressing $inFile to $outFile."

    $input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)

    $buffer = New-Object byte[]($input.Length)
    $byteCount = $input.Read($buffer, 0, $input.Length)

    if ($byteCount -ne $input.Length)
    {
        $input.Close()
        Write-Host "Failure reading $inFile."
        exit 2
    }
    $input.Close()

    $output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None)
    $gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Compress)

    $gzipStream.Write($buffer, 0, $buffer.Length)
    $gzipStream.Close()

    $output.Close()

    if ($delete)
    {
        Remove-Item $inFile
    }
}

从这个网站: Gzip已建立在PowerShell中

这篇关于我怎样才能融为一体preSS / GZIP我发布到AWS S3之前mimified的.js和.css文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆