Skip to content

Commit ab3c404

Browse files
PackageToJS: Rename --platform to --default-platform and fix tests
1 parent 27dadd9 commit ab3c404

18 files changed

+160
-193
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ SWIFT_SDK_ID ?= wasm32-unknown-wasi
33
.PHONY: bootstrap
44
bootstrap:
55
npm ci
6-
cd Plugins/PackageToJS/Templates && npm ci
76

87
.PHONY: unittest
98
unittest:

Plugins/PackageToJS/Sources/PackageToJS.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct PackageToJS {
1414
/// Name of the package (default: lowercased Package.swift name)
1515
var packageName: String?
1616
/// Target platform for the generated JavaScript (default: browser)
17-
var platform: Platform = .browser
17+
var defaultPlatform: Platform = .browser
1818
/// Whether to explain the build plan (default: false)
1919
var explain: Bool = false
2020
/// Whether to print verbose output
@@ -724,7 +724,8 @@ struct PackagingPlanner {
724724
"USE_WASI_CDN": options.useCDN,
725725
"HAS_BRIDGE": exportedSkeletons.count > 0 || importedSkeletons.count > 0,
726726
"HAS_IMPORTS": importedSkeletons.count > 0,
727-
"TARGET_PLATFORM_NODE": options.platform == .node,
727+
"TARGET_DEFAULT_PLATFORM_NODE": options.defaultPlatform == .node,
728+
"TARGET_DEFAULT_PLATFORM_BROWSER": options.defaultPlatform == .browser,
728729
]
729730
let constantSubstitutions: [String: String] = [
730731
"PACKAGE_TO_JS_MODULE_PATH": wasmFilename,

Plugins/PackageToJS/Sources/PackageToJSPlugin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ extension PackageToJS.PackageOptions {
461461
let configuration: String? =
462462
(extractor.extractOption(named: "configuration") + extractor.extractSingleDashOption(named: "c")).last
463463
let packageName = extractor.extractOption(named: "package-name").last
464-
let platform = try extractor.extractPlatformOption(named: "platform")
464+
let defaultPlatform = try extractor.extractPlatformOption(named: "default-platform")
465465
let explain = extractor.extractFlag(named: "explain")
466466
let useCDN = extractor.extractFlag(named: "use-cdn")
467467
let verbose = extractor.extractFlag(named: "verbose")
@@ -470,7 +470,7 @@ extension PackageToJS.PackageOptions {
470470
outputPath: outputPath,
471471
configuration: configuration,
472472
packageName: packageName,
473-
platform: platform,
473+
defaultPlatform: defaultPlatform,
474474
explain: explain != 0,
475475
verbose: verbose != 0,
476476
useCDN: useCDN != 0,
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import type { Exports, Imports, ModuleSource } from './instantiate.js'
2-
/* #if TARGET_PLATFORM_NODE */
3-
import type { DefaultNodeSetupOptions } from './platforms/node.js'
4-
/* #endif */
52

63
export type Options = {
4+
/* #if TARGET_DEFAULT_PLATFORM_BROWSER */
75
/**
86
* The WebAssembly module to instantiate
97
*
108
* If not provided, the module will be fetched from the default path.
119
*/
1210
module?: ModuleSource
11+
/* #endif */
1312
/* #if HAS_IMPORTS */
1413
/**
1514
* The imports to use for the module
@@ -21,21 +20,10 @@ export type Options = {
2120
/**
2221
* Instantiate and initialize the module
2322
*
24-
/* #if TARGET_PLATFORM_NODE */
25-
* This is a convenience function for Node.js environments.
26-
/* #else */
2723
* This is a convenience function for browser environments.
28-
/* #endif */
2924
* If you need a more flexible API, see `instantiate`.
3025
*/
31-
/* #if TARGET_PLATFORM_NODE */
32-
export declare function init(options?: DefaultNodeSetupOptions): Promise<{
33-
instance: WebAssembly.Instance,
34-
exports: Exports
35-
}>
36-
/* #else */
3726
export declare function init(options?: Options): Promise<{
3827
instance: WebAssembly.Instance,
3928
exports: Exports
4029
}>
41-
/* #endif */
Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
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';
3+
/* #if TARGET_DEFAULT_PLATFORM_NODE */
4+
import { defaultNodeSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory as createDefaultWorkerFactoryForNode /* #endif */} from './platforms/node.js';
55
/* #else */
6-
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory /* #endif */} from './platforms/browser.js';
6+
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory as createDefaultWorkerFactoryForBrowser /* #endif */} from './platforms/browser.js';
77
/* #endif */
88

9+
/* #if TARGET_DEFAULT_PLATFORM_NODE */
910
/** @type {import('./index.d').init} */
10-
export async function init(_options) {
11-
/* #if TARGET_PLATFORM_NODE */
11+
async function initNode(_options) {
1212
/** @type {import('./platforms/node.d.ts').DefaultNodeSetupOptions} */
13-
const options = _options || {};
14-
const instantiateOptions = await defaultNodeSetup({
15-
args: options.args,
16-
onExit: options.onExit,
13+
const options = {
14+
...(_options || {}),
1715
/* #if USE_SHARED_MEMORY */
18-
spawnWorker: options.spawnWorker || createDefaultWorkerFactory()
16+
spawnWorker: createDefaultWorkerFactoryForNode(),
1917
/* #endif */
20-
});
18+
};
19+
const instantiateOptions = await defaultNodeSetup(options);
20+
return await instantiate(instantiateOptions);
21+
}
22+
2123
/* #else */
24+
25+
/** @type {import('./index.d').init} */
26+
async function initBrowser(_options) {
2227
/** @type {import('./index.d').Options} */
2328
const options = _options || {
2429
/* #if HAS_IMPORTS */
@@ -36,9 +41,19 @@ export async function init(_options) {
3641
getImports: () => options.getImports(),
3742
/* #endif */
3843
/* #if USE_SHARED_MEMORY */
39-
spawnWorker: createDefaultWorkerFactory()
44+
spawnWorker: createDefaultWorkerFactoryForBrowser()
4045
/* #endif */
4146
})
42-
/* #endif */
4347
return await instantiate(instantiateOptions);
4448
}
49+
50+
/* #endif */
51+
52+
/** @type {import('./index.d').init} */
53+
export async function init(options) {
54+
/* #if TARGET_DEFAULT_PLATFORM_NODE */
55+
return initNode(options);
56+
/* #else */
57+
return initBrowser(options);
58+
/* #endif */
59+
}

Plugins/PackageToJS/Templates/package-lock.json

Lines changed: 0 additions & 41 deletions
This file was deleted.

Plugins/PackageToJS/Templates/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99
},
1010
"dependencies": {
1111
"@bjorn3/browser_wasi_shim": "0.3.0"
12-
},
13-
"devDependencies": {
14-
"@types/node": "^22.0.0"
1512
}
1613
}

Plugins/PackageToJS/Templates/platforms/node.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ export type DefaultNodeSetupOptions = {
1313

1414
export function defaultNodeSetup(options: DefaultNodeSetupOptions): Promise<InstantiateOptions>
1515

16-
export function createDefaultWorkerFactory(preludeScript: string): (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker
16+
export function createDefaultWorkerFactory(preludeScript?: string): (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker

Plugins/PackageToJS/Templates/platforms/node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export async function defaultNodeSetup(options) {
131131
new PreopenDirectory("/", rootFs),
132132
], { debug: false })
133133
const pkgDir = path.dirname(path.dirname(fileURLToPath(import.meta.url)))
134-
const module = await WebAssembly.compile(await readFile(path.join(pkgDir, MODULE_PATH)))
134+
const module = await WebAssembly.compile(new Uint8Array(await readFile(path.join(pkgDir, MODULE_PATH))))
135135
/* #if USE_SHARED_MEMORY */
136136
const memory = new WebAssembly.Memory(MEMORY_TYPE);
137137
const threadChannel = new DefaultNodeThreadRegistry(options.spawnWorker)

Plugins/PackageToJS/Templates/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "esnext",
4+
"lib": ["es2017", "dom"],
45
"noEmit": true,
56
"allowJs": true,
67
"skipLibCheck": true,

0 commit comments

Comments
 (0)