Skip to content

[BridgeJS] Support @JS var declarations for global scope imports #466

@kateinoigakukun

Description

@kateinoigakukun

Motivation

Currently, importing JavaScript APIs from the global scope requires users to write TypeScript declaration files and import APIs through the standard BridgeJS workflow. For simple use cases where users only need to access a few well-known global APIs like console.log() or document.getElementById(), this approach can feel heavyweight and requires additional setup steps.

Proposed Solution

Allow users to declare global scope imports directly in Swift using @JS var declarations with protocol interfaces, eliminating the need for separate .d.ts files for simple global API access.

High-level API

@JS protocol JSConsole {
    func log(_ message: String)
    func error(_ message: String)
}

@JS var console: JSConsole

@JS protocol JSElement {
    var innerHTML: String { get set }
    func addEventListener(_ event: String, _ handler: @escaping () -> Void)
}

@JS protocol JSDocument {
    func getElementById(_ id: String) -> JSElement?
}

@JS(from: .global) var document: JSDocument

@JS(from: .module("uuid"), jsName: "v4")
func uuidv4() -> String

Implementation

This feature would require adding a macro plugin to support @JS var [name]: [Type] declarations that generates getter/setter accessors interacting with the code generated by @JS protocol

If it works well, we might be able to make ImportTS just to generate @JS annotated Swift code and centralize ABI-facing code in ExportSwift

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions