自动JavaScript压缩与PowerShell

Automatic JavaScript Compression with PowerShell

本文关键字:PowerShell 压缩 JavaScript 自动      更新时间:2023-09-26

我想写一个脚本,将采取一堆。js文件,压缩它们,然后用新的替换旧的在同一文件夹。我已经尝试了一些方法,但我发现自己不断遇到这样或那样的新问题,所以我想最好是向那些比我更了解的人求助,重新开始。

谁能给我指个正确的方向?

更新:我使用了一组类似于这样的命令:

>Get-ChildItem c:'NewFolder' -recurse |
&java -jar yuicompressor-2.4.6

它似乎不希望允许这些类型的输出使用。我确信有一种方法可以使它工作,但是对于PowerShell来说,我仍然是相当新的,我不太自信我可以自己解决这个问题。

更新:使用下面建议的命令字符串,我可以让powershell给我一个似乎是新压缩的。js的读取,但它不会用压缩的文件替换现有的文件或将其写入我认为在[filename].min.js格式的同一目录下的标准输出。

更新:建议的命令的修改版本似乎可以做到这一点!

>Get-ChildItem c:'NewFolder' -exclude jquery*.js,*min.js -recurse | %{java -jar yuicompressor-2.4.6.jar ($_.fullname) -o ($_.fullname)}

然而,当命令在PowerShell中运行时,奇怪的是,我从PowerShell得到一个关于Java命令的错误消息…

java.exe: At line:4 char:72+ Get-ChildItem c:'Scripts' -exclude jquery*.js,*min.js -recurse | %{java <<<<-jar yuiccompressor -2.4.6.jar ($.fullname)- o ($ .fullname)}+ CategoryInfo: NotSpecified: (:String) [], RemoteException使用方法:java -jar yuiccompressor -x.y.z.jar [options][输入文件]

全球选项-h,——help显示此信息——type输入文件的类型读取输入文件——line-break在指定的列号之后插入一个换行符-v,——verbose显示信息和警告-o将输出放入。默认为stdout。可以使用以下语法处理多个文件:Java -jar yuiccompressor .jar -o '.css$:-min.css' *.cssJava -jar yuiccompressor .jar -o '.js$:-min.js' *.js

JavaScript选项菜单只允许最小化,不允许保留所有分号Disable -optimizations禁用所有微优化

如果没有指定输入文件,默认为stdin。在这种情况下,'type'选项是必需的。否则,'type'选项是必需的只有当输入的文件扩展名既不是'js'也不是'css'时才可以。

你知道PowerShell想告诉我什么吗?

试着这样做:

Get-ChildItem c:'NewFolder' -recurse | %{java -jar yuicompressor-x.y.z.jar $_.fullname}

%{..}foreach-object的别名。从c:'Newfolder(及其子目录)获取一组文件,并将它们作为对象传递给管道中的下一个组件。这部分是一个既不支持管道也不支持对象的外部组件,您可以将它包装在foreach中,并以它可以理解的形式提供文件——文件的全名(其中包括路径)。

这个帖子可能会给你一些答案。你用什么来最小化和压缩JavaScript库?

也就是说,我相信YUI压缩器有一个独立的可执行文件,可以从PowerShell启动。

所以,我想我终于要回馈这个社区了。首先我要说的是,我以前从未使用过Powershell,而且真的不喜欢它。我很高兴bash现在随windows一起发布了。但是,我为一个朋友做了一个有趣的项目,该项目运行在win服务器上,需要编写一些代码,以便在他的。net应用程序的代码部署到他的ec2实例后运行。我知道有十几种更好的方法可以使用真正的构建工具来做到这一点,但是……有时候剧本就是你想要的。希望你们觉得有用。它要求你安装java并拥有闭包jar;您可以尝试使用其他的缩小工具。

我需要在这篇文章中给予Chase Florell信用,让我在正确的方向上开始:如何版本javascript和css文件作为powershell构建过程的一部分?

##################################################################
# minimizeJS.ps1                                                 #
# This file removes all non-minified javascript resources        #
# and then updates all html and aspx pages to point to the       #
# minified js if they are not already.                           #
#                                                                #
# Version 1.0                                                    #
# Usage: minimizeJS.ps1 -debug true|false                        #
##################################################################
param($debug=$true)  #you can remove the =$true to force a user to specify
$maxFiles = Get-ChildItem -Path ./* -Include *.js -Exclude *.min.js, *.min.comp.js -Recurse 
$filesContainingResourceRefs = Get-ChildItem -Path ./* -Include *.html, *.aspx -Recurse 
$fileCollection = New-Object System.Collections.ArrayList
$closureJAR = 'C:'closure'compiler.jar'
$javaLocation = 'C:'Program Files'Java'jre1.8.0_131'bin'java.exe'
#Make sure debug flag is set one way or the other
if (!$debug){
  Write-Host "Debug has not been set.  Please use the -debug true|false argument."
  Exit
}elseif ($debug -eq $true){
  Write-host "Running with debug mode set to $debug."
}elseif ($debug -eq $false){
  Write-host "Running wiht debug mode set to $debug."
}else{
  Write-host "Debug has not been set properly.  Please use the -debug true|false argument."
  Exit
}
#First find everything we have a minified js version of, create an object of their names and paths, and delete the non-min file
Write-Host "Beginning minification of JS files.  Debug is $debug"
foreach ($file in $maxFiles)
    {
        $fileOld = $file.FullName
        $fileNew = $file.FullName.Replace(".js", ".min.js")
        if ($debug -eq $true){
          #Write-Host java -jar $closureJAR --js $fileOld --js_output_file $fileNew
          Write-Host "  ArgList is:  -jar  $closureJAR --js $fileOld --js_output_file $fileNew"
        }else{
            Write-Host "  Minifying: $fileOld"
            Start-Process -FilePath $javaLocation `
             -ArgumentList "-jar $closureJAR --js $fileOld --js_output_file $fileNew" `
             -RedirectStandardOutput '.'console.out' -RedirectStandardError '.'console.err'
        }
    }
Write-Host "End minification of JS files"

#Second find everything we have a minified js version of, create an object of their names and paths, and delete the non-min file
Write-Host "Beginning Removal of files...will display below"
$minFiles = Get-ChildItem -Path ./* -Filter *.min.js -Recurse 
foreach ($file in $minFiles)
    {
        #if ($file.FullName.Replace(".min.js", ".js") exists) {
        $private:nonMinifiedVersionFull = $file.FullName -replace ".min.js", ".js" #.ToString().Replace(".min.js", ".js")
        $private:nonMinifiedVersion = $file -Replace ".min.js", ".js" #.ToString().Replace(".min.js", ".js")
        Write-Host "  Removing: " $private:nonMinifiedVersion
        if ($debug -eq $false) {Remove-Item $private:nonMinifiedVersionFull -ErrorAction SilentlyContinue}
    $temp = New-Object System.Object
    $temp | Add-Member -MemberType NoteProperty -Name "minFileName" -Value $file.ToString()
    $temp | Add-Member -MemberType NoteProperty -Name "minFileFullName" -Value $file.FullName.ToString()
    $temp | Add-Member -MemberType NoteProperty -Name "maxFileName" -Value $private:nonMinifiedVersion.ToString()
    $temp | Add-Member -MemberType NoteProperty -Name "maxFileFullName" -Value $private:nonMinifiedVersionFull.ToString()
    $fileCollection.Add($temp) | Out-Null
    }
Write-Host "End Removal of Files"
if ($debug -eq $true) {
   Write-Host "The fileCollection is:"
   $fileCollection
   }
Write-Host "Beginning update of references to point to minified"
#now go through all the files that could reference them and update the references
foreach ($file2 in $filesContainingResourceRefs)
    {
        $private:file = $file2.FullName
            $fixedContent = [System.IO.File]::ReadAllText($private:file)
            #Now loop through all the min and max files in the collection
           foreach ($line in $fileCollection) {
                    $strFind    = $line.maxFileName.ToString()
                    $strReplace = $line.minFileName.ToString()
                    $fixedContent = $fixedContent.replace($strFind, $strReplace)
            }            
            if ($debug -eq $false) {[System.IO.File]::WriteAllText($private:file, $fixedContent)}

           Write-Host "  Replaced non-minified references in: " $private:file
    }
Write-Host "End update of references to point to minified"