-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add node and browser contract builder (#114)
- Loading branch information
Showing
12 changed files
with
618 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
export { BUILDER_NAMES } from './ast.utils.js'; | ||
import type { Output, Program } from '@swc/wasm-web'; | ||
import swc, { parseSync, transform } from '@swc/wasm-web'; | ||
import { walkTop } from './ast.utils.js'; | ||
import type { BuildCodeString } from './index.js'; | ||
|
||
// TODO | ||
export const buildeCodeString = (code: string): { code: string } => { | ||
return { code }; | ||
export const builder = async (code: string): Promise<Output> => { | ||
const opt = parseSync(code, { | ||
syntax: 'typescript', | ||
target: 'es2022', | ||
}); | ||
const contractAst = walkTop(opt) as Program; | ||
// const codeOpt = printSync(contractAst, {}); | ||
// console.log(opt); | ||
// console.log(codeOpt); | ||
// TODO fix error, by https://github.com/swc-project/swc/issues/7140 | ||
const contractCode = await transform(contractAst, { | ||
jsc: { | ||
parser: { | ||
syntax: 'typescript', | ||
}, | ||
target: 'es2022', | ||
}, | ||
}); | ||
|
||
return contractCode; | ||
}; | ||
|
||
export const buildCodeString: BuildCodeString = async (code) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await (swc as any)(); | ||
const buildCode = await builder(code); | ||
|
||
return buildCode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
export { BUILDER_NAMES } from './ast.utils.js'; | ||
export type BuildCodeString = (code: string) => Promise<{ code: string }>; | ||
|
||
export const buildeCodeString = (code: string): { code: string } => { | ||
// only for export type | ||
// runtime code will be export by ./node.mts and ./browser.mts | ||
export const buildCodeString: BuildCodeString = async (code) => { | ||
return { code }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
export { buildeCodeString } from './builder.js'; | ||
export { BUILDER_NAMES } from './ast.utils.js'; | ||
import { parseSync, transformSync } from '@swc/core'; | ||
import { buildCodeString as builder } from './builder.js'; | ||
import type { BuildCodeString } from './index.js'; | ||
|
||
export const buildCodeString: BuildCodeString = async (code) => { | ||
const buildCode = builder(code, { | ||
parseSync, | ||
transformSync, | ||
}); | ||
|
||
return buildCode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.