File tree Expand file tree Collapse file tree 5 files changed +61
-0
lines changed Expand file tree Collapse file tree 5 files changed +61
-0
lines changed Original file line number Diff line number Diff line change 1+ .DS_Store
2+ /.build
3+ /Packages
4+ xcuserdata /
5+ DerivedData /
6+ .swiftpm /configuration /registries.json
7+ .swiftpm /xcode /package.xcworkspace /contents.xcworkspacedata
8+ .netrc
Original file line number Diff line number Diff line change 1+ // swift-tools-version: 6.2
2+
3+ import PackageDescription
4+
5+ let package = Package (
6+ name: " NodeJS " ,
7+ dependencies: [ . package ( name: " JavaScriptKit " , path: " ../../ " ) ] ,
8+ targets: [
9+ . executableTarget(
10+ name: " NodeJS " ,
11+ dependencies: [ " JavaScriptKit " ]
12+ ) ,
13+ ] ,
14+ swiftLanguageModes: [ . v6]
15+ )
Original file line number Diff line number Diff line change 1+ # NodeJS example
2+
3+ This example demonstrates how to use JavaScriptKit with Node.js. It shows how to export Swift functions to JavaScript and run them in a Node.js environment.
4+
5+ ``` sh
6+ $ swift package --swift-sdk $SWIFT_SDK_ID js
7+ $ node main.mjs
8+ ```
9+
Original file line number Diff line number Diff line change 1+ import JavaScriptKit
2+
3+ @main
4+ struct NodeJS {
5+ static func main( ) {
6+ JSObject . global [ " greet " ] = JSClosure { args in
7+ let nameString = args [ 0 ] . string!
8+ return . string( " Hello, \( nameString) from NodeJS! " )
9+ } . jsValue
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ // @ts -check
2+
3+ import { instantiate } from "./.build/plugins/PackageToJS/outputs/Package/instantiate.js"
4+ import { defaultNodeSetup } from "./.build/plugins/PackageToJS/outputs/Package/platforms/node.js"
5+
6+ async function main ( ) {
7+ // Create a default Node.js option object
8+ const options = await defaultNodeSetup ( ) ;
9+ // Instantiate the Swift code, executing
10+ // NodeJS.main() in NodeJS.swift
11+ await instantiate ( options ) ;
12+
13+ // Call the greet function set by NodeJS.swift
14+ const greet = globalThis . greet ;
15+ console . log ( greet ( "World" ) ) ;
16+ }
17+
18+ main ( )
You can’t perform that action at this time.
0 commit comments