@@ -161,9 +161,11 @@ struct PackageToJSPlugin: CommandPlugin {
161
161
}
162
162
163
163
// Build products
164
+ let selfPackage = try findSelfPackage ( in: context. package )
164
165
let productName = try buildOptions. product ?? deriveDefaultProduct ( package : context. package )
165
166
let build = try buildWasm (
166
167
productName: productName,
168
+ selfPackage: selfPackage,
167
169
context: context,
168
170
options: buildOptions. packageOptions
169
171
)
@@ -178,14 +180,6 @@ struct PackageToJSPlugin: CommandPlugin {
178
180
} else {
179
181
context. pluginWorkDirectoryURL. appending ( path: " Package " )
180
182
}
181
- guard
182
- let selfPackage = findPackageInDependencies (
183
- package : context. package ,
184
- id: Self . JAVASCRIPTKIT_PACKAGE_ID
185
- )
186
- else {
187
- throw PackageToJSError ( " Failed to find JavaScriptKit in dependencies!? " )
188
- }
189
183
var make = MiniMake (
190
184
explain: buildOptions. packageOptions. explain,
191
185
printProgress: self . printProgress
@@ -226,9 +220,11 @@ struct PackageToJSPlugin: CommandPlugin {
226
220
exit ( 1 )
227
221
}
228
222
223
+ let selfPackage = try findSelfPackage ( in: context. package )
229
224
let productName = " \( context. package . displayName) PackageTests "
230
225
let build = try buildWasm (
231
226
productName: productName,
227
+ selfPackage: selfPackage,
232
228
context: context,
233
229
options: testOptions. packageOptions
234
230
)
@@ -264,14 +260,6 @@ struct PackageToJSPlugin: CommandPlugin {
264
260
} else {
265
261
context. pluginWorkDirectoryURL. appending ( path: " PackageTests " )
266
262
}
267
- guard
268
- let selfPackage = findPackageInDependencies (
269
- package : context. package ,
270
- id: Self . JAVASCRIPTKIT_PACKAGE_ID
271
- )
272
- else {
273
- throw PackageToJSError ( " Failed to find JavaScriptKit in dependencies!? " )
274
- }
275
263
var make = MiniMake (
276
264
explain: testOptions. packageOptions. explain,
277
265
printProgress: self . printProgress
@@ -311,6 +299,7 @@ struct PackageToJSPlugin: CommandPlugin {
311
299
312
300
private func buildWasm(
313
301
productName: String ,
302
+ selfPackage: Package ,
314
303
context: PluginContext ,
315
304
options: PackageToJS . PackageOptions
316
305
) throws
@@ -331,11 +320,7 @@ struct PackageToJSPlugin: CommandPlugin {
331
320
)
332
321
parameters. echoLogs = true
333
322
parameters. otherSwiftcFlags = [ " -color-diagnostics " ]
334
- let buildingForEmbedded =
335
- ProcessInfo . processInfo. environment [ " JAVASCRIPTKIT_EXPERIMENTAL_EMBEDDED_WASM " ] . flatMap (
336
- Bool . init
337
- ) ?? false
338
- if !buildingForEmbedded {
323
+ if !isBuildingForEmbedded( selfPackage: selfPackage) {
339
324
// NOTE: We only support static linking for now, and the new SwiftDriver
340
325
// does not infer `-static-stdlib` for WebAssembly targets intentionally
341
326
// for future dynamic linking support.
@@ -355,6 +340,31 @@ struct PackageToJSPlugin: CommandPlugin {
355
340
return try self . packageManager. build ( . product( productName) , parameters: parameters)
356
341
}
357
342
343
+ /// Check if the build is for embedded WebAssembly
344
+ private func isBuildingForEmbedded( selfPackage: Package ) -> Bool {
345
+ let coreTarget = selfPackage. targets. first { $0. name == " JavaScriptKit " }
346
+ guard let swiftTarget = coreTarget as? SwiftSourceModuleTarget else {
347
+ return false
348
+ }
349
+ // SwiftPM defines "Embedded" compilation condition when `Embedded` experimental
350
+ // feature is enabled.
351
+ // TODO: This should be replaced with a proper trait-based solution in the future.
352
+ return swiftTarget. compilationConditions. contains ( " Embedded " )
353
+ }
354
+
355
+ /// Find JavaScriptKit package in the dependencies of the given package recursively
356
+ private func findSelfPackage( in package : Package ) throws -> Package {
357
+ guard
358
+ let selfPackage = findPackageInDependencies (
359
+ package : package ,
360
+ id: Self . JAVASCRIPTKIT_PACKAGE_ID
361
+ )
362
+ else {
363
+ throw PackageToJSError ( " Failed to find JavaScriptKit in dependencies!? " )
364
+ }
365
+ return selfPackage
366
+ }
367
+
358
368
/// Clean if the build graph of the packaging process has changed
359
369
///
360
370
/// This is especially important to detect user changes debug/release
0 commit comments