goatly 0.1.0 → 0.1.2
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.
- package/README.md +874 -1
- package/bin/goatly.js +99 -0
- package/dist/cli/index.js +234 -0
- package/dist/compat/gatsby-node-entry.js +16 -0
- package/dist/compat/gatsby-package.json +6 -0
- package/dist/compat/md5-file.js +47 -0
- package/dist/compat-apis.json +72 -0
- package/dist/compat-apis.json.js +62 -0
- package/dist/graphql.js +3 -0
- package/dist/node/actions/index.js +619 -0
- package/dist/node/actions/job-store.js +180 -0
- package/dist/node/actions/jobs.js +174 -0
- package/dist/node/api-runner.js +258 -0
- package/dist/node/bootstrap.js +569 -0
- package/dist/node/build/assets.js +110 -0
- package/dist/node/build/incremental.js +208 -0
- package/dist/node/build/index.js +763 -0
- package/dist/node/build/memory-governor.js +450 -0
- package/dist/node/build/output-dirs.js +70 -0
- package/dist/node/build/page-data.js +40 -0
- package/dist/node/build/page-files.js +186 -0
- package/dist/node/build/partitioned-state.js +258 -0
- package/dist/node/build/query-worker.js +626 -0
- package/dist/node/build/query-workers.js +863 -0
- package/dist/node/build/render-thread.js +103 -0
- package/dist/node/build/render-worker.js +162 -0
- package/dist/node/build/state.js +24 -0
- package/dist/node/build/timing-breakdown.js +216 -0
- package/dist/node/build/work-clock.js +75 -0
- package/dist/node/build/worker-pool.js +242 -0
- package/dist/node/cache-layout.js +99 -0
- package/dist/node/cache.js +1430 -0
- package/dist/node/compat/chokidar-shim.js +148 -0
- package/dist/node/compat/core-utils-lmdb.js +145 -0
- package/dist/node/compat/create-node-id-shim.js +1 -0
- package/dist/node/compat/module-hooks.js +245 -0
- package/dist/node/compat/redux-shim.js +1 -0
- package/dist/node/config/index.js +162 -0
- package/dist/node/config/not-found-pages.js +95 -0
- package/dist/node/datastore/codec.js +144 -0
- package/dist/node/datastore/index.js +127 -0
- package/dist/node/datastore/logfile/cache.js +89 -0
- package/dist/node/datastore/logfile/index.js +1329 -0
- package/dist/node/datastore/logfile/record.js +71 -0
- package/dist/node/datastore/types.js +1 -0
- package/dist/node/develop/express-compat.js +207 -0
- package/dist/node/develop/graphiql.js +22 -0
- package/dist/node/develop/hydration-report.js +41 -0
- package/dist/node/develop/index.js +618 -0
- package/dist/node/develop/local-url.js +10 -0
- package/dist/node/develop/ssr-runner.js +66 -0
- package/dist/node/gql.js +5 -0
- package/dist/node/index.js +15 -0
- package/dist/node/info.js +270 -0
- package/dist/node/internal-plugins/data-bridge.js +196 -0
- package/dist/node/internal-plugins/page-creator.js +318 -0
- package/dist/node/loader/import-module.js +160 -0
- package/dist/node/node-helpers.js +66 -0
- package/dist/node/not-found-pages.js +220 -0
- package/dist/node/plugins/api-list.js +12 -0
- package/dist/node/plugins/load-plugins.js +427 -0
- package/dist/node/plugins/resolve-plugin.js +79 -0
- package/dist/node/plugins/static-exports.js +64 -0
- package/dist/node/query/compile.js +326 -0
- package/dist/node/query/dependencies.js +45 -0
- package/dist/node/query/execute.js +417 -0
- package/dist/node/query/extract.js +372 -0
- package/dist/node/query/gql-tada-introspection.js +124 -0
- package/dist/node/query/graphql-env.js +231 -0
- package/dist/node/query/graphql-runner.js +79 -0
- package/dist/node/redux/actions.js +1 -0
- package/dist/node/redux/index.js +133 -0
- package/dist/node/redux/node-changes.js +128 -0
- package/dist/node/redux/reducers.js +193 -0
- package/dist/node/redux/state.js +71 -0
- package/dist/node/renderer.js +124 -0
- package/dist/node/reporter-transports.js +259 -0
- package/dist/node/reporter.js +497 -0
- package/dist/node/schema/date.js +539 -0
- package/dist/node/schema/extensions.js +315 -0
- package/dist/node/schema/index.js +542 -0
- package/dist/node/schema/infer.js +388 -0
- package/dist/node/schema/inline-objects.js +128 -0
- package/dist/node/schema/naming.js +20 -0
- package/dist/node/schema/node-model.js +207 -0
- package/dist/node/schema/query-engine.js +1808 -0
- package/dist/node/schema/resolver-kinds.js +7 -0
- package/dist/node/schema/resolvers.js +273 -0
- package/dist/node/schema/type-builders.js +45 -0
- package/dist/node/schema/types.js +241 -0
- package/dist/node/serve.js +90 -0
- package/dist/node/site-page-nodes.js +277 -0
- package/dist/node/types.js +1 -0
- package/dist/node/utils/content-digest.js +107 -0
- package/dist/node/utils/cpus.js +104 -0
- package/dist/node/utils/create-node-id.js +51 -0
- package/dist/node/utils/env.js +25 -0
- package/dist/node/utils/fs-retry.js +74 -0
- package/dist/node/utils/memory-signals.js +441 -0
- package/dist/node/utils/memory.js +657 -0
- package/dist/node/utils/murmurhash.js +32 -0
- package/dist/node/utils/parser.js +33 -0
- package/dist/node/utils/partition-hash.js +19 -0
- package/dist/node/utils/paths.js +122 -0
- package/dist/node/utils/resolver.js +48 -0
- package/dist/node/utils/runtime.js +5 -0
- package/dist/node/utils/serializable.js +41 -0
- package/dist/node/utils/strings.js +41 -0
- package/dist/node/vite/config.js +289 -0
- package/dist/node/vite/paths.js +12 -0
- package/dist/node/vite/plugin-deps.js +232 -0
- package/dist/node/vite/plugin.js +283 -0
- package/dist/node/vite/react-compiler.js +362 -0
- package/dist/node/vite/source-files.js +83 -0
- package/dist/node/vite/static-image.js +136 -0
- package/dist/node/vite/styled-components.js +73 -0
- package/dist/node/vite/transforms.js +330 -0
- package/dist/node/wasm-data/host.js +406 -0
- package/dist/node/wasm-data/lower.js +406 -0
- package/dist/node/wasm-data/runner.js +79 -0
- package/dist/node/wasm-data/worker.js +95 -0
- package/dist/reporter.js +5 -0
- package/dist/runtime/api-runner.js +81 -0
- package/dist/runtime/app.js +256 -0
- package/dist/runtime/client-entry.js +4 -0
- package/dist/runtime/client.js +383 -0
- package/dist/runtime/default-html.js +31 -0
- package/dist/runtime/head-markup.js +100 -0
- package/dist/runtime/head-sync.js +101 -0
- package/dist/runtime/head-tags.js +45 -0
- package/dist/runtime/hydration-report.js +111 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/link.js +293 -0
- package/dist/runtime/loader.js +449 -0
- package/dist/runtime/not-found.js +61 -0
- package/dist/runtime/page-renderer.js +35 -0
- package/dist/runtime/preamble.js +47 -0
- package/dist/runtime/react-compat.js +14 -0
- package/dist/runtime/renderer-shared.js +10 -0
- package/dist/runtime/renderers/react-client.js +14 -0
- package/dist/runtime/renderers/react-server.js +99 -0
- package/dist/runtime/renderers/react.js +17 -0
- package/dist/runtime/renderers/types.js +1 -0
- package/dist/runtime/root-error-handlers.js +29 -0
- package/dist/runtime/router/history.js +132 -0
- package/dist/runtime/router/index.js +240 -0
- package/dist/runtime/router/utils.js +118 -0
- package/dist/runtime/script.js +110 -0
- package/dist/runtime/scroll.js +40 -0
- package/dist/runtime/slice.js +11 -0
- package/dist/runtime/ssr.js +240 -0
- package/dist/runtime/static-query.js +47 -0
- package/dist/utils.js +4 -0
- package/package.json +105 -3
- package/types/graphql-tada.d.ts +223 -0
- package/types/graphql.d.ts +2 -0
- package/types/index.d.ts +2287 -0
- package/types/node.d.ts +389 -0
- package/types/reporter.d.ts +187 -0
- package/types/router.d.ts +102 -0
- package/types/vendor/gql-tada.d.ts +2529 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { isCommonJsSource } from "../plugins/static-exports.js";
|
|
2
|
+
import { parseSync, walk } from "../utils/parser.js";
|
|
3
|
+
import { slash } from "../utils/paths.js";
|
|
4
|
+
import { builtinModules } from "node:module";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
// src/node/vite/plugin-deps.ts
|
|
8
|
+
/**
|
|
9
|
+
* The CommonJS packages that the browser entries of plugins installed from npm import, as entries
|
|
10
|
+
* for Vite's `optimizeDeps.include` (`"gatsby-plugin-image > prop-types"`).
|
|
11
|
+
*
|
|
12
|
+
* The dev server doesn't pre-bundle plugin packages (gatsby-browser files import their modules by
|
|
13
|
+
* path, and a pre-bundled copy would duplicate them, e.g. React contexts), and Vite doesn't
|
|
14
|
+
* discover what files in node_modules import. So the dependencies of what the site imports from a
|
|
15
|
+
* plugin package (e.g. `GatsbyImage` from gatsby-plugin-image) would be served unbundled, and
|
|
16
|
+
* CommonJS ones converted by goatly. Pre-bundled, Rolldown converts them, and Vite gives
|
|
17
|
+
* the plugins' imports of them its own CommonJS interop, as for the site's dependencies.
|
|
18
|
+
*
|
|
19
|
+
* Only a package's browser entry counts (its `browser` field, a `browser` condition in `exports`,
|
|
20
|
+
* or its `module` field), with what it imports from the package by relative path: not `main`,
|
|
21
|
+
* which is Node code in source, transformer and remark plugins. gatsby-browser files are left out:
|
|
22
|
+
* they are entries of Vite's dependency scan already. And only plugins that resolve from the site
|
|
23
|
+
* (`root`) to the same directory: Vite resolves the first part of a nested entry from there (a
|
|
24
|
+
* theme's plugins under pnpm don't).
|
|
25
|
+
*/
|
|
26
|
+
function pluginCommonJsDeps(plugins, root) {
|
|
27
|
+
const include = [];
|
|
28
|
+
const skip = /* @__PURE__ */ new Set([
|
|
29
|
+
"gatsby",
|
|
30
|
+
"goatly",
|
|
31
|
+
...plugins.map((p) => p.name)
|
|
32
|
+
]);
|
|
33
|
+
for (const { name, dir } of plugins) {
|
|
34
|
+
const pkg = readJson(path.join(dir, "package.json"));
|
|
35
|
+
if (!pkg || !resolvesFromRoot(root, name, dir)) continue;
|
|
36
|
+
const entry = browserEntry(dir, pkg);
|
|
37
|
+
if (!entry) continue;
|
|
38
|
+
for (const spec of bareImports(dir, pkg, [entry])) if (!skip.has(packageName(spec)) && isCommonJsPackage(dir, spec)) include.push(`${name} > ${spec}`);
|
|
39
|
+
}
|
|
40
|
+
return include;
|
|
41
|
+
}
|
|
42
|
+
/** Whether the package `name` resolves from `root` (as Vite resolves it) to `dir`. */
|
|
43
|
+
function resolvesFromRoot(root, name, dir) {
|
|
44
|
+
for (let d = root;; d = path.dirname(d)) {
|
|
45
|
+
const candidate = path.join(d, "node_modules", name);
|
|
46
|
+
if (fs.existsSync(path.join(candidate, "package.json"))) try {
|
|
47
|
+
return slash(fs.realpathSync(candidate)) === slash(fs.realpathSync(dir));
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
if (path.dirname(d) === d) return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The package's entry for the browser, when it has one apart from `main`. */
|
|
55
|
+
function browserEntry(dir, pkg) {
|
|
56
|
+
const exp = pkg.exports;
|
|
57
|
+
if (!(pkg.browser !== void 0 || typeof pkg.module === "string" || exp !== void 0 && JSON.stringify(exp).includes("\"browser\""))) return void 0;
|
|
58
|
+
const entry = packageEntry(dir, pkg);
|
|
59
|
+
return entry && isFile(entry) ? entry : void 0;
|
|
60
|
+
}
|
|
61
|
+
const JS_RE = /\.(m?[jt]sx?|cjs|cts)$/;
|
|
62
|
+
const BUILTINS = new Set(builtinModules);
|
|
63
|
+
/** The bare specifiers that `entries` and the package files they import import. */
|
|
64
|
+
function bareImports(dir, pkg, entries) {
|
|
65
|
+
const bare = /* @__PURE__ */ new Set();
|
|
66
|
+
const seen = /* @__PURE__ */ new Set();
|
|
67
|
+
const queue = [...entries];
|
|
68
|
+
while (queue.length) {
|
|
69
|
+
const file = queue.pop();
|
|
70
|
+
if (seen.has(file)) continue;
|
|
71
|
+
seen.add(file);
|
|
72
|
+
let code;
|
|
73
|
+
try {
|
|
74
|
+
code = fs.readFileSync(file, "utf8");
|
|
75
|
+
} catch {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
for (const spec of importSpecifiers(file, code)) if (spec.startsWith(".") || spec.startsWith("/")) {
|
|
79
|
+
const target = resolveRelative(dir, pkg, file, spec);
|
|
80
|
+
if (target && JS_RE.test(target)) queue.push(target);
|
|
81
|
+
} else if (!spec.startsWith("node:") && !BUILTINS.has(spec) && !spec.includes(":")) bare.add(spec);
|
|
82
|
+
}
|
|
83
|
+
return bare;
|
|
84
|
+
}
|
|
85
|
+
/** The specifiers of a module's static and dynamic imports, re-exports and `require()` calls. */
|
|
86
|
+
function importSpecifiers(file, code) {
|
|
87
|
+
let program;
|
|
88
|
+
try {
|
|
89
|
+
const lang = /\.[mc]?tsx?$/.test(file) ? file.endsWith("x") ? "tsx" : "ts" : "jsx";
|
|
90
|
+
program = parseSync(file, code, {
|
|
91
|
+
lang,
|
|
92
|
+
sourceType: "unambiguous"
|
|
93
|
+
}).program;
|
|
94
|
+
} catch {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
const specs = [];
|
|
98
|
+
const literal = (n) => n?.type === "Literal" && typeof n.value === "string" ? n.value : void 0;
|
|
99
|
+
walk(program, {
|
|
100
|
+
ImportDeclaration(n) {
|
|
101
|
+
if (n.importKind !== "type") specs.push(n.source.value);
|
|
102
|
+
},
|
|
103
|
+
ExportNamedDeclaration(n) {
|
|
104
|
+
if (n.source && n.exportKind !== "type") specs.push(n.source.value);
|
|
105
|
+
},
|
|
106
|
+
ExportAllDeclaration(n) {
|
|
107
|
+
if (n.exportKind !== "type") specs.push(n.source.value);
|
|
108
|
+
},
|
|
109
|
+
ImportExpression(n) {
|
|
110
|
+
const spec = literal(n.source);
|
|
111
|
+
if (spec) specs.push(spec);
|
|
112
|
+
},
|
|
113
|
+
CallExpression(n) {
|
|
114
|
+
const spec = n.callee?.type === "Identifier" && n.callee.name === "require" && literal(n.arguments[0]);
|
|
115
|
+
if (spec) specs.push(spec);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
return specs;
|
|
119
|
+
}
|
|
120
|
+
const EXTENSIONS = [
|
|
121
|
+
"",
|
|
122
|
+
".js",
|
|
123
|
+
".mjs",
|
|
124
|
+
".cjs",
|
|
125
|
+
".jsx",
|
|
126
|
+
".ts",
|
|
127
|
+
".tsx",
|
|
128
|
+
"/index.js",
|
|
129
|
+
"/index.mjs"
|
|
130
|
+
];
|
|
131
|
+
/** A relative import inside the package, with its `browser` field's replacements. */
|
|
132
|
+
function resolveRelative(dir, pkg, from, spec) {
|
|
133
|
+
const base = path.resolve(path.dirname(from), spec);
|
|
134
|
+
for (const ext of EXTENSIONS) {
|
|
135
|
+
const file = browserReplacement(dir, pkg, base + ext);
|
|
136
|
+
if (file && isFile(file)) return file;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function browserReplacement(dir, pkg, file) {
|
|
140
|
+
const map = pkg.browser;
|
|
141
|
+
if (!map || typeof map !== "object") return file;
|
|
142
|
+
const key = `./${slash(path.relative(dir, file))}`;
|
|
143
|
+
if (!(key in map)) return file;
|
|
144
|
+
const to = map[key];
|
|
145
|
+
return typeof to === "string" ? path.resolve(dir, to) : void 0;
|
|
146
|
+
}
|
|
147
|
+
/** The conditions Vite resolves `exports` with for the browser in develop. */
|
|
148
|
+
const CONDITIONS = [
|
|
149
|
+
"browser",
|
|
150
|
+
"development",
|
|
151
|
+
"module",
|
|
152
|
+
"import",
|
|
153
|
+
"default"
|
|
154
|
+
];
|
|
155
|
+
function pickExport(value) {
|
|
156
|
+
if (typeof value === "string") return value;
|
|
157
|
+
if (Array.isArray(value)) {
|
|
158
|
+
for (const v of value) {
|
|
159
|
+
const picked = pickExport(v);
|
|
160
|
+
if (picked) return picked;
|
|
161
|
+
}
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (value && typeof value === "object") {
|
|
165
|
+
for (const [key, v] of Object.entries(value)) if (CONDITIONS.includes(key)) {
|
|
166
|
+
const picked = pickExport(v);
|
|
167
|
+
if (picked) return picked;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/** The file Vite loads for `subpath` ("." for the package) of the package in `dir`, in the browser. */
|
|
172
|
+
function packageEntry(dir, pkg, subpath = ".") {
|
|
173
|
+
if (pkg.exports !== void 0) {
|
|
174
|
+
const exp = pkg.exports;
|
|
175
|
+
const hasSubpaths = exp && typeof exp === "object" && !Array.isArray(exp) && Object.keys(exp)[0]?.startsWith(".");
|
|
176
|
+
const target = pickExport(hasSubpaths ? exp[subpath] : subpath === "." ? exp : void 0);
|
|
177
|
+
if (target) return path.resolve(dir, target);
|
|
178
|
+
if (hasSubpaths) return void 0;
|
|
179
|
+
}
|
|
180
|
+
if (subpath !== ".") {
|
|
181
|
+
const base = path.resolve(dir, subpath);
|
|
182
|
+
for (const ext of EXTENSIONS) if (isFile(base + ext)) return base + ext;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const field = [
|
|
186
|
+
typeof pkg.browser === "string" ? pkg.browser : void 0,
|
|
187
|
+
pkg.module,
|
|
188
|
+
pkg.main
|
|
189
|
+
].find((f) => typeof f === "string");
|
|
190
|
+
const base = path.resolve(dir, field ?? "index.js");
|
|
191
|
+
for (const ext of EXTENSIONS) {
|
|
192
|
+
const file = browserReplacement(dir, pkg, base + ext);
|
|
193
|
+
if (file && isFile(file)) return file;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function packageName(spec) {
|
|
197
|
+
const parts = spec.split("/");
|
|
198
|
+
return spec.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
199
|
+
}
|
|
200
|
+
/** Whether `spec` resolves (from the plugin in `fromDir`) to a CommonJS module in the browser. */
|
|
201
|
+
function isCommonJsPackage(fromDir, spec) {
|
|
202
|
+
const name = packageName(spec);
|
|
203
|
+
for (let d = fromDir;; d = path.dirname(d)) {
|
|
204
|
+
const dir = path.join(d, "node_modules", name);
|
|
205
|
+
const pkg = readJson(path.join(dir, "package.json"));
|
|
206
|
+
if (pkg) {
|
|
207
|
+
const entry = packageEntry(dir, pkg, spec === name ? "." : `./${spec.slice(name.length + 1)}`);
|
|
208
|
+
if (!entry || !JS_RE.test(entry)) return false;
|
|
209
|
+
try {
|
|
210
|
+
return isCommonJsSource(fs.readFileSync(entry, "utf8"));
|
|
211
|
+
} catch {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (path.dirname(d) === d) return false;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function readJson(file) {
|
|
219
|
+
try {
|
|
220
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
221
|
+
} catch {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function isFile(file) {
|
|
226
|
+
try {
|
|
227
|
+
return fs.statSync(file).isFile();
|
|
228
|
+
} catch {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
export { pluginCommonJsDeps };
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { store } from "../redux/index.js";
|
|
2
|
+
import { resolveApiFile } from "../loader/import-module.js";
|
|
3
|
+
import { getStaticExports, isCommonJsSource } from "../plugins/static-exports.js";
|
|
4
|
+
import { pick, rankRoutes } from "../../runtime/router/utils.js";
|
|
5
|
+
import { slash } from "../utils/paths.js";
|
|
6
|
+
import { transformCjsImportsInEsm, transformCommonJsApiFile, transformGraphQLTags, transformHtmlBodyJsx, transformRequireInEsm } from "./transforms.js";
|
|
7
|
+
import { runtimeDir } from "./paths.js";
|
|
8
|
+
import { getRenderer } from "../renderer.js";
|
|
9
|
+
import { isSourceFile, nodeModulesPluginDirs } from "./source-files.js";
|
|
10
|
+
import { notFoundPagePaths, runtimeNotFoundPages } from "../not-found-pages.js";
|
|
11
|
+
import { getDataStore } from "../datastore/index.js";
|
|
12
|
+
import { warnIfPageQueryIsIgnored } from "../query/compile.js";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import fs from "node:fs";
|
|
15
|
+
import { transformWithOxc } from "vite";
|
|
16
|
+
// src/node/vite/plugin.ts
|
|
17
|
+
const VIRTUAL = {
|
|
18
|
+
asyncRequires: "virtual:goatly/async-requires",
|
|
19
|
+
browserPlugins: "virtual:goatly/browser-plugins",
|
|
20
|
+
ssrPlugins: "virtual:goatly/ssr-plugins",
|
|
21
|
+
html: "virtual:goatly/html",
|
|
22
|
+
matchPaths: "virtual:goatly/match-paths",
|
|
23
|
+
notFoundPages: "virtual:goatly/not-found-pages",
|
|
24
|
+
redirects: "virtual:goatly/redirects",
|
|
25
|
+
runtimeConfig: "virtual:goatly/runtime-config",
|
|
26
|
+
staticQueryPrefix: "virtual:goatly/static-query/",
|
|
27
|
+
renderer: "virtual:goatly/renderer",
|
|
28
|
+
rendererServer: "virtual:goatly/renderer-server",
|
|
29
|
+
rendererClient: "virtual:goatly/renderer-client"
|
|
30
|
+
};
|
|
31
|
+
function serializeOptions(options) {
|
|
32
|
+
return JSON.stringify(options ?? {}, (key, value) => {
|
|
33
|
+
if (typeof value === "function") return void 0;
|
|
34
|
+
if (value instanceof RegExp) return value.toString();
|
|
35
|
+
if (value && typeof value === "object" && "module" in value && "resolve" in value && "pluginOptions" in value) {
|
|
36
|
+
const { module: _m, gatsbyNodeModule: _g, ...rest } = value;
|
|
37
|
+
return rest;
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function getPageComponents() {
|
|
43
|
+
const map = /* @__PURE__ */ new Map();
|
|
44
|
+
for (const component of store.getState().components.values()) map.set(component.componentChunkName, component.componentPath);
|
|
45
|
+
return map;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Ranked match paths, plus static pages that would otherwise be shadowed by a matchPath. (Not those
|
|
49
|
+
* of sections' 404 pages: unknown URLs of a section get its 404 page anyway, with status 404.)
|
|
50
|
+
*/
|
|
51
|
+
function computeMatchPaths() {
|
|
52
|
+
const pages = [...store.getState().pages.values()];
|
|
53
|
+
const notFound = notFoundPagePaths();
|
|
54
|
+
const withMatch = pages.filter((p) => p.matchPath && !notFound.has(p.path));
|
|
55
|
+
if (withMatch.length === 0) return [];
|
|
56
|
+
const routes = withMatch.map((p) => ({
|
|
57
|
+
path: p.matchPath,
|
|
58
|
+
page: p.path
|
|
59
|
+
}));
|
|
60
|
+
const shadowed = pages.filter((p) => !p.matchPath && pick(routes, p.path)).map((p) => ({
|
|
61
|
+
path: p.path.replace(/\/$/, "") || "/",
|
|
62
|
+
page: p.path
|
|
63
|
+
}));
|
|
64
|
+
return rankRoutes([...shadowed, ...routes]).map((r) => ({
|
|
65
|
+
path: r.page,
|
|
66
|
+
matchPath: r.path
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
function pluginEntries(kind) {
|
|
70
|
+
return store.getState().flattenedPlugins.map((p) => ({
|
|
71
|
+
plugin: p,
|
|
72
|
+
file: kind === "browser" ? p.gatsbyBrowserPath : p.gatsbySSRPath
|
|
73
|
+
})).filter((e) => !!e.file);
|
|
74
|
+
}
|
|
75
|
+
/** (roughly) whether a .js file contains JSX */
|
|
76
|
+
const JSX_RE = /<\/?[A-Za-z]|<>/;
|
|
77
|
+
/**
|
|
78
|
+
* For Vite's dependency scanner (`optimizeDeps.rolldownOptions.plugins`): compiles JSX in the `.js`
|
|
79
|
+
* files that the goatly plugin compiles too (site sources, themes, npm plugins). Otherwise the
|
|
80
|
+
* scan fails on them, Vite skips pre-bundling and reloads the page once it found the dependencies.
|
|
81
|
+
*/
|
|
82
|
+
function scanJsxInJsPlugin(jsx) {
|
|
83
|
+
const pluginDirs = nodeModulesPluginDirs();
|
|
84
|
+
return {
|
|
85
|
+
name: "goatly:scan-jsx-in-js",
|
|
86
|
+
transform: {
|
|
87
|
+
filter: { id: /\.(m|c)?js$/ },
|
|
88
|
+
async handler(code, id) {
|
|
89
|
+
const file = slash(id);
|
|
90
|
+
if (!isSourceFile(file, pluginDirs) || !JSX_RE.test(code)) return null;
|
|
91
|
+
try {
|
|
92
|
+
return { code: (await transformWithOxc(code, file, {
|
|
93
|
+
lang: "jsx",
|
|
94
|
+
jsx: {
|
|
95
|
+
runtime: jsx.jsxRuntime === "classic" ? "classic" : "automatic",
|
|
96
|
+
importSource: jsx.jsxImportSource
|
|
97
|
+
}
|
|
98
|
+
})).code };
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** per resolved file: what the dev server converts it to, when it is a CommonJS module */
|
|
107
|
+
const cjsExportsCache = /* @__PURE__ */ new Map();
|
|
108
|
+
/** `exports.__esModule = true` / `Object.defineProperty(exports, "__esModule", …)` (Babel, tsc) */
|
|
109
|
+
const ES_MODULE_FLAG_RE = /\b(?:module\.)?exports\.__esModule\s*=|\bObject\.defineProperty\(\s*(?:module\.)?exports\s*,\s*["']__esModule["']/;
|
|
110
|
+
/**
|
|
111
|
+
* The CommonJS module that `spec` (imported by `importer`) resolves to, when the dev server serves
|
|
112
|
+
* it converted to ESM: its static export names and whether it was compiled from ESM. `null` for ES
|
|
113
|
+
* modules, pre-bundled dependencies (Vite's own interop applies to them), virtual modules and
|
|
114
|
+
* specifiers that don't resolve.
|
|
115
|
+
*/
|
|
116
|
+
async function cjsExportsOf(plugin, spec, importer) {
|
|
117
|
+
const resolved = await plugin.resolve(spec, importer, { skipSelf: true });
|
|
118
|
+
if (!resolved || resolved.external || resolved.id.startsWith("\0")) return null;
|
|
119
|
+
const file = resolved.id.split("?")[0];
|
|
120
|
+
if (!/\.(c?js)$/.test(file) || file.includes("/.vite/") || file.includes("/.cache/vite/")) return null;
|
|
121
|
+
let exports = cjsExportsCache.get(file);
|
|
122
|
+
if (exports === void 0) {
|
|
123
|
+
let code;
|
|
124
|
+
try {
|
|
125
|
+
code = fs.readFileSync(file, "utf8");
|
|
126
|
+
} catch {}
|
|
127
|
+
exports = code !== void 0 && isCommonJsSource(code) ? {
|
|
128
|
+
names: getStaticExports(file, code).filter((n) => /^[A-Za-z_$][\w$]*$/.test(n) && n !== "default" && n !== "__esModule"),
|
|
129
|
+
esModule: ES_MODULE_FLAG_RE.test(code)
|
|
130
|
+
} : null;
|
|
131
|
+
cjsExportsCache.set(file, exports);
|
|
132
|
+
}
|
|
133
|
+
return exports;
|
|
134
|
+
}
|
|
135
|
+
function goatlyVitePlugin(ctx) {
|
|
136
|
+
const program = () => store.getState().program;
|
|
137
|
+
const headTagsModule = slash(path.join(runtimeDir, "head-tags.js"));
|
|
138
|
+
const pluginDirs = nodeModulesPluginDirs();
|
|
139
|
+
const isUserFile = (file) => isSourceFile(file, pluginDirs);
|
|
140
|
+
const apiFilePattern = /\/gatsby-(browser|ssr)\.(js|jsx|cjs|mjs)$/;
|
|
141
|
+
let isServe = false;
|
|
142
|
+
let defines = {};
|
|
143
|
+
return {
|
|
144
|
+
name: "goatly",
|
|
145
|
+
enforce: "pre",
|
|
146
|
+
api: {
|
|
147
|
+
apiVersion: 1,
|
|
148
|
+
getDataStore
|
|
149
|
+
},
|
|
150
|
+
configResolved(config) {
|
|
151
|
+
isServe = config.command === "serve";
|
|
152
|
+
defines = Object.fromEntries(Object.entries(config.define ?? {}).filter(([, v]) => typeof v === "string"));
|
|
153
|
+
},
|
|
154
|
+
resolveId(id) {
|
|
155
|
+
if (id === VIRTUAL.renderer) return slash(getRenderer().runtime.shared);
|
|
156
|
+
if (id === VIRTUAL.rendererServer) return slash(getRenderer().runtime.server);
|
|
157
|
+
if (id === VIRTUAL.rendererClient) return slash(getRenderer().runtime.client);
|
|
158
|
+
if (id.startsWith("virtual:goatly/")) return `\0${id}`;
|
|
159
|
+
return null;
|
|
160
|
+
},
|
|
161
|
+
load(id) {
|
|
162
|
+
if (!id.startsWith("\0virtual:goatly/")) return null;
|
|
163
|
+
const name = id.slice(1);
|
|
164
|
+
const state = store.getState();
|
|
165
|
+
if (name === VIRTUAL.asyncRequires) return `export const components = {\n${[...getPageComponents()].map(([chunk, file]) => ` ${JSON.stringify(chunk)}: () => import(${JSON.stringify(file)}),`).join("\n")}\n}\n`;
|
|
166
|
+
if (name === VIRTUAL.browserPlugins || name === VIRTUAL.ssrPlugins) {
|
|
167
|
+
const entries = pluginEntries(name === VIRTUAL.browserPlugins ? "browser" : "ssr");
|
|
168
|
+
const imports = entries.map((e, i) => `import * as p${i} from ${JSON.stringify(slash(e.file))};`);
|
|
169
|
+
const list = entries.map((e, i) => ` { name: ${JSON.stringify(e.plugin.name)}, plugin: p${i}, options: ${serializeOptions(e.plugin.pluginOptions)} },`);
|
|
170
|
+
return `${imports.join("\n")}\nexport default [\n${list.join("\n")}\n]\n`;
|
|
171
|
+
}
|
|
172
|
+
if (name === VIRTUAL.html) {
|
|
173
|
+
const file = resolveApiFile(path.join(program().directory, "src"), "html") ?? path.join(runtimeDir, "default-html.js");
|
|
174
|
+
return `export { default } from ${JSON.stringify(slash(file))}\n`;
|
|
175
|
+
}
|
|
176
|
+
if (name === VIRTUAL.matchPaths) return `export default ${JSON.stringify(computeMatchPaths())}\n`;
|
|
177
|
+
if (name === VIRTUAL.notFoundPages) return `export default ${JSON.stringify(runtimeNotFoundPages())}\n`;
|
|
178
|
+
if (name === VIRTUAL.redirects) return `export default ${JSON.stringify(state.redirects.filter((r) => r.redirectInBrowser))}\n`;
|
|
179
|
+
if (name === VIRTUAL.runtimeConfig) return `export default ${JSON.stringify({
|
|
180
|
+
buildHash: ctx.buildHash,
|
|
181
|
+
dev: ctx.command === "develop",
|
|
182
|
+
devSSR: ctx.devSSR
|
|
183
|
+
})}\n`;
|
|
184
|
+
if (name.startsWith(VIRTUAL.staticQueryPrefix)) {
|
|
185
|
+
const hash = name.slice(VIRTUAL.staticQueryPrefix.length);
|
|
186
|
+
const data = ctx.getStaticQueryResult(hash);
|
|
187
|
+
return `export default { hash: ${JSON.stringify(hash)}, data: ${JSON.stringify(data ?? null)} }\n`;
|
|
188
|
+
}
|
|
189
|
+
return null;
|
|
190
|
+
},
|
|
191
|
+
transform: {
|
|
192
|
+
filter: { id: /\.(m|c)?[jt]sx?(\?.*)?$/ },
|
|
193
|
+
async handler(code, id) {
|
|
194
|
+
if (id.startsWith("\0")) return null;
|
|
195
|
+
const file = id.split("?")[0];
|
|
196
|
+
let result = null;
|
|
197
|
+
if (isServe && !file.includes("/.vite/") && !file.includes("/.cache/vite/") && isCommonJsSource(code)) {
|
|
198
|
+
const converted = transformCommonJsApiFile(code, file, defines);
|
|
199
|
+
if (converted != null) {
|
|
200
|
+
code = converted;
|
|
201
|
+
result = { code };
|
|
202
|
+
}
|
|
203
|
+
} else if (isServe && (isUserFile(file) || apiFilePattern.test(file)) && !file.includes("/.vite/") && !file.includes("/.cache/vite/")) {
|
|
204
|
+
const r = transformRequireInEsm(code, id, defines);
|
|
205
|
+
if (r) {
|
|
206
|
+
code = r.code;
|
|
207
|
+
result = r;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (isServe && result === null && file.includes("/node_modules/") && !file.includes("/.vite/") && !file.includes("/.cache/vite/")) {
|
|
211
|
+
const r = await transformCjsImportsInEsm(code, id, (spec) => cjsExportsOf(this, spec, id));
|
|
212
|
+
if (r) {
|
|
213
|
+
code = r.code;
|
|
214
|
+
result = r;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (code.includes("graphql")) {
|
|
218
|
+
const r = transformGraphQLTags(code, id, { virtualPrefix: VIRTUAL.staticQueryPrefix });
|
|
219
|
+
if (r) {
|
|
220
|
+
if (r.exportsPageQuery && isUserFile(file)) warnIfPageQueryIsIgnored(file);
|
|
221
|
+
code = r.code;
|
|
222
|
+
result = {
|
|
223
|
+
code: r.code,
|
|
224
|
+
map: r.map
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (isUserFile(file) && !/\/src\/html\.[jt]sx?$/.test(file) && (code.includes("<html") || code.includes("<body"))) {
|
|
229
|
+
const r = transformHtmlBodyJsx(code, id, headTagsModule);
|
|
230
|
+
if (r) {
|
|
231
|
+
code = r.code;
|
|
232
|
+
result = r;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (ctx.reactCompiler && isUserFile(file) && !file.includes("/.vite/") && !file.includes("/.cache/vite/") && !ctx.precompiledDirs?.some((re) => re.test(file))) {
|
|
236
|
+
const isServer = this.environment?.config?.consumer === "server";
|
|
237
|
+
let r;
|
|
238
|
+
try {
|
|
239
|
+
r = ctx.reactCompiler(code, file, isServer);
|
|
240
|
+
} catch (e) {
|
|
241
|
+
this.error(e);
|
|
242
|
+
}
|
|
243
|
+
if (r) {
|
|
244
|
+
code = r.code;
|
|
245
|
+
result = r;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (/\.(m|c)?js$/.test(file) && isUserFile(file) && JSX_RE.test(code)) {
|
|
249
|
+
const isClient = this.environment?.config?.consumer !== "server";
|
|
250
|
+
const out = await transformWithOxc(code, file, {
|
|
251
|
+
lang: "jsx",
|
|
252
|
+
sourcemap: true,
|
|
253
|
+
jsx: {
|
|
254
|
+
runtime: ctx.jsxRuntime === "classic" ? "classic" : "automatic",
|
|
255
|
+
importSource: ctx.jsxImportSource,
|
|
256
|
+
development: isServe,
|
|
257
|
+
refresh: isServe && isClient ? {} : void 0
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
result = {
|
|
261
|
+
code: out.code,
|
|
262
|
+
map: out.map
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
return result;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/** Re-sends a static query module to the browser after its data changed (develop). */
|
|
271
|
+
async function invalidateVirtualModule(server, name) {
|
|
272
|
+
for (const env of Object.values(server.environments)) {
|
|
273
|
+
const mod = env.moduleGraph.getModuleById(`\0${name}`);
|
|
274
|
+
if (mod) {
|
|
275
|
+
env.moduleGraph.invalidateModule(mod);
|
|
276
|
+
if (env.name === "client") await env.reloadModule(mod);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function fileExists(p) {
|
|
281
|
+
return fs.existsSync(p);
|
|
282
|
+
}
|
|
283
|
+
export { VIRTUAL, computeMatchPaths, fileExists, getPageComponents, goatlyVitePlugin, invalidateVirtualModule, scanJsxInJsPlugin };
|