@wasm-fmt/gofmt 0.0.3 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -1
- package/lib.d.ts +4 -1
- package/lib.js +12 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
[](https://github.com/wasm-fmt/gofmt/actions/workflows/build.yml)
|
|
2
2
|
[](https://www.npmjs.com/package/@wasm-fmt/gofmt)
|
|
3
3
|
|
|
4
|
+
# Install
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm install @wasm-fmt/gofmt
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
# Usage
|
|
11
|
+
|
|
12
|
+
```JavaScript
|
|
13
|
+
import { format } from '@wasm-fmt/gofmt';
|
|
14
|
+
|
|
15
|
+
const source = `
|
|
16
|
+
package main
|
|
17
|
+
import "fmt"
|
|
18
|
+
func main(){fmt.Println("Hello, 世界")}
|
|
19
|
+
`;
|
|
20
|
+
|
|
21
|
+
const formatted = await format(source);
|
|
22
|
+
console.log(formatted);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Vite users tip:
|
|
26
|
+
|
|
27
|
+
```JavaScript
|
|
28
|
+
import { format } from '@wasm-fmt/gofmt';
|
|
29
|
+
import wasmUrl from "@wasm-fmt/gofmt/lib.wasm?url";
|
|
30
|
+
|
|
31
|
+
// ...
|
|
32
|
+
|
|
33
|
+
const formatted = await format(source, new URL(wasmUrl, import.meta.url));
|
|
34
|
+
```
|
|
35
|
+
|
|
4
36
|
# Build from source
|
|
5
37
|
|
|
6
38
|
```bash
|
|
@@ -14,4 +46,4 @@ npm run build
|
|
|
14
46
|
|
|
15
47
|
# 4. test
|
|
16
48
|
npm run test
|
|
17
|
-
```
|
|
49
|
+
```
|
package/lib.d.ts
CHANGED
package/lib.js
CHANGED
|
@@ -3,18 +3,24 @@ const go = new Go();
|
|
|
3
3
|
|
|
4
4
|
let mod;
|
|
5
5
|
|
|
6
|
-
export async function format(input) {
|
|
6
|
+
export async function format(input, wasm_url) {
|
|
7
7
|
if (!mod) {
|
|
8
|
-
|
|
8
|
+
if (!wasm_url) {
|
|
9
|
+
wasm_url = new URL("lib.wasm", import.meta.url);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (typeof wasm_url === "string") {
|
|
13
|
+
wasm_url = new URL(wasm_url);
|
|
14
|
+
}
|
|
9
15
|
|
|
10
|
-
if (
|
|
16
|
+
if (typeof __webpack_require__ !== "function" && wasm_url.protocol === "file:") {
|
|
11
17
|
const fs = await import("node:fs");
|
|
12
|
-
const bytes = fs.readFileSync(
|
|
18
|
+
const bytes = fs.readFileSync(wasm_url);
|
|
13
19
|
mod = new WebAssembly.Module(bytes);
|
|
14
20
|
} else if ("instantiateStreaming" in WebAssembly) {
|
|
15
|
-
mod = await WebAssembly.compileStreaming(fetch(
|
|
21
|
+
mod = await WebAssembly.compileStreaming(fetch(wasm_url), go.importObject);
|
|
16
22
|
} else {
|
|
17
|
-
const response = await fetch(
|
|
23
|
+
const response = await fetch(wasm_url);
|
|
18
24
|
const bytes = await response.arrayBuffer();
|
|
19
25
|
mod = new WebAssembly.Module(bytes);
|
|
20
26
|
}
|