Skip to content

Commit 29ad3fe

Browse files
authored
fix: update dnt and make changes to get it working on node (#5)
1 parent d1433f8 commit 29ad3fe

9 files changed

Lines changed: 83 additions & 50 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//}
1515
- uses: actions/setup-node@v2
1616
with:
17-
node-version: '14.x'
17+
node-version: '16.x'
1818
registry-url: 'https://registry.npmjs.org'
1919
- name: npm build
2020
run: deno run -A --no-check ./scripts/build_npm.ts ${{steps.get_tag_version.outputs.TAG_VERSION}}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
npm
1+
npm

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2021 David Sherret
3+
Copyright (c) 2020-2022 David Sherret
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ console.log(tsFormatter.formatText("file.ts", "const t = 5;"));
3535

3636
```ts
3737
import { createFromBuffer } from "@dprint/formatter";
38+
import { getBuffer } from "@dprint/json";
3839

39-
const formatter = createFromBuffer(fs.readFileSync("./json.wasm"));
40+
// or provide something like fs.readFileSync("./json.wasm")
41+
const formatter = createFromBuffer(getBuffer());
4042

4143
console.log(formatter.formatText("test.json", "{test: 5}"));
4244
```

bvm.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"binaries": [
33
{
4-
"path": "https://bvm.land/deno/1.14.3.json",
5-
"checksum": "270150c4a72748bcb630da76d4402a0b4c99702f034cba62724f05e792116a9e",
6-
"version": "1.14.3"
4+
"path": "https://bvm.land/deno/1.17.2.json",
5+
"checksum": "ca0aff221bb79cf83ac67a1abd568ed1390211589db251e148b560c7b0ea3fbe",
6+
"version": "1.17.2"
77
}
88
]
99
}

dprint.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"**/*-lock.json"
1515
],
1616
"plugins": [
17-
"https://plugins.dprint.dev/typescript-0.57.0.wasm",
18-
"https://plugins.dprint.dev/json-0.13.0.wasm",
19-
"https://plugins.dprint.dev/markdown-0.10.0.wasm"
17+
"https://plugins.dprint.dev/typescript-0.61.0.wasm",
18+
"https://plugins.dprint.dev/json-0.14.0.wasm",
19+
"https://plugins.dprint.dev/markdown-0.12.0.wasm"
2020
]
2121
}

mod.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// Copyright 2020-2021 by David Sherret. All rights reserved.
2-
// This work is licensed under the terms of the MIT license.
3-
// For a copy, see <https://opensource.org/licenses/MIT>.
4-
51
/** Formats code. */
62
export interface Formatter {
73
/**
@@ -89,17 +85,40 @@ export function createImportObject(): WebAssembly.Imports {
8985
};
9086
}
9187

88+
export interface ResponseLike {
89+
arrayBuffer(): Promise<BufferSource>;
90+
}
91+
9292
/**
9393
* Creates a formatter from the specified streaming source.
9494
* @remarks This is the most efficient way to create a formatter.
9595
* @param response - The streaming source to create the formatter from.
9696
*/
9797
export function createStreaming(
98-
response: Promise<Response>,
98+
response: Promise<ResponseLike>,
9999
): Promise<Formatter> {
100-
return WebAssembly
101-
.instantiateStreaming(response,createImportObject())
102-
.then((obj) => createFromInstance(obj.instance));
100+
if (typeof WebAssembly.instantiateStreaming === "function") {
101+
return WebAssembly
102+
// deno-lint-ignore no-explicit-any
103+
.instantiateStreaming(response as any, createImportObject())
104+
.then((obj) => createFromInstance(obj.instance));
105+
} else {
106+
// fallback for node.js
107+
return getArrayBuffer()
108+
.then((buffer) => createFromBuffer(buffer));
109+
}
110+
111+
function getArrayBuffer() {
112+
if (isResponse(response)) {
113+
return response.arrayBuffer();
114+
} else {
115+
return response.then((response) => response.arrayBuffer());
116+
}
117+
118+
function isResponse(response: unknown): response is ResponseLike {
119+
return (response as Response).arrayBuffer != null;
120+
}
121+
}
103122
}
104123

105124
/**

mod_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts";
1+
import { assertEquals } from "https://deno.land/std@0.113.0/testing/asserts.ts";
22
import { createFromBuffer, createStreaming, Formatter, GlobalConfiguration } from "./mod.ts";
33

44
Deno.test("it should create streaming", async () => {

scripts/build_npm.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
1-
import { build } from "https://deno.land/x/dnt@0.0.11/mod.ts";
2-
3-
await build({
4-
entryPoints: ["mod.ts"],
5-
typeCheck: true,
6-
test: false,
7-
outDir: "./npm",
8-
package: {
9-
name: "@dprint/formatter",
10-
version: Deno.args[0],
11-
description: "Wasm formatter for dprint plugins.",
12-
repository: {
13-
type: "git",
14-
url: "git+https://github.com/dprint/js-formatter.git",
15-
},
16-
keywords: [
17-
"dprint",
18-
"formatter",
19-
"wasm",
20-
],
21-
author: "David Sherret",
22-
license: "MIT",
23-
bugs: {
24-
url: "https://github.com/dprint/js-formatter/issues",
25-
},
26-
homepage: "https://github.com/dprint/js-formatter#readme",
27-
},
28-
});
29-
30-
Deno.copyFileSync("LICENSE", "npm/LICENSE");
31-
Deno.copyFileSync("README.md", "npm/README.md");
1+
import { build } from "https://deno.land/x/dnt@0.14.0/mod.ts";
2+
3+
await build({
4+
entryPoints: ["mod.ts"],
5+
typeCheck: true,
6+
test: true,
7+
outDir: "./npm",
8+
shims: {
9+
deno: {
10+
test: "dev",
11+
},
12+
customDev: [{
13+
globalNames: ["fetch"],
14+
package: {
15+
name: "undici",
16+
version: "^4.12.1",
17+
},
18+
}],
19+
},
20+
package: {
21+
name: "@dprint/formatter",
22+
version: Deno.args[0],
23+
description: "Wasm formatter for dprint plugins.",
24+
repository: {
25+
type: "git",
26+
url: "git+https://github.com/dprint/js-formatter.git",
27+
},
28+
keywords: [
29+
"dprint",
30+
"formatter",
31+
"wasm",
32+
],
33+
author: "David Sherret",
34+
license: "MIT",
35+
bugs: {
36+
url: "https://github.com/dprint/js-formatter/issues",
37+
},
38+
homepage: "https://github.com/dprint/js-formatter#readme",
39+
},
40+
});
41+
42+
Deno.copyFileSync("LICENSE", "npm/LICENSE");
43+
Deno.copyFileSync("README.md", "npm/README.md");

0 commit comments

Comments
 (0)