From 39f029e6d9add57137bd0d283c73fea4d79e1436 Mon Sep 17 00:00:00 2001 From: Griko Nibras Date: Sun, 18 Jun 2023 02:23:20 +0700 Subject: [PATCH] feat: compile generated chain srcs with esbuild --- packages/graz/package.json | 1 + packages/graz/src/cli/build-js.ts | 13 +++++++++++++ packages/graz/src/cli/index.ts | 13 +++++++++---- packages/graz/stubs/chains-index.ts.stub | 4 ---- pnpm-lock.yaml | 3 +++ 5 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 packages/graz/src/cli/build-js.ts diff --git a/packages/graz/package.json b/packages/graz/package.json index 3f5de022..da21abc0 100644 --- a/packages/graz/package.json +++ b/packages/graz/package.json @@ -43,6 +43,7 @@ "@walletconnect/utils": "^2.8.1", "@web3modal/standalone": "^2.4.3", "commander": "^11.0.0", + "esbuild": "^0.18.4", "zustand": "^4.3.8" }, "devDependencies": { diff --git a/packages/graz/src/cli/build-js.ts b/packages/graz/src/cli/build-js.ts new file mode 100644 index 00000000..22fc4e8b --- /dev/null +++ b/packages/graz/src/cli/build-js.ts @@ -0,0 +1,13 @@ +import * as esbuild from "esbuild"; +import { globby } from "globby"; + +export const buildJs = async (format: esbuild.Format) => { + const paths = await globby(["chains/**/*.ts"]); + await esbuild.build({ + allowOverwrite: true, + bundle: false, + entryPoints: [...paths], + format, + outdir: "chains", + }); +}; diff --git a/packages/graz/src/cli/index.ts b/packages/graz/src/cli/index.ts index 4f623934..f38f9daf 100644 --- a/packages/graz/src/cli/index.ts +++ b/packages/graz/src/cli/index.ts @@ -3,8 +3,10 @@ import os from "node:os"; import * as p from "@clack/prompts"; import { Command } from "commander"; +import type { Format } from "esbuild"; import pMap from "p-map"; +import { buildJs } from "./build-js"; import { cloneRegistry } from "./clone-registry"; import { getChainPaths } from "./get-chain-paths"; import { makeRootSources } from "./make-root-sources"; @@ -33,25 +35,25 @@ const cli = async () => { "-T, --testnet ", 'generate given testnet chain paths separated by spaces (e.g. "atlantic bitcannadev cheqdtestnet")', ) + .option("--format ", "specify javascript module format: cjs, esm (defaults to cjs)") .action(async (options) => { const customRegistry = options.registry as string | undefined; const mainnetFilter = options.mainnet as string[] | undefined; const testnetFilter = options.testnet as string[] | undefined; + const jsFormat: Format = options.format === "esm" || options.format === "cjs" ? options.format : "cjs"; + p.intro("graz generate"); const s = p.spinner(); - // p.log.step("Cloning chain registry..."); s.start(`Cloning chain registry`); await cloneRegistry(customRegistry); s.stop("Cloned chain registry ✅"); - // p.log.step("Retrieving chain paths..."); s.start("Retrieving chain paths"); const { mainnetPaths, testnetPaths } = await getChainPaths({ mainnetFilter, testnetFilter }); s.stop("Retrieved chain paths ✅"); - // p.log.step("Generating chain sources..."); s.start("Generating chain sources"); await fs.rm("chains/", { recursive: true, force: true }); await pMap([...mainnetPaths, ...testnetPaths], makeSources, { @@ -59,11 +61,14 @@ const cli = async () => { }); s.stop("Generated chain sources ✅"); - // p.log.step("Generating chain index..."); s.start("Generating chain index"); await makeRootSources({ mainnetPaths, testnetPaths }); s.stop("Generated chain index ✅"); + s.start("Compiling typescript sources to javascript"); + await buildJs(jsFormat); + s.stop("Compiled sources ✅"); + p.outro('Generate complete! You can import `mainnetChains` and `testnetChains` from "graz/chains". 🎉'); }); diff --git a/packages/graz/stubs/chains-index.ts.stub b/packages/graz/stubs/chains-index.ts.stub index 6d41fda0..d6096418 100644 --- a/packages/graz/stubs/chains-index.ts.stub +++ b/packages/graz/stubs/chains-index.ts.stub @@ -1,9 +1,5 @@ -import { createRequire } from "node:module"; - import type { ChainData, ChainName, ReturnTuple } from "./generated"; -const require = createRequire(import.meta.url); - export * from "./generated"; export const getChainData = (pathOrPaths: T | T[]) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 54eaf5db..86d09731 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -284,6 +284,9 @@ importers: commander: specifier: ^11.0.0 version: 11.0.0 + esbuild: + specifier: ^0.18.4 + version: 0.18.4 zustand: specifier: ^4.3.8 version: 4.3.8(react@18.2.0)