goatly 0.1.0 → 0.1.1

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 (158) hide show
  1. package/README.md +853 -1
  2. package/bin/goatly.js +91 -0
  3. package/dist/cli/index.js +234 -0
  4. package/dist/compat/gatsby-node-entry.js +16 -0
  5. package/dist/compat/gatsby-package.json +6 -0
  6. package/dist/compat/md5-file.js +47 -0
  7. package/dist/compat-apis.json +72 -0
  8. package/dist/compat-apis.json.js +62 -0
  9. package/dist/graphql.js +3 -0
  10. package/dist/node/actions/index.js +619 -0
  11. package/dist/node/actions/job-store.js +180 -0
  12. package/dist/node/actions/jobs.js +174 -0
  13. package/dist/node/api-runner.js +258 -0
  14. package/dist/node/bootstrap.js +539 -0
  15. package/dist/node/build/assets.js +110 -0
  16. package/dist/node/build/incremental.js +208 -0
  17. package/dist/node/build/index.js +752 -0
  18. package/dist/node/build/memory-governor.js +448 -0
  19. package/dist/node/build/output-dirs.js +70 -0
  20. package/dist/node/build/page-data.js +40 -0
  21. package/dist/node/build/page-files.js +54 -0
  22. package/dist/node/build/partitioned-state.js +258 -0
  23. package/dist/node/build/query-worker.js +536 -0
  24. package/dist/node/build/query-workers.js +832 -0
  25. package/dist/node/build/render-worker.js +131 -0
  26. package/dist/node/build/state.js +24 -0
  27. package/dist/node/build/timing-breakdown.js +155 -0
  28. package/dist/node/build/worker-pool.js +236 -0
  29. package/dist/node/cache-layout.js +99 -0
  30. package/dist/node/cache.js +1430 -0
  31. package/dist/node/compat/chokidar-shim.js +148 -0
  32. package/dist/node/compat/core-utils-lmdb.js +145 -0
  33. package/dist/node/compat/create-node-id-shim.js +1 -0
  34. package/dist/node/compat/module-hooks.js +245 -0
  35. package/dist/node/compat/redux-shim.js +1 -0
  36. package/dist/node/config/index.js +162 -0
  37. package/dist/node/config/not-found-pages.js +95 -0
  38. package/dist/node/datastore/codec.js +144 -0
  39. package/dist/node/datastore/index.js +127 -0
  40. package/dist/node/datastore/logfile/cache.js +89 -0
  41. package/dist/node/datastore/logfile/index.js +1329 -0
  42. package/dist/node/datastore/logfile/record.js +71 -0
  43. package/dist/node/datastore/types.js +1 -0
  44. package/dist/node/develop/express-compat.js +207 -0
  45. package/dist/node/develop/graphiql.js +22 -0
  46. package/dist/node/develop/hydration-report.js +41 -0
  47. package/dist/node/develop/index.js +618 -0
  48. package/dist/node/develop/local-url.js +10 -0
  49. package/dist/node/develop/ssr-runner.js +66 -0
  50. package/dist/node/gql.js +5 -0
  51. package/dist/node/index.js +15 -0
  52. package/dist/node/info.js +270 -0
  53. package/dist/node/internal-plugins/data-bridge.js +196 -0
  54. package/dist/node/internal-plugins/page-creator.js +318 -0
  55. package/dist/node/loader/import-module.js +160 -0
  56. package/dist/node/node-helpers.js +66 -0
  57. package/dist/node/not-found-pages.js +220 -0
  58. package/dist/node/plugins/api-list.js +12 -0
  59. package/dist/node/plugins/load-plugins.js +427 -0
  60. package/dist/node/plugins/resolve-plugin.js +79 -0
  61. package/dist/node/plugins/static-exports.js +64 -0
  62. package/dist/node/query/compile.js +326 -0
  63. package/dist/node/query/dependencies.js +45 -0
  64. package/dist/node/query/execute.js +417 -0
  65. package/dist/node/query/extract.js +372 -0
  66. package/dist/node/query/gql-tada-introspection.js +124 -0
  67. package/dist/node/query/graphql-env.js +231 -0
  68. package/dist/node/query/graphql-runner.js +79 -0
  69. package/dist/node/redux/actions.js +1 -0
  70. package/dist/node/redux/index.js +133 -0
  71. package/dist/node/redux/node-changes.js +128 -0
  72. package/dist/node/redux/reducers.js +193 -0
  73. package/dist/node/redux/state.js +71 -0
  74. package/dist/node/renderer.js +124 -0
  75. package/dist/node/reporter-transports.js +259 -0
  76. package/dist/node/reporter.js +497 -0
  77. package/dist/node/schema/date.js +539 -0
  78. package/dist/node/schema/extensions.js +315 -0
  79. package/dist/node/schema/index.js +542 -0
  80. package/dist/node/schema/infer.js +388 -0
  81. package/dist/node/schema/inline-objects.js +128 -0
  82. package/dist/node/schema/naming.js +20 -0
  83. package/dist/node/schema/node-model.js +207 -0
  84. package/dist/node/schema/query-engine.js +1808 -0
  85. package/dist/node/schema/resolver-kinds.js +7 -0
  86. package/dist/node/schema/resolvers.js +273 -0
  87. package/dist/node/schema/type-builders.js +45 -0
  88. package/dist/node/schema/types.js +241 -0
  89. package/dist/node/serve.js +90 -0
  90. package/dist/node/site-page-nodes.js +277 -0
  91. package/dist/node/types.js +1 -0
  92. package/dist/node/utils/content-digest.js +107 -0
  93. package/dist/node/utils/create-node-id.js +51 -0
  94. package/dist/node/utils/env.js +24 -0
  95. package/dist/node/utils/fs-retry.js +74 -0
  96. package/dist/node/utils/memory-signals.js +410 -0
  97. package/dist/node/utils/memory.js +400 -0
  98. package/dist/node/utils/murmurhash.js +32 -0
  99. package/dist/node/utils/parser.js +33 -0
  100. package/dist/node/utils/partition-hash.js +19 -0
  101. package/dist/node/utils/paths.js +122 -0
  102. package/dist/node/utils/resolver.js +48 -0
  103. package/dist/node/utils/runtime.js +5 -0
  104. package/dist/node/utils/serializable.js +41 -0
  105. package/dist/node/utils/strings.js +41 -0
  106. package/dist/node/vite/config.js +289 -0
  107. package/dist/node/vite/paths.js +12 -0
  108. package/dist/node/vite/plugin-deps.js +232 -0
  109. package/dist/node/vite/plugin.js +281 -0
  110. package/dist/node/vite/react-compiler.js +362 -0
  111. package/dist/node/vite/source-files.js +83 -0
  112. package/dist/node/vite/static-image.js +136 -0
  113. package/dist/node/vite/styled-components.js +73 -0
  114. package/dist/node/vite/transforms.js +299 -0
  115. package/dist/node/wasm-data/host.js +406 -0
  116. package/dist/node/wasm-data/lower.js +406 -0
  117. package/dist/node/wasm-data/runner.js +79 -0
  118. package/dist/node/wasm-data/worker.js +95 -0
  119. package/dist/reporter.js +5 -0
  120. package/dist/runtime/api-runner.js +81 -0
  121. package/dist/runtime/app.js +256 -0
  122. package/dist/runtime/client-entry.js +4 -0
  123. package/dist/runtime/client.js +383 -0
  124. package/dist/runtime/default-html.js +31 -0
  125. package/dist/runtime/head-markup.js +100 -0
  126. package/dist/runtime/head-sync.js +101 -0
  127. package/dist/runtime/head-tags.js +45 -0
  128. package/dist/runtime/hydration-report.js +111 -0
  129. package/dist/runtime/index.js +12 -0
  130. package/dist/runtime/link.js +293 -0
  131. package/dist/runtime/loader.js +449 -0
  132. package/dist/runtime/not-found.js +61 -0
  133. package/dist/runtime/page-renderer.js +35 -0
  134. package/dist/runtime/preamble.js +47 -0
  135. package/dist/runtime/react-compat.js +14 -0
  136. package/dist/runtime/renderer-shared.js +10 -0
  137. package/dist/runtime/renderers/react-client.js +14 -0
  138. package/dist/runtime/renderers/react-server.js +99 -0
  139. package/dist/runtime/renderers/react.js +17 -0
  140. package/dist/runtime/renderers/types.js +1 -0
  141. package/dist/runtime/root-error-handlers.js +29 -0
  142. package/dist/runtime/router/history.js +132 -0
  143. package/dist/runtime/router/index.js +240 -0
  144. package/dist/runtime/router/utils.js +118 -0
  145. package/dist/runtime/script.js +110 -0
  146. package/dist/runtime/scroll.js +40 -0
  147. package/dist/runtime/slice.js +11 -0
  148. package/dist/runtime/ssr.js +240 -0
  149. package/dist/runtime/static-query.js +47 -0
  150. package/dist/utils.js +4 -0
  151. package/package.json +105 -3
  152. package/types/graphql-tada.d.ts +223 -0
  153. package/types/graphql.d.ts +2 -0
  154. package/types/index.d.ts +2287 -0
  155. package/types/node.d.ts +380 -0
  156. package/types/reporter.d.ts +187 -0
  157. package/types/router.d.ts +102 -0
  158. package/types/vendor/gql-tada.d.ts +2529 -0
@@ -0,0 +1,148 @@
1
+ import path from "node:path";
2
+ import fs from "node:fs";
3
+ import { EventEmitter } from "node:events";
4
+ import picomatch from "picomatch";
5
+ // src/node/compat/chokidar-shim.ts
6
+ const unix = (p) => p.replace(/\\/g, "/");
7
+ function createIgnoreMatcher(ignored, cwd = "") {
8
+ const list = ignored === void 0 ? [] : Array.isArray(ignored) ? ignored : [ignored];
9
+ const positive = [];
10
+ const negative = [];
11
+ const add = (pattern, target) => {
12
+ const glob = picomatch(pattern, { dot: true });
13
+ target.push((p) => p === pattern || glob(p));
14
+ };
15
+ for (const matcher of list) if (typeof matcher === "function") positive.push(matcher);
16
+ else if (matcher instanceof RegExp) positive.push((p) => matcher.test(p));
17
+ else if (typeof matcher === "string") {
18
+ const negated = matcher.startsWith("!");
19
+ const raw = negated ? matcher.slice(1) : matcher;
20
+ const pattern = unix(path.isAbsolute(raw) ? raw : path.join(cwd, raw));
21
+ if (negated) {
22
+ add(pattern, negative);
23
+ continue;
24
+ }
25
+ add(pattern, positive);
26
+ if (!picomatch.scan(pattern).isGlob) add(`${pattern}/**`, positive);
27
+ }
28
+ return (p, stats) => {
29
+ const normalized = unix(p);
30
+ if (negative.some((m) => m(normalized))) return false;
31
+ return positive.some((m) => m(normalized, stats));
32
+ };
33
+ }
34
+ var FSWatcher = class extends EventEmitter {
35
+ options;
36
+ closed = false;
37
+ scanning = Promise.resolve();
38
+ readyEmitted = false;
39
+ watched = /* @__PURE__ */ new Map();
40
+ isIgnored;
41
+ constructor(options = {}) {
42
+ super();
43
+ this.options = {
44
+ followSymlinks: true,
45
+ ...options
46
+ };
47
+ this.isIgnored = createIgnoreMatcher(options.ignored, options.cwd);
48
+ }
49
+ add(paths) {
50
+ const list = typeof paths === "string" ? [paths] : [...paths];
51
+ this.scanning = this.scanning.then(() => new Promise((resolve) => setImmediate(resolve))).then(async () => {
52
+ for (const p of list) await this.scanRoot(p);
53
+ if (!this.closed && !this.readyEmitted) {
54
+ this.readyEmitted = true;
55
+ this.emit("ready");
56
+ }
57
+ }).catch((error) => {
58
+ if (!this.closed) this.emit("error", error);
59
+ });
60
+ return this;
61
+ }
62
+ unwatch(_paths) {
63
+ return this;
64
+ }
65
+ async close() {
66
+ this.closed = true;
67
+ this.removeAllListeners();
68
+ }
69
+ getWatched() {
70
+ return Object.fromEntries([...this.watched].map(([dir, names]) => [dir, [...names].sort()]));
71
+ }
72
+ display(p) {
73
+ return this.options.cwd ? path.relative(this.options.cwd, p) : p;
74
+ }
75
+ emitEvent(event, p, stats) {
76
+ if (this.closed || this.options.ignoreInitial) return;
77
+ const shown = this.display(p);
78
+ if (this.options.alwaysStat && stats) {
79
+ this.emit(event, shown, stats);
80
+ this.emit("all", event, shown, stats);
81
+ } else {
82
+ this.emit(event, shown);
83
+ this.emit("all", event, shown);
84
+ }
85
+ }
86
+ async scanRoot(input) {
87
+ const target = path.normalize(this.options.cwd && !path.isAbsolute(input) ? path.join(this.options.cwd, input) : input);
88
+ let stats;
89
+ try {
90
+ stats = this.options.followSymlinks ? await fs.promises.stat(target) : await fs.promises.lstat(target);
91
+ } catch (e) {
92
+ if (e.code === "ENOENT") return;
93
+ throw e;
94
+ }
95
+ if (this.isIgnored(target, stats)) return;
96
+ const parent = path.dirname(target);
97
+ if (!this.watched.has(parent)) this.watched.set(parent, /* @__PURE__ */ new Set());
98
+ this.watched.get(parent).add(path.basename(target));
99
+ if (stats.isDirectory()) {
100
+ this.emitEvent("addDir", target, stats);
101
+ await this.scanDirectory(target, 0);
102
+ } else this.emitEvent("add", target, stats);
103
+ }
104
+ async scanDirectory(dir, depth) {
105
+ if (this.closed) return;
106
+ if (this.options.depth !== void 0 && depth > this.options.depth) return;
107
+ let entries;
108
+ try {
109
+ entries = await fs.promises.readdir(dir, { withFileTypes: true });
110
+ } catch (e) {
111
+ const code = e.code;
112
+ if (code === "ENOENT" || code === "EACCES" || code === "EPERM") return;
113
+ throw e;
114
+ }
115
+ const names = /* @__PURE__ */ new Set();
116
+ this.watched.set(dir, names);
117
+ for (const entry of entries) {
118
+ const full = path.join(dir, entry.name);
119
+ let isDirectory = entry.isDirectory();
120
+ let isFile = entry.isFile();
121
+ let stats;
122
+ if (entry.isSymbolicLink()) {
123
+ if (!this.options.followSymlinks) continue;
124
+ try {
125
+ stats = await fs.promises.stat(full);
126
+ } catch {
127
+ continue;
128
+ }
129
+ isDirectory = stats.isDirectory();
130
+ isFile = stats.isFile();
131
+ } else if (this.options.alwaysStat) stats = await fs.promises.stat(full);
132
+ if (this.isIgnored(full, stats)) continue;
133
+ names.add(entry.name);
134
+ if (isDirectory) {
135
+ this.emitEvent("addDir", full, stats);
136
+ await this.scanDirectory(full, depth + 1);
137
+ } else if (isFile) this.emitEvent("add", full, stats);
138
+ }
139
+ }
140
+ };
141
+ function watch(paths, options = {}) {
142
+ return new FSWatcher(options).add(paths);
143
+ }
144
+ var chokidar_shim_default = {
145
+ watch,
146
+ FSWatcher
147
+ };
148
+ export { FSWatcher, chokidar_shim_default as default, watch };
@@ -0,0 +1,145 @@
1
+ import { openBuiltinSqlite } from "../cache.js";
2
+ import path from "node:path";
3
+ import fs from "node:fs";
4
+ // src/node/compat/core-utils-lmdb.ts
5
+ var SqliteStore = class {
6
+ db;
7
+ getStmt;
8
+ putStmt;
9
+ removeStmt;
10
+ clearStmt;
11
+ inTransaction = false;
12
+ constructor(db) {
13
+ this.db = db;
14
+ db.exec("PRAGMA busy_timeout = 60000");
15
+ db.exec("PRAGMA synchronous = NORMAL");
16
+ db.exec("PRAGMA journal_mode = WAL");
17
+ db.exec("CREATE TABLE IF NOT EXISTS kv (db TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL, PRIMARY KEY (db, key)) WITHOUT ROWID");
18
+ this.getStmt = db.prepare("SELECT value FROM kv WHERE db = ? AND key = ?");
19
+ this.putStmt = db.prepare("INSERT OR REPLACE INTO kv (db, key, value) VALUES (?, ?, ?)");
20
+ this.removeStmt = db.prepare("DELETE FROM kv WHERE db = ? AND key = ?");
21
+ this.clearStmt = db.prepare("DELETE FROM kv WHERE db = ?");
22
+ }
23
+ get(name, key) {
24
+ const row = this.getStmt.get(name, key);
25
+ if (row == null) return void 0;
26
+ try {
27
+ return JSON.parse(row.value);
28
+ } catch {
29
+ return;
30
+ }
31
+ }
32
+ put(name, key, value) {
33
+ this.putStmt.run(name, key, JSON.stringify(value));
34
+ }
35
+ remove(name, key) {
36
+ const existed = this.getStmt.get(name, key) != null;
37
+ this.removeStmt.run(name, key);
38
+ return existed;
39
+ }
40
+ /** runs `callback` in a write transaction if the key does not exist (lmdb's conditional write) */
41
+ ifNoExists(name, key, callback) {
42
+ if (this.inTransaction) {
43
+ if (this.getStmt.get(name, key) != null) return false;
44
+ callback();
45
+ return true;
46
+ }
47
+ this.db.exec("BEGIN IMMEDIATE");
48
+ this.inTransaction = true;
49
+ try {
50
+ const missing = this.getStmt.get(name, key) == null;
51
+ if (missing) callback();
52
+ this.db.exec("COMMIT");
53
+ return missing;
54
+ } catch (e) {
55
+ try {
56
+ this.db.exec("ROLLBACK");
57
+ } catch {}
58
+ throw e;
59
+ } finally {
60
+ this.inTransaction = false;
61
+ }
62
+ }
63
+ clear(name) {
64
+ this.clearStmt.run(name);
65
+ }
66
+ close() {
67
+ this.db.close();
68
+ }
69
+ };
70
+ /** without SQLite: the same API on Maps, for this process only */
71
+ var MemoryStore = class {
72
+ dbs = /* @__PURE__ */ new Map();
73
+ table(name) {
74
+ let t = this.dbs.get(name);
75
+ if (!t) this.dbs.set(name, t = /* @__PURE__ */ new Map());
76
+ return t;
77
+ }
78
+ get(name, key) {
79
+ const value = this.table(name).get(key);
80
+ return value === void 0 ? void 0 : JSON.parse(value);
81
+ }
82
+ put(name, key, value) {
83
+ this.table(name).set(key, JSON.stringify(value));
84
+ }
85
+ remove(name, key) {
86
+ return this.table(name).delete(key);
87
+ }
88
+ ifNoExists(name, key, callback) {
89
+ if (this.table(name).has(key)) return false;
90
+ callback();
91
+ return true;
92
+ }
93
+ clear(name) {
94
+ this.table(name).clear();
95
+ }
96
+ close() {}
97
+ };
98
+ function open(options) {
99
+ const dbPath = typeof options === "string" ? options : options?.path;
100
+ let store = new MemoryStore();
101
+ if (dbPath) {
102
+ const file = `${dbPath}.sqlite`;
103
+ fs.mkdirSync(path.dirname(file), { recursive: true });
104
+ const db = openBuiltinSqlite(file);
105
+ if (db) store = new SqliteStore(db);
106
+ }
107
+ return {
108
+ openDB(dbOptions) {
109
+ const name = typeof dbOptions === "string" ? dbOptions : dbOptions.name;
110
+ return {
111
+ get: (key) => store.get(name, String(key)),
112
+ put: async (key, value) => (store.put(name, String(key), value), true),
113
+ remove: async (key) => store.remove(name, String(key)),
114
+ ifNoExists: async (key, callback) => store.ifNoExists(name, String(key), callback),
115
+ clearAsync: async () => store.clear(name)
116
+ };
117
+ },
118
+ async close() {
119
+ store.close();
120
+ }
121
+ };
122
+ }
123
+ function getLmdb() {
124
+ return { open };
125
+ }
126
+ /**
127
+ * Deletes the mutexes gatsby-core-utils left in its database. A process killed while it fetched a
128
+ * remote file (Ctrl+C during sourcing, a crash) never releases its mutex, and the next build would
129
+ * wait for it forever. Gatsby releases them all when a build starts (releaseAllMutexes in its
130
+ * initialize service); called the same way, in the main process before any plugin runs. The
131
+ * downloaded files' records ("remote-file") are kept.
132
+ */
133
+ function releaseCoreUtilsMutexes(cacheDir) {
134
+ const file = path.join(cacheDir, "data", "gatsby-core-utils.sqlite");
135
+ if (!fs.existsSync(file)) return;
136
+ const db = openBuiltinSqlite(file);
137
+ if (!db) return;
138
+ try {
139
+ db.exec("PRAGMA busy_timeout = 60000");
140
+ db.exec("DELETE FROM kv WHERE db = 'mutex'");
141
+ } catch {} finally {
142
+ db.close();
143
+ }
144
+ }
145
+ export { getLmdb, releaseCoreUtilsMutexes };
@@ -0,0 +1 @@
1
+ export { createNodeId } from "../utils/create-node-id.js";
@@ -0,0 +1,245 @@
1
+ import { isBun } from "../utils/runtime.js";
2
+ import { slash } from "../utils/paths.js";
3
+ import { parseSync } from "../utils/parser.js";
4
+ import * as nodeModule from "node:module";
5
+ import Module, { createRequire } from "node:module";
6
+ import path from "node:path";
7
+ import fs from "node:fs";
8
+ import { fileURLToPath, pathToFileURL } from "node:url";
9
+ // src/node/compat/module-hooks.ts
10
+ const here = path.dirname(fileURLToPath(import.meta.url));
11
+ const packageRoot = path.resolve(here, "../../..");
12
+ function distFile(rel) {
13
+ return path.join(packageRoot, "dist", rel);
14
+ }
15
+ const MAP = {
16
+ gatsby: "compat/gatsby-node-entry.js",
17
+ "gatsby/graphql": "graphql.js",
18
+ "gatsby/reporter": "reporter.js",
19
+ "gatsby/utils": "utils.js",
20
+ "gatsby/package.json": "compat/gatsby-package.json",
21
+ "gatsby/apis.json": "compat-apis.json",
22
+ "gatsby/dist/redux": "node/compat/redux-shim.js",
23
+ "gatsby/dist/redux/index": "node/compat/redux-shim.js",
24
+ "gatsby/dist/redux/index.js": "node/compat/redux-shim.js",
25
+ "gatsby/dist/schema/types/type-builders": "node/schema/type-builders.js",
26
+ "gatsby/dist/utils/create-node-id": "node/compat/create-node-id-shim.js"
27
+ };
28
+ /**
29
+ * Internal modules of Gatsby packages replaced by goatly (see compat/md5-file and
30
+ * compat/core-utils-lmdb)
31
+ */
32
+ function mapInternalSpecifier(specifier, parent) {
33
+ if (!parent || !/[\\/]gatsby-core-utils[\\/]/.test(parent)) return void 0;
34
+ if (specifier === "./md5-file" || specifier === "./md5-file.js" || specifier === "./md5-file.mjs") return distFile("compat/md5-file.js");
35
+ if ((specifier === "./get-lmdb" || specifier === "./get-lmdb.js" || specifier === "./get-lmdb.mjs") && /[\\/]utils[\\/][^\\/]+$/.test(parent)) return distFile("node/compat/core-utils-lmdb.js");
36
+ }
37
+ /** `goatly build`: plugins get a chokidar that does not watch (see compat/chokidar-shim) */
38
+ let replaceChokidar = false;
39
+ const KEEP_CHOKIDAR = /[\\/]node_modules[\\/](?:\.pnpm[\\/][^\\/]+[\\/]node_modules[\\/])?(?:vite|rolldown|@vitejs|@rolldown)[\\/]/;
40
+ function mapWatcherSpecifier(specifier, parent) {
41
+ if (!replaceChokidar || specifier !== "chokidar" || !parent) return void 0;
42
+ const parentPath = parent.startsWith("file:") ? fileURLToPath(parent) : parent;
43
+ if (KEEP_CHOKIDAR.test(parentPath) || parentPath.startsWith(packageRoot + path.sep)) return void 0;
44
+ return distFile("node/compat/chokidar-shim.js");
45
+ }
46
+ function mapGatsbySpecifier(specifier) {
47
+ if (specifier !== "gatsby" && !specifier.startsWith("gatsby/")) return void 0;
48
+ const target = MAP[specifier];
49
+ return target ? distFile(target) : void 0;
50
+ }
51
+ let registered = false;
52
+ /**
53
+ * Redirects `require("gatsby/...")` / `import "gatsby/..."` coming from Gatsby plugins
54
+ * to goatly equivalents, so unmodified npm plugins work even when the real
55
+ * `gatsby` package is not installed (and without an npm alias).
56
+ *
57
+ * - Node.js: `module.registerHooks` (sync resolve hook, covers import and require)
58
+ * - Bun: patches `Module._resolveFilename` (covers require, which all npm Gatsby
59
+ * plugins use). For ESM `import "gatsby/..."` under Bun, alias the package:
60
+ * `"gatsby": "npm:goatly"`.
61
+ */
62
+ function registerGatsbyModuleHooks(options = {}) {
63
+ if (options.build && process.env.GOATLY_BUILD_FILE_WATCHERS !== "true") replaceChokidar = true;
64
+ if (registered) return;
65
+ registered = true;
66
+ const registerHooks = nodeModule.registerHooks;
67
+ if (!isBun && typeof registerHooks === "function") {
68
+ registerHooks({ resolve(specifier, context, nextResolve) {
69
+ const mapped = mapGatsbySpecifier(specifier) ?? mapInternalSpecifier(specifier, context.parentURL) ?? mapWatcherSpecifier(specifier, context.parentURL);
70
+ if (mapped) return {
71
+ url: pathToFileURL(mapped).href,
72
+ shortCircuit: true,
73
+ format: mapped.endsWith(".json") ? "json" : "module"
74
+ };
75
+ return nextResolve(specifier, context);
76
+ } });
77
+ return;
78
+ }
79
+ const M = Module;
80
+ const original = M._resolveFilename;
81
+ if (typeof original !== "function") return;
82
+ M._resolveFilename = function resolveFilename(request, ...rest) {
83
+ const parent = rest[0];
84
+ const mapped = mapGatsbySpecifier(request) ?? mapInternalSpecifier(request, parent?.filename) ?? mapWatcherSpecifier(request, parent?.filename);
85
+ if (mapped) return mapped;
86
+ return original.call(this, request, ...rest);
87
+ };
88
+ }
89
+ /** The hooks of the current renderer (the runtime hooks below dispatch to them). */
90
+ let rendererHooks;
91
+ let dispatchersInstalled = false;
92
+ /** Bun: the current renderer's load filter as it is registered there */
93
+ let currentBunFilter;
94
+ const bunSpecifiers = /* @__PURE__ */ new Set();
95
+ const bunFilters = /* @__PURE__ */ new Set();
96
+ /**
97
+ * Sets the Node hooks of the site's renderer (gatsby-config `renderer`), on top of the ones above:
98
+ * `resolve` maps bare specifiers to files, `load` rewrites the source of matching files. The
99
+ * runtime hooks are installed once per thread and dispatch to the current renderer's hooks, so a
100
+ * later build in the same process with another renderer (or other options) gets its own.
101
+ *
102
+ * - Node.js: `module.registerHooks` (import and require), plus `Module._resolveFilename` for
103
+ * `require.resolve`, which does not go through the resolve hook.
104
+ * - Bun: `Module._resolveFilename` (require) and `Bun.plugin`. Bun has no hook for bare specifiers
105
+ * of ESM imports: each of the renderer's `specifiers` becomes a module that re-exports the file
106
+ * `resolve` maps it to (`build.module`, which takes precedence over node_modules for import and
107
+ * require). `onLoad` cannot pass a file on unchanged, and its contents are always ESM, so `load`
108
+ * only sees ES modules there: `.cjs`/`.cts` files are left out of its filter, and other CommonJS
109
+ * files it matches fail with an error that says so.
110
+ */
111
+ function setRendererNodeHooks(hooks) {
112
+ rendererHooks = hooks && {
113
+ resolve: hooks.resolve?.bind(hooks),
114
+ specifiers: hooks.specifiers,
115
+ load: hooks.load && {
116
+ filter: new RegExp(hooks.load.filter.source, hooks.load.filter.flags.replace(/[gy]/g, "")),
117
+ transform: hooks.load.transform.bind(hooks.load)
118
+ }
119
+ };
120
+ currentBunFilter = rendererHooks?.load && esmOnly(rendererHooks.load.filter);
121
+ if (!rendererHooks) return;
122
+ installRendererDispatchers();
123
+ if (isBun) registerBunRendererPlugin(rendererHooks);
124
+ }
125
+ function installRendererDispatchers() {
126
+ if (dispatchersInstalled) return;
127
+ dispatchersInstalled = true;
128
+ const registerHooks = nodeModule.registerHooks;
129
+ if (!isBun && typeof registerHooks === "function") registerHooks({
130
+ resolve(specifier, context, nextResolve) {
131
+ const mapped = rendererHooks?.resolve?.(specifier);
132
+ return mapped ? {
133
+ url: pathToFileURL(mapped).href,
134
+ shortCircuit: true
135
+ } : nextResolve(specifier, context);
136
+ },
137
+ load(url, context, nextLoad) {
138
+ const result = nextLoad(url, context);
139
+ const load = rendererHooks?.load;
140
+ if (!load || !url.startsWith("file:") || result.source == null) return result;
141
+ const file = fileURLToPath(url);
142
+ if (!load.filter.test(slash(file))) return result;
143
+ const source = result.source;
144
+ const code = typeof source === "string" ? source : Buffer.from(source).toString("utf8");
145
+ const transformed = load.transform(file, code);
146
+ return transformed === void 0 ? result : {
147
+ ...result,
148
+ source: transformed
149
+ };
150
+ }
151
+ });
152
+ const M = Module;
153
+ const original = M._resolveFilename;
154
+ if (typeof original !== "function") return;
155
+ M._resolveFilename = function resolveFilename(request, ...rest) {
156
+ return rendererHooks?.resolve?.(request) ?? original.call(this, request, ...rest);
157
+ };
158
+ }
159
+ /** Bun: registers what this renderer adds (Bun cannot remove plugins, so each part only once). */
160
+ function registerBunRendererPlugin(hooks) {
161
+ const Bun = globalThis.Bun;
162
+ if (!Bun?.plugin) return;
163
+ const specifiers = (hooks.specifiers ?? []).filter((s) => !bunSpecifiers.has(s));
164
+ const load = hooks.load;
165
+ const filterKey = load && `${load.filter.source}/${load.filter.flags}`;
166
+ const newFilter = load && !bunFilters.has(filterKey);
167
+ if (!specifiers.length && !newFilter) return;
168
+ for (const s of specifiers) bunSpecifiers.add(s);
169
+ if (newFilter) bunFilters.add(filterKey);
170
+ Bun.plugin({
171
+ name: "goatly:renderer",
172
+ setup(build) {
173
+ if (newFilter) build.onLoad({ filter: bunPathFilter(esmOnly(load.filter)) }, (args) => {
174
+ const code = fs.readFileSync(args.path, "utf8");
175
+ const current = rendererHooks?.load;
176
+ const transformed = current && currentBunFilter?.test(slash(args.path)) ? current.transform(args.path, code) ?? code : code;
177
+ if (looksLikeCommonJs(args.path, transformed)) throw new Error(`The renderer's nodeHooks.load filter matches ${args.path}, a CommonJS file. On Bun, load can only transform ES modules: narrow the filter.`);
178
+ return {
179
+ contents: transformed,
180
+ loader: bunLoader(args.path)
181
+ };
182
+ });
183
+ for (const specifier of specifiers) build.module(specifier, () => {
184
+ const file = rendererHooks?.resolve?.(specifier);
185
+ if (!file) throw new Error(`The renderer no longer maps "${specifier}"`);
186
+ const exports = packageRequire(file);
187
+ if (Object.prototype.toString.call(exports) === "[object Module]") return {
188
+ exports,
189
+ loader: "object"
190
+ };
191
+ const q = JSON.stringify(file);
192
+ return {
193
+ contents: `import m from ${q}\nexport default m\nexport * from ${q}\n`,
194
+ loader: "js"
195
+ };
196
+ });
197
+ }
198
+ });
199
+ }
200
+ /**
201
+ * The filter without `.cjs`/`.cts` files (and without the stateful `g`/`y` flags). The lookahead
202
+ * is anchored at the start; the filter itself still matches anywhere in the path (or at its own
203
+ * `^`, which only matches at the start).
204
+ */
205
+ function esmOnly(filter) {
206
+ return new RegExp(`^(?!.*\\.c[jt]s$)[\\s\\S]*?(?:${filter.source})`, filter.flags.replace(/[gy]/g, ""));
207
+ }
208
+ /**
209
+ * Bun: the filter it registers `onLoad` with. Bun matches it against the native path, so on
210
+ * Windows every `/` the filter matches also matches `\\` (a superset: the callback tests the
211
+ * original filter against the slashed path, as on Node). Elsewhere the filter itself.
212
+ */
213
+ function bunPathFilter(filter, platform = process.platform) {
214
+ if (platform !== "win32") return filter;
215
+ const src = filter.source;
216
+ let out = "";
217
+ let inClass = false;
218
+ for (let i = 0; i < src.length; i++) {
219
+ const c = src[i];
220
+ if (c === "\\") {
221
+ const pair = src.slice(i, i + 2);
222
+ i++;
223
+ if (pair === "\\/") out += inClass ? "\\/\\\\" : "[\\/\\\\]";
224
+ else out += pair;
225
+ } else if (c === "/") out += inClass ? "/\\\\" : "[\\/\\\\]";
226
+ else {
227
+ if (c === "[" && !inClass) inClass = true;
228
+ else if (c === "]" && inClass) inClass = false;
229
+ out += c;
230
+ }
231
+ }
232
+ return new RegExp(out, filter.flags);
233
+ }
234
+ function bunLoader(file) {
235
+ const ext = /\.([cm]?[jt]sx?)$/.exec(file)?.[1] ?? "js";
236
+ return ext.endsWith("tsx") ? "tsx" : ext.endsWith("ts") ? "ts" : ext.endsWith("jsx") ? "jsx" : "js";
237
+ }
238
+ /** No import/export statements, but `module.exports`, `exports.x =` or `require(…)`. */
239
+ function looksLikeCommonJs(file, code) {
240
+ if (/\.m[jt]s$/.test(file)) return false;
241
+ if (parseSync(file, code, { lang: bunLoader(file) }).module.hasModuleSyntax) return false;
242
+ return /\bmodule\.exports\b|\bexports\.[\w$]+\s*=|\brequire\s*\(/.test(code);
243
+ }
244
+ const packageRequire = createRequire(import.meta.url);
245
+ export { bunPathFilter, esmOnly, mapGatsbySpecifier, mapInternalSpecifier, mapWatcherSpecifier, packageRequire, packageRoot, registerGatsbyModuleHooks, setRendererNodeHooks };
@@ -0,0 +1 @@
1
+ export { emitter, store } from "../redux/index.js";