Skip to content

Commit ae2cc40

Browse files
Fallback to simple copy if wasm-opt is not installed
1 parent 3aa9cb8 commit ae2cc40

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

Diff for: Plugins/PackageToJS/Sources/PackageToJS.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,28 @@ extension PackagingSystem {
117117
}
118118

119119
final class DefaultPackagingSystem: PackagingSystem {
120+
121+
private let printWarning: (String) -> Void
122+
123+
init(printWarning: @escaping (String) -> Void) {
124+
self.printWarning = printWarning
125+
}
126+
120127
func npmInstall(packageDir: String) throws {
121128
try runCommand(try which("npm"), ["-C", packageDir, "install"])
122129
}
123130

131+
lazy var warnMissingWasmOpt: () = {
132+
self.printWarning("Warning: wasm-opt is not installed, optimizations will not be applied")
133+
}()
134+
124135
func wasmOpt(_ arguments: [String], input: String, output: String) throws {
125-
try runCommand(try which("wasm-opt"), arguments + ["-o", output, input])
136+
guard let wasmOpt = try? which("wasm-opt") else {
137+
_ = warnMissingWasmOpt
138+
try FileManager.default.copyItem(atPath: input, toPath: output)
139+
return
140+
}
141+
try runCommand(wasmOpt, arguments + ["-o", output, input])
126142
}
127143

128144
private func runCommand(_ command: URL, _ arguments: [String]) throws {
@@ -190,7 +206,7 @@ struct PackagingPlanner {
190206
configuration: String,
191207
triple: String,
192208
selfPath: BuildPath = BuildPath(absolute: #filePath),
193-
system: any PackagingSystem = DefaultPackagingSystem()
209+
system: any PackagingSystem
194210
) {
195211
self.options = options
196212
self.packageId = packageId

Diff for: Plugins/PackageToJS/Sources/PackageToJSPlugin.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ extension PackagingPlanner {
446446
) {
447447
let outputBaseName = outputDir.lastPathComponent
448448
let (configuration, triple) = PackageToJS.deriveBuildConfiguration(wasmProductArtifact: wasmProductArtifact)
449+
let system = DefaultPackagingSystem(printWarning: printStderr)
449450
self.init(
450451
options: options,
451452
packageId: context.package.id,
@@ -454,7 +455,8 @@ extension PackagingPlanner {
454455
outputDir: BuildPath(absolute: outputDir.path),
455456
wasmProductArtifact: BuildPath(absolute: wasmProductArtifact.path),
456457
configuration: configuration,
457-
triple: triple
458+
triple: triple,
459+
system: system
458460
)
459461
}
460462
}

0 commit comments

Comments
 (0)