Skip to content

Commit

Permalink
feat: add node and browser contract builder (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
eng-cc authored Jul 25, 2023
1 parent ff2a8a5 commit a4912d7
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 283 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"type": "module",
"homepage": "https://github.com/chain-web/trustack#readme",
"dependencies": {
"@types/jest": "^29.5.2",
"@types/node": "^20.4.1",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
"eslint": "^8.44.0",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.4",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-define-config": "^1.21.0",
"eslint-plugin-import": "^2.27.5",
Expand All @@ -38,7 +38,7 @@
"jest": "^29.6.1",
"lint-staged": "^13.2.3",
"prettier": "^2.8.8",
"simple-git-hooks": "^2.8.1",
"simple-git-hooks": "^2.9.0",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/contract_builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
"typings": "./src/index.ts",
"scripts": {
"test": "tsc -p ../tsconfig.build.json && node ./dist/src/cli.js build ./examples/coin/index.ts && rm -rf ./dist",
"dev-build": "tsc -w -p ./tsconfig.build.json",
"build": "tsc -p ./tsconfig.build.json"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@swc/core": "^1.3.68",
"@swc/wasm-web": "^1.3.66",
"@swc/core": "^1.3.70",
"@swc/wasm-web": "^1.3.70",
"@trustack/vm": "workspace:^1.0.0",
"cac": "^6.7.14",
"chalk": "^5.3.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/contract_builder/src/ast.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ExportDeclaration,
Expression,
ExpressionStatement,
Module,
Program,
Span,
} from '@swc/core';

Expand All @@ -15,7 +15,7 @@ export const BUILDER_NAMES = {
CONTRACT_CLASS_NAME: '__contract_class_name__',
};

export const walkTop = (ast: Module): Module => {
export const walkTop = (ast: Program): Program => {
// 处理import
while (ast.body[0].type === 'ImportDeclaration') {
const node = ast.body[0];
Expand Down
35 changes: 32 additions & 3 deletions packages/contract_builder/src/browser.ts
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;
};
49 changes: 11 additions & 38 deletions packages/contract_builder/src/builder.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { resolve } from 'path';
import { readFileSync, writeFileSync } from 'fs';
import { bytes } from 'multiformats';
import { Output, parseSync, printSync, transformSync } from '@swc/core';
import chalk from 'chalk';
import type { BuildOption } from './cli.js';
import { walkTop } from './ast.utils.js';
import type { Output, Program, Options, ParseOptions } from '@swc/core';

export const buildeCodeString = (code: string): Output => {
const opt = parseSync(code, {
export const buildCodeString = (
code: string,
buildConfig: {
parseSync: (code: string, opt: ParseOptions) => Program;
transformSync: (ast: Program, opt: Options) => Output;
},
): Output => {
const opt = buildConfig.parseSync(code, {
syntax: 'typescript',
target: 'es2022',
});
const contractAst = walkTop(opt);
const codeOpt = printSync(contractAst, {});
// const codeOpt = printSync(contractAst, {});
// console.log(opt);
// console.log(codeOpt);
const contractCode = transformSync(contractAst, {
const contractCode = buildConfig.transformSync(contractAst, {
jsc: {
parser: {
syntax: 'typescript',
Expand All @@ -26,31 +27,3 @@ export const buildeCodeString = (code: string): Output => {

return contractCode;
};

export const builder = async (input: string, opts: BuildOption) => {
try {
console.log(chalk.green('starting build contract...'));
input = resolve(input, './');
// console.log(codeSnippet);
let code = readFileSync(input).toString();
// console.log(code);
const res = buildeCodeString(code);
// console.log(contractCode);
const resultUint8 = bytes.fromString(res.code);
// const resultU8String = `export default new Uint8Array([${resultUint8.toString()}]);`;
// console.log(resultUint8);
writeFileSync(
resolve(input, '../index.contract.bin'),
resultUint8.toString(),
{
flag: 'w+',
},
);
writeFileSync(resolve(input, '../index.contract.js'), res.code, {
flag: 'w+',
});
console.log(chalk.green('contract build success'));
} catch (error) {
console.log(error);
}
};
40 changes: 39 additions & 1 deletion packages/contract_builder/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#!/usr/bin/env node
/* eslint-disable import/no-nodejs-modules */
/* eslint-disable no-console */
/* eslint-disable node/shebang */

import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { cac } from 'cac';
import chalk from 'chalk';
import { parseSync, transformSync } from '@swc/core';

import { bytes } from 'multiformats';
import pkg from '../package.json' assert { type: 'json' };
import { builder } from './builder.js';
import { buildCodeString } from './builder.js';
const version = pkg.version;
const cli = cac('sk-contract-builder');

Expand All @@ -13,6 +20,37 @@ export interface BuildOption {
watch?: boolean;
}

const builder = async (input: string, opts: BuildOption) => {

Check warning on line 23 in packages/contract_builder/src/cli.ts

View workflow job for this annotation

GitHub Actions / check

'opts' is defined but never used. Allowed unused args must match /^_/u
try {
console.log(chalk.green('starting build contract...'));
input = resolve(input, './');
// console.log(codeSnippet);
const code = readFileSync(input).toString();
// console.log(code);
const res = buildCodeString(code, {
parseSync,
transformSync,
});
// console.log(contractCode);
const resultUint8 = bytes.fromString(res.code);
// const resultU8String = `export default new Uint8Array([${resultUint8.toString()}]);`;
// console.log(resultUint8);
writeFileSync(
resolve(input, '../index.contract.bin'),
resultUint8.toString(),
{
flag: 'w+',
},
);
writeFileSync(resolve(input, '../index.contract.js'), res.code, {
flag: 'w+',
});
console.log(chalk.green('contract build success'));
} catch (error) {
console.log(error);
}
};

cli
.command('build [contract]')
.option('--output <path>', `[string] output file`)
Expand Down
5 changes: 4 additions & 1 deletion packages/contract_builder/src/index.ts
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 };
};
13 changes: 12 additions & 1 deletion packages/contract_builder/src/node.mts
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;
};
10 changes: 9 additions & 1 deletion packages/docs/src/content/docs/zh-cn/dev_log/202307.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ description: ''
- run: git submodule update --init --recursive
```
- [ ] 解决了 GitHub action 问题时发现 yml 配置中有很多重复的配置,比如上面配置,可以将这些配置提取到一个单独的文件中,然后在 yml 中引入
- [ ] 解决 GitHub action 问题时发现 yml 配置中有很多重复的配置,比如上面配置,可以将这些配置提取到一个单独的文件中,然后在 yml 中引入
### 7-20~7-25
被 swc 的 bug 卡了几天
- [ ] 计划把在浏览器中加载并编译合约的流程跑通(sad),但因为 swc-wasm 的问题,暂时没法跑通,等待 swc-wasm 的[修复](https://github.com/swc-project/swc/issues/7140)
- [x] 整理了 contract_builder 的代码,实现 cli、node、browser 三种方式的导出
- [x] vite dev 处理@swc/wasm-web,不能走预编译,配置了排除
1 change: 1 addition & 0 deletions packages/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@trustack/common": "workspace:^1.0.0",
"@trustack/contract": "workspace:^1.0.0",
"@trustack/contract_builder": "workspace:^1.0.0",
"@trustack/node-modules-polyfill": "^0.2.5",
"@trustack/rollup-plugin-node-polyfills": "^0.2.2",
"@xstate/react": "^3.2.2",
Expand Down
1 change: 1 addition & 0 deletions packages/webui/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default defineConfig({
target: 'es2020',
supported: { bigint: true },
},
exclude: ['@swc/wasm-web'],
},
define: {
global: 'globalThis',
Expand Down
Loading

0 comments on commit a4912d7

Please sign in to comment.