Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
omochi committed Apr 17, 2024
1 parent a31c9da commit 9e539a2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
37 changes: 35 additions & 2 deletions BrowserTests/Sources/GenPagesModule/GenPages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public struct GenPages {
try generateEntryHTML(page: page)
}
try generateViteConfig()
try generateIndexPage()
}

private static func pages(rootDir: URL) throws -> [String] {
Expand All @@ -41,7 +42,7 @@ public struct GenPages {
<title>\(page)</title>
<script type="module">
import { load } from "/src/loader/load.ts";
load("/.build/debug/\(page).wasm");
load("/.build/wasm32-unknown-wasi/debug/\(page).wasm");
</script>
</head>
<body>
Expand Down Expand Up @@ -80,8 +81,40 @@ public struct GenPages {
""")

let code = parts.joined(separator: "\n")

let file = rootDir.appending(component: "vite.config.js")
try code.write(to: file, atomically: true, encoding: .utf8)
}

private func generateIndexPage() throws {
var parts: [String] = []

parts.append("""
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
<ul>
""")

for page in pages {
parts.append("""
<li>
<a href="./pages/\(page).html">\(page)</a>
</li>
""")
}

parts.append("""
</ul>
</body>
</html>
""")

let code = parts.joined(separator: "\n")
let file = rootDir.appending(component: "index.html")
try code.write(to: file, atomically: true, encoding: .utf8)
}
}
5 changes: 4 additions & 1 deletion BrowserTests/Sources/QSComponents/main.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React
import SRTDOM
import JavaScriptKit

// https://ja.react.dev/learn
Expand All @@ -22,4 +23,6 @@ struct MyApp: Component {
}
}

print("hello wasm")
let body = try JSWindow.global.document.body.unwrap("body")
let root = ReactRoot(element: body)
root.render(node: MyApp())
14 changes: 14 additions & 0 deletions BrowserTests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
<ul>
<li>
<a href="./pages/QSComponents.html">QSComponents</a>
</li>
</ul>
</body>
</html>
2 changes: 1 addition & 1 deletion BrowserTests/pages/QSComponents.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>QSComponents</title>
<script type="module">
import { load } from "/src/loader/load.ts";
load("/.build/debug/QSComponents.wasm");
load("/.build/wasm32-unknown-wasi/debug/QSComponents.wasm");
</script>
</head>
<body>
Expand Down
10 changes: 10 additions & 0 deletions Sources/SRTDOM/JSDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ public struct JSDocument: ConvertibleToJSObject & ConstructibleFromJSValue {

public var jsObject: JSObject


public var body: JSHTMLElement? {
get { .unsafeConstruct(from: jsValue.body) }
nonmutating set { jsValue.body = newValue.jsValue }
}

public var documentElement: JSHTMLElement {
.unsafeConstruct(from: jsValue.documentElement)
}

public func createElement(_ tagName: String) throws -> JSHTMLElement {
try .mustConstruct(from: try jsValue.throws.createElement(tagName))
}
Expand Down
8 changes: 2 additions & 6 deletions Sources/SRTDOM/JSText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ public protocol JSTextProtocol: JSNodeProtocol {

extension JSTextProtocol {
public var data: String {
get {
.unsafeConstruct(from: jsValue.data)
}
nonmutating set {
jsValue.data = newValue.jsValue
}
get { .unsafeConstruct(from: jsValue.data) }
nonmutating set { jsValue.data = newValue.jsValue }
}
}

Expand Down

0 comments on commit 9e539a2

Please sign in to comment.