@wasm-fmt/gofmt 0.5.1 → 0.6.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/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.6.0]
9
+
10
+ ### Breaking
11
+ - **Entry point change:** The default entry point has been restructured. If you are upgrading from a version below 0.6.0 and want the previous behavior, import from `@wasm-fmt/gofmt/web` instead of the default entry point.
package/README.md CHANGED
@@ -16,8 +16,28 @@ npx jsr add @fmt/gofmt
16
16
 
17
17
  # Usage
18
18
 
19
- ```JavaScript
20
- import init, { format } from "@wasm-fmt/gofmt";
19
+ ## Node.js / Deno / Bun / Bundler
20
+
21
+ ```javascript
22
+ import { format } from "@wasm-fmt/gofmt";
23
+
24
+ const source = `
25
+ package main
26
+ import "fmt"
27
+ func main(){fmt.Println("Hello, 世界")
28
+ }
29
+ `;
30
+
31
+ const formatted = format(source);
32
+ console.log(formatted);
33
+ ```
34
+
35
+ ## Web
36
+
37
+ For web environments, you need to initialize WASM module manually:
38
+
39
+ ```javascript
40
+ import init, { format } from "@wasm-fmt/gofmt/web";
21
41
 
22
42
  await init();
23
43
 
@@ -32,12 +52,24 @@ const formatted = format(source);
32
52
  console.log(formatted);
33
53
  ```
34
54
 
35
- Vite users tip:
55
+ ### Vite
36
56
 
37
57
  ```JavaScript
38
58
  import init, { format } from "@wasm-fmt/gofmt/vite";
59
+
60
+ await init();
61
+ // ...
39
62
  ```
40
63
 
64
+ ## Entry Points
65
+
66
+ - `.` - Auto-detects environment (Node.js uses node, Webpack uses bundler, default is ESM)
67
+ - `./node` - Node.js environment (no init required)
68
+ - `./esm` - ESM environments like Deno (no init required)
69
+ - `./bundler` - Bundlers like Webpack (no init required)
70
+ - `./web` - Web browsers (requires manual init)
71
+ - `./vite` - Vite bundler (requires manual init)
72
+
41
73
  # Build from source
42
74
 
43
75
  ```bash
@@ -49,8 +81,8 @@ import init, { format } from "@wasm-fmt/gofmt/vite";
49
81
  git clone https://github.com/wasm-fmt/gofmt.git
50
82
 
51
83
  # 4. build
52
- pnpm build
84
+ npm run build
53
85
 
54
- # 5. test
55
- pnpm run /^test:/
86
+ # 6. test
87
+ npm run test:node
56
88
  ```
package/gofmt.d.wasm.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * WebAssembly module for gofmt.
3
+ * @module
4
+ */
1
5
  export declare const memory: WebAssembly.Memory;
2
6
  export declare function _initialize(): void;
3
7
  export declare function alloc(size: number): number;
package/gofmt_entry.d.ts CHANGED
@@ -1,14 +1,11 @@
1
- export type InitInput =
2
- | RequestInfo
3
- | URL
4
- | Response
5
- | BufferSource
6
- | WebAssembly.Module;
7
- export type SyncInitInput = BufferSource | WebAssembly.Module;
8
- import type * as InitOutput from "./gofmt.d.wasm.ts";
1
+ /**
2
+ * gofmt default entry for formatting Go source code.
3
+ * @module
4
+ */
9
5
 
10
- export default function initAsync(
11
- init_input?: InitInput | Promise<InitInput>,
12
- ): Promise<InitOutput>;
13
- export declare function initSync(buffer_or_module: SyncInitInput): InitOutput;
6
+ /**
7
+ * Formats a Go source code.
8
+ * @param input - The Go source code to format
9
+ * @returns The formatted Go source code
10
+ */
14
11
  export declare function format(input: string): string;
package/gofmt_esm.js CHANGED
@@ -1,3 +1,9 @@
1
+ /* @ts-self-types="./gofmt_entry.d.ts" */
2
+ /**
3
+ * Loads the Wasm module via source phase import.
4
+ * @module
5
+ */
6
+ // prettier-ignore
1
7
  import source wasmModule from "./gofmt.wasm";
2
8
  import { format as _format } from "./gofmt.js";
3
9
  /**
@@ -9,17 +15,9 @@ const instance = new WebAssembly.Instance(wasmModule);
9
15
  /**
10
16
  * @type {WASM}
11
17
  */
12
- let wasm = instance.exports;
18
+ const wasm = instance.exports;
13
19
  wasm._initialize();
14
20
 
15
- export function initSync() {
16
- return wasm;
17
- }
18
-
19
- export default async function initAsync() {
20
- return wasm;
21
- }
22
-
23
21
  export function format(source) {
24
22
  return _format(wasm, source);
25
23
  }
package/gofmt_node.js CHANGED
@@ -1,101 +1,26 @@
1
1
  /* @ts-self-types="./gofmt_entry.d.ts" */
2
+ /**
3
+ * Loads the Wasm module using Node's fs API.
4
+ * Consider using `./esm` entry if your environment supports source phase import.
5
+ * @module
6
+ */
7
+ import { readFileSync } from "node:fs";
2
8
  import { format as _format } from "./gofmt.js";
3
9
 
4
- let wasm, wasmModule;
10
+ const wasmUrl = new URL("gofmt.wasm", import.meta.url);
11
+ const wasmBytes = readFileSync(wasmUrl);
12
+ const wasmModule = new WebAssembly.Module(wasmBytes);
5
13
 
6
- export function initSync(buffer_or_module) {
7
- if (wasm !== void 0) return wasm;
14
+ /**
15
+ * @import * as WASM from "./gofmt.wasm"
16
+ */
8
17
 
9
- if (!(buffer_or_module instanceof WebAssembly.Module)) {
10
- buffer_or_module = new WebAssembly.Module(buffer_or_module);
11
- }
12
-
13
- return finalize_init(
14
- new WebAssembly.Instance(buffer_or_module),
15
- buffer_or_module,
16
- );
17
- }
18
-
19
- export default async function initAsync(init_input) {
20
- if (wasm !== void 0) return wasm;
21
-
22
- if (init_input === void 0) {
23
- init_input = new URL("gofmt.wasm", import.meta.url);
24
- }
25
-
26
- if (typeof init_input === "string") {
27
- init_input = new URL(init_input);
28
- }
29
-
30
- if (init_input instanceof URL && init_input.protocol === "file:") {
31
- const [{ readFile }, { fileURLToPath }] = await Promise.all([
32
- import("fs/promises"),
33
- import("url"),
34
- ]);
35
- init_input = readFile(fileURLToPath(init_input));
36
- } else if (
37
- (typeof Request === "function" && init_input instanceof Request) ||
38
- init_input instanceof URL
39
- ) {
40
- init_input = fetch(init_input);
41
- }
42
-
43
- const { instance, module } = await load(await init_input);
44
-
45
- return finalize_init(instance, module);
46
- }
47
-
48
- async function load(module, imports) {
49
- if (typeof Response === "function" && module instanceof Response) {
50
- if (typeof WebAssembly.instantiateStreaming === "function") {
51
- try {
52
- return await WebAssembly.instantiateStreaming(module, imports);
53
- } catch (e) {
54
- const validResponse =
55
- module.ok && expectedResponseType(module.type);
56
-
57
- if (
58
- validResponse &&
59
- module.headers.get("Content-Type") !== "application/wasm"
60
- ) {
61
- console.warn(
62
- "`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
63
- e,
64
- );
65
- } else {
66
- throw e;
67
- }
68
- }
69
- }
70
-
71
- const bytes = await module.arrayBuffer();
72
- return await WebAssembly.instantiate(bytes, imports);
73
- } else {
74
- const instance = await WebAssembly.instantiate(module, imports);
75
-
76
- if (instance instanceof WebAssembly.Instance) {
77
- return { instance, module };
78
- } else {
79
- return instance;
80
- }
81
- }
82
-
83
- function expectedResponseType(type) {
84
- switch (type) {
85
- case "basic":
86
- case "cors":
87
- case "default":
88
- return true;
89
- }
90
- return false;
91
- }
92
- }
93
-
94
- function finalize_init(instance, module) {
95
- wasm = instance.exports, wasmModule = module;
96
- wasm._initialize();
97
- return wasm;
98
- }
18
+ const instance = new WebAssembly.Instance(wasmModule);
19
+ /**
20
+ * @type {WASM}
21
+ */
22
+ const wasm = instance.exports;
23
+ wasm._initialize();
99
24
 
100
25
  export function format(source) {
101
26
  return _format(wasm, source);
package/gofmt_vite.js CHANGED
@@ -1,9 +1,37 @@
1
- /* @ts-self-types="./gofmt_entry.d.ts" */
1
+ /* @ts-self-types="./gofmt_web.d.ts" */
2
+ /**
3
+ * Loads the Wasm module for Vite and bundlers supporting `?init` imports.
4
+ * @module
5
+ */
6
+ import init from "./gofmt.wasm?init";
2
7
  import initAsync from "./gofmt_web.js";
3
- import wasm_url from "./gofmt.wasm?url";
8
+ import { format as _format } from "./gofmt.js";
4
9
 
5
- export default function (input = wasm_url) {
6
- return initAsync(input);
10
+ let wasm, wasmModule;
11
+
12
+ function finalize_init(instance, module) {
13
+ wasm = instance.exports;
14
+ wasmModule = module;
15
+ wasm._initialize();
16
+ return wasm;
17
+ }
18
+
19
+ export default async function initAsync() {
20
+ if (wasm !== void 0) return wasm;
21
+ const instance = await init();
22
+ return finalize_init(instance);
7
23
  }
8
24
 
9
- export * from "./gofmt_web.js";
25
+ export function initSync(module) {
26
+ if (wasm !== void 0) return wasm;
27
+
28
+ if (!(module instanceof WebAssembly.Module)) {
29
+ module = new WebAssembly.Module(module);
30
+ }
31
+ const instance = new WebAssembly.Instance(module);
32
+ return finalize_init(instance, module);
33
+ }
34
+
35
+ export function format(source) {
36
+ return _format(wasm, source);
37
+ }
package/gofmt_web.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * gofmt entry with initialization functions.
3
+ * @module
4
+ */
5
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
6
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
7
+ import type * as InitOutput from "./gofmt.wasm";
8
+
9
+ /**
10
+ * Initializes the WASM module asynchronously.
11
+ * @param init_input - Optional URL/path to the WASM file, or any valid InitInput
12
+ */
13
+ export default function initAsync(init_input?: InitInput): Promise<InitOutput>;
14
+ /**
15
+ * Initializes the WASM module synchronously.
16
+ * @param buffer_or_module - The WASM module or buffer source
17
+ */
18
+ export declare function initSync(buffer_or_module: BufferSource | WebAssembly.Module): InitOutput;
19
+
20
+ export * from "gofmt_entry.d.ts";
package/gofmt_web.js CHANGED
@@ -1,4 +1,9 @@
1
- /* @ts-self-types="./gofmt_entry.d.ts" */
1
+ /* @ts-self-types="./gofmt_web.d.ts" */
2
+ /**
3
+ * Loads the Wasm module via Web Fetch API (browsers).
4
+ * Requires calling init().
5
+ * @module
6
+ */
2
7
  import { format as _format } from "./gofmt.js";
3
8
  let wasm, wasmModule;
4
9
 
@@ -8,13 +13,9 @@ async function load(module, imports) {
8
13
  try {
9
14
  return await WebAssembly.instantiateStreaming(module, imports);
10
15
  } catch (e) {
11
- const validResponse =
12
- module.ok && expectedResponseType(module.type);
16
+ const validResponse = module.ok && expectedResponseType(module.type);
13
17
 
14
- if (
15
- validResponse &&
16
- module.headers.get("Content-Type") !== "application/wasm"
17
- ) {
18
+ if (validResponse && module.headers.get("Content-Type") !== "application/wasm") {
18
19
  console.warn(
19
20
  "`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
20
21
  e,
@@ -49,7 +50,7 @@ async function load(module, imports) {
49
50
  }
50
51
 
51
52
  function finalize_init(instance, module) {
52
- wasm = instance.exports, wasmModule = module;
53
+ ((wasm = instance.exports), (wasmModule = module));
53
54
  wasm._initialize();
54
55
  return wasm;
55
56
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@wasm-fmt/gofmt",
3
3
  "description": "A wasm based golang formatter",
4
4
  "author": "magic-akari <akari.ccino@gamil.com>",
5
- "version": "0.5.1",
5
+ "version": "0.6.0",
6
6
  "license": "MIT",
7
7
  "keywords": [
8
8
  "wasm",
@@ -21,18 +21,37 @@
21
21
  "exports": {
22
22
  ".": {
23
23
  "types": "./gofmt_entry.d.ts",
24
+ "webpack": "./gofmt_bundle.js",
24
25
  "node": "./gofmt_node.js",
26
+ "default": "./gofmt_esm.js"
27
+ },
28
+ "./esm": {
29
+ "types": "./gofmt_entry.d.ts",
30
+ "default": "./gofmt_esm.js"
31
+ },
32
+ "./node": {
33
+ "types": "./gofmt_entry.d.ts",
34
+ "default": "./gofmt_node.js"
35
+ },
36
+ "./bundler": {
37
+ "types": "./gofmt_entry.d.ts",
38
+ "default": "./gofmt_bundle.js"
39
+ },
40
+ "./web": {
41
+ "types": "./gofmt_web.d.ts",
25
42
  "default": "./gofmt_web.js"
26
43
  },
27
44
  "./vite": {
28
- "types": "./gofmt_entry.d.ts",
45
+ "types": "./gofmt_web.d.ts",
29
46
  "default": "./gofmt_vite.js"
30
47
  },
48
+ "./wasm": "./gofmt.wasm",
31
49
  "./package.json": "./package.json",
32
50
  "./*": "./*"
33
51
  },
34
52
  "scripts": {
35
53
  "build": "./scripts/build.sh",
54
+ "fmt": "dprint fmt",
36
55
  "test:go": "go test ./src -v",
37
56
  "test:node": "node --test test_node/node.test.js",
38
57
  "test:deno": "deno test test_deno --allow-read",