apibara 2.1.0-beta.1 → 2.1.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/chunks/add.mjs +12 -7
  2. package/dist/chunks/dev.mjs +25 -6
  3. package/dist/chunks/init.mjs +3 -7
  4. package/dist/chunks/start.mjs +2 -1
  5. package/dist/core/index.mjs +85 -42
  6. package/dist/create/index.d.mts +2 -1
  7. package/dist/create/index.d.ts +2 -1
  8. package/dist/create/index.mjs +185 -121
  9. package/dist/hooks/index.mjs +3 -1
  10. package/dist/rolldown/index.d.mts +7 -0
  11. package/dist/rolldown/index.d.ts +7 -0
  12. package/dist/rolldown/index.mjs +126 -0
  13. package/dist/runtime/dev.mjs +3 -0
  14. package/dist/runtime/internal/app.d.ts +1 -1
  15. package/dist/runtime/internal/app.mjs +12 -11
  16. package/dist/runtime/start.mjs +5 -0
  17. package/dist/shared/apibara.af330c7d.mjs +15 -0
  18. package/dist/types/index.d.mts +18 -15
  19. package/dist/types/index.d.ts +18 -15
  20. package/package.json +13 -15
  21. package/src/cli/commands/add.ts +12 -6
  22. package/src/cli/commands/dev.ts +27 -5
  23. package/src/cli/commands/init.ts +3 -7
  24. package/src/cli/commands/start.ts +1 -0
  25. package/src/core/build/build.ts +13 -5
  26. package/src/core/build/dev.ts +44 -23
  27. package/src/core/build/error.ts +9 -14
  28. package/src/core/build/prod.ts +15 -10
  29. package/src/core/build/types.ts +8 -0
  30. package/src/core/config/defaults.ts +3 -0
  31. package/src/core/config/resolvers/runtime-config.resolver.ts +21 -2
  32. package/src/core/config/update.ts +1 -1
  33. package/src/create/add.ts +26 -12
  34. package/src/create/constants.ts +9 -10
  35. package/src/create/init.ts +28 -14
  36. package/src/create/templates.ts +154 -118
  37. package/src/create/utils.ts +10 -0
  38. package/src/hooks/useRuntimeConfig.ts +2 -1
  39. package/src/rolldown/config.ts +108 -0
  40. package/src/rolldown/index.ts +2 -0
  41. package/src/rolldown/plugins/config.ts +25 -0
  42. package/src/{rollup → rolldown}/plugins/indexers.ts +3 -3
  43. package/src/runtime/dev.ts +3 -0
  44. package/src/runtime/internal/app.ts +30 -13
  45. package/src/runtime/start.ts +5 -0
  46. package/src/types/config.ts +12 -7
  47. package/src/types/hooks.ts +8 -5
  48. package/src/types/index.ts +1 -1
  49. package/src/types/rolldown.ts +5 -0
  50. package/src/types/virtual/config.d.ts +4 -1
  51. package/src/types/virtual/indexers.d.ts +4 -1
  52. package/src/utils/helper.ts +15 -0
  53. package/dist/rollup/index.d.mts +0 -6
  54. package/dist/rollup/index.d.ts +0 -6
  55. package/dist/rollup/index.mjs +0 -150
  56. package/src/rollup/config.ts +0 -87
  57. package/src/rollup/index.ts +0 -2
  58. package/src/rollup/plugins/config.ts +0 -12
  59. package/src/rollup/plugins/esm-shim.ts +0 -69
  60. package/src/types/rollup.ts +0 -8
@@ -33,6 +33,9 @@ const startCommand = defineCommand({
33
33
  await Promise.all(
34
34
  selectedIndexers.map(async (indexer) => {
35
35
  const indexerInstance = createIndexer(indexer, preset);
36
+ if (!indexerInstance) {
37
+ return;
38
+ }
36
39
  const client = createClient(
37
40
  indexerInstance.streamConfig,
38
41
  indexerInstance.options.streamUrl
@@ -1,2 +1,2 @@
1
1
  export declare const availableIndexers: any;
2
- export declare function createIndexer(indexerName: string, preset?: string): import("@apibara/indexer").Indexer<unknown, unknown>;
2
+ export declare function createIndexer(indexerName: string, preset?: string): import("@apibara/indexer").Indexer<unknown, unknown> | undefined;
@@ -6,6 +6,7 @@ import {
6
6
  inMemoryPersistence,
7
7
  logger
8
8
  } from "@apibara/indexer/plugins";
9
+ import consola from "consola";
9
10
  import { config } from "#apibara-internal-virtual/config";
10
11
  import { indexers } from "#apibara-internal-virtual/indexers";
11
12
  import { createLogger } from "./logger.mjs";
@@ -30,27 +31,27 @@ export function createIndexer(indexerName, preset) {
30
31
  `Specified indexer "${indexerName}" but it was not defined`
31
32
  );
32
33
  }
33
- const definition = typeof indexerDefinition.indexer === "function" ? indexerDefinition.indexer(runtimeConfig) : indexerDefinition.indexer;
34
- let reporter = createLogger({
34
+ const indexerModule = indexerDefinition.indexer?.default;
35
+ if (indexerModule === void 0) {
36
+ consola.warn(
37
+ `Specified indexer "${indexerName}" but it does not export a default. Ignoring.`
38
+ );
39
+ return;
40
+ }
41
+ const definition = typeof indexerModule === "function" ? indexerModule(runtimeConfig) : indexerModule;
42
+ const reporter = createLogger({
35
43
  indexer: indexerName,
36
44
  preset,
37
45
  indexers: availableIndexers
38
46
  });
39
- if (config.logger) {
40
- reporter = config.logger({
41
- indexer: indexerName,
42
- preset,
43
- indexers: availableIndexers
44
- });
45
- }
46
47
  definition.plugins = [
47
48
  internalContext({
48
49
  indexerName,
49
50
  availableIndexers
50
51
  }),
52
+ logger({ logger: reporter }),
51
53
  inMemoryPersistence(),
52
- ...definition.plugins ?? [],
53
- logger({ logger: reporter })
54
+ ...definition.plugins ?? []
54
55
  ];
55
56
  return _createIndexer(definition);
56
57
  }
@@ -1,6 +1,7 @@
1
1
  import { runWithReconnect } from "@apibara/indexer";
2
2
  import { createClient } from "@apibara/protocol";
3
3
  import { defineCommand, runMain } from "citty";
4
+ import consola from "consola";
4
5
  import { createIndexer } from "./internal/app.mjs";
5
6
  const startCommand = defineCommand({
6
7
  meta: {
@@ -21,6 +22,10 @@ const startCommand = defineCommand({
21
22
  async run({ args }) {
22
23
  const { indexer, preset } = args;
23
24
  const indexerInstance = createIndexer(indexer, preset);
25
+ if (!indexerInstance) {
26
+ consola.error(`Specified indexer "${indexer}" but it was not defined`);
27
+ process.exit(1);
28
+ }
24
29
  const client = createClient(
25
30
  indexerInstance.streamConfig,
26
31
  indexerInstance.options.streamUrl
@@ -0,0 +1,15 @@
1
+ function deserialize(str) {
2
+ return JSON.parse(
3
+ str,
4
+ (_, value) => typeof value === "string" && value.match(/^\d+n$/) ? BigInt(value.slice(0, -1)) : value
5
+ );
6
+ }
7
+ function serialize(obj) {
8
+ return JSON.stringify(
9
+ obj,
10
+ (_, value) => typeof value === "bigint" ? `${value.toString()}n` : value,
11
+ " "
12
+ );
13
+ }
14
+
15
+ export { deserialize as d, serialize as s };
@@ -1,25 +1,19 @@
1
1
  import { ConsolaInstance } from 'consola';
2
2
  import { NestedHooks, Hookable } from 'hookable';
3
3
  import { ConsolaReporter } from '@apibara/indexer/plugins';
4
- import { RollupCommonJSOptions } from '@rollup/plugin-commonjs';
5
4
  import { C12InputConfig, WatchConfigOptions, ResolvedConfig, ConfigWatcher } from 'c12';
6
- import { WatchOptions } from 'chokidar';
7
- import { InputOptions, OutputOptions } from 'rollup';
5
+ import { RolldownOptions, WatchOptions, InputOptions, OutputOptions } from 'rolldown';
8
6
 
9
7
  type DeepPartial<T> = T extends Record<string, any> ? {
10
8
  [P in keyof T]?: DeepPartial<T[P]> | T[P];
11
9
  } : T;
12
10
 
13
- type RollupConfig = InputOptions & {
14
- output: OutputOptions;
15
- };
16
-
17
11
  interface ApibaraHooks {
18
- "rollup:before": (apibara: Apibara, rollupConfig: RollupConfig) => void;
12
+ "rolldown:before": (apibara: Apibara, rolldownConfig: RolldownOptions) => void;
19
13
  compiled: (apibara: Apibara) => void;
20
- "dev:restart": () => void;
21
- "dev:reload": () => void;
22
- "rollup:reload": () => void;
14
+ "dev:restart": () => Promise<void>;
15
+ "dev:reload": () => Promise<void>;
16
+ "rolldown:reload": () => Promise<void>;
23
17
  restart: () => void;
24
18
  close: () => void;
25
19
  }
@@ -58,13 +52,18 @@ interface ApibaraOptions<T extends Record<string, DeepPartial<Pick<ApibaraConfig
58
52
  outputDir: string;
59
53
  indexersDir: string;
60
54
  dev: boolean;
61
- watchOptions: WatchOptions;
55
+ watchOptions: WatchOptions["watch"];
62
56
  hooks: NestedHooks<ApibaraHooks>;
63
57
  logger?: LoggerFactory;
64
- rollupConfig?: Partial<RollupConfig>;
58
+ rolldownConfig?: Partial<RolldownOptions>;
59
+ /**
60
+ * @deprecated Use rolldownConfig instead. This option will be removed in future releases.
61
+ */
62
+ rollupConfig?: unknown;
65
63
  sourceMap?: boolean;
66
64
  entry: string;
67
- commonJS?: RollupCommonJSOptions;
65
+ node: boolean;
66
+ exportConditions?: string[];
68
67
  typescript: {
69
68
  strict?: boolean;
70
69
  internalPaths?: boolean;
@@ -85,6 +84,10 @@ interface Apibara {
85
84
  updateConfig: (config: ApibaraDynamicConfig) => void | Promise<void>;
86
85
  }
87
86
 
87
+ type RolldownConfig = InputOptions & {
88
+ output: OutputOptions;
89
+ };
90
+
88
91
  type ApibaraRuntimeConfig = Record<string, unknown>;
89
92
 
90
- export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactory, RollupConfig };
93
+ export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactory, RolldownConfig };
@@ -1,25 +1,19 @@
1
1
  import { ConsolaInstance } from 'consola';
2
2
  import { NestedHooks, Hookable } from 'hookable';
3
3
  import { ConsolaReporter } from '@apibara/indexer/plugins';
4
- import { RollupCommonJSOptions } from '@rollup/plugin-commonjs';
5
4
  import { C12InputConfig, WatchConfigOptions, ResolvedConfig, ConfigWatcher } from 'c12';
6
- import { WatchOptions } from 'chokidar';
7
- import { InputOptions, OutputOptions } from 'rollup';
5
+ import { RolldownOptions, WatchOptions, InputOptions, OutputOptions } from 'rolldown';
8
6
 
9
7
  type DeepPartial<T> = T extends Record<string, any> ? {
10
8
  [P in keyof T]?: DeepPartial<T[P]> | T[P];
11
9
  } : T;
12
10
 
13
- type RollupConfig = InputOptions & {
14
- output: OutputOptions;
15
- };
16
-
17
11
  interface ApibaraHooks {
18
- "rollup:before": (apibara: Apibara, rollupConfig: RollupConfig) => void;
12
+ "rolldown:before": (apibara: Apibara, rolldownConfig: RolldownOptions) => void;
19
13
  compiled: (apibara: Apibara) => void;
20
- "dev:restart": () => void;
21
- "dev:reload": () => void;
22
- "rollup:reload": () => void;
14
+ "dev:restart": () => Promise<void>;
15
+ "dev:reload": () => Promise<void>;
16
+ "rolldown:reload": () => Promise<void>;
23
17
  restart: () => void;
24
18
  close: () => void;
25
19
  }
@@ -58,13 +52,18 @@ interface ApibaraOptions<T extends Record<string, DeepPartial<Pick<ApibaraConfig
58
52
  outputDir: string;
59
53
  indexersDir: string;
60
54
  dev: boolean;
61
- watchOptions: WatchOptions;
55
+ watchOptions: WatchOptions["watch"];
62
56
  hooks: NestedHooks<ApibaraHooks>;
63
57
  logger?: LoggerFactory;
64
- rollupConfig?: Partial<RollupConfig>;
58
+ rolldownConfig?: Partial<RolldownOptions>;
59
+ /**
60
+ * @deprecated Use rolldownConfig instead. This option will be removed in future releases.
61
+ */
62
+ rollupConfig?: unknown;
65
63
  sourceMap?: boolean;
66
64
  entry: string;
67
- commonJS?: RollupCommonJSOptions;
65
+ node: boolean;
66
+ exportConditions?: string[];
68
67
  typescript: {
69
68
  strict?: boolean;
70
69
  internalPaths?: boolean;
@@ -85,6 +84,10 @@ interface Apibara {
85
84
  updateConfig: (config: ApibaraDynamicConfig) => void | Promise<void>;
86
85
  }
87
86
 
87
+ type RolldownConfig = InputOptions & {
88
+ output: OutputOptions;
89
+ };
90
+
88
91
  type ApibaraRuntimeConfig = Record<string, unknown>;
89
92
 
90
- export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactory, RollupConfig };
93
+ export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactory, RolldownConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apibara",
3
- "version": "2.1.0-beta.1",
3
+ "version": "2.1.0-beta.11",
4
4
  "type": "module",
5
5
  "main": "./dist/core/index.mjs",
6
6
  "exports": {
@@ -20,9 +20,9 @@
20
20
  "types": "./dist/core/index.d.ts",
21
21
  "import": "./dist/core/index.mjs"
22
22
  },
23
- "./rollup": {
24
- "types": "./dist/rollup/index.d.ts",
25
- "import": "./dist/rollup/index.mjs"
23
+ "./rolldown": {
24
+ "types": "./dist/rolldown/index.d.ts",
25
+ "import": "./dist/rolldown/index.mjs"
26
26
  },
27
27
  "./types": {
28
28
  "types": "./dist/types/index.d.ts",
@@ -73,10 +73,12 @@
73
73
  "playground:prepare": "pnpm playground prepare --dir playground",
74
74
  "playground:dev": "pnpm playground dev --dir playground",
75
75
  "playground:build": "pnpm playground build --dir playground",
76
- "playground:start": "pnpm playground start --dir playground --indexer starknet"
76
+ "playground:start": "pnpm playground start --dir playground --indexer starknet",
77
+ "playground:init": "pnpm playground init playground",
78
+ "playground:add": "pnpm playground add --dir playground"
77
79
  },
78
80
  "devDependencies": {
79
- "@apibara/starknet": "2.1.0-beta.1",
81
+ "@apibara/starknet": "2.1.0-beta.11",
80
82
  "@types/fs-extra": "^11.0.4",
81
83
  "@types/node": "^20.14.0",
82
84
  "@types/prompts": "^2.4.9",
@@ -87,19 +89,15 @@
87
89
  "vitest": "^1.6.0"
88
90
  },
89
91
  "dependencies": {
90
- "@apibara/indexer": "2.1.0-beta.1",
91
- "@apibara/protocol": "2.1.0-beta.1",
92
- "@rollup/plugin-commonjs": "^26.0.1",
93
- "@rollup/plugin-json": "^6.1.0",
94
- "@rollup/plugin-node-resolve": "^15.2.3",
95
- "@rollup/plugin-typescript": "^11.1.6",
92
+ "@apibara/indexer": "2.1.0-beta.11",
93
+ "@apibara/protocol": "2.1.0-beta.11",
94
+ "@rollup/plugin-replace": "^6.0.2",
96
95
  "@rollup/plugin-virtual": "^3.0.2",
97
96
  "c12": "^1.11.1",
98
97
  "chokidar": "^3.6.0",
99
98
  "citty": "^0.1.6",
100
99
  "consola": "^3.2.3",
101
100
  "defu": "^6.1.4",
102
- "esbuild": "^0.23.0",
103
101
  "fs-extra": "^11.2.0",
104
102
  "hookable": "^5.5.3",
105
103
  "klona": "^2.0.6",
@@ -109,9 +107,9 @@
109
107
  "perfect-debounce": "^1.0.0",
110
108
  "picocolors": "^1.1.1",
111
109
  "pkg-types": "^1.1.3",
110
+ "prettier": "^3.5.2",
112
111
  "prompts": "^2.4.2",
113
- "rollup": "^4.18.1",
114
- "rollup-plugin-esbuild": "^6.1.1",
112
+ "rolldown": "1.0.0-beta.3",
115
113
  "ts-morph": "^25.0.1",
116
114
  "tslib": "^2.6.3",
117
115
  "untyped": "^1.4.2"
@@ -10,28 +10,33 @@ export default defineCommand({
10
10
  args: {
11
11
  indexerId: {
12
12
  type: "positional",
13
- description: "Indexer ID",
13
+ description: "Indexer ID - must be in kebab-case",
14
14
  required: false,
15
15
  },
16
16
  chain: {
17
17
  type: "string",
18
- description: "Chain",
18
+ description: "Blockchain - ethereum, beaconchain, starknet",
19
19
  },
20
20
  network: {
21
21
  type: "string",
22
- description: "Network",
22
+ description: "Network - mainnet, sepolia, other",
23
23
  },
24
24
  storage: {
25
25
  type: "string",
26
- description: "Storage",
26
+ description: "Storage - postgres, none",
27
27
  },
28
28
  dnaUrl: {
29
29
  type: "string",
30
- description: "DNA URL",
30
+ description: "DNA URL - https://custom-dna-url.apibara.org",
31
+ },
32
+ dir: {
33
+ type: "string",
34
+ description:
35
+ "Root directory - apibara project root where apibara.config is located | default: current working directory",
31
36
  },
32
37
  },
33
38
  async run({ args }) {
34
- const { indexerId, chain, network, storage, dnaUrl } = args;
39
+ const { indexerId, chain, network, storage, dnaUrl, dir } = args;
35
40
 
36
41
  await addIndexer({
37
42
  argIndexerId: indexerId,
@@ -39,6 +44,7 @@ export default defineCommand({
39
44
  argNetwork: network,
40
45
  argStorage: storage,
41
46
  argDnaUrl: dnaUrl,
47
+ argRootDir: dir,
42
48
  });
43
49
  },
44
50
  });
@@ -26,10 +26,20 @@ export default defineCommand({
26
26
  type: "string",
27
27
  description: "Preset to use",
28
28
  },
29
+ alwaysReindex: {
30
+ type: "boolean",
31
+ default: false,
32
+ description:
33
+ "Reindex the indexers from the starting block on every restart (default: false)",
34
+ },
29
35
  },
30
36
  async run({ args }) {
31
37
  const rootDir = resolve((args.dir || args._dir || ".") as string);
32
38
 
39
+ if (args.alwaysReindex) {
40
+ process.env.APIBARA_ALWAYS_REINDEX = "true";
41
+ }
42
+
33
43
  let apibara: Apibara;
34
44
  let childProcess: ChildProcess | undefined;
35
45
 
@@ -46,6 +56,7 @@ export default defineCommand({
46
56
  apibara = await createApibara(
47
57
  {
48
58
  rootDir,
59
+ preset: args.preset,
49
60
  },
50
61
  {
51
62
  watch: true,
@@ -79,19 +90,21 @@ export default defineCommand({
79
90
  await writeTypes(apibara);
80
91
  await build(apibara);
81
92
 
82
- apibara.hooks.hook("dev:restart", () => {
93
+ apibara.hooks.hook("dev:restart", async () => {
83
94
  if (childProcess) {
84
95
  apibara.logger.info("Change detected, stopping indexers to restart");
85
- childProcess.kill();
96
+ await killProcess(childProcess);
86
97
  childProcess = undefined;
87
98
  }
88
99
  });
89
100
 
90
- apibara.hooks.hook("dev:reload", () => {
101
+ apibara.hooks.hook("dev:reload", async () => {
91
102
  if (childProcess) {
92
- childProcess.kill();
103
+ apibara.logger.info("Restarting indexers");
104
+ await killProcess(childProcess);
105
+ childProcess = undefined;
93
106
  } else {
94
- apibara.logger.success("Restarting indexers");
107
+ apibara.logger.info("Starting indexers");
95
108
  }
96
109
 
97
110
  const childArgs = [
@@ -118,3 +131,12 @@ export default defineCommand({
118
131
  await reload();
119
132
  },
120
133
  });
134
+
135
+ async function killProcess(childProcess: ChildProcess | undefined) {
136
+ if (childProcess) {
137
+ await new Promise((resolve) => {
138
+ childProcess.once("exit", resolve);
139
+ childProcess.kill();
140
+ });
141
+ }
142
+ }
@@ -18,23 +18,19 @@ export default defineCommand({
18
18
  default: "ts",
19
19
  alias: "l",
20
20
  },
21
- "no-create-indexer": {
21
+ noIndexer: {
22
22
  type: "boolean",
23
23
  description: "Do not create an indexer after initialization",
24
24
  default: false,
25
25
  },
26
26
  },
27
27
  async run({ args }) {
28
- const {
29
- dir: targetDir,
30
- "no-create-indexer": noCreateIndexer,
31
- language,
32
- } = args;
28
+ const { dir: targetDir, noIndexer, language } = args;
33
29
 
34
30
  await initializeProject({
35
31
  argTargetDir: targetDir,
36
32
  argLanguage: language,
37
- argNoCreateIndexer: noCreateIndexer,
33
+ argNoCreateIndexer: noIndexer,
38
34
  });
39
35
  },
40
36
  });
@@ -28,6 +28,7 @@ export default defineCommand({
28
28
 
29
29
  const apibara = await createApibara({
30
30
  rootDir,
31
+ preset,
31
32
  });
32
33
 
33
34
  apibara.logger.start(
@@ -1,14 +1,22 @@
1
- import { getRollupConfig } from "apibara/rollup";
1
+ import { getRolldownConfig } from "apibara/rolldown";
2
2
  import type { Apibara } from "apibara/types";
3
+ import { colors } from "consola/utils";
3
4
  import { watchDev } from "./dev";
4
5
  import { buildProduction } from "./prod";
5
6
 
6
7
  export async function build(apibara: Apibara) {
7
- const rollupConfig = getRollupConfig(apibara);
8
+ const rolldownConfig = getRolldownConfig(apibara);
8
9
 
9
- await apibara.hooks.callHook("rollup:before", apibara, rollupConfig);
10
+ await apibara.hooks.callHook("rolldown:before", apibara, rolldownConfig);
11
+
12
+ if (apibara.options.rollupConfig) {
13
+ apibara.logger.error(
14
+ `\n${colors.cyan("apibara.config:")} rollupConfig is deprecated. Use rolldownConfig instead`,
15
+ );
16
+ process.exit(1);
17
+ }
10
18
 
11
19
  return apibara.options.dev
12
- ? await watchDev(apibara, rollupConfig)
13
- : await buildProduction(apibara, rollupConfig);
20
+ ? await watchDev(apibara, rolldownConfig)
21
+ : await buildProduction(apibara, rolldownConfig);
14
22
  }
@@ -1,59 +1,67 @@
1
- import type { Apibara, RollupConfig } from "apibara/types";
1
+ import type { Apibara } from "apibara/types";
2
2
  import { watch } from "chokidar";
3
3
  import defu from "defu";
4
4
  import { join } from "pathe";
5
5
  import { debounce } from "perfect-debounce";
6
- import * as rollup from "rollup";
7
- import { formatRollupError } from "./error";
8
-
9
- export async function watchDev(apibara: Apibara, rollupConfig: RollupConfig) {
10
- let rollupWatcher: rollup.RollupWatcher;
6
+ import * as rolldown from "rolldown";
7
+ import { formatRolldownError } from "./error";
11
8
 
9
+ export async function watchDev(
10
+ apibara: Apibara,
11
+ rolldownConfig: rolldown.RolldownOptions,
12
+ ) {
13
+ let rolldownWatcher: rolldown.RolldownWatcher;
12
14
  async function load() {
13
- if (rollupWatcher) {
14
- await rollupWatcher.close();
15
+ apibara.logger.start("Setting up a dev server");
16
+ if (rolldownWatcher) {
17
+ await rolldownWatcher.close();
15
18
  }
16
- rollupWatcher = startRollupWatcher(apibara, rollupConfig);
19
+ rolldownWatcher = startRolldownWatcher(apibara, rolldownConfig);
17
20
  }
18
- const reload = debounce(load);
21
+ const reload = debounce(async () => await load());
19
22
 
20
- const watchPatterns = [join(apibara.options.rootDir, "indexers")];
23
+ const watchPatterns = getWatchPatterns(apibara);
21
24
 
22
25
  const watchReloadEvents = new Set(["add", "addDir", "unlink", "unlinkDir"]);
23
26
  const reloadWatcher = watch(watchPatterns, { ignoreInitial: true }).on(
24
27
  "all",
25
- (event) => {
28
+ async (event) => {
26
29
  if (watchReloadEvents.has(event)) {
27
- reload();
30
+ await reload();
28
31
  }
29
32
  },
30
33
  );
31
34
 
32
35
  apibara.hooks.hook("close", () => {
33
- rollupWatcher.close();
36
+ rolldownWatcher.close();
34
37
  reloadWatcher.close();
35
38
  });
36
39
 
37
- apibara.hooks.hook("rollup:reload", () => reload());
40
+ apibara.hooks.hook("rolldown:reload", async () => await reload());
38
41
 
39
42
  await load();
40
43
  }
41
44
 
42
- function startRollupWatcher(apibara: Apibara, rollupConfig: RollupConfig) {
43
- const watcher = rollup.watch(
44
- defu(rollupConfig, {
45
+ function startRolldownWatcher(
46
+ apibara: Apibara,
47
+ rolldownConfig: rolldown.RolldownOptions,
48
+ ) {
49
+ const ignorePatterns = getIgnorePatterns(apibara);
50
+ const watcher = rolldown.watch(
51
+ defu(rolldownConfig, {
45
52
  watch: {
46
- chokidar: apibara.options.watchOptions,
53
+ exclude: ignorePatterns,
54
+ ...((apibara.options.watchOptions ?? {}) as rolldown.WatchOptions),
47
55
  },
48
56
  }),
49
57
  );
50
58
  let start: number;
51
59
 
52
- watcher.on("event", (event) => {
60
+ watcher.on("event", async (event) => {
53
61
  switch (event.code) {
54
62
  // The watcher is (re)starting
55
63
  case "START": {
56
- apibara.hooks.callHook("dev:restart");
64
+ await apibara.hooks.callHook("dev:restart");
57
65
  return;
58
66
  }
59
67
 
@@ -70,15 +78,28 @@ function startRollupWatcher(apibara: Apibara, rollupConfig: RollupConfig) {
70
78
  "Indexers built",
71
79
  start ? `in ${Date.now() - start} ms` : "",
72
80
  );
73
- apibara.hooks.callHook("dev:reload");
81
+ await apibara.hooks.callHook("dev:reload");
74
82
  return;
75
83
  }
76
84
 
77
85
  // Encountered an error while bundling
78
86
  case "ERROR": {
79
- apibara.logger.error(formatRollupError(event.error));
87
+ apibara.logger.error(formatRolldownError(event.error));
80
88
  }
81
89
  }
82
90
  });
83
91
  return watcher;
84
92
  }
93
+
94
+ const getWatchPatterns = (apibara: Apibara) => [
95
+ join(apibara.options.rootDir, "indexers"),
96
+ ];
97
+
98
+ const getIgnorePatterns = (apibara: Apibara) => [
99
+ "**/.apibara/**",
100
+ "**/.git/**",
101
+ "**/.DS_Store",
102
+ "**/node_modules/**",
103
+ "**/dist/**",
104
+ "**/.turbo/**",
105
+ ];
@@ -1,29 +1,24 @@
1
- import type esbuild from "esbuild";
2
1
  import { isAbsolute, relative } from "pathe";
3
- import type rollup from "rollup";
2
+ import type * as rolldown from "rolldown";
4
3
 
5
- export function formatRollupError(
6
- _error: rollup.RollupError | esbuild.OnResolveResult,
7
- ) {
4
+ export function formatRolldownError(_error: rolldown.RollupError) {
8
5
  try {
9
6
  const logs: string[] = [_error.toString()];
10
7
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
11
- const errors = (_error as any)?.errors || [_error as rollup.RollupError];
8
+ const errors = (_error as any)?.errors || [_error as rolldown.RollupError];
12
9
  for (const error of errors) {
13
- const id = error.path || error.id || (_error as rollup.RollupError).id;
10
+ const id = error.path || error.id || (_error as rolldown.RollupError).id;
14
11
  let path = isAbsolute(id) ? relative(process.cwd(), id) : id;
15
- const location =
16
- (error as rollup.RollupError).loc ||
17
- (error as esbuild.PartialMessage).location;
12
+ const location = (error as rolldown.RollupError).loc;
18
13
  if (location) {
19
14
  path += `:${location.line}:${location.column}`;
20
15
  }
21
- const text =
22
- (error as esbuild.PartialMessage).text ||
23
- (error as rollup.RollupError).frame;
16
+ const text = (error as rolldown.RollupError).frame;
24
17
 
25
18
  logs.push(
26
- `Rollup error while processing \`${path}\`` + text ? "\n\n" + text : "",
19
+ `Rolldown error while processing \`${path}\`` + text
20
+ ? "\n\n" + text
21
+ : "",
27
22
  );
28
23
  }
29
24
  return logs.join("\n");