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,362 @@
|
|
|
1
|
+
import { store } from "../redux/index.js";
|
|
2
|
+
import reporter from "../reporter.js";
|
|
3
|
+
import { importModule } from "../loader/import-module.js";
|
|
4
|
+
import { findInstalledPackage } from "../info.js";
|
|
5
|
+
import { slash } from "../utils/paths.js";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import { pathToFileURL } from "node:url";
|
|
10
|
+
// src/node/vite/react-compiler.ts
|
|
11
|
+
/**
|
|
12
|
+
* React Compiler without Babel: the `reactCompiler` option of gatsby-config, or
|
|
13
|
+
* `babel-plugin-react-compiler` in the site's Babel config (`.babelrc`, `babel.config.*`) or
|
|
14
|
+
* registered with `setBabelPlugin`, runs Oxc's Rust port of React Compiler (the optional peer
|
|
15
|
+
* dependency `oxc-transform-react`) on the site's sources, before their JSX is compiled.
|
|
16
|
+
*/
|
|
17
|
+
const BABEL_PLUGIN_RE = /(^|[\\/])babel-plugin-react-compiler([\\/]|$)/;
|
|
18
|
+
const BABEL_PLUGIN_NAMES = /* @__PURE__ */ new Set(["babel-plugin-react-compiler", "react-compiler"]);
|
|
19
|
+
const OPTION_KEYS = [
|
|
20
|
+
"compilationMode",
|
|
21
|
+
"panicThreshold",
|
|
22
|
+
"target",
|
|
23
|
+
"gating",
|
|
24
|
+
"dynamicGating",
|
|
25
|
+
"outputMode",
|
|
26
|
+
"reportDiagnostics",
|
|
27
|
+
"eslintSuppressionRules",
|
|
28
|
+
"flowSuppressions",
|
|
29
|
+
"ignoreUseNoForget",
|
|
30
|
+
"customOptOutDirectives",
|
|
31
|
+
"sources",
|
|
32
|
+
"environment"
|
|
33
|
+
];
|
|
34
|
+
const ENUMS = {
|
|
35
|
+
compilationMode: [
|
|
36
|
+
"infer",
|
|
37
|
+
"syntax",
|
|
38
|
+
"annotation",
|
|
39
|
+
"all"
|
|
40
|
+
],
|
|
41
|
+
panicThreshold: [
|
|
42
|
+
"none",
|
|
43
|
+
"critical_errors",
|
|
44
|
+
"all_errors"
|
|
45
|
+
],
|
|
46
|
+
target: [
|
|
47
|
+
"17",
|
|
48
|
+
"18",
|
|
49
|
+
"19"
|
|
50
|
+
],
|
|
51
|
+
outputMode: [
|
|
52
|
+
"client",
|
|
53
|
+
"ssr",
|
|
54
|
+
"lint"
|
|
55
|
+
]
|
|
56
|
+
};
|
|
57
|
+
const isPlainObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
58
|
+
const isStringArray = (v) => Array.isArray(v) && v.every((s) => typeof s === "string");
|
|
59
|
+
/** Problems of a `reactCompiler` option in gatsby-config (empty when it is valid). */
|
|
60
|
+
function validateReactCompilerConfig(value) {
|
|
61
|
+
if (value === void 0 || typeof value === "boolean") return [];
|
|
62
|
+
if (!isPlainObject(value)) return [`"reactCompiler" must be true, false or an object of React Compiler options`];
|
|
63
|
+
const problems = [];
|
|
64
|
+
for (const [key, allowed] of Object.entries(ENUMS)) {
|
|
65
|
+
const v = value[key];
|
|
66
|
+
if (v !== void 0 && !allowed.includes(String(v))) problems.push(`"reactCompiler.${key}" must be one of ${allowed.map((a) => `"${a}"`).join(", ")}, got ${JSON.stringify(v)}`);
|
|
67
|
+
}
|
|
68
|
+
for (const key of ["eslintSuppressionRules", "customOptOutDirectives"]) if (value[key] !== void 0 && !isStringArray(value[key])) problems.push(`"reactCompiler.${key}" must be an array of strings`);
|
|
69
|
+
if (value.sources !== void 0 && typeof value.sources !== "function" && !isStringArray(value.sources)) problems.push(`"reactCompiler.sources" must be an array of strings or a function`);
|
|
70
|
+
for (const key of [
|
|
71
|
+
"gating",
|
|
72
|
+
"dynamicGating",
|
|
73
|
+
"environment"
|
|
74
|
+
]) if (value[key] !== void 0 && !isPlainObject(value[key])) problems.push(`"reactCompiler.${key}" must be an object`);
|
|
75
|
+
if (isPlainObject(value.gating) && (typeof value.gating.source !== "string" || typeof value.gating.importSpecifierName !== "string")) problems.push(`"reactCompiler.gating" needs a "source" and an "importSpecifierName"`);
|
|
76
|
+
if (isPlainObject(value.dynamicGating) && typeof value.dynamicGating.source !== "string") problems.push(`"reactCompiler.dynamicGating" needs a "source"`);
|
|
77
|
+
return problems;
|
|
78
|
+
}
|
|
79
|
+
/** Option keys of a `reactCompiler` object that are not React Compiler options of Oxc. */
|
|
80
|
+
function unknownReactCompilerKeys(value) {
|
|
81
|
+
if (!isPlainObject(value)) return [];
|
|
82
|
+
return Object.keys(value).filter((k) => !OPTION_KEYS.includes(k));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Maps the options of `babel-plugin-react-compiler` to the `reactCompiler` option. Options Oxc's
|
|
86
|
+
* port has no equivalent for (e.g. `logger`, function-valued environment settings) are returned in
|
|
87
|
+
* `ignored`. `noEmit: true` becomes `outputMode: "lint"`.
|
|
88
|
+
*/
|
|
89
|
+
function fromBabelPluginOptions(babel = {}) {
|
|
90
|
+
const config = {};
|
|
91
|
+
const ignored = [];
|
|
92
|
+
for (const [key, value] of Object.entries(babel)) {
|
|
93
|
+
if (value === void 0 || value === null) continue;
|
|
94
|
+
if (key === "noEmit") {
|
|
95
|
+
if (value === true && babel.outputMode === void 0) config.outputMode = "lint";
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (key === "environment" && isPlainObject(value)) {
|
|
99
|
+
const env = {};
|
|
100
|
+
for (const [k, v] of Object.entries(value)) if (typeof v === "function") ignored.push(`environment.${k}`);
|
|
101
|
+
else env[k] = v;
|
|
102
|
+
config.environment = env;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (!OPTION_KEYS.includes(key)) {
|
|
106
|
+
ignored.push(key);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
config[key] = value;
|
|
110
|
+
}
|
|
111
|
+
if (typeof config.target === "number") config.target = String(config.target);
|
|
112
|
+
return {
|
|
113
|
+
config,
|
|
114
|
+
ignored
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** `[name, options]` of a Babel plugin entry, when it is babel-plugin-react-compiler */
|
|
118
|
+
function matchBabelEntry(entry) {
|
|
119
|
+
const [name, options] = Array.isArray(entry) ? entry : [entry, void 0];
|
|
120
|
+
if (typeof name !== "string") return null;
|
|
121
|
+
if (!BABEL_PLUGIN_NAMES.has(name) && !BABEL_PLUGIN_RE.test(name)) return null;
|
|
122
|
+
return isPlainObject(options) ? options : {};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Options of `babel-plugin-react-compiler` registered via `setBabelPlugin` for `stage` (merged in
|
|
126
|
+
* order); without one for `stage`, another stage's apply (as for styled-components).
|
|
127
|
+
*/
|
|
128
|
+
function findReactCompilerBabelRegistration(stage) {
|
|
129
|
+
const stages = store.getState().babelrc.stages;
|
|
130
|
+
const candidates = [stage, ...Object.keys(stages).filter((s) => s !== stage)];
|
|
131
|
+
for (const s of candidates) {
|
|
132
|
+
const matches = (stages[s]?.plugins ?? []).filter((p) => typeof p?.name === "string" && BABEL_PLUGIN_RE.test(p.name));
|
|
133
|
+
if (matches.length) return Object.assign({}, ...matches.map((p) => p.options ?? {}));
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/** Babel's config files, in the order Babel looks for them in the site's root */
|
|
138
|
+
const BABEL_CONFIG_FILES = [
|
|
139
|
+
"babel.config.js",
|
|
140
|
+
"babel.config.cjs",
|
|
141
|
+
"babel.config.mjs",
|
|
142
|
+
"babel.config.json",
|
|
143
|
+
".babelrc",
|
|
144
|
+
".babelrc.js",
|
|
145
|
+
".babelrc.cjs",
|
|
146
|
+
".babelrc.mjs",
|
|
147
|
+
".babelrc.json"
|
|
148
|
+
];
|
|
149
|
+
/** a stand-in for Babel's config API, for function configs (`module.exports = (api) => ...`) */
|
|
150
|
+
function babelApiStub(envName) {
|
|
151
|
+
const cache = () => void 0;
|
|
152
|
+
cache.forever = cache.never = () => void 0;
|
|
153
|
+
cache.using = (fn) => fn();
|
|
154
|
+
cache.invalidate = (fn) => fn();
|
|
155
|
+
return {
|
|
156
|
+
version: "7.0.0",
|
|
157
|
+
cache,
|
|
158
|
+
env: (match) => match === void 0 ? envName : typeof match === "function" ? match(envName) : [].concat(match).includes(envName),
|
|
159
|
+
caller: () => void 0,
|
|
160
|
+
assertVersion: () => void 0
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/** plugins of a Babel config object, including those of `env[envName]` */
|
|
164
|
+
function babelConfigPlugins(config, envName) {
|
|
165
|
+
if (!isPlainObject(config)) return [];
|
|
166
|
+
const plugins = Array.isArray(config.plugins) ? config.plugins : [];
|
|
167
|
+
const envPlugins = config.env?.[envName]?.plugins;
|
|
168
|
+
return Array.isArray(envPlugins) ? [...plugins, ...envPlugins] : plugins;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Options of `babel-plugin-react-compiler` in the site's Babel config file (Gatsby uses the site's
|
|
172
|
+
* `.babelrc` or `babel.config.*`), or null without one. Top-level `plugins` and those of
|
|
173
|
+
* `env.development`/`env.production` count; `overrides` are not read.
|
|
174
|
+
*/
|
|
175
|
+
async function findReactCompilerInBabelConfig(directory, stage) {
|
|
176
|
+
const envName = process.env.BABEL_ENV || (stage.startsWith("build") ? "production" : "development");
|
|
177
|
+
for (const name of BABEL_CONFIG_FILES) {
|
|
178
|
+
const file = path.join(directory, name);
|
|
179
|
+
if (!fs.existsSync(file)) continue;
|
|
180
|
+
let config;
|
|
181
|
+
try {
|
|
182
|
+
if (name === ".babelrc" || name.endsWith(".json")) config = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
183
|
+
else {
|
|
184
|
+
const mod = await importModule(file);
|
|
185
|
+
config = mod?.default ?? mod;
|
|
186
|
+
if (typeof config === "function") config = await config(babelApiStub(envName));
|
|
187
|
+
}
|
|
188
|
+
} catch (e) {
|
|
189
|
+
reporter.verbose(`React Compiler: could not read "${file}": ${e.message}`);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
for (const entry of babelConfigPlugins(config, envName)) {
|
|
193
|
+
const options = matchBabelEntry(entry);
|
|
194
|
+
if (options) return {
|
|
195
|
+
file,
|
|
196
|
+
options
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The React version React Compiler targets: the installed `react` (17, 18 or 19+), or 19 with
|
|
204
|
+
* another renderer (Redact provides `react/compiler-runtime`).
|
|
205
|
+
*/
|
|
206
|
+
function detectReactCompilerTarget(directory, rendererName) {
|
|
207
|
+
if (rendererName !== "react") return "19";
|
|
208
|
+
const version = findInstalledPackage(directory, "react")?.version;
|
|
209
|
+
if (!version) return "19";
|
|
210
|
+
const major = Number.parseInt(version ?? "", 10);
|
|
211
|
+
if (major <= 17) return "17";
|
|
212
|
+
if (major === 18) return "18";
|
|
213
|
+
return "19";
|
|
214
|
+
}
|
|
215
|
+
/** Normalizes the effective options (gatsby-config or Babel) into what the transform uses. */
|
|
216
|
+
function resolveReactCompilerOptions(config, ctx) {
|
|
217
|
+
const c = config === true ? {} : config;
|
|
218
|
+
const outputMode = c.outputMode ?? "client";
|
|
219
|
+
const { sources, reportDiagnostics } = c;
|
|
220
|
+
const rest = {};
|
|
221
|
+
for (const key of OPTION_KEYS) if (![
|
|
222
|
+
"sources",
|
|
223
|
+
"reportDiagnostics",
|
|
224
|
+
"outputMode"
|
|
225
|
+
].includes(key) && c[key] !== void 0) rest[key] = c[key];
|
|
226
|
+
return {
|
|
227
|
+
options: {
|
|
228
|
+
...rest,
|
|
229
|
+
target: c.target ?? ctx.target,
|
|
230
|
+
...Array.isArray(sources) ? { sources } : typeof sources === "function" ? { sources: [""] } : {}
|
|
231
|
+
},
|
|
232
|
+
outputMode,
|
|
233
|
+
reportDiagnostics: reportDiagnostics ?? outputMode === "lint",
|
|
234
|
+
sourcesFilter: typeof sources === "function" ? sources : void 0,
|
|
235
|
+
source: ctx.source
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
let loaded;
|
|
239
|
+
/** Loads `oxc-transform-react` from goatly's dependencies or the site's. */
|
|
240
|
+
function loadOxcTransformReact(directory) {
|
|
241
|
+
return loaded ??= (async () => {
|
|
242
|
+
for (const base of [import.meta.url, path.join(directory, "package.json")]) {
|
|
243
|
+
let resolved;
|
|
244
|
+
try {
|
|
245
|
+
resolved = createRequire(base).resolve("oxc-transform-react");
|
|
246
|
+
} catch {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
return import(pathToFileURL(resolved).href);
|
|
250
|
+
}
|
|
251
|
+
loaded = void 0;
|
|
252
|
+
throw new Error("oxc-transform-react is not installed");
|
|
253
|
+
})();
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Whether `react-compiler-runtime` (needed with React 17/18) is installed for the site: looked up in
|
|
257
|
+
* node_modules like Vite resolves the site's imports (not NODE_PATH).
|
|
258
|
+
*/
|
|
259
|
+
function hasCompilerRuntimePackage(directory) {
|
|
260
|
+
return !!findInstalledPackage(directory, "react-compiler-runtime");
|
|
261
|
+
}
|
|
262
|
+
let announced = false;
|
|
263
|
+
/**
|
|
264
|
+
* The React Compiler setup for `stage`: from the `reactCompiler` option of gatsby-config, else
|
|
265
|
+
* from `babel-plugin-react-compiler` in Babel's config (`setBabelPlugin`, `.babelrc`,
|
|
266
|
+
* `babel.config.*`). Null when React Compiler is off. Panics when `oxc-transform-react` (or, for
|
|
267
|
+
* React 17/18, `react-compiler-runtime`) is missing.
|
|
268
|
+
*/
|
|
269
|
+
async function resolveReactCompiler(stage, rendererName) {
|
|
270
|
+
const { config, program } = store.getState();
|
|
271
|
+
const directory = program.directory;
|
|
272
|
+
let effective = null;
|
|
273
|
+
let source = "gatsby-config";
|
|
274
|
+
if (config.reactCompiler !== void 0) {
|
|
275
|
+
if (config.reactCompiler === false) return null;
|
|
276
|
+
effective = config.reactCompiler;
|
|
277
|
+
} else {
|
|
278
|
+
const registered = findReactCompilerBabelRegistration(stage);
|
|
279
|
+
const fromFile = registered ? null : await findReactCompilerInBabelConfig(directory, stage);
|
|
280
|
+
const babelOptions = registered ?? fromFile?.options;
|
|
281
|
+
if (!babelOptions) return null;
|
|
282
|
+
source = registered ? "setBabelPlugin" : path.basename(fromFile.file);
|
|
283
|
+
const { config: mapped, ignored } = fromBabelPluginOptions(babelOptions);
|
|
284
|
+
const problems = validateReactCompilerConfig(mapped);
|
|
285
|
+
if (problems.length) reporter.panic(`Invalid babel-plugin-react-compiler options (${source}):\n- ${problems.join("\n- ")}`);
|
|
286
|
+
if (ignored.length && !announced) reporter.warn(`React Compiler: the babel-plugin-react-compiler options ${ignored.map((k) => `"${k}"`).join(", ")} (${source}) have no equivalent in Oxc's React Compiler and are ignored.`);
|
|
287
|
+
effective = mapped;
|
|
288
|
+
}
|
|
289
|
+
const target = detectReactCompilerTarget(directory, rendererName);
|
|
290
|
+
const resolved = resolveReactCompilerOptions(effective, {
|
|
291
|
+
stage,
|
|
292
|
+
target,
|
|
293
|
+
source
|
|
294
|
+
});
|
|
295
|
+
try {
|
|
296
|
+
await loadOxcTransformReact(directory);
|
|
297
|
+
} catch {
|
|
298
|
+
reporter.panic(`React Compiler is enabled (${source}), and goatly runs it with Oxc's port instead of Babel: install the "oxc-transform-react" package (e.g. npm install -D oxc-transform-react).`);
|
|
299
|
+
}
|
|
300
|
+
const effectiveTarget = resolved.options.target;
|
|
301
|
+
if (resolved.outputMode !== "lint" && (effectiveTarget === "17" || effectiveTarget === "18") && !hasCompilerRuntimePackage(directory)) reporter.panic(`React Compiler targets React ${effectiveTarget}, whose compiled components import "react-compiler-runtime": install it (e.g. npm install react-compiler-runtime).`);
|
|
302
|
+
if (!announced) {
|
|
303
|
+
announced = true;
|
|
304
|
+
reporter.info(`React Compiler (Oxc, experimental) is on (${source}): target React ${effectiveTarget}, ${resolved.outputMode === "lint" ? "lint only (the code is left unchanged)" : `${resolved.outputMode} output`}`);
|
|
305
|
+
}
|
|
306
|
+
return resolved;
|
|
307
|
+
}
|
|
308
|
+
/** for tests */
|
|
309
|
+
function resetReactCompilerState() {
|
|
310
|
+
announced = false;
|
|
311
|
+
reported.clear();
|
|
312
|
+
}
|
|
313
|
+
/** the diagnostics last reported per file, so that each change is reported once */
|
|
314
|
+
const reported = /* @__PURE__ */ new Map();
|
|
315
|
+
/** Formats one Oxc diagnostic for the terminal. */
|
|
316
|
+
function formatDiagnostic(error) {
|
|
317
|
+
return (error.codeframe ?? `${error.message}${error.helpMessage ? `\n${error.helpMessage}` : ""}`).replace(/^\n+/, "").trimEnd();
|
|
318
|
+
}
|
|
319
|
+
const isCompilerDiagnostic = (e) => (e.codeframe ?? "").includes("react-compiler") || e.message.includes("React Compiler");
|
|
320
|
+
/** The transform of the `goatly` Vite plugin, for files it decided are the site's sources. */
|
|
321
|
+
async function createReactCompilerTransform(resolved, directory) {
|
|
322
|
+
const { transformSync } = await loadOxcTransformReact(directory);
|
|
323
|
+
const outputFor = (isServer) => resolved.outputMode === "lint" ? "lint" : isServer && resolved.outputMode === "ssr" ? "ssr" : "client";
|
|
324
|
+
return (code, file, isServer) => {
|
|
325
|
+
if (file.endsWith(".d.ts")) return null;
|
|
326
|
+
if (resolved.sourcesFilter && !resolved.sourcesFilter(file)) return null;
|
|
327
|
+
const lang = /\.(m|c)?js$/.test(file) ? "jsx" : void 0;
|
|
328
|
+
const outputMode = outputFor(isServer);
|
|
329
|
+
const result = transformSync(file, code, {
|
|
330
|
+
lang,
|
|
331
|
+
sourcemap: true,
|
|
332
|
+
jsx: "preserve",
|
|
333
|
+
reactCompiler: {
|
|
334
|
+
...resolved.options,
|
|
335
|
+
outputMode,
|
|
336
|
+
reportDiagnostics: resolved.reportDiagnostics
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
if (result.fatal) {
|
|
340
|
+
if (!result.errors.some(isCompilerDiagnostic)) return null;
|
|
341
|
+
throw new Error(`React Compiler failed on ${file}:\n${result.errors.map(formatDiagnostic).join("\n\n")}`);
|
|
342
|
+
}
|
|
343
|
+
if (resolved.reportDiagnostics) {
|
|
344
|
+
const diagnostics = result.errors.filter(isCompilerDiagnostic);
|
|
345
|
+
const signature = diagnostics.map(formatDiagnostic).join("\n\n");
|
|
346
|
+
if ((reported.get(file) ?? "") !== signature) {
|
|
347
|
+
reported.set(file, signature);
|
|
348
|
+
if (signature) {
|
|
349
|
+
const rel = slash(path.relative(directory, file)) || file;
|
|
350
|
+
reporter.warn(`React Compiler: ${diagnostics.length} diagnostic${diagnostics.length === 1 ? "" : "s"} in ${rel}\n${signature}`);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (outputMode === "lint") return null;
|
|
355
|
+
if (outputMode === "client" && !/compiler-runtime["']/.test(result.code)) return null;
|
|
356
|
+
return {
|
|
357
|
+
code: result.code,
|
|
358
|
+
map: result.map
|
|
359
|
+
};
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
export { BABEL_CONFIG_FILES, createReactCompilerTransform, detectReactCompilerTarget, findReactCompilerBabelRegistration, findReactCompilerInBabelConfig, formatDiagnostic, fromBabelPluginOptions, loadOxcTransformReact, resetReactCompilerState, resolveReactCompiler, resolveReactCompilerOptions, unknownReactCompilerKeys, validateReactCompilerConfig };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { store } from "../redux/index.js";
|
|
2
|
+
import { slash } from "../utils/paths.js";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
// src/node/vite/source-files.ts
|
|
5
|
+
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
6
|
+
const isWindows = process.platform === "win32";
|
|
7
|
+
/**
|
|
8
|
+
* The slashed real path(s) of a directory, as they can appear in Vite's module ids. On Windows,
|
|
9
|
+
* fs.realpathSync keeps 8.3 short names (C:/Users/RUNNER~1, e.g. in %TEMP%) while
|
|
10
|
+
* fs.realpathSync.native expands them (C:/Users/runneradmin), and Vite resolves with either (the JS
|
|
11
|
+
* one until `net use` has answered, then the native one), so both forms are returned there.
|
|
12
|
+
*/
|
|
13
|
+
function realDirs(dir) {
|
|
14
|
+
let real;
|
|
15
|
+
try {
|
|
16
|
+
real = slash(fs.realpathSync(dir));
|
|
17
|
+
} catch {
|
|
18
|
+
return [dir];
|
|
19
|
+
}
|
|
20
|
+
if (!isWindows) return [real];
|
|
21
|
+
try {
|
|
22
|
+
const native = slash(fs.realpathSync.native(dir));
|
|
23
|
+
return native === real ? [real] : [real, native];
|
|
24
|
+
} catch {
|
|
25
|
+
return [real];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Windows paths are case-insensitive (NTFS), and their spelling can differ between sources */
|
|
29
|
+
const comparable = (p) => isWindows ? p.toLowerCase() : p;
|
|
30
|
+
/**
|
|
31
|
+
* Directories of the site's plugins that are installed in node_modules: slashed real paths, like the
|
|
32
|
+
* module ids Vite uses (a plugin configured by path, or a `plugins/` entry, can be a symlink into
|
|
33
|
+
* node_modules/.pnpm).
|
|
34
|
+
*/
|
|
35
|
+
function nodeModulesPluginDirs() {
|
|
36
|
+
return store.getState().flattenedPlugins.map((p) => p.resolve).filter((dir) => typeof dir === "string").flatMap(realDirs).filter((dir) => dir.includes("/node_modules/"));
|
|
37
|
+
}
|
|
38
|
+
/** The site's plugins installed in node_modules: package name and (real, slashed) directory. */
|
|
39
|
+
function nodeModulesPlugins() {
|
|
40
|
+
const plugins = /* @__PURE__ */ new Map();
|
|
41
|
+
for (const plugin of store.getState().flattenedPlugins) {
|
|
42
|
+
if (typeof plugin.resolve !== "string") continue;
|
|
43
|
+
const dir = realDirs(plugin.resolve)[0];
|
|
44
|
+
if (dir.includes("/node_modules/")) plugins.set(plugin.name, dir);
|
|
45
|
+
}
|
|
46
|
+
return [...plugins].map(([name, dir]) => ({
|
|
47
|
+
name,
|
|
48
|
+
dir
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Package names of the site's plugins installed in node_modules (by real path, as above). The dev
|
|
53
|
+
* server renders them through Vite with the site's transforms instead of loading their sources with
|
|
54
|
+
* Node, which can't run JSX or extensionless imports, and doesn't pre-bundle them, because
|
|
55
|
+
* gatsby-browser files import their modules by path (a pre-bundled copy would duplicate them, e.g.
|
|
56
|
+
* React contexts). Builds bundle every package anyway.
|
|
57
|
+
*/
|
|
58
|
+
function nodeModulesPluginNames() {
|
|
59
|
+
const names = /* @__PURE__ */ new Set();
|
|
60
|
+
for (const plugin of store.getState().flattenedPlugins) if (typeof plugin.resolve === "string" && realDirs(plugin.resolve).some((dir) => dir.includes("/node_modules/"))) names.add(plugin.name);
|
|
61
|
+
return [...names];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Whether `file` is compiled like the site's own sources (TypeScript, JSX in `.js`, `<html>`/`<body>`
|
|
65
|
+
* in `Head`, styled-components): everything outside node_modules (incl. linked workspace
|
|
66
|
+
* packages), gatsby themes, and the files of plugins installed from npm (e.g. their
|
|
67
|
+
* `gatsby-browser.ts`/`gatsby-ssr.tsx`), but not the plugins' own dependencies.
|
|
68
|
+
*/
|
|
69
|
+
function isSourceFile(file, pluginDirs = nodeModulesPluginDirs()) {
|
|
70
|
+
file = comparable(file);
|
|
71
|
+
if (!file.includes("/node_modules/")) return true;
|
|
72
|
+
if (file.slice(file.lastIndexOf("/node_modules/")).startsWith("/node_modules/gatsby-theme-")) return true;
|
|
73
|
+
return pluginDirs.map(comparable).some((dir) => file.startsWith(`${dir}/`) && !file.slice(dir.length).includes("/node_modules/"));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The same rule as {@link isSourceFile}, as one RegExp matching the node_modules files that are *not*
|
|
77
|
+
* compiled (Vite's `oxc.exclude` only takes patterns).
|
|
78
|
+
*/
|
|
79
|
+
function nonSourceNodeModulesRe(pluginDirs = nodeModulesPluginDirs()) {
|
|
80
|
+
const plugins = pluginDirs.length ? `(?!(?:${pluginDirs.map(escapeRe).join("|")})/(?!(?:.*/)?node_modules/))` : "";
|
|
81
|
+
return new RegExp(`^${plugins}.*/node_modules/(?!gatsby-theme-|.*/node_modules/)`, isWindows ? "i" : "");
|
|
82
|
+
}
|
|
83
|
+
export { isSourceFile, nodeModulesPluginDirs, nodeModulesPluginNames, nodeModulesPlugins, nonSourceNodeModulesRe };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { runPreprocessSource } from "../query/compile.js";
|
|
2
|
+
import { store } from "../redux/index.js";
|
|
3
|
+
import { slash } from "../utils/paths.js";
|
|
4
|
+
import { isSourceFile, nodeModulesPluginDirs } from "./source-files.js";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import { MagicString } from "magic-string";
|
|
9
|
+
// src/node/vite/static-image.ts
|
|
10
|
+
const BABEL_PLUGIN_RE = /(^|[\\/])babel-plugin-parse-static-images(\.js)?$/;
|
|
11
|
+
/**
|
|
12
|
+
* The `cacheDir` and package of gatsby-plugin-image's `babel-plugin-parse-static-images`, which its
|
|
13
|
+
* onCreateBabelConfig registers via `setBabelPlugin` (for every stage).
|
|
14
|
+
*/
|
|
15
|
+
function findStaticImageBabelPlugin(stage) {
|
|
16
|
+
const stages = store.getState().babelrc.stages;
|
|
17
|
+
const candidates = [stage, ...Object.keys(stages).filter((s) => s !== stage)];
|
|
18
|
+
for (const s of candidates) {
|
|
19
|
+
const match = (stages[s]?.plugins ?? []).find((p) => typeof p?.name === "string" && BABEL_PLUGIN_RE.test(p.name));
|
|
20
|
+
if (match?.options?.cacheDir) return {
|
|
21
|
+
pluginFile: match.name,
|
|
22
|
+
cacheDir: match.options.cacheDir
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
/** gatsby-plugin-image's own parser and helpers, so the hashes match its preprocessSource */
|
|
28
|
+
function loadHelpers(pluginFile) {
|
|
29
|
+
const req = createRequire(pluginFile);
|
|
30
|
+
const traverse = req("@babel/traverse");
|
|
31
|
+
return {
|
|
32
|
+
traverse: traverse.default ?? traverse,
|
|
33
|
+
babelParseToAst: req("./node-apis/parser").babelParseToAst,
|
|
34
|
+
...req("./babel-helpers")
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Replaces gatsby-plugin-image's `babel-plugin-parse-static-images` (goatly runs no Babel):
|
|
39
|
+
* gives each `<StaticImage>` the image data that the plugin's `preprocessSource` generated during
|
|
40
|
+
* query extraction, as an `__imageData` prop (imported from `.cache/caches/gatsby-plugin-image`),
|
|
41
|
+
* or an `__error` prop that the component shows. Props whose value isn't known at build time are
|
|
42
|
+
* reported like Gatsby does. `preprocessSource` runs for each file first (once per contents, shared
|
|
43
|
+
* with query extraction): for files outside src/, and for files that changed in develop.
|
|
44
|
+
*/
|
|
45
|
+
function staticImagePlugin(options) {
|
|
46
|
+
const pluginDirs = nodeModulesPluginDirs();
|
|
47
|
+
const ownDir = `${slash(path.dirname(path.dirname(options.pluginFile)))}/`;
|
|
48
|
+
let helpers;
|
|
49
|
+
return {
|
|
50
|
+
name: "goatly:static-image",
|
|
51
|
+
enforce: "pre",
|
|
52
|
+
configureServer(server) {
|
|
53
|
+
fs.mkdirSync(options.cacheDir, { recursive: true });
|
|
54
|
+
const timers = /* @__PURE__ */ new Map();
|
|
55
|
+
const watcher = fs.watch(options.cacheDir, (_event, name) => {
|
|
56
|
+
if (!name?.endsWith(".json")) return;
|
|
57
|
+
const file = slash(path.join(options.cacheDir, name));
|
|
58
|
+
clearTimeout(timers.get(file));
|
|
59
|
+
timers.set(file, setTimeout(() => {
|
|
60
|
+
timers.delete(file);
|
|
61
|
+
for (const env of Object.values(server.environments)) for (const mod of env.moduleGraph.getModulesByFile(file) ?? []) {
|
|
62
|
+
env.moduleGraph.invalidateModule(mod);
|
|
63
|
+
if (env.name === "client") env.reloadModule(mod);
|
|
64
|
+
}
|
|
65
|
+
}, 50));
|
|
66
|
+
});
|
|
67
|
+
const close = server.close.bind(server);
|
|
68
|
+
server.close = async () => {
|
|
69
|
+
watcher.close();
|
|
70
|
+
for (const timer of timers.values()) clearTimeout(timer);
|
|
71
|
+
timers.clear();
|
|
72
|
+
return close();
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
transform: {
|
|
76
|
+
filter: {
|
|
77
|
+
id: /\.(m?[jt]sx?|[mc]ts|cjs)(\?|$)/,
|
|
78
|
+
code: "StaticImage"
|
|
79
|
+
},
|
|
80
|
+
async handler(code, id) {
|
|
81
|
+
const file = id.split("?")[0];
|
|
82
|
+
if (!code.includes("gatsby-plugin-image") || file.startsWith(ownDir)) return null;
|
|
83
|
+
if (!isSourceFile(file, pluginDirs)) return null;
|
|
84
|
+
helpers ??= loadHelpers(options.pluginFile);
|
|
85
|
+
const { traverse, babelParseToAst, evaluateImageAttributes, hashOptions } = helpers;
|
|
86
|
+
const found = [];
|
|
87
|
+
traverse(babelParseToAst(code, file), { JSXOpeningElement(nodePath) {
|
|
88
|
+
if (!nodePath.get("name").referencesImport("gatsby-plugin-image", "StaticImage")) return;
|
|
89
|
+
const unresolved = [];
|
|
90
|
+
const props = evaluateImageAttributes(nodePath, (prop) => unresolved.push(prop));
|
|
91
|
+
props.filename = slash(file);
|
|
92
|
+
found.push({
|
|
93
|
+
at: nodePath.node.name.end,
|
|
94
|
+
hash: hashOptions(props),
|
|
95
|
+
props,
|
|
96
|
+
error: unresolved.length ? `Could not find values for the following props at build time: ${unresolved.join(", ")}` : void 0
|
|
97
|
+
});
|
|
98
|
+
} });
|
|
99
|
+
if (!found.length) return null;
|
|
100
|
+
const dataFile = (hash) => path.join(options.cacheDir, `${hash}.json`);
|
|
101
|
+
const contents = await fs.promises.readFile(file, "utf8").catch(() => code);
|
|
102
|
+
await runPreprocessSource(file, contents);
|
|
103
|
+
const s = new MagicString(code);
|
|
104
|
+
const imports = [];
|
|
105
|
+
for (const [i, image] of found.entries()) {
|
|
106
|
+
if (image.error) this.warn(`[gatsby-plugin-image] ${image.error}`);
|
|
107
|
+
const json = dataFile(image.hash);
|
|
108
|
+
const hasData = image.props.src !== void 0 && fs.existsSync(json);
|
|
109
|
+
let attrs = "";
|
|
110
|
+
if (!hasData) {
|
|
111
|
+
const message = `No data found for image "${image.props.src}"\n${image.error ?? ""}${image.props.src === void 0 ? "" : `Could not read image data file "${json}". This may mean that the images in "${file}" were not processed.`}`;
|
|
112
|
+
this.warn(`[gatsby-plugin-image] ${message}`);
|
|
113
|
+
attrs = ` __error={${JSON.stringify(message)}}`;
|
|
114
|
+
} else {
|
|
115
|
+
const name = `__goatlyStaticImage${i}`;
|
|
116
|
+
imports.push(`import ${name} from ${JSON.stringify(slash(json))};`);
|
|
117
|
+
if (image.error) attrs += ` __error={${JSON.stringify(image.error)}}`;
|
|
118
|
+
attrs += ` __imageData={${name}}`;
|
|
119
|
+
this.addWatchFile(json);
|
|
120
|
+
}
|
|
121
|
+
s.appendLeft(image.at, attrs);
|
|
122
|
+
}
|
|
123
|
+
const start = code.startsWith("#!") ? code.indexOf("\n") + 1 : 0;
|
|
124
|
+
if (imports.length) s.appendLeft(start, imports.join(""));
|
|
125
|
+
return {
|
|
126
|
+
code: s.toString(),
|
|
127
|
+
map: s.generateMap({
|
|
128
|
+
hires: "boundary",
|
|
129
|
+
source: file
|
|
130
|
+
})
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
export { findStaticImageBabelPlugin, staticImagePlugin };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { store } from "../redux/index.js";
|
|
2
|
+
import { isSourceFile, nodeModulesPluginDirs } from "./source-files.js";
|
|
3
|
+
import { transformWithOxc } from "vite";
|
|
4
|
+
// src/node/vite/styled-components.ts
|
|
5
|
+
const BABEL_PLUGIN_RE = /(^|[\\/])babel-plugin-styled-components([\\/]|$)/;
|
|
6
|
+
/**
|
|
7
|
+
* Options of `babel-plugin-styled-components` registered via `setBabelPlugin` (e.g. by
|
|
8
|
+
* gatsby-plugin-styled-components) for `stage`. Several registrations merge their options in
|
|
9
|
+
* order, like Gatsby's reducer. Without one for `stage`, another stage's apply: `setBabelPlugin`
|
|
10
|
+
* without a stage registers for "develop" only.
|
|
11
|
+
*/
|
|
12
|
+
function findStyledComponentsBabelOptions(stage) {
|
|
13
|
+
const stages = store.getState().babelrc.stages;
|
|
14
|
+
const candidates = [stage, ...Object.keys(stages).filter((s) => s !== stage)];
|
|
15
|
+
for (const s of candidates) {
|
|
16
|
+
const matches = (stages[s]?.plugins ?? []).filter((p) => typeof p?.name === "string" && BABEL_PLUGIN_RE.test(p.name));
|
|
17
|
+
if (matches.length) return Object.assign({}, ...matches.map((p) => p.options ?? {}));
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
/** Maps babel-plugin-styled-components options to Oxc's built-in styled-components transform. */
|
|
22
|
+
function toOxcStyledComponentsOptions(babel) {
|
|
23
|
+
return {
|
|
24
|
+
displayName: babel.displayName ?? true,
|
|
25
|
+
fileName: babel.fileName ?? true,
|
|
26
|
+
minify: babel.minify ?? true,
|
|
27
|
+
pure: babel.pure ?? false,
|
|
28
|
+
...babel.namespace ? { namespace: babel.namespace } : {},
|
|
29
|
+
...babel.topLevelImportPaths?.length ? { topLevelImportPaths: babel.topLevelImportPaths } : {},
|
|
30
|
+
...babel.meaninglessFileNames ? { meaninglessFileNames: babel.meaninglessFileNames } : {},
|
|
31
|
+
ssr: true,
|
|
32
|
+
transpileTemplateLiterals: false
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function langFor(file) {
|
|
36
|
+
if (/\.[mc]?ts$/.test(file)) return "ts";
|
|
37
|
+
if (file.endsWith(".tsx")) return "tsx";
|
|
38
|
+
return "jsx";
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Replaces `babel-plugin-styled-components` (goatly runs no Babel): adds `displayName` and a
|
|
42
|
+
* stable `componentId` to every styled component, like Gatsby did. Applies to the files Gatsby ran
|
|
43
|
+
* the site's Babel config on: everything outside node_modules (incl. linked workspace packages),
|
|
44
|
+
* gatsby themes and the files of plugins installed from npm.
|
|
45
|
+
*/
|
|
46
|
+
function styledComponentsPlugin(babelOptions) {
|
|
47
|
+
const options = toOxcStyledComponentsOptions(babelOptions);
|
|
48
|
+
const importPaths = ["styled-components", ...options.topLevelImportPaths ?? []];
|
|
49
|
+
const pluginDirs = nodeModulesPluginDirs();
|
|
50
|
+
return {
|
|
51
|
+
name: "goatly:styled-components",
|
|
52
|
+
enforce: "pre",
|
|
53
|
+
transform: {
|
|
54
|
+
filter: { id: /\.(m?[jt]sx?|[mc]ts|cjs)(\?|$)/ },
|
|
55
|
+
async handler(code, id) {
|
|
56
|
+
const file = id.split("?")[0];
|
|
57
|
+
if (!isSourceFile(file, pluginDirs)) return null;
|
|
58
|
+
if (!importPaths.some((p) => code.includes(p))) return null;
|
|
59
|
+
const out = await transformWithOxc(code, file, {
|
|
60
|
+
lang: langFor(file),
|
|
61
|
+
jsx: "preserve",
|
|
62
|
+
sourcemap: true,
|
|
63
|
+
plugins: { styledComponents: options }
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
code: out.code,
|
|
67
|
+
map: out.map
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export { findStyledComponentsBabelOptions, styledComponentsPlugin, toOxcStyledComponentsOptions };
|