Skip to content

Commit f60bfd6

Browse files
committed
adds a --platform flag to js to control the target platform of the generated code
1 parent 2c183f3 commit f60bfd6

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Plugins/PackageToJS/Sources/PackageToJS.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ struct PackageToJS {
88
var configuration: String?
99
/// Name of the package (default: lowercased Package.swift name)
1010
var packageName: String?
11+
/// Target platform for the generated JavaScript (default: browser)
12+
var platform: String?
1113
/// Whether to explain the build plan (default: false)
1214
var explain: Bool = false
1315
/// Whether to print verbose output
@@ -717,6 +719,7 @@ struct PackagingPlanner {
717719
"USE_WASI_CDN": options.useCDN,
718720
"HAS_BRIDGE": exportedSkeletons.count > 0 || importedSkeletons.count > 0,
719721
"HAS_IMPORTS": importedSkeletons.count > 0,
722+
"TARGET_PLATFORM_NODE": options.platform == "node",
720723
]
721724
let constantSubstitutions: [String: String] = [
722725
"PACKAGE_TO_JS_MODULE_PATH": wasmFilename,

Plugins/PackageToJS/Sources/PackageToJSPlugin.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ extension PackageToJS.PackageOptions {
446446
let configuration: String? =
447447
(extractor.extractOption(named: "configuration") + extractor.extractSingleDashOption(named: "c")).last
448448
let packageName = extractor.extractOption(named: "package-name").last
449+
let platform = extractor.extractOption(named: "platform").last
449450
let explain = extractor.extractFlag(named: "explain")
450451
let useCDN = extractor.extractFlag(named: "use-cdn")
451452
let verbose = extractor.extractFlag(named: "verbose")
@@ -454,6 +455,7 @@ extension PackageToJS.PackageOptions {
454455
outputPath: outputPath,
455456
configuration: configuration,
456457
packageName: packageName,
458+
platform: platform,
457459
explain: explain != 0,
458460
verbose: verbose != 0,
459461
useCDN: useCDN != 0,
@@ -466,6 +468,7 @@ extension PackageToJS.PackageOptions {
466468
--output <path> Path to the output directory (default: .build/plugins/PackageToJS/outputs/Package)
467469
-c, --configuration <name> The build configuration to use (values: debug, release; default: debug)
468470
--package-name <name> Name of the package (default: lowercased Package.swift name)
471+
--platform <name> Target platform for generated JavaScript (values: browser, node; default: browser)
469472
--use-cdn Whether to use CDN for dependency packages
470473
--enable-code-coverage Whether to enable code coverage collection
471474
--explain Whether to explain the build plan

Plugins/PackageToJS/Templates/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
// @ts-check
22
import { instantiate } from './instantiate.js';
3+
/* #if TARGET_PLATFORM_NODE */
4+
import { defaultNodeSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory /* #endif */} from './platforms/node.js';
5+
/* #else */
36
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory /* #endif */} from './platforms/browser.js';
7+
/* #endif */
48

59
/** @type {import('./index.d').init} */
610
export async function init(_options) {
11+
/* #if TARGET_PLATFORM_NODE */
12+
/** @type {import('./platforms/node.d.ts').DefaultNodeSetupOptions} */
13+
const options = _options || {};
14+
const instantiateOptions = await defaultNodeSetup({
15+
args: options.args,
16+
onExit: options.onExit,
17+
/* #if USE_SHARED_MEMORY */
18+
spawnWorker: options.spawnWorker || createDefaultWorkerFactory()
19+
/* #endif */
20+
});
21+
/* #else */
722
/** @type {import('./index.d').Options} */
823
const options = _options || {
924
/* #if HAS_IMPORTS */
@@ -24,5 +39,6 @@ export async function init(_options) {
2439
spawnWorker: createDefaultWorkerFactory()
2540
/* #endif */
2641
})
42+
/* #endif */
2743
return await instantiate(instantiateOptions);
2844
}

0 commit comments

Comments
 (0)