JavaScript Minifier for Swift
Free online javascript minifier with Swift code examples
Working with javascript minifier in Swift? Our free online javascript minifier helps Swift developers format, validate, and process data instantly. Below you will find Swift code examples using Custom / JavaScriptCore so you can achieve the same result programmatically in your own projects.
Try the JavaScript Minifier Online
Use our free JavaScript Minifier directly in your browser — no setup required.
Open JavaScript MinifierSwift Code Example
import Foundation
func minifyJS(_ js: String) -> String {
var result = js
// Remove single-line comments
result = result.replacingOccurrences(
of: "//[^\n]*", with: "", options: .regularExpression)
// Remove multi-line comments
result = result.replacingOccurrences(
of: "/\\*[\\s\\S]*?\\*/", with: "", options: .regularExpression)
// Collapse whitespace
result = result.replacingOccurrences(
of: "\\s+", with: " ", options: .regularExpression)
// Remove spaces around operators
result = result.replacingOccurrences(
of: "\\s*([={}();,+])\\s*", with: "$1", options: .regularExpression)
return result.trimmingCharacters(in: .whitespaces)
}
let js = "function greet(name) { var msg = 'Hello, ' + name; console.log(msg); }"
print(minifyJS(js))Quick Setup
Library: Custom / JavaScriptCore
// Built-in JavaScriptCore frameworkSwift Tips & Best Practices
- For production, use a dedicated JS minifier in your build pipeline
- JavaScriptCore can execute JS but not minify it
- Consider calling a Node.js minifier from Swift for full features