Skip to content

Commit

Permalink
feat(wasm): Support more targets (JohnnyMorganz#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Jan 27, 2024
1 parent c6e8619 commit 049bb7c
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 16 deletions.
7 changes: 5 additions & 2 deletions wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
stylua.bundler/
stylua.node/
stylua.web/
stylua_lib.cjs
LICENSE.md
README.md
*.tgz
9 changes: 7 additions & 2 deletions wasm/build-wasm.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# TODO: Ensure that version is up to date
cp README.md wasm/
cp LICENSE.md wasm/
npx [email protected] build --target nodejs --out-dir wasm/stylua.node -- --features lua52,lua53,lua54,luau
npx [email protected] build --target bundler --out-dir wasm/stylua.bundler -- --features lua52,lua53,lua54,luau
npx [email protected] build --target web --out-dir wasm/stylua.web -- --features lua52,lua53,lua54,luau

# workaround for bundler usage
echo "export { getImports as __getImports, finalizeInit as __finalizeInit }" >> wasm/stylua.web/stylua_lib.js

# bundle for node CommonJS
npx [email protected] wasm/src/stylua_lib_node.cjs --file wasm/stylua_lib.cjs --format cjs
70 changes: 58 additions & 12 deletions wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,66 @@
"url": "https://github.com/johnnymorganz/stylua"
},
"files": [
"stylua.node/stylua_lib_bg.wasm",
"stylua.node/stylua_lib.js",
"stylua.node/stylua_lib_bg.js",
"stylua.node/stylua_lib.d.ts",
"stylua.bundler/stylua_lib_bg.wasm",
"stylua.bundler/stylua_lib.js",
"stylua.bundler/stylua_lib_bg.js",
"stylua.bundler/stylua_lib.d.ts",
"stylua.web/stylua_lib_bg.wasm.d.ts",
"stylua.web/stylua_lib_bg.wasm",
"stylua.web/stylua_lib.d.ts",
"stylua.web/stylua_lib.js",
"stylua_lib_bundler_wbg.cjs",
"stylua_lib_bundler.d.ts",
"stylua_lib_bundler.js",
"stylua_lib.cjs",
"stylua_lib.d.cts",
"stylua_lib.d.mts",
"stylua_lib.mjs",
"LICENSE.md"
],
"main": "stylua.node/stylua_lib.js",
"module": "stylua.bundler/stylua_lib.js",
"types": "stylua.bundler/stylua_lib.d.ts",
"sideEffects": false,
"type": "module",
"main": "stylua_lib.cjs",
"module": "stylua_lib_bundler.js",
"types": "stylua.web/stylua_lib.d.ts",
"exports": {
".": {
"webpack": {
"node": "./stylua_lib.mjs",
"default": "./stylua_lib_bundler.js"
},
"require": {
"types": "./stylua_lib.d.cts",
"default": "./stylua_lib.cjs"
},
"node": {
"types": "./stylua_lib.d.mts",
"default": "./stylua_lib.mjs"
},
"deno": {
"types": "./stylua_lib.d.mts",
"default": "./stylua_lib.mjs"
},
"bun": {
"types": "./stylua_lib.d.mts",
"default": "./stylua_lib.mjs"
},
"default": {
"types": "./stylua_lib_bundler.d.ts",
"default": "./stylua_lib_bundler.js"
}
},
"./web": {
"types": "./stylua.web/stylua_lib.d.ts",
"default": "./stylua.web/stylua_lib.js"
},
"./web/*": "./stylua.web/*",
"./stylua_lib_bg.wasm": "./stylua.web/stylua_lib_bg.wasm",
"./package.josn": "./package.josn",
"./*": "./*"
},
"browser": {
"wbg": "./stylua_lib_bundler_wbg.cjs"
},
"sideEffects": [
"stylua_lib.mjs",
"stylua_lib_bundler.js"
],
"keywords": [
"cli",
"utility",
Expand Down
7 changes: 7 additions & 0 deletions wasm/src/stylua_lib_node.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from "../stylua.web/stylua_lib.js";
import { initSync } from "../stylua.web/stylua_lib.js";

const path = require("path").join(__dirname, "stylua.web/stylua_lib_bg.wasm");
const bytes = require("fs").readFileSync(path);

initSync(bytes);
2 changes: 2 additions & 0 deletions wasm/stylua_lib.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type * from "./stylua.web/stylua_lib";
export declare const initSync: never;
2 changes: 2 additions & 0 deletions wasm/stylua_lib.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type * from "./stylua.web/stylua_lib";
export declare const initSync: never;
7 changes: 7 additions & 0 deletions wasm/stylua_lib.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from "node:fs";
import { initSync } from "./stylua.web/stylua_lib.js";

const wasm = new URL("./stylua.web/stylua_lib_bg.wasm", import.meta.url);
initSync(fs.readFileSync(wasm));

export * from "./stylua.web/stylua_lib.js";
2 changes: 2 additions & 0 deletions wasm/stylua_lib_bundler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type * from "./stylua.web/stylua_lib";
export declare const initSync: never;
7 changes: 7 additions & 0 deletions wasm/stylua_lib_bundler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as wasm from "./stylua.web/stylua_lib_bg.wasm";

import { __finalizeInit } from "./stylua.web/stylua_lib.js";

__finalizeInit({ exports: wasm });

export * from "./stylua.web/stylua_lib.js";
1 change: 1 addition & 0 deletions wasm/stylua_lib_bundler_wbg.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./stylua.web/stylua_lib.js").__getImports().wbg;

0 comments on commit 049bb7c

Please sign in to comment.