Skip to content

Commit 049bb7c

Browse files
authored
feat(wasm): Support more targets (#848)
1 parent c6e8619 commit 049bb7c

10 files changed

+98
-16
lines changed

wasm/.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
stylua.bundler/
2-
stylua.node/
1+
stylua.web/
2+
stylua_lib.cjs
3+
LICENSE.md
4+
README.md
5+
*.tgz

wasm/build-wasm.sh

100644100755
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# TODO: Ensure that version is up to date
22
cp README.md wasm/
33
cp LICENSE.md wasm/
4-
npx [email protected] build --target nodejs --out-dir wasm/stylua.node -- --features lua52,lua53,lua54,luau
5-
npx [email protected] build --target bundler --out-dir wasm/stylua.bundler -- --features lua52,lua53,lua54,luau
4+
npx [email protected] build --target web --out-dir wasm/stylua.web -- --features lua52,lua53,lua54,luau
5+
6+
# workaround for bundler usage
7+
echo "export { getImports as __getImports, finalizeInit as __finalizeInit }" >> wasm/stylua.web/stylua_lib.js
8+
9+
# bundle for node CommonJS
10+
npx [email protected] wasm/src/stylua_lib_node.cjs --file wasm/stylua_lib.cjs --format cjs

wasm/package.json

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,66 @@
1212
"url": "https://github.com/johnnymorganz/stylua"
1313
},
1414
"files": [
15-
"stylua.node/stylua_lib_bg.wasm",
16-
"stylua.node/stylua_lib.js",
17-
"stylua.node/stylua_lib_bg.js",
18-
"stylua.node/stylua_lib.d.ts",
19-
"stylua.bundler/stylua_lib_bg.wasm",
20-
"stylua.bundler/stylua_lib.js",
21-
"stylua.bundler/stylua_lib_bg.js",
22-
"stylua.bundler/stylua_lib.d.ts",
15+
"stylua.web/stylua_lib_bg.wasm.d.ts",
16+
"stylua.web/stylua_lib_bg.wasm",
17+
"stylua.web/stylua_lib.d.ts",
18+
"stylua.web/stylua_lib.js",
19+
"stylua_lib_bundler_wbg.cjs",
20+
"stylua_lib_bundler.d.ts",
21+
"stylua_lib_bundler.js",
22+
"stylua_lib.cjs",
23+
"stylua_lib.d.cts",
24+
"stylua_lib.d.mts",
25+
"stylua_lib.mjs",
2326
"LICENSE.md"
2427
],
25-
"main": "stylua.node/stylua_lib.js",
26-
"module": "stylua.bundler/stylua_lib.js",
27-
"types": "stylua.bundler/stylua_lib.d.ts",
28-
"sideEffects": false,
28+
"type": "module",
29+
"main": "stylua_lib.cjs",
30+
"module": "stylua_lib_bundler.js",
31+
"types": "stylua.web/stylua_lib.d.ts",
32+
"exports": {
33+
".": {
34+
"webpack": {
35+
"node": "./stylua_lib.mjs",
36+
"default": "./stylua_lib_bundler.js"
37+
},
38+
"require": {
39+
"types": "./stylua_lib.d.cts",
40+
"default": "./stylua_lib.cjs"
41+
},
42+
"node": {
43+
"types": "./stylua_lib.d.mts",
44+
"default": "./stylua_lib.mjs"
45+
},
46+
"deno": {
47+
"types": "./stylua_lib.d.mts",
48+
"default": "./stylua_lib.mjs"
49+
},
50+
"bun": {
51+
"types": "./stylua_lib.d.mts",
52+
"default": "./stylua_lib.mjs"
53+
},
54+
"default": {
55+
"types": "./stylua_lib_bundler.d.ts",
56+
"default": "./stylua_lib_bundler.js"
57+
}
58+
},
59+
"./web": {
60+
"types": "./stylua.web/stylua_lib.d.ts",
61+
"default": "./stylua.web/stylua_lib.js"
62+
},
63+
"./web/*": "./stylua.web/*",
64+
"./stylua_lib_bg.wasm": "./stylua.web/stylua_lib_bg.wasm",
65+
"./package.josn": "./package.josn",
66+
"./*": "./*"
67+
},
68+
"browser": {
69+
"wbg": "./stylua_lib_bundler_wbg.cjs"
70+
},
71+
"sideEffects": [
72+
"stylua_lib.mjs",
73+
"stylua_lib_bundler.js"
74+
],
2975
"keywords": [
3076
"cli",
3177
"utility",

wasm/src/stylua_lib_node.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export * from "../stylua.web/stylua_lib.js";
2+
import { initSync } from "../stylua.web/stylua_lib.js";
3+
4+
const path = require("path").join(__dirname, "stylua.web/stylua_lib_bg.wasm");
5+
const bytes = require("fs").readFileSync(path);
6+
7+
initSync(bytes);

wasm/stylua_lib.d.cts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type * from "./stylua.web/stylua_lib";
2+
export declare const initSync: never;

wasm/stylua_lib.d.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type * from "./stylua.web/stylua_lib";
2+
export declare const initSync: never;

wasm/stylua_lib.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fs from "node:fs";
2+
import { initSync } from "./stylua.web/stylua_lib.js";
3+
4+
const wasm = new URL("./stylua.web/stylua_lib_bg.wasm", import.meta.url);
5+
initSync(fs.readFileSync(wasm));
6+
7+
export * from "./stylua.web/stylua_lib.js";

wasm/stylua_lib_bundler.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type * from "./stylua.web/stylua_lib";
2+
export declare const initSync: never;

wasm/stylua_lib_bundler.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as wasm from "./stylua.web/stylua_lib_bg.wasm";
2+
3+
import { __finalizeInit } from "./stylua.web/stylua_lib.js";
4+
5+
__finalizeInit({ exports: wasm });
6+
7+
export * from "./stylua.web/stylua_lib.js";

wasm/stylua_lib_bundler_wbg.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./stylua.web/stylua_lib.js").__getImports().wbg;

0 commit comments

Comments
 (0)