Skip to content

Commit b45b6c6

Browse files
committed
feat: contract builder can build single file result
1 parent 04fa7b2 commit b45b6c6

File tree

10 files changed

+379
-44
lines changed

10 files changed

+379
-44
lines changed

packages/contract/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
"engines": {
1414
"node": ">=16.0.0"
1515
},
16+
"exports": {
17+
".": {
18+
"import": "./dist/index.mjs",
19+
"types": "./dist/index.d.mts"
20+
},
21+
"./contractHelper": {
22+
"import": "./dist/contractHelper.mjs",
23+
"types": "./dist/contractHelper.d.mts"
24+
},
25+
"./contract": {
26+
"import": "./dist/contract.mjs",
27+
"types": "./src/contract.d.ts"
28+
}
29+
},
1630
"keywords": [],
1731
"author": "",
1832
"license": "ISC",

packages/contract_builder/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616
"author": "",
1717
"license": "ISC",
1818
"dependencies": {
19+
"@rollup/plugin-commonjs": "^25.0.4",
20+
"@rollup/plugin-node-resolve": "^15.1.0",
21+
"@rollup/plugin-terser": "^0.4.3",
22+
"@rollup/plugin-typescript": "^11.1.2",
23+
"@rollup/plugin-wasm": "^6.1.3",
1924
"@swc/core": "^1.3.72",
2025
"@swc/wasm-web": "^1.3.72",
2126
"@trustack/vm": "workspace:^1.0.0",
2227
"cac": "^6.7.14",
2328
"chalk": "^5.3.0",
24-
"multiformats": "^11.0.2"
29+
"multiformats": "^11.0.2",
30+
"rollup": "^3.28.0",
31+
"rollup-plugin-typescript2": "^0.35.0",
32+
"tslib": "^2.6.1",
33+
"typescript": "^5.1.6"
2534
}
2635
}

packages/contract_builder/src/ast.utils.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,28 @@ export const BUILDER_NAMES = {
1717

1818
export const walkTop = (ast: Program): Program => {
1919
// 处理import
20-
while (ast.body[0].type === 'ImportDeclaration') {
21-
const node = ast.body[0];
22-
if (node.source.value === BUILDER_NAMES.MAIN_PACKAGE) {
23-
ast.body.shift();
20+
ast.body.forEach((topNode, i) => {
21+
if (topNode.type === 'ImportDeclaration') {
22+
if (topNode.source.value === BUILDER_NAMES.MAIN_PACKAGE) {
23+
// remove import
24+
ast.body[i] = {
25+
type: 'EmptyStatement',
26+
span: topNode.span,
27+
};
28+
}
2429
}
25-
}
30+
});
2631

2732
// remove export
28-
ast.body = ast.body.map((topNode, i) => {
29-
if (
30-
['ExportDeclaration', 'ExportDefaultDeclaration'].includes(topNode.type)
31-
) {
32-
// remove export
33-
return (topNode as ExportDeclaration).declaration;
34-
}
35-
return topNode;
36-
});
33+
// ast.body = ast.body.map((topNode, i) => {
34+
// if (
35+
// ['ExportDeclaration', 'ExportDefaultDeclaration'].includes(topNode.type)
36+
// ) {
37+
// // remove export
38+
// return (topNode as ExportDeclaration).declaration;
39+
// }
40+
// return topNode;
41+
// });
3742

3843
// 除super之外的constructor逻辑
3944
const constructorExpressionStatements: ExpressionStatement[] = [];

packages/contract_builder/src/builder.ts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { walkTop } from './ast.utils.js';
2-
import type { Output, Program, Options, ParseOptions } from '@swc/core';
2+
import {
3+
Output,
4+
Program,
5+
Options,
6+
ParseOptions,
7+
parseSync,
8+
bundle,
9+
} from '@swc/core';
10+
import { rollup } from 'rollup';
11+
import terser from '@rollup/plugin-terser';
12+
import typescript from 'rollup-plugin-typescript2';
13+
import commonjs from '@rollup/plugin-commonjs';
14+
import { nodeResolve } from '@rollup/plugin-node-resolve';
15+
import { wasm } from '@rollup/plugin-wasm';
316

417
export const buildCodeString = (
518
code: string,
@@ -8,11 +21,7 @@ export const buildCodeString = (
821
transformSync: (ast: Program, opt: Options) => Output;
922
},
1023
): Output => {
11-
const opt = buildConfig.parseSync(code, {
12-
syntax: 'typescript',
13-
target: 'es2022',
14-
});
15-
const contractAst = walkTop(opt);
24+
const contractAst = parseAndProcessCode(code);
1625
// const codeOpt = printSync(contractAst, {});
1726
// console.log(opt);
1827
// console.log(codeOpt);
@@ -27,3 +36,35 @@ export const buildCodeString = (
2736

2837
return contractCode;
2938
};
39+
40+
export const parseAndProcessCode = (code: string) => {
41+
const opt = parseSync(code, {
42+
syntax: 'typescript',
43+
target: 'es2022',
44+
});
45+
const contractAst = walkTop(opt);
46+
47+
return contractAst;
48+
};
49+
export const boundleContract = async (entry: string) => {
50+
const bundle = await rollup({
51+
input: entry,
52+
plugins: [
53+
(typescript as any)({
54+
check: false,
55+
}),
56+
(commonjs as any)(),
57+
wasm(),
58+
(terser as any)({
59+
ecma: 2020,
60+
keep_classnames: true,
61+
}),
62+
nodeResolve(),
63+
],
64+
});
65+
const res = await bundle.generate({
66+
format: 'esm',
67+
sourcemap: false,
68+
});
69+
return res.output[0].code;
70+
};

packages/contract_builder/src/cli.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
/* eslint-disable no-console */
44
/* eslint-disable node/shebang */
55

6-
import { readFileSync, writeFileSync } from 'fs';
6+
import { readFileSync, rmSync, writeFileSync } from 'fs';
77
import { resolve } from 'path';
88
import { cac } from 'cac';
99
import chalk from 'chalk';
10-
import { parseSync, transformSync } from '@swc/core';
10+
import { printSync } from '@swc/core';
1111

1212
import { bytes } from 'multiformats';
1313
import pkg from '../package.json' assert { type: 'json' };
14-
import { buildCodeString } from './builder.js';
14+
import { boundleContract, parseAndProcessCode } from './builder.js';
1515
const version = pkg.version;
1616
const cli = cac('sk-contract-builder');
1717

@@ -24,27 +24,30 @@ const builder = async (input: string, opts: BuildOption) => {
2424
try {
2525
console.log(chalk.green('starting build contract...'));
2626
input = resolve(input, './');
27-
// console.log(codeSnippet);
27+
let fileName: any = input.split('/').pop()?.split('.');
28+
fileName?.pop();
29+
fileName = fileName?.join('.');
2830
const code = readFileSync(input).toString();
2931
// console.log(code);
30-
const res = buildCodeString(code, {
31-
parseSync,
32-
transformSync,
32+
const ast = parseAndProcessCode(code);
33+
const tsCode = printSync(ast, {});
34+
const tsFile = resolve(input, `../${fileName}.processed.ts`);
35+
writeFileSync(tsFile, tsCode.code, {
36+
flag: 'w+',
3337
});
34-
// console.log(contractCode);
35-
const resultUint8 = bytes.fromString(res.code);
36-
// const resultU8String = `export default new Uint8Array([${resultUint8.toString()}]);`;
38+
const boundleCode = await boundleContract(tsFile);
39+
// const boundleCodeFile = resolve(input, '../index.contract.boundle.js');
40+
// writeFileSync(boundleCodeFile, boundleCode, {
41+
// flag: 'w+',
42+
// });
43+
44+
const resultUint8 = bytes.fromString(boundleCode);
45+
const resultU8String = `export default new Uint8Array([${resultUint8.toString()}]);`;
3746
// console.log(resultUint8);
38-
writeFileSync(
39-
resolve(input, '../index.contract.bin'),
40-
resultUint8.toString(),
41-
{
42-
flag: 'w+',
43-
},
44-
);
45-
writeFileSync(resolve(input, '../index.contract.js'), res.code, {
47+
writeFileSync(resolve(input, `../${fileName}.js`), resultU8String, {
4648
flag: 'w+',
4749
});
50+
rmSync(tsFile);
4851
console.log(chalk.green('contract build success'));
4952
} catch (error) {
5053
console.log(error);

packages/land_app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@math.gl/web-mercator": "^3.6.3",
3535
"@trustack/common": "workspace:^1.0.0",
3636
"@trustack/contract": "workspace:^1.0.0",
37+
"@trustack/contract_builder": "workspace:^1.0.0",
3738
"@trustack/node-modules-polyfill": "^0.2.5",
3839
"@trustack/rollup-plugin-node-polyfills": "^0.2.2",
3940
"@xstate/react": "^3.2.2",

packages/sknode/tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"declaration": true,
1414
"isolatedModules": false,
1515
"esModuleInterop": true,
16-
"types": ["node", "jest", "@trustack/contract/src/contract"],
16+
"types": ["node", "jest", "@trustack/contract/contract"],
1717
"lib": ["ESNext", "DOM"],
1818
"outDir": "./dist",
1919
"baseUrl": "./src"

packages/tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"declaration": true,
1414
"isolatedModules": false,
1515
"esModuleInterop": true,
16-
"types": ["node", "jest", "@trustack/contract/src/contract"],
16+
"types": ["node", "jest", "@trustack/contract/contract"],
1717
"lib": ["ESNext", "DOM"]
1818
},
1919
"exclude": ["/**/__tests__"]

packages/webui/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"antd": "^5.7.3",
2525
"buffer": "^6.0.3",
2626
"esbuild": "^0.17.19",
27+
"lodash": "^4.17.21",
2728
"mime-types": "^2.1.35",
2829
"multiformats": "^11.0.2",
2930
"react": "^18.2.0",
@@ -36,6 +37,7 @@
3637
"devDependencies": {
3738
"@playwright/test": "^1.36.2",
3839
"@rollup/plugin-inject": "^5.0.3",
40+
"@types/lodash": "^4.14.196",
3941
"@types/react": "^18.2.17",
4042
"@types/react-dom": "^18.2.7",
4143
"@vitejs/plugin-react-swc": "^3.3.2",

0 commit comments

Comments
 (0)