@wasm-fmt/gofmt 0.2.0 → 0.3.0

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/.editorconfig ADDED
@@ -0,0 +1,11 @@
1
+ # Check http://editorconfig.org for more information
2
+ # This is the main config file for this project:
3
+ root = true
4
+
5
+ [*]
6
+ charset = utf-8
7
+ trim_trailing_whitespace = true
8
+ end_of_line = lf
9
+ indent_style = space
10
+ insert_final_newline = true
11
+ indent_size = 4
package/.gitattributes ADDED
@@ -0,0 +1,5 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
3
+ *.js linguist-detectable=false
4
+ *.mjs linguist-detectable=false
5
+ *.sh linguist-detectable=false
package/README.md CHANGED
@@ -10,27 +10,25 @@ npm install @wasm-fmt/gofmt
10
10
  # Usage
11
11
 
12
12
  ```JavaScript
13
- import { format } from '@wasm-fmt/gofmt';
13
+ import init, { format } from '@wasm-fmt/gofmt';
14
+
15
+ await init();
14
16
 
15
17
  const source = `
16
18
  package main
17
19
  import "fmt"
18
- func main(){fmt.Println("Hello, 世界")}
20
+ func main(){fmt.Println("Hello, 世界")
21
+ }
19
22
  `;
20
23
 
21
- const formatted = await format(source);
24
+ const formatted = format(source);
22
25
  console.log(formatted);
23
26
  ```
24
27
 
25
28
  Vite users tip:
26
29
 
27
30
  ```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));
31
+ import init, { format } from '@wasm-fmt/gofmt/vite';
34
32
  ```
35
33
 
36
34
  # Build from source
package/gen_patch.sh ADDED
@@ -0,0 +1,23 @@
1
+ current_dir=$(pwd)
2
+ tmp_dir=$(mktemp -d)
3
+
4
+ cd $tmp_dir
5
+ git init
6
+
7
+ cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js $tmp_dir/go_wasm.js
8
+ git add -f .
9
+ git commit -m "init"
10
+
11
+ cp $current_dir/go_wasm.js $tmp_dir/go_wasm.js
12
+ git add -f .
13
+
14
+ git diff \
15
+ --cached \
16
+ --no-color \
17
+ --ignore-space-at-eol \
18
+ --no-ext-diff \
19
+ --src-prefix=a/ \
20
+ --dst-prefix=b/ \
21
+ >$current_dir/go_wasm.patch
22
+
23
+ rm -rf $tmp_dir
package/lib.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- export declare function format(
2
- input: string,
3
- wasm_url?: string | URL
4
- ): Promise<string>;
1
+ export default function init(wasm_url?: string | URL): Promise<void>;
2
+ export declare function format(input: string): string;
package/lib.js CHANGED
@@ -3,37 +3,42 @@ const go = new Go();
3
3
 
4
4
  let mod;
5
5
 
6
- export async function format(input, wasm_url) {
7
- if (!mod) {
8
- if (!wasm_url) {
9
- wasm_url = new URL("lib.wasm", import.meta.url);
10
- }
6
+ export default async function init(wasm_url) {
7
+ if (!mod) {
8
+ if (!wasm_url) {
9
+ wasm_url = new URL("lib.wasm", import.meta.url);
10
+ }
11
11
 
12
- if (typeof wasm_url === "string") {
13
- wasm_url = new URL(wasm_url);
14
- }
12
+ if (typeof wasm_url === "string") {
13
+ wasm_url = new URL(wasm_url);
14
+ }
15
15
 
16
- if (typeof __webpack_require__ !== "function" && wasm_url.protocol === "file:") {
17
- const fs = await import("node:fs");
18
- const bytes = fs.readFileSync(wasm_url);
19
- mod = new WebAssembly.Module(bytes);
20
- } else if ("compileStreaming" in WebAssembly) {
21
- mod = await WebAssembly.compileStreaming(fetch(wasm_url));
22
- } else {
23
- const response = await fetch(wasm_url);
24
- const bytes = await response.arrayBuffer();
25
- mod = new WebAssembly.Module(bytes);
16
+ if (
17
+ typeof __webpack_require__ !== "function" &&
18
+ wasm_url.protocol === "file:"
19
+ ) {
20
+ const fs = await import("node:fs");
21
+ const bytes = fs.readFileSync(wasm_url);
22
+ mod = new WebAssembly.Module(bytes);
23
+ } else if ("compileStreaming" in WebAssembly) {
24
+ mod = await WebAssembly.compileStreaming(fetch(wasm_url));
25
+ } else {
26
+ const response = await fetch(wasm_url);
27
+ const bytes = await response.arrayBuffer();
28
+ mod = new WebAssembly.Module(bytes);
29
+ }
26
30
  }
27
- }
31
+ }
28
32
 
29
- const inst = new WebAssembly.Instance(mod, go.importObject);
30
- go.run(inst);
33
+ export function format(input) {
34
+ const inst = new WebAssembly.Instance(mod, go.importObject);
35
+ go.run(inst);
31
36
 
32
- const input_len = go.storeString(input);
33
- const output_len = inst.exports.format(input_len);
34
- if (output_len < 0) {
35
- throw new Error(go.loadString(-output_len));
36
- }
37
+ const input_len = go.storeString(input);
38
+ const output_len = inst.exports.format(input_len);
39
+ if (output_len < 0) {
40
+ throw new Error(go.loadString(-output_len));
41
+ }
37
42
 
38
- return go.loadString(output_len);
43
+ return go.loadString(output_len);
39
44
  }
package/package.json CHANGED
@@ -1,11 +1,23 @@
1
1
  {
2
2
  "name": "@wasm-fmt/gofmt",
3
3
  "author": "magic-akari <akari.ccino@gamil.com>",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "description": "wasm based gofmt",
6
6
  "main": "lib.js",
7
7
  "types": "lib.d.ts",
8
8
  "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./lib.d.ts",
12
+ "default": "./lib.js"
13
+ },
14
+ "./vite": {
15
+ "types": "./lib.d.ts",
16
+ "default": "./vite.js"
17
+ },
18
+ "./package.json": "./package.json",
19
+ "./*": "./*"
20
+ },
9
21
  "scripts": {
10
22
  "go_wasm": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./go_wasm.js",
11
23
  "patch": "git apply ./go_wasm.patch",
@@ -16,7 +28,11 @@
16
28
  "engines": {
17
29
  "node": ">=16.17.0"
18
30
  },
19
- "keywords": [],
31
+ "keywords": [
32
+ "wasm",
33
+ "golang",
34
+ "formatter"
35
+ ],
20
36
  "license": "MIT",
21
37
  "publishConfig": {
22
38
  "access": "public"
package/test.js CHANGED
@@ -1,31 +1,29 @@
1
- import assert from "node:assert";
1
+ import assert from "node:assert/strict";
2
2
  import fs from "node:fs/promises";
3
3
  import test from "node:test";
4
- import { format } from "./lib.js";
4
+ import init, { format } from "./lib.js";
5
+
6
+ await init();
5
7
 
6
8
  const files = (await fs.readdir("testdata"))
7
- .filter((f) => f.endsWith(".input"))
8
- .map((f) => {
9
- return {
10
- input_name: f,
11
- golden_name: f.replace(".input", ".golden"),
12
- };
13
- });
9
+ .filter((f) => f.endsWith(".input"))
10
+ .map((f) => {
11
+ return {
12
+ input_name: f,
13
+ golden_name: f.replace(".input", ".golden"),
14
+ };
15
+ });
14
16
 
15
17
  for (const { input_name, golden_name } of files) {
16
- await test(
17
- `format ${input_name}`,
18
- { skip: input_name[0] === "." },
19
- async () => {
20
- const [input, expected] = await Promise.all(
21
- [input_name, golden_name].map((f) =>
22
- fs.readFile(`testdata/${f}`, "utf-8")
23
- )
24
- );
18
+ test(`format ${input_name}`, { skip: input_name[0] === "." }, async () => {
19
+ const [input, expected] = await Promise.all(
20
+ [input_name, golden_name].map((f) =>
21
+ fs.readFile(`testdata/${f}`, "utf-8")
22
+ )
23
+ );
25
24
 
26
- const actual = await format(input);
25
+ const actual = format(input);
27
26
 
28
- assert.strictEqual(actual, expected);
29
- }
30
- );
27
+ assert.equal(actual, expected);
28
+ });
31
29
  }
package/vite.js ADDED
@@ -0,0 +1,8 @@
1
+ import init from "./lib.js";
2
+ import wasm from "./lib.wasm?url";
3
+
4
+ export default function __wbg_init(input = wasm) {
5
+ return init(input);
6
+ }
7
+
8
+ export * from "./lib.js";