@@ -101,17 +101,15 @@ struct PackageToJS: CommandPlugin {
101101 }
102102
103103 // 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 {
106106 for diagnostic in Self . friendlyBuildDiagnostics {
107107 if let message = diagnostic ( build, arguments) {
108108 printStderr ( " \n " + message)
109109 }
110110 }
111111 exit ( 1 )
112112 }
113-
114- let productArtifact = try build. findWasmArtifact ( for: productName)
115113 let outputDir =
116114 if let outputPath = options. outputPath {
117115 URL ( fileURLWithPath: outputPath)
@@ -135,7 +133,7 @@ struct PackageToJS: CommandPlugin {
135133 }
136134
137135 private func buildWasm( options: Options , context: PluginContext ) throws -> (
138- build : PackageManager . BuildResult , productName : String
136+ productArtifact : URL ? , build : PackageManager . BuildResult
139137 ) {
140138 var parameters = PackageManager . BuildParameters (
141139 configuration: . inherit,
@@ -158,15 +156,32 @@ struct PackageToJS: CommandPlugin {
158156 }
159157 let productName = try options. product ?? deriveDefaultProduct ( package : context. package )
160158 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)
162177 }
163178
164179 /// Construct the build plan and return the root task key
165180 private func constructPackagingPlan(
166181 make: inout MiniMake ,
167182 options: Options ,
168183 context: PluginContext ,
169- wasmProductArtifact: PackageManager . BuildResult . BuiltArtifact ,
184+ wasmProductArtifact: URL ,
170185 selfPackage: Package ,
171186 outputDir: URL
172187 ) -> MiniMake . TaskKey {
@@ -194,10 +209,10 @@ struct PackageToJS: CommandPlugin {
194209 // Copy the wasm product artifact
195210 let wasmFilename = " main.wasm "
196211 let wasm = make. addTask (
197- inputFiles: [ selfPath, wasmProductArtifact. url . path] , inputTasks: [ outputDirTask] ,
212+ inputFiles: [ selfPath, wasmProductArtifact. path] , inputTasks: [ outputDirTask] ,
198213 output: outputDir. appending ( path: wasmFilename) . path
199214 ) {
200- try syncFile ( from: wasmProductArtifact. url . path, to: $0. output)
215+ try syncFile ( from: wasmProductArtifact. path, to: $0. output)
201216 }
202217 packageInputs. append ( wasm)
203218
@@ -231,6 +246,8 @@ struct PackageToJS: CommandPlugin {
231246 for (file, output) in [
232247 ( " Plugins/PackageToJS/Templates/index.js " , " index.js " ) ,
233248 ( " 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 " ) ,
234251 ( " Sources/JavaScriptKit/Runtime/index.mjs " , " runtime.js " ) ,
235252 ] {
236253 let inputPath = selfPackageURL. appending ( path: file)
@@ -291,9 +308,7 @@ internal func deriveDefaultProduct(package: Package) throws -> String {
291308
292309extension PackageManager . BuildResult {
293310 /// 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 {
297312 let executables = self . builtArtifacts. filter {
298313 ( $0. kind == . executable) && ( $0. url. lastPathComponent == " \( product) .wasm " )
299314 }
@@ -307,7 +322,7 @@ extension PackageManager.BuildResult {
307322 " Failed to disambiguate executable product artifacts from \( executables. map ( \. url. path) . joined ( separator: " , " ) ) "
308323 )
309324 }
310- return executable
325+ return executable. url
311326 }
312327}
313328
0 commit comments