Minify JS and CSS files

This commit is contained in:
Christoph Hagen 2022-09-05 15:56:05 +02:00
parent a69da0fa35
commit cfb68f5237
4 changed files with 107 additions and 12 deletions

View File

@ -14,7 +14,17 @@ extension URL {
}
var isDirectory: Bool {
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
do {
let resources = try resourceValues(forKeys: [.isDirectoryKey])
guard let isDirectory = resources.isDirectory else {
print("No isDirectory info for \(path)")
return false
}
return isDirectory
} catch {
print("Failed to get directory information from \(path): \(error)")
return false
}
}
var exists: Bool {

View File

@ -3,4 +3,6 @@ import Foundation
struct Configuration {
let pageImageWidth: Int
let minifyCSSandJS: Bool
}

View File

@ -7,6 +7,8 @@ typealias SourceTextFile = (content: String, didChange: Bool)
final class FileSystem {
private static let tempFileName = "temp.bin"
private static let hashesFileName = "hashes.json"
private let input: URL
@ -19,6 +21,10 @@ final class FileSystem {
input.appendingPathComponent(FileSystem.hashesFileName)
}
private var tempFile: URL {
input.appendingPathComponent(FileSystem.tempFileName)
}
/**
The hashes of all accessed files from the previous run
@ -357,7 +363,21 @@ final class FileSystem {
Add a file as required, so that it will be copied to the output directory.
*/
func require(file: String) {
requiredFiles.insert(file)
let url = input.appendingPathComponent(file)
guard url.exists, url.isDirectory else {
requiredFiles.insert(file)
return
}
do {
try FileManager.default
.contentsOfDirectory(atPath: url.path)
.forEach {
// Recurse into subfolders
require(file: file + "/" + $0)
}
} catch {
log.add(error: "Failed to read folder \(file): \(error)", source: source)
}
}
/**
@ -391,17 +411,11 @@ final class FileSystem {
}
continue
}
let data: Data
do {
data = try Data(contentsOf: sourceUrl)
} catch {
log.add(error: "Failed to read data at \(sourceUrl.path)", source: source, error: error)
continue
}
if writeIfChanged(data, to: destinationUrl) {
if copyFileIfChanged(from: sourceUrl, to: destinationUrl) {
copiedFiles.insert(file)
}
}
try? tempFile.delete()
for (file, source) in expectedFiles {
guard !isExternal(file: file) else {
continue
@ -422,6 +436,52 @@ final class FileSystem {
}
}
private func copyFileIfChanged(from sourceUrl: URL, to destinationUrl: URL) -> Bool {
guard configuration.minifyCSSandJS else {
return copyBinaryFileIfChanged(from: sourceUrl, to: destinationUrl)
}
switch sourceUrl.pathExtension.lowercased() {
case "js":
return minifyJS(at: sourceUrl, andWriteTo: destinationUrl)
case "css":
return minifyCSS(at: sourceUrl, andWriteTo: destinationUrl)
default:
return copyBinaryFileIfChanged(from: sourceUrl, to: destinationUrl)
}
}
private func copyBinaryFileIfChanged(from sourceUrl: URL, to destinationUrl: URL) -> Bool {
do {
let data = try Data(contentsOf: sourceUrl)
return writeIfChanged(data, to: destinationUrl)
} catch {
log.add(error: "Failed to read data at \(sourceUrl.path)", source: source, error: error)
return false
}
}
private func minifyJS(at sourceUrl: URL, andWriteTo destinationUrl: URL) -> Bool {
let command = "uglifyjs \(sourceUrl.path) > \(tempFile.path)"
do {
_ = try safeShell(command)
return copyBinaryFileIfChanged(from: tempFile, to: destinationUrl)
} catch {
log.add(error: "Failed to minify \(sourceUrl.path): \(error)", source: source)
return false
}
}
private func minifyCSS(at sourceUrl: URL, andWriteTo destinationUrl: URL) -> Bool {
let command = "cleancss \(sourceUrl.path) -o \(tempFile.path)"
do {
_ = try safeShell(command)
return copyBinaryFileIfChanged(from: tempFile, to: destinationUrl)
} catch {
log.add(error: "Failed to minify \(sourceUrl.path): \(error)", source: source)
return false
}
}
private func cleanRelativeURL(_ raw: String) -> String {
let raw = raw.dropAfterLast("#") // Clean links to page content
guard raw.contains("..") else {
@ -468,7 +528,7 @@ final class FileSystem {
guard !externalFiles.isEmpty else {
return
}
print("\(externalFiles.count) external files needed:")
print("\(externalFiles.count) external resources needed:")
for file in externalFiles.sorted() {
print(" " + file)
}
@ -553,6 +613,27 @@ final class FileSystem {
let data = string.data(using: .utf8)!
return writeIfChanged(data, to: url)
}
// MARK: Running other tasks
@discardableResult
func safeShell(_ command: String) throws -> String {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-cl", command]
task.executableURL = URL(fileURLWithPath: "/bin/zsh")
task.standardInput = nil
try task.run()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
return output
}
}
private extension Digest {

View File

@ -3,7 +3,9 @@ import Foundation
private let contentDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace")
private let outputDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace/Site")
let configuration = Configuration(pageImageWidth: 748)
let configuration = Configuration(
pageImageWidth: 748,
minifyCSSandJS: true)
let log = ValidationLog()
let files = FileSystem(in: contentDirectory, to: outputDirectory)