@wasm-fmt/dart_fmt 0.1.0 → 0.1.2

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 CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  # Install
4
4
 
5
- [![npm](https://img.shields.io/npm/v/@wasm-fmt/dart_fmt)](https://www.npmjs.com/package/@wasm-fmt/dart_fmt)
5
+ [![npm](https://img.shields.io/npm/v/@wasm-fmt/dart_fmt?color=00B4AB)](https://www.npmjs.com/package/@wasm-fmt/dart_fmt)
6
6
 
7
7
  ```bash
8
8
  npm install @wasm-fmt/dart_fmt
9
9
  ```
10
10
 
11
- [![jsr.io](https://jsr.io/badges/@fmt/dart-fmt)](https://jsr.io/@fmt/dart-fmt)
11
+ [![jsr.io](https://jsr.io/badges/@fmt/dart-fmt?color=00B4AB)](https://jsr.io/@fmt/dart-fmt)
12
12
 
13
13
  ```bash
14
14
  npx jsr add @fmt/dart-fmt
@@ -29,8 +29,26 @@ console.log(formatted);
29
29
 
30
30
  For Vite users:
31
31
 
32
+ Add `"@wasm-fmt/dart_fmt"` to `optimizeDeps.exclude` in your vite config:
33
+
34
+ ```JSON
35
+ {
36
+ "optimizeDeps": {
37
+ "exclude": ["@wasm-fmt/dart_fmt"]
38
+ }
39
+ }
40
+ ```
41
+
42
+ <details>
43
+ <summary>
44
+ If you cannot change the vite config, you can use another import entry
45
+
46
+ </summary>
47
+
32
48
  ```JavaScript
33
49
  import init, { format } from "@wasm-fmt/dart_fmt/vite";
34
50
 
35
51
  // ...
36
52
  ```
53
+
54
+ </details>
package/dart_fmt.js CHANGED
@@ -1,22 +1,10 @@
1
- import { instantiate } from "./dart_fmt.mjs";
1
+ import { format as dart_fmt, instantiate, invoke } from "./dart_fmt.mjs";
2
2
 
3
3
  let wasm;
4
4
 
5
5
  function get_imports() {}
6
6
  function init_memory() {}
7
7
 
8
- // export function initSync(module) {
9
- // if (wasm !== undefined) return wasm;
10
-
11
- // const imports = get_imports();
12
-
13
- // init_memory(imports);
14
-
15
- // module = normalize(module);
16
-
17
- // return (wasm = instantiate(module));
18
- // }
19
-
20
8
  function normalize(module) {
21
9
  if (!(module instanceof WebAssembly.Module)) {
22
10
  return new WebAssembly.Module(module);
@@ -42,9 +30,13 @@ export default async function (input) {
42
30
 
43
31
  init_memory(imports);
44
32
 
45
- return (wasm = await load(await input)
33
+ wasm = await load(await input)
46
34
  .then(normalize)
47
- .then(instantiate));
35
+ .then(instantiate);
36
+
37
+ invoke(wasm);
38
+
39
+ return wasm;
48
40
  }
49
41
 
50
42
  async function load(module) {
@@ -70,43 +62,7 @@ async function load(module) {
70
62
  return module;
71
63
  }
72
64
 
73
- function stringFromDartString(string) {
74
- const totalLength = wasm.exports.$stringLength(string);
75
- let result = "";
76
- let index = 0;
77
- while (index < totalLength) {
78
- let chunkLength = Math.min(totalLength - index, 0xffff);
79
- const array = new Array(chunkLength);
80
- for (let i = 0; i < chunkLength; i++) {
81
- array[i] = wasm.exports.$stringRead(string, index++);
82
- }
83
- result += String.fromCharCode(...array);
84
- }
85
- return result;
86
- }
87
-
88
- function stringToDartString(string) {
89
- const length = string.length;
90
- let range = 0;
91
- for (let i = 0; i < length; i++) {
92
- range |= string.codePointAt(i);
93
- }
94
- if (range < 256) {
95
- const dartString = wasm.exports.$stringAllocate1(length);
96
- for (let i = 0; i < length; i++) {
97
- wasm.exports.$stringWrite1(dartString, i, string.codePointAt(i));
98
- }
99
- return dartString;
100
- } else {
101
- const dartString = wasm.exports.$stringAllocate2(length);
102
- for (let i = 0; i < length; i++) {
103
- wasm.exports.$stringWrite2(dartString, i, string.charCodeAt(i));
104
- }
105
- return dartString;
106
- }
107
- }
108
-
109
- export function format(source, filename, config = {}) {
65
+ export function format(source, filename = "stdin.dart", config = {}) {
110
66
  const options = { lineEnding: "\n" };
111
67
  if (config.line_width) {
112
68
  options.pageWidth = config.line_width;
@@ -115,13 +71,11 @@ export function format(source, filename, config = {}) {
115
71
  options.lineEnding = "\r\n";
116
72
  }
117
73
 
118
- const sourceString = stringToDartString(source);
119
- const filenameString = stringToDartString(filename);
120
- const configString = stringToDartString(JSON.stringify(options));
121
- const result = wasm.exports.format(
122
- sourceString,
123
- filenameString,
124
- configString,
125
- );
126
- return stringFromDartString(result);
74
+ const result = dart_fmt(source, filename, JSON.stringify(options));
75
+ const err = result[0] === "x";
76
+ const output = result.slice(1);
77
+ if (err) {
78
+ throw new Error(output);
79
+ }
80
+ return output;
127
81
  }