From 0aa8958905526bf0d6e377eb98683377e66e873c Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 12:01:42 +0000 Subject: [PATCH 01/10] playing with cjs exports --- packages/abi-ts/package.json | 7 +++++-- packages/abi-ts/tsup.config.ts | 13 ++----------- tsup.config.base.ts | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 tsup.config.base.ts diff --git a/packages/abi-ts/package.json b/packages/abi-ts/package.json index f9a5aaac38..534b7879d5 100644 --- a/packages/abi-ts/package.json +++ b/packages/abi-ts/package.json @@ -10,7 +10,10 @@ "license": "MIT", "type": "module", "exports": { - "./internal": "./dist/exports/internal.js" + "./internal": { + "import": "./dist/exports/internal.js", + "default": "./dist/exports/internal.cjs" + } }, "typesVersions": { "*": { @@ -20,7 +23,7 @@ } }, "bin": { - "abi-ts": "./bin/abi-ts.js" + "abi-ts": "./bin/abi-ts.cjs" }, "files": [ "bin", diff --git a/packages/abi-ts/tsup.config.ts b/packages/abi-ts/tsup.config.ts index 8a576478a7..e3e5f78bc4 100644 --- a/packages/abi-ts/tsup.config.ts +++ b/packages/abi-ts/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/exports/internal.ts", "src/bin/abi-ts.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/tsup.config.base.ts b/tsup.config.base.ts new file mode 100644 index 0000000000..1157466ac1 --- /dev/null +++ b/tsup.config.base.ts @@ -0,0 +1,17 @@ +import { Options } from "tsup"; + +export function baseConfig(opts: Options): Options { + return { + target: "esnext", + format: ["esm", "cjs"], + minify: true, + sourcemap: true, + // don't generate DTS during watch mode because it's slow + // we're likely using TS source in this mode anyway + dts: !opts.watch, + // don't clean during watch mode to avoid removing + // previously-built DTS files, which other build tasks + // depend on + clean: !opts.watch, + }; +} From c85646e1821c8dfe5fee1a32f65c23d704bc785f Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 12:19:55 +0000 Subject: [PATCH 02/10] fix bin, add cjs types --- packages/abi-ts/package.json | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/abi-ts/package.json b/packages/abi-ts/package.json index 534b7879d5..4a7ec9488e 100644 --- a/packages/abi-ts/package.json +++ b/packages/abi-ts/package.json @@ -11,8 +11,14 @@ "type": "module", "exports": { "./internal": { - "import": "./dist/exports/internal.js", - "default": "./dist/exports/internal.cjs" + "import": { + "import": "./dist/exports/internal.js", + "types": "./dist/exports/internal.d.ts" + }, + "require": { + "require": "./dist/exports/internal.cjs", + "types": "./dist/exports/internal.d.cts" + } } }, "typesVersions": { @@ -23,7 +29,7 @@ } }, "bin": { - "abi-ts": "./bin/abi-ts.cjs" + "abi-ts": "./bin/abi-ts.js" }, "files": [ "bin", From 3d68f1598c0076231dbd5889d340135cd743eadc Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 14:20:24 +0000 Subject: [PATCH 03/10] use base config everywhere --- packages/block-logs-stream/tsup.config.ts | 13 ++----------- packages/cli/tsup.config.ts | 13 ++----------- packages/common/tsup.config.ts | 13 ++----------- packages/config/tsup.config.ts | 13 ++----------- packages/create-mud/tsup.config.ts | 13 ++----------- packages/dev-tools/tsup.config.ts | 13 ++----------- packages/entrykit/tsup.config.ts | 11 ++--------- packages/explorer/tsup.config.ts | 13 ++----------- packages/faucet/tsup.config.ts | 13 ++----------- packages/gas-report/tsup.config.ts | 13 ++----------- packages/protocol-parser/tsup.config.ts | 13 ++----------- packages/react/tsup.config.ts | 13 ++----------- packages/recs/tsup.config.ts | 13 ++----------- packages/schema-type/tsup.config.ts | 15 ++------------- packages/solhint-config-mud/tsup.config.ts | 13 ++----------- packages/solhint-plugin-mud/tsup.config.ts | 13 ++----------- packages/stash/tsup.config.ts | 13 ++----------- packages/store-indexer/tsup.config.ts | 13 ++----------- packages/store-sync/tsup.config.ts | 13 ++----------- packages/store/tsup.config.ts | 13 ++----------- packages/utils/tsup.config.ts | 13 ++----------- packages/vite-plugin-mud/tsup.config.ts | 13 ++----------- .../world-module-callwithsignature/tsup.config.ts | 13 ++----------- packages/world-module-erc20/tsup.config.ts | 12 ++++-------- packages/world-module-metadata/tsup.config.ts | 13 ++----------- packages/world-modules/tsup.config.ts | 13 ++----------- packages/world/tsup.config.ts | 13 ++----------- tsup.config.base.ts | 4 ++++ 28 files changed, 60 insertions(+), 294 deletions(-) diff --git a/packages/block-logs-stream/tsup.config.ts b/packages/block-logs-stream/tsup.config.ts index 67e39a2e97..2971082f7b 100644 --- a/packages/block-logs-stream/tsup.config.ts +++ b/packages/block-logs-stream/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/exports/index.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/cli/tsup.config.ts b/packages/cli/tsup.config.ts index 9d7d80827a..c307577d6c 100644 --- a/packages/cli/tsup.config.ts +++ b/packages/cli/tsup.config.ts @@ -3,6 +3,7 @@ import { globSync } from "glob"; import { readFileSync } from "node:fs"; import path from "node:path"; import { MudPackages } from "./src/common"; +import { baseConfig } from "../../tsup.config.base"; const mudWorkspace = path.normalize(`${__dirname}/../..`); @@ -18,19 +19,9 @@ const mudPackages: MudPackages = Object.fromEntries( ); export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/index.ts", "src/mud.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, env: { MUD_PACKAGES: JSON.stringify(mudPackages), }, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/common/tsup.config.ts b/packages/common/tsup.config.ts index 851bbf70b4..5df16f9c38 100644 --- a/packages/common/tsup.config.ts +++ b/packages/common/tsup.config.ts @@ -1,6 +1,8 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { index: "src/index.ts", actions: "src/actions/index.ts", @@ -13,15 +15,4 @@ export default defineConfig((opts) => ({ kms: "src/exports/kms.ts", internal: "src/exports/internal.ts", }, - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/config/tsup.config.ts b/packages/config/tsup.config.ts index 787c6581ac..3a5748aef6 100644 --- a/packages/config/tsup.config.ts +++ b/packages/config/tsup.config.ts @@ -1,20 +1,11 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { index: "src/exports/index.ts", internal: "src/exports/internal.ts", "deprecated/node": "src/deprecated/node/index.ts", }, - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/create-mud/tsup.config.ts b/packages/create-mud/tsup.config.ts index ab757db1bd..5a0751d293 100644 --- a/packages/create-mud/tsup.config.ts +++ b/packages/create-mud/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/bin/cli.ts"], - target: "esnext", - format: ["esm"], - minify: true, - sourcemap: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/dev-tools/tsup.config.ts b/packages/dev-tools/tsup.config.ts index b70d7c190f..c153547af9 100644 --- a/packages/dev-tools/tsup.config.ts +++ b/packages/dev-tools/tsup.config.ts @@ -1,5 +1,6 @@ import { defineConfig } from "tsup"; import packageJson from "./package.json"; +import { baseConfig } from "../../tsup.config.base"; // tsup doesn't bundle deps by default (https://tsup.egoist.dev/#excluding-packages), // but we want to do that for dev-tools because it's used as a standalone package. @@ -13,21 +14,11 @@ const peerDeps = Object.keys(packageJson.peerDependencies); const bundledDeps = Object.keys(packageJson.dependencies).filter((dep) => !peerDeps.includes(dep)); export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/index.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, injectStyle: true, // bundle all non-peer deps noExternal: bundledDeps, // don't code split otherwise dep imports in bundle seem to break splitting: false, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/entrykit/tsup.config.ts b/packages/entrykit/tsup.config.ts index e86bc067f4..436412f738 100644 --- a/packages/entrykit/tsup.config.ts +++ b/packages/entrykit/tsup.config.ts @@ -1,20 +1,13 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; // import packageJson from "./package.json"; export default defineConfig((opts) => ({ + ...baseConfig(opts), outDir: "dist/tsup", entry: ["src/exports/index.ts", "src/exports/internal.ts", "src/bin/deploy.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, // Because we're injecting CSS via shadow DOM, we'll disable style injection and load CSS as a base64 string. // TODO: figure out how to do this conditionally for only specific imports? injectStyle: false, loader: { ".css": "text" }, - dts: true, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/explorer/tsup.config.ts b/packages/explorer/tsup.config.ts index 88ca1c4ba1..233313af36 100644 --- a/packages/explorer/tsup.config.ts +++ b/packages/explorer/tsup.config.ts @@ -1,17 +1,8 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), tsconfig: "tsconfig.tsup.json", entry: ["src/bin/explorer.ts", "src/exports/observer.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: false, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/faucet/tsup.config.ts b/packages/faucet/tsup.config.ts index 5cd8ce1630..3342bcd00e 100644 --- a/packages/faucet/tsup.config.ts +++ b/packages/faucet/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/index.ts", "src/bin/faucet-server.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/gas-report/tsup.config.ts b/packages/gas-report/tsup.config.ts index a8aa90e47a..b9c66d3594 100644 --- a/packages/gas-report/tsup.config.ts +++ b/packages/gas-report/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["ts/exports/internal.ts", "ts/bin/gas-report.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/protocol-parser/tsup.config.ts b/packages/protocol-parser/tsup.config.ts index 83042e6610..ab87846edb 100644 --- a/packages/protocol-parser/tsup.config.ts +++ b/packages/protocol-parser/tsup.config.ts @@ -1,19 +1,10 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { index: "src/exports/index.ts", internal: "src/exports/internal.ts", }, - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/react/tsup.config.ts b/packages/react/tsup.config.ts index aaaddb4b65..0bb933e250 100644 --- a/packages/react/tsup.config.ts +++ b/packages/react/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/index.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/recs/tsup.config.ts b/packages/recs/tsup.config.ts index 6043a2f8b8..bef05479e6 100644 --- a/packages/recs/tsup.config.ts +++ b/packages/recs/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/index.ts", "src/deprecated/index.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/schema-type/tsup.config.ts b/packages/schema-type/tsup.config.ts index 438778ca20..6c4ea10eae 100644 --- a/packages/schema-type/tsup.config.ts +++ b/packages/schema-type/tsup.config.ts @@ -1,22 +1,11 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { index: "src/typescript/exports/index.ts", internal: "src/typescript/exports/internal.ts", deprecated: "src/typescript/exports/deprecated.ts", }, - outDir: "dist", - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - injectStyle: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/solhint-config-mud/tsup.config.ts b/packages/solhint-config-mud/tsup.config.ts index 77ce5a3131..0bb933e250 100644 --- a/packages/solhint-config-mud/tsup.config.ts +++ b/packages/solhint-config-mud/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/index.ts"], - target: "esnext", - format: ["cjs"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/solhint-plugin-mud/tsup.config.ts b/packages/solhint-plugin-mud/tsup.config.ts index 77ce5a3131..0bb933e250 100644 --- a/packages/solhint-plugin-mud/tsup.config.ts +++ b/packages/solhint-plugin-mud/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/index.ts"], - target: "esnext", - format: ["cjs"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/stash/tsup.config.ts b/packages/stash/tsup.config.ts index c857ad8084..f0538dc123 100644 --- a/packages/stash/tsup.config.ts +++ b/packages/stash/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/exports/index.ts", "src/exports/internal.ts", "src/exports/react.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/store-indexer/tsup.config.ts b/packages/store-indexer/tsup.config.ts index ab84c8d956..2cab48deb0 100644 --- a/packages/store-indexer/tsup.config.ts +++ b/packages/store-indexer/tsup.config.ts @@ -1,6 +1,8 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: [ "src/index.ts", "src/bin/postgres-frontend.ts", @@ -8,15 +10,4 @@ export default defineConfig((opts) => ({ "src/bin/postgres-decoded-indexer.ts", "src/bin/sqlite-indexer.ts", ], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/store-sync/tsup.config.ts b/packages/store-sync/tsup.config.ts index 63c13f2207..899fee714a 100644 --- a/packages/store-sync/tsup.config.ts +++ b/packages/store-sync/tsup.config.ts @@ -1,6 +1,8 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: [ "src/index.ts", "src/sqlite/index.ts", @@ -14,15 +16,4 @@ export default defineConfig((opts) => ({ "src/exports/internal.ts", "src/exports/react.ts", ], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/store/tsup.config.ts b/packages/store/tsup.config.ts index 535643e9bb..67faa149d8 100644 --- a/packages/store/tsup.config.ts +++ b/packages/store/tsup.config.ts @@ -1,21 +1,12 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { "mud.config": "mud.config.ts", index: "ts/exports/index.ts", internal: "ts/exports/internal.ts", codegen: "ts/codegen/index.ts", }, - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/utils/tsup.config.ts b/packages/utils/tsup.config.ts index aaaddb4b65..0bb933e250 100644 --- a/packages/utils/tsup.config.ts +++ b/packages/utils/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/index.ts"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/vite-plugin-mud/tsup.config.ts b/packages/vite-plugin-mud/tsup.config.ts index 5f37c58ca7..7a3b05261a 100644 --- a/packages/vite-plugin-mud/tsup.config.ts +++ b/packages/vite-plugin-mud/tsup.config.ts @@ -1,16 +1,7 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: ["src/**"], - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/world-module-callwithsignature/tsup.config.ts b/packages/world-module-callwithsignature/tsup.config.ts index ef3d13e8ee..9559462b65 100644 --- a/packages/world-module-callwithsignature/tsup.config.ts +++ b/packages/world-module-callwithsignature/tsup.config.ts @@ -1,19 +1,10 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { "mud.config": "mud.config.ts", internal: "ts/exports/internal.ts", }, - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/world-module-erc20/tsup.config.ts b/packages/world-module-erc20/tsup.config.ts index 3f2057dd81..9559462b65 100644 --- a/packages/world-module-erc20/tsup.config.ts +++ b/packages/world-module-erc20/tsup.config.ts @@ -1,14 +1,10 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; -export default defineConfig({ +export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { "mud.config": "mud.config.ts", internal: "ts/exports/internal.ts", }, - target: "esnext", - format: ["esm"], - dts: !process.env.TSUP_SKIP_DTS, - sourcemap: true, - clean: true, - minify: true, -}); +})); diff --git a/packages/world-module-metadata/tsup.config.ts b/packages/world-module-metadata/tsup.config.ts index 1636dcc6c3..6b360b4e59 100644 --- a/packages/world-module-metadata/tsup.config.ts +++ b/packages/world-module-metadata/tsup.config.ts @@ -1,18 +1,9 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { "mud.config": "mud.config.ts", }, - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/world-modules/tsup.config.ts b/packages/world-modules/tsup.config.ts index 1636dcc6c3..6b360b4e59 100644 --- a/packages/world-modules/tsup.config.ts +++ b/packages/world-modules/tsup.config.ts @@ -1,18 +1,9 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { "mud.config": "mud.config.ts", }, - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/packages/world/tsup.config.ts b/packages/world/tsup.config.ts index 1a2f57b7a2..e46bf8c00d 100644 --- a/packages/world/tsup.config.ts +++ b/packages/world/tsup.config.ts @@ -1,21 +1,12 @@ import { defineConfig } from "tsup"; +import { baseConfig } from "../../tsup.config.base"; export default defineConfig((opts) => ({ + ...baseConfig(opts), entry: { "mud.config": "mud.config.ts", index: "ts/exports/index.ts", internal: "ts/exports/internal.ts", node: "ts/node/index.ts", }, - target: "esnext", - format: ["esm"], - sourcemap: true, - minify: true, - // don't generate DTS during watch mode because it's slow - // we're likely using TS source in this mode anyway - dts: !opts.watch, - // don't clean during watch mode to avoid removing - // previously-built DTS files, which other build tasks - // depend on - clean: !opts.watch, })); diff --git a/tsup.config.base.ts b/tsup.config.base.ts index 1157466ac1..9fbae3b24d 100644 --- a/tsup.config.base.ts +++ b/tsup.config.base.ts @@ -1,5 +1,9 @@ import { Options } from "tsup"; +/** + * Shared tsup config across all packages. + * @dev Be careful adjusting this config! + */ export function baseConfig(opts: Options): Options { return { target: "esnext", From 31d3b934110d94b95fcf3c356d2cc15f109121dc Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 14:49:46 +0000 Subject: [PATCH 04/10] wrap top-level await usages --- packages/entrykit/src/bin/deploy.ts | 168 ++++++------- packages/faucet/src/bin/faucet-server.ts | 76 +++--- .../src/bin/postgres-decoded-indexer.ts | 154 ++++++------ .../src/bin/postgres-frontend.ts | 72 +++--- .../store-indexer/src/bin/postgres-indexer.ts | 224 +++++++++--------- .../store-indexer/src/bin/sqlite-indexer.ts | 224 +++++++++--------- 6 files changed, 471 insertions(+), 447 deletions(-) diff --git a/packages/entrykit/src/bin/deploy.ts b/packages/entrykit/src/bin/deploy.ts index 38292faf25..c5ad5515e1 100644 --- a/packages/entrykit/src/bin/deploy.ts +++ b/packages/entrykit/src/bin/deploy.ts @@ -25,109 +25,113 @@ import { getChainId } from "viem/actions"; import { writeContract } from "@latticexyz/common"; import { entryPoint07Address } from "viem/account-abstraction"; -// TODO: parse env with arktype (to avoid zod dep) and throw when absent +// Workaround for: +// Top-level await is currently not supported with the "cjs" output format +(async () => { + // TODO: parse env with arktype (to avoid zod dep) and throw when absent -const privateKey = process.env.PRIVATE_KEY; -if (!isHex(privateKey)) { - // TODO: detect anvil and automatically put this env var where it needs to go? - throw new Error( - `Missing \`PRIVATE_KEY\` environment variable. If you're using Anvil, run + const privateKey = process.env.PRIVATE_KEY; + if (!isHex(privateKey)) { + // TODO: detect anvil and automatically put this env var where it needs to go? + throw new Error( + `Missing \`PRIVATE_KEY\` environment variable. If you're using Anvil, run echo "PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" > .env to use a prefunded Anvil account.`, - ); -} -const account = privateKeyToAccount(privateKey); -const rpcUrl = await getRpcUrl(); + ); + } + const account = privateKeyToAccount(privateKey); + const rpcUrl = await getRpcUrl(); -const client = createClient({ account, transport: http(rpcUrl) }); + const client = createClient({ account, transport: http(rpcUrl) }); -const chainId = await getChainId(client); + const chainId = await getChainId(client); -// TODO: deployer address flag/env var? -const deployerAddress = await ensureDeployer(client); + // TODO: deployer address flag/env var? + const deployerAddress = await ensureDeployer(client); -// https://github.com/eth-infinitism/account-abstraction/blob/b3bae63bd9bc0ed394dfca8668008213127adb62/hardhat.config.ts#L11 -const entryPointSalt = "0x90d8084deab30c2a37c45e8d47f49f2f7965183cb6990a98943ef94940681de3"; -const entryPointAddress = getContractAddress({ - deployerAddress, - bytecode: entryPointArtifact.bytecode as Hex, - salt: entryPointSalt, -}); -if (entryPointAddress !== entryPoint07Address) { - throw new Error( - `Unexpected EntryPoint v0.7 address\n\n Expected: ${entryPoint07Address}\nActual: ${entryPointAddress}`, - ); -} - -// Deploy entrypoint first, because following deploys need to be able to call it. -await ensureContractsDeployed({ - client, - deployerAddress, - contracts: [ - { - bytecode: entryPointArtifact.bytecode as Hex, - salt: entryPointSalt, - deployedBytecodeSize: size(entryPointArtifact.deployedBytecode as Hex), - debugLabel: "EntryPoint v0.7", - }, - ], -}); - -await ensureContractsDeployed({ - client, - deployerAddress, - contracts: [ - { - bytecode: concatHex([ - simpleAccountFactoryArtifact.bytecode as Hex, - encodeAbiParameters(parseAbiParameters("address"), [entryPointAddress]), - ]), - deployedBytecodeSize: size(simpleAccountFactoryArtifact.deployedBytecode as Hex), - debugLabel: "SimpleAccountFactory", - }, - ], -}); - -if (chainId === 31337) { - const localPaymasterBytecode = concatHex([ - localPaymasterArtifact.bytecode.object as Hex, - encodeAbiParameters(parseAbiParameters("address"), [entryPointAddress]), - ]); - const localPaymasterAddress = getContractAddress({ deployerAddress, bytecode: localPaymasterBytecode }); + // https://github.com/eth-infinitism/account-abstraction/blob/b3bae63bd9bc0ed394dfca8668008213127adb62/hardhat.config.ts#L11 + const entryPointSalt = "0x90d8084deab30c2a37c45e8d47f49f2f7965183cb6990a98943ef94940681de3"; + const entryPointAddress = getContractAddress({ + deployerAddress, + bytecode: entryPointArtifact.bytecode as Hex, + salt: entryPointSalt, + }); + if (entryPointAddress !== entryPoint07Address) { + throw new Error( + `Unexpected EntryPoint v0.7 address\n\n Expected: ${entryPoint07Address}\nActual: ${entryPointAddress}`, + ); + } + // Deploy entrypoint first, because following deploys need to be able to call it. await ensureContractsDeployed({ client, deployerAddress, contracts: [ { - bytecode: localPaymasterBytecode, - deployedBytecodeSize: size(localPaymasterArtifact.deployedBytecode.object as Hex), - debugLabel: "GenerousPaymaster", + bytecode: entryPointArtifact.bytecode as Hex, + salt: entryPointSalt, + deployedBytecodeSize: size(entryPointArtifact.deployedBytecode as Hex), + debugLabel: "EntryPoint v0.7", }, ], }); - const tx = await writeContract(client, { - chain: null, - address: entryPointAddress, - abi: [ + await ensureContractsDeployed({ + client, + deployerAddress, + contracts: [ { - inputs: [{ name: "account", type: "address" }], - name: "depositTo", - outputs: [], - stateMutability: "payable", - type: "function", + bytecode: concatHex([ + simpleAccountFactoryArtifact.bytecode as Hex, + encodeAbiParameters(parseAbiParameters("address"), [entryPointAddress]), + ]), + deployedBytecodeSize: size(simpleAccountFactoryArtifact.deployedBytecode as Hex), + debugLabel: "SimpleAccountFactory", }, ], - functionName: "depositTo", - args: [localPaymasterAddress], - value: parseEther("100"), }); - await waitForTransactions({ client, hashes: [tx] }); - console.log("\nFunded local paymaster at:", localPaymasterAddress, "\n"); -} -console.log("\nEntryKit contracts are ready!\n"); -process.exit(0); + if (chainId === 31337) { + const localPaymasterBytecode = concatHex([ + localPaymasterArtifact.bytecode.object as Hex, + encodeAbiParameters(parseAbiParameters("address"), [entryPointAddress]), + ]); + const localPaymasterAddress = getContractAddress({ deployerAddress, bytecode: localPaymasterBytecode }); + + await ensureContractsDeployed({ + client, + deployerAddress, + contracts: [ + { + bytecode: localPaymasterBytecode, + deployedBytecodeSize: size(localPaymasterArtifact.deployedBytecode.object as Hex), + debugLabel: "GenerousPaymaster", + }, + ], + }); + + const tx = await writeContract(client, { + chain: null, + address: entryPointAddress, + abi: [ + { + inputs: [{ name: "account", type: "address" }], + name: "depositTo", + outputs: [], + stateMutability: "payable", + type: "function", + }, + ], + functionName: "depositTo", + args: [localPaymasterAddress], + value: parseEther("100"), + }); + await waitForTransactions({ client, hashes: [tx] }); + console.log("\nFunded local paymaster at:", localPaymasterAddress, "\n"); + } + + console.log("\nEntryKit contracts are ready!\n"); + process.exit(0); +})(); diff --git a/packages/faucet/src/bin/faucet-server.ts b/packages/faucet/src/bin/faucet-server.ts index 657a06c213..cb8cbcd2e7 100644 --- a/packages/faucet/src/bin/faucet-server.ts +++ b/packages/faucet/src/bin/faucet-server.ts @@ -7,39 +7,43 @@ import { privateKeyToAccount } from "viem/accounts"; import { AppRouter, createAppRouter } from "../createAppRouter"; import { parseEnv } from "./parseEnv"; -const env = parseEnv(); - -const client = createClient({ - transport: http(env.RPC_HTTP_URL), -}); - -const faucetAccount = privateKeyToAccount(env.FAUCET_PRIVATE_KEY); - -// @see https://fastify.dev/docs/latest/ -const server = fastify({ - maxParamLength: 5000, - logger: true, -}); - -await server.register(import("@fastify/compress")); -await server.register(import("@fastify/cors")); - -// k8s healthchecks -server.get("/healthz", (req, res) => res.code(200).send()); -server.get("/readyz", (req, res) => res.code(200).send()); - -// @see https://trpc.io/docs/server/adapters/fastify -server.register(fastifyTRPCPlugin, { - prefix: "/trpc", - trpcOptions: { - router: createAppRouter(), - createContext: async () => ({ - client, - faucetAccount, - dripAmount: env.DRIP_AMOUNT_ETHER, - }), - }, -}); - -await server.listen({ host: env.HOST, port: env.PORT }); -console.log(`faucet server listening on http://${env.HOST}:${env.PORT}`); +// Workaround for: +// Top-level await is currently not supported with the "cjs" output format +(async (): Promise => { + const env = parseEnv(); + + const client = createClient({ + transport: http(env.RPC_HTTP_URL), + }); + + const faucetAccount = privateKeyToAccount(env.FAUCET_PRIVATE_KEY); + + // @see https://fastify.dev/docs/latest/ + const server = fastify({ + maxParamLength: 5000, + logger: true, + }); + + await server.register(import("@fastify/compress")); + await server.register(import("@fastify/cors")); + + // k8s healthchecks + server.get("/healthz", (req, res) => res.code(200).send()); + server.get("/readyz", (req, res) => res.code(200).send()); + + // @see https://trpc.io/docs/server/adapters/fastify + server.register(fastifyTRPCPlugin, { + prefix: "/trpc", + trpcOptions: { + router: createAppRouter(), + createContext: async () => ({ + client, + faucetAccount, + dripAmount: env.DRIP_AMOUNT_ETHER, + }), + }, + }); + + await server.listen({ host: env.HOST, port: env.PORT }); + console.log(`faucet server listening on http://${env.HOST}:${env.PORT}`); +})(); diff --git a/packages/store-indexer/src/bin/postgres-decoded-indexer.ts b/packages/store-indexer/src/bin/postgres-decoded-indexer.ts index ba49e79b7f..efefca1ee4 100644 --- a/packages/store-indexer/src/bin/postgres-decoded-indexer.ts +++ b/packages/store-indexer/src/bin/postgres-decoded-indexer.ts @@ -15,93 +15,97 @@ import { getClientOptions } from "./getClientOptions"; import { getChainId } from "viem/actions"; import { getRpcClient } from "@latticexyz/block-logs-stream"; -const env = parseEnv( - z.intersection( - indexerEnvSchema, - z.object({ - DATABASE_URL: z.string(), - HEALTHCHECK_HOST: z.string().optional(), - HEALTHCHECK_PORT: z.coerce.number().optional(), - SENTRY_DSN: z.string().optional(), - }), - ), -); +// Workaround for: +// Top-level await is currently not supported with the "cjs" output format +(async (): Promise => { + const env = parseEnv( + z.intersection( + indexerEnvSchema, + z.object({ + DATABASE_URL: z.string(), + HEALTHCHECK_HOST: z.string().optional(), + HEALTHCHECK_PORT: z.coerce.number().optional(), + SENTRY_DSN: z.string().optional(), + }), + ), + ); -const clientOptions = await getClientOptions(env); + const clientOptions = await getClientOptions(env); -const chainId = await getChainId(getRpcClient(clientOptions)); -const database = drizzle(postgres(env.DATABASE_URL, { prepare: false })); + const chainId = await getChainId(getRpcClient(clientOptions)); + const database = drizzle(postgres(env.DATABASE_URL, { prepare: false })); -const { storageAdapter, tables } = await createStorageAdapter({ ...clientOptions, database }); + const { storageAdapter, tables } = await createStorageAdapter({ ...clientOptions, database }); -let startBlock = env.START_BLOCK; + let startBlock = env.START_BLOCK; -// Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. -// TODO: query if the DB exists instead of try/catch -try { - const chainState = await database - .select() - .from(tables.configTable) - .where(eq(tables.configTable.chainId, chainId)) - .limit(1) - .execute() - // Get the first record in a way that returns a possible `undefined` - // TODO: move this to `.findFirst` after upgrading drizzle or `rows[0]` after enabling `noUncheckedIndexedAccess: true` - .then((rows) => rows.find(() => true)); + // Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. + // TODO: query if the DB exists instead of try/catch + try { + const chainState = await database + .select() + .from(tables.configTable) + .where(eq(tables.configTable.chainId, chainId)) + .limit(1) + .execute() + // Get the first record in a way that returns a possible `undefined` + // TODO: move this to `.findFirst` after upgrading drizzle or `rows[0]` after enabling `noUncheckedIndexedAccess: true` + .then((rows) => rows.find(() => true)); - if (chainState?.blockNumber != null) { - startBlock = chainState.blockNumber + 1n; - console.log("resuming from block number", startBlock); + if (chainState?.blockNumber != null) { + startBlock = chainState.blockNumber + 1n; + console.log("resuming from block number", startBlock); + } + } catch (error) { + // ignore errors for now } -} catch (error) { - // ignore errors for now -} -const { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({ - ...clientOptions, - storageAdapter, - followBlockTag: env.FOLLOW_BLOCK_TAG, - startBlock, - maxBlockRange: env.MAX_BLOCK_RANGE, - address: env.STORE_ADDRESS, -}); + const { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({ + ...clientOptions, + storageAdapter, + followBlockTag: env.FOLLOW_BLOCK_TAG, + startBlock, + maxBlockRange: env.MAX_BLOCK_RANGE, + address: env.STORE_ADDRESS, + }); -storedBlockLogs$.subscribe(); + storedBlockLogs$.subscribe(); -let isCaughtUp = false; -combineLatest([latestBlockNumber$, storedBlockLogs$]) - .pipe( - filter( - ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => - latestBlockNumber === lastBlockNumberProcessed, - ), - first(), - ) - .subscribe(() => { - isCaughtUp = true; - console.log("all caught up"); - }); + let isCaughtUp = false; + combineLatest([latestBlockNumber$, storedBlockLogs$]) + .pipe( + filter( + ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => + latestBlockNumber === lastBlockNumberProcessed, + ), + first(), + ) + .subscribe(() => { + isCaughtUp = true; + console.log("all caught up"); + }); -if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) { - const { default: Koa } = await import("koa"); - const { default: cors } = await import("@koa/cors"); + if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) { + const { default: Koa } = await import("koa"); + const { default: cors } = await import("@koa/cors"); - const server = new Koa(); + const server = new Koa(); - if (env.SENTRY_DSN) { - server.use(sentry(env.SENTRY_DSN)); - } + if (env.SENTRY_DSN) { + server.use(sentry(env.SENTRY_DSN)); + } - server.use(cors()); - server.use( - healthcheck({ - isReady: () => isCaughtUp, - }), - ); - server.use(helloWorld()); + server.use(cors()); + server.use( + healthcheck({ + isReady: () => isCaughtUp, + }), + ); + server.use(helloWorld()); - server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT }); - console.log( - `postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}`, - ); -} + server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT }); + console.log( + `postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}`, + ); + } +})(); diff --git a/packages/store-indexer/src/bin/postgres-frontend.ts b/packages/store-indexer/src/bin/postgres-frontend.ts index bd6c868864..daa822d328 100644 --- a/packages/store-indexer/src/bin/postgres-frontend.ts +++ b/packages/store-indexer/src/bin/postgres-frontend.ts @@ -15,44 +15,48 @@ import { healthcheck } from "../koa-middleware/healthcheck"; import { helloWorld } from "../koa-middleware/helloWorld"; import { metrics } from "../koa-middleware/metrics"; -const env = parseEnv( - z.intersection( - frontendEnvSchema, - z.object({ - DATABASE_URL: z.string(), - SENTRY_DSN: z.string().optional(), - }), - ), -); +// Workaround for: +// Top-level await is currently not supported with the "cjs" output format +(async (): Promise => { + const env = parseEnv( + z.intersection( + frontendEnvSchema, + z.object({ + DATABASE_URL: z.string(), + SENTRY_DSN: z.string().optional(), + }), + ), + ); -const database = postgres(env.DATABASE_URL, { prepare: false }); + const database = postgres(env.DATABASE_URL, { prepare: false }); -const server = new Koa(); + const server = new Koa(); -if (env.SENTRY_DSN) { - server.use(sentry(env.SENTRY_DSN)); -} + if (env.SENTRY_DSN) { + server.use(sentry(env.SENTRY_DSN)); + } -server.use(cors()); -server.use(healthcheck()); -server.use( - metrics({ - isHealthy: () => true, - isReady: () => true, - }), -); -server.use(helloWorld()); -server.use(apiRoutes(database)); + server.use(cors()); + server.use(healthcheck()); + server.use( + metrics({ + isHealthy: () => true, + isReady: () => true, + }), + ); + server.use(helloWorld()); + server.use(apiRoutes(database)); -server.use( - createKoaMiddleware({ - prefix: "/trpc", - router: createAppRouter(), - createContext: async () => ({ - queryAdapter: await createQueryAdapter(drizzle(database)), + server.use( + createKoaMiddleware({ + prefix: "/trpc", + router: createAppRouter(), + createContext: async () => ({ + queryAdapter: await createQueryAdapter(drizzle(database)), + }), }), - }), -); + ); -server.listen({ host: env.HOST, port: env.PORT }); -console.log(`postgres indexer frontend listening on http://${env.HOST}:${env.PORT}`); + server.listen({ host: env.HOST, port: env.PORT }); + console.log(`postgres indexer frontend listening on http://${env.HOST}:${env.PORT}`); +})(); diff --git a/packages/store-indexer/src/bin/postgres-indexer.ts b/packages/store-indexer/src/bin/postgres-indexer.ts index ce6a36ab0d..47c31249de 100644 --- a/packages/store-indexer/src/bin/postgres-indexer.ts +++ b/packages/store-indexer/src/bin/postgres-indexer.ts @@ -12,118 +12,122 @@ import { getClientOptions } from "./getClientOptions"; import { getBlock, getChainId } from "viem/actions"; import { getRpcClient } from "@latticexyz/block-logs-stream"; -const env = parseEnv( - z.intersection( - indexerEnvSchema, - z.object({ - DATABASE_URL: z.string(), - HEALTHCHECK_HOST: z.string().optional(), - HEALTHCHECK_PORT: z.coerce.number().optional(), - }), - ), -); - -const clientOptions = await getClientOptions(env); - -const chainId = await getChainId(getRpcClient(clientOptions)); -const database = drizzle(postgres(env.DATABASE_URL, { prepare: false })); - -if (await shouldCleanDatabase(database, chainId)) { - console.log("outdated database detected, clearing data to start fresh"); - await cleanDatabase(database); -} - -const { storageAdapter, tables } = await createStorageAdapter({ ...clientOptions, database }); - -let startBlock = env.START_BLOCK; - -async function getLatestStoredBlockNumber(): Promise { - // Fetch latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. - // TODO: query if the DB exists instead of try/catch - try { - const chainState = await database - .select() - .from(tables.configTable) - .where(eq(tables.configTable.chainId, chainId)) - .limit(1) - .execute() - // Get the first record in a way that returns a possible `undefined` - // TODO: move this to `.findFirst` after upgrading drizzle or `rows[0]` after enabling `noUncheckedIndexedAccess: true` - .then((rows) => rows.find(() => true)); - - return chainState?.blockNumber; - } catch (error) { - // ignore errors for now - } -} - -async function getDistanceFromFollowBlock(): Promise { - const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([ - getLatestStoredBlockNumber(), - getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG }), - ]); - return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n); -} - -const latestStoredBlockNumber = await getLatestStoredBlockNumber(); -if (latestStoredBlockNumber != null) { - startBlock = latestStoredBlockNumber + 1n; - console.log("resuming from block number", startBlock); -} - -const { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({ - ...clientOptions, - storageAdapter, - followBlockTag: env.FOLLOW_BLOCK_TAG, - startBlock, - maxBlockRange: env.MAX_BLOCK_RANGE, - address: env.STORE_ADDRESS, -}); - -storedBlockLogs$.subscribe(); - -let isCaughtUp = false; -combineLatest([latestBlockNumber$, storedBlockLogs$]) - .pipe( - filter( - ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => - latestBlockNumber === lastBlockNumberProcessed, +// Workaround for: +// Top-level await is currently not supported with the "cjs" output format +(async (): Promise => { + const env = parseEnv( + z.intersection( + indexerEnvSchema, + z.object({ + DATABASE_URL: z.string(), + HEALTHCHECK_HOST: z.string().optional(), + HEALTHCHECK_PORT: z.coerce.number().optional(), + }), ), - first(), - ) - .subscribe(() => { - isCaughtUp = true; - console.log("all caught up"); - }); + ); -if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) { - const { default: Koa } = await import("koa"); - const { default: cors } = await import("@koa/cors"); - const { healthcheck } = await import("../koa-middleware/healthcheck"); - const { metrics } = await import("../koa-middleware/metrics"); - const { helloWorld } = await import("../koa-middleware/helloWorld"); + const clientOptions = await getClientOptions(env); - const server = new Koa(); + const chainId = await getChainId(getRpcClient(clientOptions)); + const database = drizzle(postgres(env.DATABASE_URL, { prepare: false })); - server.use(cors()); - server.use( - healthcheck({ - isReady: () => isCaughtUp, - }), - ); - server.use( - metrics({ - isHealthy: () => true, - isReady: () => isCaughtUp, - getLatestStoredBlockNumber, - getDistanceFromFollowBlock, - followBlockTag: env.FOLLOW_BLOCK_TAG, - }), - ); - server.use(helloWorld()); + if (await shouldCleanDatabase(database, chainId)) { + console.log("outdated database detected, clearing data to start fresh"); + await cleanDatabase(database); + } - server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT }); - console.log( - `postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}`, - ); -} + const { storageAdapter, tables } = await createStorageAdapter({ ...clientOptions, database }); + + let startBlock = env.START_BLOCK; + + async function getLatestStoredBlockNumber(): Promise { + // Fetch latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. + // TODO: query if the DB exists instead of try/catch + try { + const chainState = await database + .select() + .from(tables.configTable) + .where(eq(tables.configTable.chainId, chainId)) + .limit(1) + .execute() + // Get the first record in a way that returns a possible `undefined` + // TODO: move this to `.findFirst` after upgrading drizzle or `rows[0]` after enabling `noUncheckedIndexedAccess: true` + .then((rows) => rows.find(() => true)); + + return chainState?.blockNumber; + } catch (error) { + // ignore errors for now + } + } + + async function getDistanceFromFollowBlock(): Promise { + const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([ + getLatestStoredBlockNumber(), + getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG }), + ]); + return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n); + } + + const latestStoredBlockNumber = await getLatestStoredBlockNumber(); + if (latestStoredBlockNumber != null) { + startBlock = latestStoredBlockNumber + 1n; + console.log("resuming from block number", startBlock); + } + + const { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({ + ...clientOptions, + storageAdapter, + followBlockTag: env.FOLLOW_BLOCK_TAG, + startBlock, + maxBlockRange: env.MAX_BLOCK_RANGE, + address: env.STORE_ADDRESS, + }); + + storedBlockLogs$.subscribe(); + + let isCaughtUp = false; + combineLatest([latestBlockNumber$, storedBlockLogs$]) + .pipe( + filter( + ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => + latestBlockNumber === lastBlockNumberProcessed, + ), + first(), + ) + .subscribe(() => { + isCaughtUp = true; + console.log("all caught up"); + }); + + if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) { + const { default: Koa } = await import("koa"); + const { default: cors } = await import("@koa/cors"); + const { healthcheck } = await import("../koa-middleware/healthcheck"); + const { metrics } = await import("../koa-middleware/metrics"); + const { helloWorld } = await import("../koa-middleware/helloWorld"); + + const server = new Koa(); + + server.use(cors()); + server.use( + healthcheck({ + isReady: () => isCaughtUp, + }), + ); + server.use( + metrics({ + isHealthy: () => true, + isReady: () => isCaughtUp, + getLatestStoredBlockNumber, + getDistanceFromFollowBlock, + followBlockTag: env.FOLLOW_BLOCK_TAG, + }), + ); + server.use(helloWorld()); + + server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT }); + console.log( + `postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}`, + ); + } +})(); diff --git a/packages/store-indexer/src/bin/sqlite-indexer.ts b/packages/store-indexer/src/bin/sqlite-indexer.ts index f7137094db..36adcba29b 100644 --- a/packages/store-indexer/src/bin/sqlite-indexer.ts +++ b/packages/store-indexer/src/bin/sqlite-indexer.ts @@ -22,131 +22,135 @@ import { getClientOptions } from "./getClientOptions"; import { getRpcClient } from "@latticexyz/block-logs-stream"; import { getBlock, getChainId } from "viem/actions"; -const env = parseEnv( - z.intersection( - z.intersection(indexerEnvSchema, frontendEnvSchema), - z.object({ - SQLITE_FILENAME: z.string().default("indexer.db"), - SENTRY_DSN: z.string().optional(), - }), - ), -); +// Workaround for: +// Top-level await is currently not supported with the "cjs" output format +(async (): Promise => { + const env = parseEnv( + z.intersection( + z.intersection(indexerEnvSchema, frontendEnvSchema), + z.object({ + SQLITE_FILENAME: z.string().default("indexer.db"), + SENTRY_DSN: z.string().optional(), + }), + ), + ); -const clientOptions = await getClientOptions(env); + const clientOptions = await getClientOptions(env); -const chainId = await getChainId(getRpcClient(clientOptions)); -const database = drizzle(new Database(env.SQLITE_FILENAME)); + const chainId = await getChainId(getRpcClient(clientOptions)); + const database = drizzle(new Database(env.SQLITE_FILENAME)); -let startBlock = env.START_BLOCK; + let startBlock = env.START_BLOCK; -async function getCurrentChainState(): Promise< - | { - schemaVersion: number; - chainId: number; - lastUpdatedBlockNumber: bigint | null; - lastError: string | null; + async function getCurrentChainState(): Promise< + | { + schemaVersion: number; + chainId: number; + lastUpdatedBlockNumber: bigint | null; + lastError: string | null; + } + | undefined + > { + // This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. + try { + const currentChainStates = database.select().from(chainState).where(eq(chainState.chainId, chainId)).all(); + // TODO: replace this type workaround with `noUncheckedIndexedAccess: true` when we can fix all the issues related (https://github.com/latticexyz/mud/issues/1212) + const currentChainState: (typeof currentChainStates)[number] | undefined = currentChainStates[0]; + return currentChainState; + } catch (error) { + // ignore errors, this is optional } - | undefined -> { - // This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. - try { - const currentChainStates = database.select().from(chainState).where(eq(chainState.chainId, chainId)).all(); - // TODO: replace this type workaround with `noUncheckedIndexedAccess: true` when we can fix all the issues related (https://github.com/latticexyz/mud/issues/1212) - const currentChainState: (typeof currentChainStates)[number] | undefined = currentChainStates[0]; - return currentChainState; - } catch (error) { - // ignore errors, this is optional } -} - -async function getLatestStoredBlockNumber(): Promise { - const currentChainState = await getCurrentChainState(); - return currentChainState?.lastUpdatedBlockNumber ?? undefined; -} -async function getDistanceFromFollowBlock(): Promise { - const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([ - getLatestStoredBlockNumber(), - getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG }), - ]); - return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n); -} + async function getLatestStoredBlockNumber(): Promise { + const currentChainState = await getCurrentChainState(); + return currentChainState?.lastUpdatedBlockNumber ?? undefined; + } -const currentChainState = await getCurrentChainState(); -if (currentChainState) { - // Reset the db if the version changed - if (currentChainState.schemaVersion != schemaVersion) { - console.log( - "schema version changed from", - currentChainState.schemaVersion, - "to", - schemaVersion, - "recreating database", - ); - fs.truncateSync(env.SQLITE_FILENAME); - } else if (currentChainState.lastUpdatedBlockNumber != null) { - // Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. - console.log("resuming from block number", currentChainState.lastUpdatedBlockNumber + 1n); - startBlock = currentChainState.lastUpdatedBlockNumber + 1n; + async function getDistanceFromFollowBlock(): Promise { + const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([ + getLatestStoredBlockNumber(), + getBlock(getRpcClient(clientOptions), { blockTag: env.FOLLOW_BLOCK_TAG }), + ]); + return latestFollowBlock.number - (latestStoredBlockNumber ?? -1n); } -} -const { latestBlockNumber$, storedBlockLogs$ } = await syncToSqlite({ - ...clientOptions, - database, - followBlockTag: env.FOLLOW_BLOCK_TAG, - startBlock, - maxBlockRange: env.MAX_BLOCK_RANGE, - address: env.STORE_ADDRESS, -}); + const currentChainState = await getCurrentChainState(); + if (currentChainState) { + // Reset the db if the version changed + if (currentChainState.schemaVersion != schemaVersion) { + console.log( + "schema version changed from", + currentChainState.schemaVersion, + "to", + schemaVersion, + "recreating database", + ); + fs.truncateSync(env.SQLITE_FILENAME); + } else if (currentChainState.lastUpdatedBlockNumber != null) { + // Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. + console.log("resuming from block number", currentChainState.lastUpdatedBlockNumber + 1n); + startBlock = currentChainState.lastUpdatedBlockNumber + 1n; + } + } -let isCaughtUp = false; -combineLatest([latestBlockNumber$, storedBlockLogs$]) - .pipe( - filter( - ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => - latestBlockNumber === lastBlockNumberProcessed, - ), - first(), - ) - .subscribe(() => { - isCaughtUp = true; - console.log("all caught up"); + const { latestBlockNumber$, storedBlockLogs$ } = await syncToSqlite({ + ...clientOptions, + database, + followBlockTag: env.FOLLOW_BLOCK_TAG, + startBlock, + maxBlockRange: env.MAX_BLOCK_RANGE, + address: env.STORE_ADDRESS, }); -const server = new Koa(); + let isCaughtUp = false; + combineLatest([latestBlockNumber$, storedBlockLogs$]) + .pipe( + filter( + ([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => + latestBlockNumber === lastBlockNumberProcessed, + ), + first(), + ) + .subscribe(() => { + isCaughtUp = true; + console.log("all caught up"); + }); -if (env.SENTRY_DSN) { - server.use(sentry(env.SENTRY_DSN)); -} + const server = new Koa(); -server.use(cors()); -server.use( - healthcheck({ - isReady: () => isCaughtUp, - }), -); -server.use( - metrics({ - isHealthy: () => true, - isReady: () => isCaughtUp, - getLatestStoredBlockNumber, - getDistanceFromFollowBlock, - followBlockTag: env.FOLLOW_BLOCK_TAG, - }), -); -server.use(helloWorld()); -server.use(apiRoutes(database)); + if (env.SENTRY_DSN) { + server.use(sentry(env.SENTRY_DSN)); + } + + server.use(cors()); + server.use( + healthcheck({ + isReady: () => isCaughtUp, + }), + ); + server.use( + metrics({ + isHealthy: () => true, + isReady: () => isCaughtUp, + getLatestStoredBlockNumber, + getDistanceFromFollowBlock, + followBlockTag: env.FOLLOW_BLOCK_TAG, + }), + ); + server.use(helloWorld()); + server.use(apiRoutes(database)); -server.use( - createKoaMiddleware({ - prefix: "/trpc", - router: createAppRouter(), - createContext: async () => ({ - queryAdapter: await createQueryAdapter(database), + server.use( + createKoaMiddleware({ + prefix: "/trpc", + router: createAppRouter(), + createContext: async () => ({ + queryAdapter: await createQueryAdapter(database), + }), }), - }), -); + ); -server.listen({ host: env.HOST, port: env.PORT }); -console.log(`sqlite indexer frontend listening on http://${env.HOST}:${env.PORT}`); + server.listen({ host: env.HOST, port: env.PORT }); + console.log(`sqlite indexer frontend listening on http://${env.HOST}:${env.PORT}`); +})(); From 5981391ab94e68d0ff04c2f99466200290b04d93 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 15:22:52 +0000 Subject: [PATCH 05/10] not needed --- packages/cli/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index aa73a4c58e..e6ae3734d5 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -28,7 +28,7 @@ ], "scripts": { "build": "pnpm run build:js && pnpm run build:test-tables", - "build:js": "tsup && chmod +x ./dist/mud.js", + "build:js": "tsup", "build:test-tables": "tsx ./scripts/generate-test-tables.ts", "clean": "pnpm run clean:js && pnpm run clean:test-tables", "clean:js": "shx rm -rf dist", From 200d923f62baaa4e61ff93488a67c72087f55940 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 15:27:51 +0000 Subject: [PATCH 06/10] add exports --- packages/block-logs-stream/package.json | 11 +- packages/cli/package.json | 11 +- packages/common/package.json | 110 ++++++++++++++-- packages/config/package.json | 33 ++++- packages/dev-tools/package.json | 11 +- packages/entrykit/package.json | 22 +++- packages/explorer/package.json | 11 +- packages/faucet/package.json | 11 +- packages/gas-report/package.json | 11 +- packages/protocol-parser/package.json | 22 +++- packages/react/package.json | 11 +- packages/recs/package.json | 22 +++- packages/schema-type/package.json | 33 ++++- packages/solhint-config-mud/package.json | 11 +- packages/solhint-plugin-mud/package.json | 11 +- packages/stash/package.json | 33 ++++- packages/store-indexer/package.json | 11 +- packages/store-sync/package.json | 121 ++++++++++++++++-- packages/store/package.json | 44 ++++++- packages/utils/package.json | 11 +- .../package.json | 22 +++- packages/world-module-erc20/package.json | 22 +++- packages/world-module-metadata/package.json | 11 +- packages/world-modules/package.json | 11 +- packages/world/package.json | 44 ++++++- 25 files changed, 610 insertions(+), 61 deletions(-) diff --git a/packages/block-logs-stream/package.json b/packages/block-logs-stream/package.json index 497bc13456..354d0c4778 100644 --- a/packages/block-logs-stream/package.json +++ b/packages/block-logs-stream/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/cli/package.json b/packages/cli/package.json index e6ae3734d5..579dba0ed5 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/common/package.json b/packages/common/package.json index 2c9f9306cf..9863b49022 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -10,16 +10,106 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./actions": "./dist/actions.js", - "./chains": "./dist/chains.js", - "./codegen": "./dist/codegen.js", - "./errors": "./dist/errors.js", - "./foundry": "./dist/foundry.js", - "./type-utils": "./dist/type-utils.js", - "./utils": "./dist/utils.js", - "./kms": "./dist/kms.js", - "./internal": "./dist/internal.js", + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./actions": { + "import": { + "import": "./dist/actions.js", + "types": "./dist/actions.d.ts" + }, + "require": { + "require": "./dist/actions.cjs", + "types": "./dist/actions.d.cts" + } + }, + "./chains": { + "import": { + "import": "./dist/chains.js", + "types": "./dist/chains.d.ts" + }, + "require": { + "require": "./dist/chains.cjs", + "types": "./dist/chains.d.cts" + } + }, + "./codegen": { + "import": { + "import": "./dist/codegen.js", + "types": "./dist/codegen.d.ts" + }, + "require": { + "require": "./dist/codegen.cjs", + "types": "./dist/codegen.d.cts" + } + }, + "./errors": { + "import": { + "import": "./dist/errors.js", + "types": "./dist/errors.d.ts" + }, + "require": { + "require": "./dist/errors.cjs", + "types": "./dist/errors.d.cts" + } + }, + "./foundry": { + "import": { + "import": "./dist/foundry.js", + "types": "./dist/foundry.d.ts" + }, + "require": { + "require": "./dist/foundry.cjs", + "types": "./dist/foundry.d.cts" + } + }, + "./type-utils": { + "import": { + "import": "./dist/type-utils.js", + "types": "./dist/type-utils.d.ts" + }, + "require": { + "require": "./dist/type-utils.cjs", + "types": "./dist/type-utils.d.cts" + } + }, + "./utils": { + "import": { + "import": "./dist/utils.js", + "types": "./dist/utils.d.ts" + }, + "require": { + "require": "./dist/utils.cjs", + "types": "./dist/utils.d.cts" + } + }, + "./kms": { + "import": { + "import": "./dist/kms.js", + "types": "./dist/kms.d.ts" + }, + "require": { + "require": "./dist/kms.cjs", + "types": "./dist/kms.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + }, "./tsconfig.base.json": "./tsconfig.base.json" }, "typesVersions": { diff --git a/packages/config/package.json b/packages/config/package.json index 05aa45c9b7..e832064a3a 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -10,9 +10,36 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./internal": "./dist/internal.js", - "./node": "./dist/deprecated/node.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + }, + "./node": { + "import": { + "import": "./dist/deprecated/node.js", + "types": "./dist/deprecated/node.d.ts" + }, + "require": { + "require": "./dist/deprecated/node.cjs", + "types": "./dist/deprecated/node.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/dev-tools/package.json b/packages/dev-tools/package.json index a9b6064a53..8fa70d0498 100644 --- a/packages/dev-tools/package.json +++ b/packages/dev-tools/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/entrykit/package.json b/packages/entrykit/package.json index 04e17d0b2b..e136281dec 100644 --- a/packages/entrykit/package.json +++ b/packages/entrykit/package.json @@ -10,8 +10,26 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/tsup/exports/index.js", - "./internal": "./dist/tsup/exports/internal.js" + ".": { + "import": { + "import": "./dist/tsup/exports/index.js", + "types": "./dist/tsup/exports/index.d.ts" + }, + "require": { + "require": "./dist/tsup/exports/index.cjs", + "types": "./dist/tsup/exports/index.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/tsup/exports/internal.js", + "types": "./dist/tsup/exports/internal.d.ts" + }, + "require": { + "require": "./dist/tsup/exports/internal.cjs", + "types": "./dist/tsup/exports/internal.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/explorer/package.json b/packages/explorer/package.json index 365ad35bbe..d214590fb5 100644 --- a/packages/explorer/package.json +++ b/packages/explorer/package.json @@ -4,7 +4,16 @@ "description": "World Explorer is a tool for visually exploring and manipulating the state of worlds", "type": "module", "exports": { - "./observer": "./dist/exports/observer.js" + "./observer": { + "import": { + "import": "./dist/exports/observer.js", + "types": "./dist/exports/observer.d.ts" + }, + "require": { + "require": "./dist/exports/observer.cjs", + "types": "./dist/exports/observer.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/faucet/package.json b/packages/faucet/package.json index 621494561d..fa29ed6d9b 100644 --- a/packages/faucet/package.json +++ b/packages/faucet/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/gas-report/package.json b/packages/gas-report/package.json index 015fd48b02..d6b3ae9a88 100644 --- a/packages/gas-report/package.json +++ b/packages/gas-report/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - "./internal": "./dist/exports/internal.js" + "./internal": { + "import": { + "import": "./dist/exports/internal.js", + "types": "./dist/exports/internal.d.ts" + }, + "require": { + "require": "./dist/exports/internal.cjs", + "types": "./dist/exports/internal.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/protocol-parser/package.json b/packages/protocol-parser/package.json index 48a489dcd6..7e83c80bbe 100644 --- a/packages/protocol-parser/package.json +++ b/packages/protocol-parser/package.json @@ -10,8 +10,26 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./internal": "./dist/internal.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/react/package.json b/packages/react/package.json index 8ba8d5cf92..79d35e3092 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/recs/package.json b/packages/recs/package.json index 16388ce45f..3e073c08fc 100644 --- a/packages/recs/package.json +++ b/packages/recs/package.json @@ -9,8 +9,26 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./deprecated": "./dist/deprecated/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./deprecated": { + "import": { + "import": "./dist/deprecated/index.js", + "types": "./dist/deprecated/index.d.ts" + }, + "require": { + "require": "./dist/deprecated/index.cjs", + "types": "./dist/deprecated/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/schema-type/package.json b/packages/schema-type/package.json index aaf6fe4204..8961afcc32 100644 --- a/packages/schema-type/package.json +++ b/packages/schema-type/package.json @@ -10,9 +10,36 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./internal": "./dist/internal.js", - "./deprecated": "./dist/deprecated.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + }, + "./deprecated": { + "import": { + "import": "./dist/deprecated.js", + "types": "./dist/deprecated.d.ts" + }, + "require": { + "require": "./dist/deprecated.cjs", + "types": "./dist/deprecated.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/solhint-config-mud/package.json b/packages/solhint-config-mud/package.json index 1118e36133..fce0d56abe 100644 --- a/packages/solhint-config-mud/package.json +++ b/packages/solhint-config-mud/package.json @@ -8,7 +8,16 @@ }, "license": "MIT", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/solhint-plugin-mud/package.json b/packages/solhint-plugin-mud/package.json index f1144a99d7..f7bc5e3dda 100644 --- a/packages/solhint-plugin-mud/package.json +++ b/packages/solhint-plugin-mud/package.json @@ -8,7 +8,16 @@ }, "license": "MIT", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/stash/package.json b/packages/stash/package.json index f435aaf2f5..0eb3ef8065 100644 --- a/packages/stash/package.json +++ b/packages/stash/package.json @@ -10,9 +10,36 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./internal": "./dist/internal.js", - "./react": "./dist/react.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + }, + "./react": { + "import": { + "import": "./dist/react.js", + "types": "./dist/react.d.ts" + }, + "require": { + "require": "./dist/react.cjs", + "types": "./dist/react.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/store-indexer/package.json b/packages/store-indexer/package.json index 79b3b2e7e2..b3fce0000a 100644 --- a/packages/store-indexer/package.json +++ b/packages/store-indexer/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/store-sync/package.json b/packages/store-sync/package.json index 16b35ec609..c1f3a50d4f 100644 --- a/packages/store-sync/package.json +++ b/packages/store-sync/package.json @@ -10,17 +10,116 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./indexer-client": "./dist/indexer-client/index.js", - "./internal": "./dist/exports/internal.js", - "./react": "./dist/exports/react.js", - "./postgres": "./dist/postgres/index.js", - "./postgres-decoded": "./dist/postgres-decoded/index.js", - "./recs": "./dist/recs/index.js", - "./sqlite": "./dist/sqlite/index.js", - "./trpc-indexer": "./dist/trpc-indexer/index.js", - "./world": "./dist/world/index.js", - "./zustand": "./dist/zustand/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./indexer-client": { + "import": { + "import": "./dist/indexer-client/index.js", + "types": "./dist/indexer-client/index.d.ts" + }, + "require": { + "require": "./dist/indexer-client/index.cjs", + "types": "./dist/indexer-client/index.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/exports/internal.js", + "types": "./dist/exports/internal.d.ts" + }, + "require": { + "require": "./dist/exports/internal.cjs", + "types": "./dist/exports/internal.d.cts" + } + }, + "./react": { + "import": { + "import": "./dist/exports/react.js", + "types": "./dist/exports/react.d.ts" + }, + "require": { + "require": "./dist/exports/react.cjs", + "types": "./dist/exports/react.d.cts" + } + }, + "./postgres": { + "import": { + "import": "./dist/postgres/index.js", + "types": "./dist/postgres/index.d.ts" + }, + "require": { + "require": "./dist/postgres/index.cjs", + "types": "./dist/postgres/index.d.cts" + } + }, + "./postgres-decoded": { + "import": { + "import": "./dist/postgres-decoded/index.js", + "types": "./dist/postgres-decoded/index.d.ts" + }, + "require": { + "require": "./dist/postgres-decoded/index.cjs", + "types": "./dist/postgres-decoded/index.d.cts" + } + }, + "./recs": { + "import": { + "import": "./dist/recs/index.js", + "types": "./dist/recs/index.d.ts" + }, + "require": { + "require": "./dist/recs/index.cjs", + "types": "./dist/recs/index.d.cts" + } + }, + "./sqlite": { + "import": { + "import": "./dist/sqlite/index.js", + "types": "./dist/sqlite/index.d.ts" + }, + "require": { + "require": "./dist/sqlite/index.cjs", + "types": "./dist/sqlite/index.d.cts" + } + }, + "./trpc-indexer": { + "import": { + "import": "./dist/trpc-indexer/index.js", + "types": "./dist/trpc-indexer/index.d.ts" + }, + "require": { + "require": "./dist/trpc-indexer/index.cjs", + "types": "./dist/trpc-indexer/index.d.cts" + } + }, + "./world": { + "import": { + "import": "./dist/world/index.js", + "types": "./dist/world/index.d.ts" + }, + "require": { + "require": "./dist/world/index.cjs", + "types": "./dist/world/index.d.cts" + } + }, + "./zustand": { + "import": { + "import": "./dist/zustand/index.js", + "types": "./dist/zustand/index.d.ts" + }, + "require": { + "require": "./dist/zustand/index.cjs", + "types": "./dist/zustand/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/store/package.json b/packages/store/package.json index 0a4b8106f4..4b7e690b7d 100644 --- a/packages/store/package.json +++ b/packages/store/package.json @@ -10,10 +10,46 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./internal": "./dist/internal.js", - "./mud.config": "./dist/mud.config.js", - "./codegen": "./dist/codegen.js", + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + }, + "./mud.config": { + "import": { + "import": "./dist/mud.config.js", + "types": "./dist/mud.config.d.ts" + }, + "require": { + "require": "./dist/mud.config.cjs", + "types": "./dist/mud.config.d.cts" + } + }, + "./codegen": { + "import": { + "import": "./dist/codegen.js", + "types": "./dist/codegen.d.ts" + }, + "require": { + "require": "./dist/codegen.cjs", + "types": "./dist/codegen.d.cts" + } + }, "./out/*": "./out/*" }, "typesVersions": { diff --git a/packages/utils/package.json b/packages/utils/package.json index 747fce6888..de9118f9c2 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -9,7 +9,16 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js" + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } }, "typesVersions": { "*": { diff --git a/packages/world-module-callwithsignature/package.json b/packages/world-module-callwithsignature/package.json index 280b728846..0fe983c03d 100644 --- a/packages/world-module-callwithsignature/package.json +++ b/packages/world-module-callwithsignature/package.json @@ -10,8 +10,26 @@ "license": "MIT", "type": "module", "exports": { - "./internal": "./dist/internal.js", - "./mud.config": "./dist/mud.config.js", + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + }, + "./mud.config": { + "import": { + "import": "./dist/mud.config.js", + "types": "./dist/mud.config.d.ts" + }, + "require": { + "require": "./dist/mud.config.cjs", + "types": "./dist/mud.config.d.cts" + } + }, "./out/*": "./out/*" }, "typesVersions": { diff --git a/packages/world-module-erc20/package.json b/packages/world-module-erc20/package.json index 20ce400196..305d4a0944 100644 --- a/packages/world-module-erc20/package.json +++ b/packages/world-module-erc20/package.json @@ -10,8 +10,26 @@ "license": "MIT", "type": "module", "exports": { - "./internal": "./dist/internal.js", - "./mud.config": "./dist/mud.config.js", + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + }, + "./mud.config": { + "import": { + "import": "./dist/mud.config.js", + "types": "./dist/mud.config.d.ts" + }, + "require": { + "require": "./dist/mud.config.cjs", + "types": "./dist/mud.config.d.cts" + } + }, "./out/*": "./out/*" }, "typesVersions": { diff --git a/packages/world-module-metadata/package.json b/packages/world-module-metadata/package.json index 32ab08679d..6137b6bd52 100644 --- a/packages/world-module-metadata/package.json +++ b/packages/world-module-metadata/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - "./mud.config": "./dist/mud.config.js", + "./mud.config": { + "import": { + "import": "./dist/mud.config.js", + "types": "./dist/mud.config.d.ts" + }, + "require": { + "require": "./dist/mud.config.cjs", + "types": "./dist/mud.config.d.cts" + } + }, "./out/*": "./out/*" }, "typesVersions": { diff --git a/packages/world-modules/package.json b/packages/world-modules/package.json index 7df6e9be5f..dc942cb0c9 100644 --- a/packages/world-modules/package.json +++ b/packages/world-modules/package.json @@ -10,7 +10,16 @@ "license": "MIT", "type": "module", "exports": { - "./internal/mud.config": "./dist/mud.config.js", + "./internal/mud.config": { + "import": { + "import": "./dist/mud.config.js", + "types": "./dist/mud.config.d.ts" + }, + "require": { + "require": "./dist/mud.config.cjs", + "types": "./dist/mud.config.d.cts" + } + }, "./out/*": "./out/*" }, "typesVersions": { diff --git a/packages/world/package.json b/packages/world/package.json index 0108b6bb71..c793a0a705 100644 --- a/packages/world/package.json +++ b/packages/world/package.json @@ -10,10 +10,46 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js", - "./internal": "./dist/internal.js", - "./mud.config": "./dist/mud.config.js", - "./node": "./dist/node.js", + ".": { + "import": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "require": { + "require": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + }, + "./internal": { + "import": { + "import": "./dist/internal.js", + "types": "./dist/internal.d.ts" + }, + "require": { + "require": "./dist/internal.cjs", + "types": "./dist/internal.d.cts" + } + }, + "./mud.config": { + "import": { + "import": "./dist/mud.config.js", + "types": "./dist/mud.config.d.ts" + }, + "require": { + "require": "./dist/mud.config.cjs", + "types": "./dist/mud.config.d.cts" + } + }, + "./node": { + "import": { + "import": "./dist/node.js", + "types": "./dist/node.d.ts" + }, + "require": { + "require": "./dist/node.cjs", + "types": "./dist/node.d.cts" + } + }, "./out/*": "./out/*" }, "typesVersions": { From db10579654973def825dc3127e552a0f5b7ba714 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 07:20:01 -0800 Subject: [PATCH 07/10] Create popular-buses-explain.md --- .changeset/popular-buses-explain.md | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .changeset/popular-buses-explain.md diff --git a/.changeset/popular-buses-explain.md b/.changeset/popular-buses-explain.md new file mode 100644 index 0000000000..e38f59a98f --- /dev/null +++ b/.changeset/popular-buses-explain.md @@ -0,0 +1,32 @@ +--- +"@latticexyz/abi-ts": patch +"@latticexyz/block-logs-stream": patch +"@latticexyz/cli": patch +"@latticexyz/common": patch +"@latticexyz/config": patch +"create-mud": patch +"@latticexyz/dev-tools": patch +"@latticexyz/entrykit": patch +"@latticexyz/explorer": patch +"@latticexyz/faucet": patch +"@latticexyz/gas-report": patch +"@latticexyz/protocol-parser": patch +"@latticexyz/react": patch +"@latticexyz/recs": patch +"@latticexyz/schema-type": patch +"solhint-config-mud": patch +"solhint-plugin-mud": patch +"@latticexyz/stash": patch +"@latticexyz/store-indexer": patch +"@latticexyz/store-sync": patch +"@latticexyz/store": patch +"@latticexyz/utils": patch +"vite-plugin-mud": patch +"@latticexyz/world-module-callwithsignature": patch +"@latticexyz/world-module-erc20": patch +"@latticexyz/world-module-metadata": patch +"@latticexyz/world-modules": patch +"@latticexyz/world": patch +--- + +Added CJS exports alongside existing ESM exports. From c0c1791cec543f6a8f09a608c49c8bdd1b8d163d Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 15:43:16 +0000 Subject: [PATCH 08/10] don't minify --- tsup.config.base.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tsup.config.base.ts b/tsup.config.base.ts index 9fbae3b24d..9c4440d80a 100644 --- a/tsup.config.base.ts +++ b/tsup.config.base.ts @@ -8,7 +8,6 @@ export function baseConfig(opts: Options): Options { return { target: "esnext", format: ["esm", "cjs"], - minify: true, sourcemap: true, // don't generate DTS during watch mode because it's slow // we're likely using TS source in this mode anyway From 9fa07c8489f1fbf167d0db01cd9aaa645a29c8e2 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 15:55:55 +0000 Subject: [PATCH 09/10] enable shims --- tsup.config.base.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tsup.config.base.ts b/tsup.config.base.ts index 9c4440d80a..e3c65b2b6b 100644 --- a/tsup.config.base.ts +++ b/tsup.config.base.ts @@ -16,5 +16,7 @@ export function baseConfig(opts: Options): Options { // previously-built DTS files, which other build tasks // depend on clean: !opts.watch, + // replaces __dirname with import.meta.url in ESM + shims: true, }; } From 9947c3dcd0f9679b3549bb2a7b71b35b8059e85d Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 4 Feb 2025 16:07:45 +0000 Subject: [PATCH 10/10] make it consistent --- packages/explorer/src/bin/explorer.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/explorer/src/bin/explorer.ts b/packages/explorer/src/bin/explorer.ts index 8eca284392..a6ccdfc48c 100755 --- a/packages/explorer/src/bin/explorer.ts +++ b/packages/explorer/src/bin/explorer.ts @@ -120,9 +120,7 @@ process.on("exit", () => { explorerProcess?.kill(); }); -async function main() { +(async function main() { await startStoreIndexer(); await startExplorer(); -} - -main().catch(console.error); +})();