@@ -101,17 +101,15 @@ struct PackageToJS: CommandPlugin {
101
101
}
102
102
103
103
// Build products
104
- let ( build , productName ) = try buildWasm ( options: options, context: context)
105
- guard build . succeeded else {
104
+ let ( productArtifact , build ) = try buildWasm ( options: options, context: context)
105
+ guard let productArtifact = productArtifact else {
106
106
for diagnostic in Self . friendlyBuildDiagnostics {
107
107
if let message = diagnostic ( build, arguments) {
108
108
printStderr ( " \n " + message)
109
109
}
110
110
}
111
111
exit ( 1 )
112
112
}
113
-
114
- let productArtifact = try build. findWasmArtifact ( for: productName)
115
113
let outputDir =
116
114
if let outputPath = options. outputPath {
117
115
URL ( fileURLWithPath: outputPath)
@@ -135,7 +133,7 @@ struct PackageToJS: CommandPlugin {
135
133
}
136
134
137
135
private func buildWasm( options: Options , context: PluginContext ) throws -> (
138
- build : PackageManager . BuildResult , productName : String
136
+ productArtifact : URL ? , build : PackageManager . BuildResult
139
137
) {
140
138
var parameters = PackageManager . BuildParameters (
141
139
configuration: . inherit,
@@ -158,15 +156,32 @@ struct PackageToJS: CommandPlugin {
158
156
}
159
157
let productName = try options. product ?? deriveDefaultProduct ( package : context. package )
160
158
let build = try self . packageManager. build ( . product( productName) , parameters: parameters)
161
- return ( build, productName)
159
+
160
+ var productArtifact : URL ?
161
+ if build. succeeded {
162
+ let testProductName = " \( context. package . displayName) PackageTests "
163
+ if productName == testProductName {
164
+ for fileExtension in [ " wasm " , " xctest " ] {
165
+ let path = " .build/debug/ \( testProductName) . \( fileExtension) "
166
+ if FileManager . default. fileExists ( atPath: path) {
167
+ productArtifact = URL ( fileURLWithPath: path)
168
+ break
169
+ }
170
+ }
171
+ } else {
172
+ productArtifact = try build. findWasmArtifact ( for: productName)
173
+ }
174
+ }
175
+
176
+ return ( productArtifact, build)
162
177
}
163
178
164
179
/// Construct the build plan and return the root task key
165
180
private func constructPackagingPlan(
166
181
make: inout MiniMake ,
167
182
options: Options ,
168
183
context: PluginContext ,
169
- wasmProductArtifact: PackageManager . BuildResult . BuiltArtifact ,
184
+ wasmProductArtifact: URL ,
170
185
selfPackage: Package ,
171
186
outputDir: URL
172
187
) -> MiniMake . TaskKey {
@@ -194,10 +209,10 @@ struct PackageToJS: CommandPlugin {
194
209
// Copy the wasm product artifact
195
210
let wasmFilename = " main.wasm "
196
211
let wasm = make. addTask (
197
- inputFiles: [ selfPath, wasmProductArtifact. url . path] , inputTasks: [ outputDirTask] ,
212
+ inputFiles: [ selfPath, wasmProductArtifact. path] , inputTasks: [ outputDirTask] ,
198
213
output: outputDir. appending ( path: wasmFilename) . path
199
214
) {
200
- try syncFile ( from: wasmProductArtifact. url . path, to: $0. output)
215
+ try syncFile ( from: wasmProductArtifact. path, to: $0. output)
201
216
}
202
217
packageInputs. append ( wasm)
203
218
@@ -231,6 +246,8 @@ struct PackageToJS: CommandPlugin {
231
246
for (file, output) in [
232
247
( " Plugins/PackageToJS/Templates/index.js " , " index.js " ) ,
233
248
( " Plugins/PackageToJS/Templates/index.d.ts " , " index.d.ts " ) ,
249
+ ( " Plugins/PackageToJS/Templates/instantiate.js " , " instantiate.js " ) ,
250
+ ( " Plugins/PackageToJS/Templates/instantiate.d.ts " , " instantiate.d.ts " ) ,
234
251
( " Sources/JavaScriptKit/Runtime/index.mjs " , " runtime.js " ) ,
235
252
] {
236
253
let inputPath = selfPackageURL. appending ( path: file)
@@ -291,9 +308,7 @@ internal func deriveDefaultProduct(package: Package) throws -> String {
291
308
292
309
extension PackageManager . BuildResult {
293
310
/// Find `.wasm` executable artifact
294
- internal func findWasmArtifact( for product: String ) throws
295
- -> PackageManager . BuildResult . BuiltArtifact
296
- {
311
+ internal func findWasmArtifact( for product: String ) throws -> URL {
297
312
let executables = self . builtArtifacts. filter {
298
313
( $0. kind == . executable) && ( $0. url. lastPathComponent == " \( product) .wasm " )
299
314
}
@@ -307,7 +322,7 @@ extension PackageManager.BuildResult {
307
322
" Failed to disambiguate executable product artifacts from \( executables. map ( \. url. path) . joined ( separator: " , " ) ) "
308
323
)
309
324
}
310
- return executable
325
+ return executable. url
311
326
}
312
327
}
313
328
0 commit comments