forked from dustinmoris/DustedCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinifyJavaScript.ps1
49 lines (40 loc) · 1.24 KB
/
MinifyJavaScript.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[CmdletBinding()]
param
(
[Parameter(Position = 0, Mandatory = $true)]
[string] $SolutionDir
)
Function Compress-JavaScriptFile
{
[CmdletBinding()]
param
(
[string] $JavaScriptFilePath
)
$jsFile = Get-Item -Path $JavaScriptFilePath
$content = [System.IO.File]::ReadAllText($jsFile.FullName)
$body = @{input = $content}
$response = Invoke-WebRequest -Uri "https://javascript-minifier.com/raw" -Method Post -Body $body
if ($response.StatusCode -ne 200)
{
throw ("Could not compress JavaScript file $JavaScriptFilePath" +
[System.Environment]::NewLine +
[System.Environment]::NewLine +
"Response:" +
[System.Environment]::NewLine +
$response.RawContent)
}
$compressedContent = $response.Content
$newFilePath = $JavaScriptFilePath.Replace(".js", ".min.js")
Set-Content -Path $newFilePath -Value $compressedContent -Force
}
# -----------------
# BEGIN:
$ErrorActionPreference = "Stop"
Write-Output "-----"
Write-Output "Compressing JavaScript Files"
Write-Output "-----"
Get-ChildItem $SolutionDir -Recurse -Include *.js -Exclude *.min.js | % {
Write-Output "Compressing $_"
Compress-JavaScriptFile -JavaScriptFilePath $_
}