Skip to content

Commit

Permalink
use swc for build (#517)
Browse files Browse the repository at this point in the history
* use swc for build

* fix sidecar

* bump
  • Loading branch information
ermalkaleci authored Nov 6, 2023
1 parent 684a4ea commit 12dcac6
Show file tree
Hide file tree
Showing 37 changed files with 971 additions and 344 deletions.
13 changes: 13 additions & 0 deletions .cjsswcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"exclude": [".*\\.test.ts$"],
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2021"
},
"module": {
"type": "commonjs"
}
}
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ lib/
dist/
packages/core/scripts/
chopsticks.js
packages/core/src/wasm-executor/browser-wasm-executor.js
packages/core/src/wasm-executor/node-wasm-executor.js
33 changes: 33 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "import", "sort-imports-es6-autofix"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:import/recommended",
"plugin:import/typescript"
],
"rules": {
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"sort-imports-es6-autofix/sort-imports-es6": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
},
"settings": {
"import/resolver": {
"typescript": {
"project": "tsconfig.json"
}
}
}
}
33 changes: 0 additions & 33 deletions .eslintrc.js

This file was deleted.

13 changes: 13 additions & 0 deletions .esmswcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"exclude": [".*\\.test.ts$"],
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "esnext"
},
"module": {
"type": "es6"
}
}
2 changes: 1 addition & 1 deletion executor/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion executor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chopsticks-executor"
version = "0.9.1-2"
version = "0.9.1-3"
description = "Chopsticks executor"
repository = "https://github.com/AcalaNetwork/chopsticks"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion executor/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@acala-network/chopsticks-executor",
"description": "Chopsticks executor",
"version": "0.9.1-2",
"version": "0.9.1-3",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand Down
15 changes: 15 additions & 0 deletions loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as tsConfigPaths from 'tsconfig-paths'
import { pathToFileURL } from 'url'
import { resolve as resolveTs } from 'ts-node/esm'

const { absoluteBaseUrl, paths } = tsConfigPaths.loadConfig()
const matchPath = tsConfigPaths.createMatchPath(absoluteBaseUrl, paths)

export function resolve(specifier, ctx, defaultResolve) {
const match = matchPath(specifier)
return match
? resolveTs(pathToFileURL(`${match}`).href, ctx, defaultResolve)
: resolveTs(specifier, ctx, defaultResolve)
}

export { load, transformSource } from 'ts-node/esm'
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"license": "Apache-2.0",
"packageManager": "[email protected]",
"private": true,
"type": "module",
"workspaces": [
"packages/*",
"executor"
Expand All @@ -18,15 +19,16 @@
"check": "cd executor && cargo check --locked",
"test": "vitest run",
"test:watch": "vitest",
"start": "yarn script:start",
"dev": "yarn script:run",
"start": "NODE_OPTIONS='--loader ts-node/esm --loader ./loader.js --no-warnings --experimental-specifier-resolution=node' node packages/chopsticks/src/cli.ts",
"dev": "NODE_OPTIONS='--loader ts-node/esm --loader ./loader.js --no-warnings --experimental-specifier-resolution=node' LOG_LEVEL=trace node packages/chopsticks/src/cli.ts -- --config=configs/dev.yml",
"script:start": "yarn start",
"docs:prep": "yarn workspaces foreach -pvit run docs:prep && cp docs/* docs-src",
"docs:dev": "yarn docs:prep && vitepress dev",
"docs:build": "yarn docs:prep && vitepress build",
"docs:preview": "yarn docs:build && vitepress preview"
},
"engines": {
"node": ">=v14"
"node": ">=v18"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -38,7 +40,7 @@
"*.{js,ts,css,md}": "prettier --write"
},
"devDependencies": {
"@swc/core": "^1.3.95",
"@swc/core": "^1.3.96",
"@types/node": "^20.5.7",
"@types/prettier": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^6.5.0",
Expand All @@ -50,6 +52,7 @@
"eslint-plugin-sort-imports-es6-autofix": "^0.6.0",
"husky": "^8.0.3",
"prettier": "^3.0.2",
"ts-node": "^10.9.1",
"typedoc": "^0.25.1",
"typedoc-plugin-markdown": "^3.16.0",
"typescript": "^5.1.6",
Expand Down
File renamed without changes.
26 changes: 11 additions & 15 deletions packages/chopsticks/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
{
"name": "@acala-network/chopsticks",
"version": "0.9.1-2",
"version": "0.9.1-3",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"bin": "./chopsticks.js",
"bin": "./chopsticks.cjs",
"type": "module",
"scripts": {
"clean": "rm -rf dist",
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json && echo '{\"type\": \"module\"}' > ./dist/esm/package.json && yarn copyfiles",
"copyfiles": "cp -r src/utils/template dist/cjs/utils/ && cp -r src/utils/template dist/esm/utils/",
"script:start": "cd ../..; ts-node --transpile-only -r tsconfig-paths/register packages/chopsticks/src/cli.ts",
"script:run": "cd ../..; LOG_LEVEL=trace ts-node-dev --transpile-only -r tsconfig-paths/register --inspect --notify=false packages/chopsticks/src/cli.ts -- --config=configs/dev.yml",
"dev:karura": "cd ../..; ts-node-dev --transpile-only --inspect -r tsconfig-paths/register --notify=false packages/chopsticks/src/cli.ts -- --config=configs/karura.yml",
"dev:acala": "cd ../..; ts-node-dev --transpile-only --inspect -r tsconfig-paths/register --notify=false packages/chopsticks/src/cli.ts -- --config=configs/acala.yml",
"dev:polkadot": "cd ../..; ts-node-dev --transpile-only --inspect -r tsconfig-paths/register --notify=false packages/chopsticks/src/cli.ts -- --config=configs/polkadot.yml",
"dev:moonriver": "cd ../..; ts-node-dev --transpile-only --inspect -r tsconfig-paths/register --notify=false packages/chopsticks/src/cli.ts -- --config=configs/moonriver.yml",
"dev:moonbeam": "cd ../..; ts-node-dev --transpile-only --inspect -r tsconfig-paths/register --notify=false packages/chopsticks/src/cli.ts -- --config=configs/moonbeam.yml",
"build": "yarn clean && yarn build:cjs && yarn build:esm && yarn build:types",
"build:cjs": "swc ./src --config-file ../../.cjsswcrc -d dist/cjs --copy-files && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
"build:esm": "swc ./src --config-file ../../.esmswcrc -d dist/esm --copy-files",
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly --outDir dist/types",
"docs:prep": "typedoc"
},
"dependencies": {
Expand All @@ -32,21 +28,21 @@
"zod": "^3.22.3"
},
"devDependencies": {
"@swc/cli": "0.1.62",
"@swc/core": "^1.3.96",
"@types/global-agent": "^2.1.1",
"@types/js-yaml": "^4.0.5",
"@types/lodash": "^4.14.199",
"@types/node": "^20.5.7",
"@types/ws": "^8.5.6",
"@types/yargs": "^17.0.29",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^5.1.6"
},
"files": [
"dist/esm/**",
"dist/cjs/**",
"dist/types/**",
"chopsticks.js"
"chopsticks.cjs"
],
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand All @@ -71,6 +67,6 @@
"default": "./dist/esm/plugins/*.js"
},
"./package.json": "./package.json",
"./package.esm.json": "./dist/esm/package.json"
"./package.cjs.json": "./dist/cjs/package.json"
}
}
3 changes: 1 addition & 2 deletions packages/chopsticks/src/utils/generate-html-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { HexString } from '@polkadot/util/types'
import { decodeStorageDiff } from './decoder'
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import _ from 'lodash'
import url from 'node:url'

export const generateHtmlDiff = async (block: Block, diff: [HexString, HexString | null][]) => {
const { oldState, delta } = await decodeStorageDiff(block, diff)
const htmlTemplate = readFileSync(url.resolve(__filename, './template/diff.html'), 'utf-8')
const htmlTemplate = readFileSync(new URL('template/diff.html', import.meta.url), 'utf-8')
return _.template(htmlTemplate)({ left: JSON.stringify(oldState), delta: JSON.stringify(delta) })
}

Expand Down
8 changes: 0 additions & 8 deletions packages/chopsticks/tsconfig.esm.json

This file was deleted.

10 changes: 8 additions & 2 deletions packages/chopsticks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist/cjs",
"declarationDir": "dist/types"
},
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts"],
"references": [{ "path": "../core/tsconfig.json" }, { "path": "../db/tsconfig.json" }]
"references": [{ "path": "../core/tsconfig.json" }, { "path": "../db/tsconfig.json" }],
"ts-node": {
"esm": true,
"swc": true,
"transpileOnly": true,
"files": true,
"experimentalSpecifierResolution": "node"
}
}
4 changes: 2 additions & 2 deletions packages/chopsticks/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"entryPoints": ["src/types.ts"],
"out": "../../docs-src/chopsticks",
"plugin": "typedoc-plugin-markdown",
"plugin": ["typedoc-plugin-markdown"],
"readme": "none",
"tsconfig": "tsconfig.esm.json"
"tsconfig": "tsconfig.json"
}
13 changes: 9 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "@acala-network/chopsticks-core",
"version": "0.9.1-2",
"version": "0.9.1-3",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"type": "module",
"scripts": {
"clean": "rm -rf dist",
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json && echo '{\"type\": \"module\"}' > ./dist/esm/package.json && yarn copyfiles",
"copyfiles": "cp -r src/wasm-executor/*.mjs dist/cjs/wasm-executor/ && cp -r src/wasm-executor/*.mjs dist/esm/wasm-executor/",
"build": "yarn clean && yarn build:cjs && yarn build:esm && yarn build:types",
"build:cjs": "swc ./src --config-file ../../.cjsswcrc -d dist/cjs && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
"build:esm": "swc ./src --config-file ../../.esmswcrc -d dist/esm",
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly --outDir dist/types",
"docs:prep": "typedoc"
},
"dependencies": {
Expand All @@ -21,6 +24,8 @@
"zod": "^3.22.3"
},
"devDependencies": {
"@swc/cli": "0.1.62",
"@swc/core": "^1.3.96",
"@types/lodash": "^4.14.199",
"typescript": "^5.1.6"
},
Expand All @@ -46,7 +51,7 @@
"default": "./dist/esm/index.js"
},
"./package.json": "./package.json",
"./package.esm.json": "./dist/esm/package.json"
"./package.cjs.json": "./dist/cjs/package.json"
},
"browser": {
"./dist/cjs/wasm-executor/node-worker.js": "./dist/cjs/wasm-executor/browser-worker.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ import * as Comlink from 'comlink'
import * as pkg from '@acala-network/chopsticks-executor'

const getRuntimeVersion = async (code) => {
await pkg.wasmReady
return pkg.get_runtime_version(code)
await pkg.wasmReady
return pkg.get_runtime_version(code)
}

// trie_version: 0 for old trie, 1 for new trie
const calculateStateRoot = async (entries, trie_version) => {
await pkg.wasmReady
return pkg.calculate_state_root(entries, trie_version)
await pkg.wasmReady
return pkg.calculate_state_root(entries, trie_version)
}

const decodeProof = async (trieRootHash, keys, nodes) => {
await pkg.wasmReady
return pkg.decode_proof(trieRootHash, keys, nodes)
await pkg.wasmReady
return pkg.decode_proof(trieRootHash, keys, nodes)
}

const createProof = async (nodes, entries) => {
await pkg.wasmReady
return pkg.create_proof(nodes, entries)
await pkg.wasmReady
return pkg.create_proof(nodes, entries)
}

const runTask = async (task, callback) => {
await pkg.wasmReady
return pkg.run_task(task, callback, 'info')
await pkg.wasmReady
return pkg.run_task(task, callback, 'info')
}

const wasmExecutor = { runTask, getRuntimeVersion, calculateStateRoot, createProof, decodeProof }
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/wasm-executor/browser-worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { wrap } from 'comlink'

export const startWorker = async <T>() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const worker = new Worker(new URL('browser-wasm-executor.mjs', import.meta.url), {
const worker = new Worker(new URL('browser-wasm-executor.js', import.meta.url), {
type: 'module',
name: 'chopsticks-wasm-executor',
})
Expand Down
Loading

0 comments on commit 12dcac6

Please sign in to comment.