Skip to content

Commit

Permalink
binding: support typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Jan 27, 2024
1 parent 11eb6c2 commit 95d60dd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/binding/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"peerDependencies": {
"@ruby/head-wasm-wasi": "2.3",
"@ruby/wasm-wasi": "2.3",
"@swc/core": "1.3",
"primate": "0.27",
"pyodide": "0.24"
},
Expand All @@ -35,6 +36,9 @@
"@ruby/wasm-wasi": {
"optional": true
},
"swc/core" :{
"optional": true
},
"pyodide": {
"optional": true
}
Expand Down
1 change: 1 addition & 0 deletions packages/binding/src/bindings/exports.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as go } from "./go/module.js";
export { default as python } from "./python/module.js";
export { default as ruby } from "./ruby/module.js";
export { default as typescript } from "./typescript/module.js";
15 changes: 15 additions & 0 deletions packages/binding/src/bindings/typescript/imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import swc from "@swc/core";

const options = {
jsc: {
parser: {
syntax: "typescript",
},
target: "esnext",
},
};


const transform = code => swc.transform(code, options);

export default async code => (await transform(code)).code;
34 changes: 34 additions & 0 deletions packages/binding/src/bindings/typescript/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { filter } from "rcompat/object";
import { peers } from "../common/exports.js";
import depend from "../depend.js";

export default ({
extension = ".ts",
} = {}) => {
const name = "typescript";
const dependencies = ["@swc/core"];
const on = filter(peers, ([key]) => dependencies.includes(key));
let transpile;

return {
name: `primate:${name}`,
async init(app, next) {
await depend(on, `binding:${name}`);

transpile = (await import("./imports.js")).default;

return next(app);
},
async stage(app, next) {
app.register(extension, {
route: async (directory, file) => {
const path = directory.join(file);
const base = path.directory;
const js = path.base.concat(".js");
await base.join(js).write((await transpile(await path.text())));
},
});
return next(app);
},
};
};

0 comments on commit 95d60dd

Please sign in to comment.