@fluxwp/plugin-kit 0.0.0-stage → 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.
- package/README.md +163 -2
- package/dist/codegen.js +264 -0
- package/dist/flx-kit.js +476 -0
- package/dist/modules.js +19 -0
- package/dist/vite.js +152 -0
- package/package.json +80 -4
- package/runtime-exports.json +316 -0
- package/src/codegen.ts +117 -0
- package/src/css.d.ts +1 -0
- package/src/modules.ts +23 -0
- package/src/vite.ts +150 -0
- package/types/codegen.d.ts +18 -0
- package/types/modules.d.ts +8 -0
- package/types/vite.d.ts +12 -0
package/dist/flx-kit.js
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// packages/plugin-kit/bin/flx-kit.ts
|
|
4
|
+
import { readFileSync as readFileSync2, writeFileSync as writeFileSync2, mkdirSync as mkdirSync2 } from "node:fs";
|
|
5
|
+
import { basename as basename2, join as join2, resolve as resolve2, relative as relative2 } from "node:path";
|
|
6
|
+
|
|
7
|
+
// packages/plugin-kit/src/codegen.ts
|
|
8
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
10
|
+
import { buildSchema, parse as parse3 } from "graphql";
|
|
11
|
+
import { codegen as gqlCodegen } from "@graphql-codegen/core";
|
|
12
|
+
import * as typescriptOperations from "@graphql-codegen/typescript-operations";
|
|
13
|
+
|
|
14
|
+
// packages/graphql/scripts/lib.ts
|
|
15
|
+
import { createHash } from "node:crypto";
|
|
16
|
+
import {
|
|
17
|
+
Kind,
|
|
18
|
+
parse,
|
|
19
|
+
print,
|
|
20
|
+
validate,
|
|
21
|
+
visit
|
|
22
|
+
} from "graphql";
|
|
23
|
+
|
|
24
|
+
class GenerateError extends Error {
|
|
25
|
+
}
|
|
26
|
+
var NAME = /^Flx[A-Z][A-Za-z0-9]*$/;
|
|
27
|
+
function collectOperations(schema, files) {
|
|
28
|
+
const problems = [];
|
|
29
|
+
const byPlugin = new Map;
|
|
30
|
+
for (const f of files) {
|
|
31
|
+
let doc;
|
|
32
|
+
try {
|
|
33
|
+
doc = parse(f.body, { noLocation: false });
|
|
34
|
+
} catch (e) {
|
|
35
|
+
problems.push(`${f.path}: ${e.message}`);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const entry = byPlugin.get(f.plugin) ?? { ops: [], frags: new Map };
|
|
39
|
+
byPlugin.set(f.plugin, entry);
|
|
40
|
+
for (const def of doc.definitions) {
|
|
41
|
+
if (def.kind === Kind.OPERATION_DEFINITION) {
|
|
42
|
+
if (def.operation === "subscription")
|
|
43
|
+
problems.push(`${f.path}: subscriptions are not supported`);
|
|
44
|
+
else
|
|
45
|
+
entry.ops.push({ node: def, path: f.path });
|
|
46
|
+
} else if (def.kind === Kind.FRAGMENT_DEFINITION) {
|
|
47
|
+
if (entry.frags.has(def.name.value))
|
|
48
|
+
problems.push(`${f.path}: fragment ${def.name.value} is defined twice in ${f.plugin}`);
|
|
49
|
+
entry.frags.set(def.name.value, def);
|
|
50
|
+
} else {
|
|
51
|
+
problems.push(`${f.path}: only operations and fragments belong in operation files`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const seen = new Map;
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const [plugin, { ops, frags }] of byPlugin) {
|
|
58
|
+
for (const { node, path } of ops) {
|
|
59
|
+
const name = node.name?.value;
|
|
60
|
+
if (!name) {
|
|
61
|
+
problems.push(`${path}: every operation needs a name`);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (!NAME.test(name))
|
|
65
|
+
problems.push(`${path}: operation ${name} must be PascalCase and start with "Flx"`);
|
|
66
|
+
const other = seen.get(name);
|
|
67
|
+
if (other)
|
|
68
|
+
problems.push(`${path}: operation ${name} is also defined in ${other}`);
|
|
69
|
+
seen.set(name, path);
|
|
70
|
+
const used = new Map;
|
|
71
|
+
const walk = (n) => visit(n, {
|
|
72
|
+
FragmentSpread(s) {
|
|
73
|
+
const name = s.name.value;
|
|
74
|
+
if (used.has(name))
|
|
75
|
+
return;
|
|
76
|
+
const frag = frags.get(name);
|
|
77
|
+
if (!frag) {
|
|
78
|
+
problems.push(`${path}: fragment ${name} is not defined in ${plugin}`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
used.set(name, frag);
|
|
82
|
+
walk(frag);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
walk(node);
|
|
86
|
+
const doc = {
|
|
87
|
+
kind: Kind.DOCUMENT,
|
|
88
|
+
definitions: [node, ...[...used.values()].sort((a, b) => a.name.value.localeCompare(b.name.value))]
|
|
89
|
+
};
|
|
90
|
+
for (const err of validate(schema, doc))
|
|
91
|
+
problems.push(`${path}: ${err.message}`);
|
|
92
|
+
const printed = print(stripLocations(doc));
|
|
93
|
+
out.push({ plugin, name, kind: node.operation, document: printed, id: sha256(printed), path });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (problems.length)
|
|
97
|
+
throw new GenerateError(problems.join(`
|
|
98
|
+
`));
|
|
99
|
+
return out.sort((a, b) => a.name.localeCompare(b.name));
|
|
100
|
+
}
|
|
101
|
+
function stripLocations(doc) {
|
|
102
|
+
return JSON.parse(JSON.stringify(doc, (k, v) => k === "loc" ? undefined : v));
|
|
103
|
+
}
|
|
104
|
+
var sha256 = (s) => createHash("sha256").update(s).digest("hex");
|
|
105
|
+
function manifests(ops) {
|
|
106
|
+
const by = new Map;
|
|
107
|
+
for (const op of ops) {
|
|
108
|
+
const m = by.get(op.plugin) ?? {};
|
|
109
|
+
m[op.id] = op.document;
|
|
110
|
+
by.set(op.plugin, m);
|
|
111
|
+
}
|
|
112
|
+
const out = new Map;
|
|
113
|
+
for (const [plugin, m] of by) {
|
|
114
|
+
const sorted = Object.fromEntries(Object.entries(m).sort(([a], [b]) => a.localeCompare(b)));
|
|
115
|
+
out.set(plugin, JSON.stringify(sorted, null, 2) + `
|
|
116
|
+
`);
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
var HEADER = "// Generated by packages/graphql/scripts/generate.ts. Do not edit; run `bun run --filter @fluxwp/graphql generate`.\n";
|
|
121
|
+
function typeNames(op) {
|
|
122
|
+
const suffix = op.kind === "query" ? "Query" : "Mutation";
|
|
123
|
+
const base = op.name.endsWith(suffix) ? op.name : op.name + suffix;
|
|
124
|
+
return { result: base, variables: base + "Variables" };
|
|
125
|
+
}
|
|
126
|
+
function operationsModule(ops) {
|
|
127
|
+
const lines = [HEADER, "import type { FlxOperation } from '../operation';"];
|
|
128
|
+
if (ops.length)
|
|
129
|
+
lines.push("import type * as T from './types';");
|
|
130
|
+
lines.push("");
|
|
131
|
+
for (const op of ops) {
|
|
132
|
+
const t = typeNames(op);
|
|
133
|
+
lines.push(`/** ${op.kind} ${op.name} (${op.plugin}, ${op.path}) */`, `export const ${op.name}: FlxOperation<T.${t.result}, T.${t.variables}> = {`, ` id: '${op.id}',`, ` name: '${op.name}',`, ` kind: '${op.kind}',`, ` plugin: '${op.plugin}',`, ` document: ${JSON.stringify(op.document)},`, "};", "");
|
|
134
|
+
}
|
|
135
|
+
if (!ops.length)
|
|
136
|
+
lines.push("export {};", "");
|
|
137
|
+
return lines.join(`
|
|
138
|
+
`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// packages/graphql/scripts/parts.ts
|
|
142
|
+
import {
|
|
143
|
+
buildASTSchema,
|
|
144
|
+
concatAST,
|
|
145
|
+
getNamedType,
|
|
146
|
+
isInputObjectType,
|
|
147
|
+
isInterfaceType,
|
|
148
|
+
isObjectType,
|
|
149
|
+
isUnionType,
|
|
150
|
+
Kind as Kind2,
|
|
151
|
+
lexicographicSortSchema,
|
|
152
|
+
parse as parse2,
|
|
153
|
+
print as print2,
|
|
154
|
+
printSchema
|
|
155
|
+
} from "graphql";
|
|
156
|
+
var CORE_PART = "packages/graphql/schema/core.graphql";
|
|
157
|
+
var OWNER = /^(plugins|themes)\/[A-Za-z0-9_.-]+$/;
|
|
158
|
+
function partPath(owner) {
|
|
159
|
+
if (owner === "")
|
|
160
|
+
return CORE_PART;
|
|
161
|
+
if (!OWNER.test(owner))
|
|
162
|
+
throw new Error(`invalid schema owner "${owner}"`);
|
|
163
|
+
return `${owner}/graphql/schema.graphql`;
|
|
164
|
+
}
|
|
165
|
+
var header = (owner) => `# GraphQL schema snapshot: ${owner === "" ? "WordPress / WPGraphQL core" : owner}.
|
|
166
|
+
` + "# Generated by `bun run --filter @fluxwp/graphql schema`. Do not edit.\n\n";
|
|
167
|
+
function hasFields(d) {
|
|
168
|
+
return d.kind === Kind2.OBJECT_TYPE_DEFINITION || d.kind === Kind2.INTERFACE_TYPE_DEFINITION || d.kind === Kind2.INPUT_OBJECT_TYPE_DEFINITION;
|
|
169
|
+
}
|
|
170
|
+
function isTypeDefinition(d) {
|
|
171
|
+
return d.kind === Kind2.OBJECT_TYPE_DEFINITION || d.kind === Kind2.INTERFACE_TYPE_DEFINITION || d.kind === Kind2.INPUT_OBJECT_TYPE_DEFINITION || d.kind === Kind2.ENUM_TYPE_DEFINITION || d.kind === Kind2.UNION_TYPE_DEFINITION || d.kind === Kind2.SCALAR_TYPE_DEFINITION;
|
|
172
|
+
}
|
|
173
|
+
function splitSchema(sdl, owners) {
|
|
174
|
+
const schema = lexicographicSortSchema(buildASTSchema(parse2(sdl, { noLocation: true })));
|
|
175
|
+
const doc = parse2(printSchema(schema), { noLocation: true });
|
|
176
|
+
for (const owner of new Set(Object.values(owners)))
|
|
177
|
+
partPath(owner);
|
|
178
|
+
const fieldOwner = (type, field) => {
|
|
179
|
+
const own = owners[`${type}.${field}`];
|
|
180
|
+
if (own !== undefined)
|
|
181
|
+
return own;
|
|
182
|
+
const t = schema.getType(type);
|
|
183
|
+
if (isObjectType(t) || isInterfaceType(t)) {
|
|
184
|
+
for (const i of t.getInterfaces()) {
|
|
185
|
+
const viaInterface = owners[`${i.name}.${field}`];
|
|
186
|
+
if (viaInterface !== undefined)
|
|
187
|
+
return viaInterface;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return;
|
|
191
|
+
};
|
|
192
|
+
const typeOwner = new Map;
|
|
193
|
+
for (const name of Object.keys(schema.getTypeMap())) {
|
|
194
|
+
if (name.startsWith("__"))
|
|
195
|
+
continue;
|
|
196
|
+
const own = owners[name];
|
|
197
|
+
if (own !== undefined)
|
|
198
|
+
typeOwner.set(name, own);
|
|
199
|
+
}
|
|
200
|
+
const reachedBy = new Map;
|
|
201
|
+
const refs = (t, context) => {
|
|
202
|
+
const out = [];
|
|
203
|
+
if (isObjectType(t) || isInterfaceType(t)) {
|
|
204
|
+
for (const f of Object.values(t.getFields())) {
|
|
205
|
+
const o = fieldOwner(t.name, f.name);
|
|
206
|
+
if (o !== undefined && o !== context)
|
|
207
|
+
continue;
|
|
208
|
+
out.push(getNamedType(f.type), ...f.args.map((a) => getNamedType(a.type)));
|
|
209
|
+
}
|
|
210
|
+
out.push(...t.getInterfaces());
|
|
211
|
+
}
|
|
212
|
+
if (isInterfaceType(t) || isUnionType(t))
|
|
213
|
+
out.push(...schema.getPossibleTypes(t));
|
|
214
|
+
if (isInputObjectType(t))
|
|
215
|
+
for (const f of Object.values(t.getFields()))
|
|
216
|
+
out.push(getNamedType(f.type));
|
|
217
|
+
return out;
|
|
218
|
+
};
|
|
219
|
+
const walk = (start, context) => {
|
|
220
|
+
const queue = [...start];
|
|
221
|
+
const seen = new Set;
|
|
222
|
+
while (queue.length) {
|
|
223
|
+
const t = queue.pop();
|
|
224
|
+
if (seen.has(t.name) || t.name.startsWith("__"))
|
|
225
|
+
continue;
|
|
226
|
+
seen.add(t.name);
|
|
227
|
+
const own = typeOwner.get(t.name);
|
|
228
|
+
if (own !== undefined && own !== context)
|
|
229
|
+
continue;
|
|
230
|
+
if (own === undefined) {
|
|
231
|
+
const set = reachedBy.get(t.name) ?? new Set;
|
|
232
|
+
set.add(context);
|
|
233
|
+
reachedBy.set(t.name, set);
|
|
234
|
+
}
|
|
235
|
+
queue.push(...refs(t, context));
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
const contexts = new Map;
|
|
239
|
+
const add = (owner, t) => {
|
|
240
|
+
if (!t)
|
|
241
|
+
return;
|
|
242
|
+
const list = contexts.get(owner) ?? [];
|
|
243
|
+
list.push(t);
|
|
244
|
+
contexts.set(owner, list);
|
|
245
|
+
};
|
|
246
|
+
for (const [coordinate, owner] of Object.entries(owners)) {
|
|
247
|
+
const [typeName, fieldName] = coordinate.split(".");
|
|
248
|
+
const t = schema.getType(typeName);
|
|
249
|
+
if (!fieldName)
|
|
250
|
+
add(owner, t);
|
|
251
|
+
else if (isObjectType(t) || isInterfaceType(t) || isInputObjectType(t)) {
|
|
252
|
+
const f = t.getFields()[fieldName];
|
|
253
|
+
if (f) {
|
|
254
|
+
add(owner, getNamedType(f.type));
|
|
255
|
+
if ("args" in f)
|
|
256
|
+
for (const a of f.args)
|
|
257
|
+
add(owner, getNamedType(a.type));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
for (const [owner, start] of contexts)
|
|
262
|
+
walk(start, owner);
|
|
263
|
+
for (const [name, by] of reachedBy) {
|
|
264
|
+
if (typeOwner.has(name) || !name.startsWith("Flx") || by.size !== 1)
|
|
265
|
+
continue;
|
|
266
|
+
typeOwner.set(name, [...by][0]);
|
|
267
|
+
}
|
|
268
|
+
const defs = new Map;
|
|
269
|
+
const exts = new Map;
|
|
270
|
+
const put = (owner, d) => {
|
|
271
|
+
const list = defs.get(owner) ?? [];
|
|
272
|
+
list.push(d);
|
|
273
|
+
defs.set(owner, list);
|
|
274
|
+
};
|
|
275
|
+
for (const d of doc.definitions) {
|
|
276
|
+
if (!isTypeDefinition(d)) {
|
|
277
|
+
put("", d);
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
const owner = typeOwner.get(d.name.value) ?? "";
|
|
281
|
+
if (!hasFields(d) || !d.fields) {
|
|
282
|
+
put(owner, d);
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const mine = [];
|
|
286
|
+
for (const f of d.fields) {
|
|
287
|
+
const o = fieldOwner(d.name.value, f.name.value) ?? owner;
|
|
288
|
+
if (o === owner) {
|
|
289
|
+
mine.push(f);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const byType = exts.get(o) ?? new Map;
|
|
293
|
+
exts.set(o, byType);
|
|
294
|
+
const ext = byType.get(d.name.value) ?? { def: d, fields: [] };
|
|
295
|
+
ext.fields.push(f);
|
|
296
|
+
byType.set(d.name.value, ext);
|
|
297
|
+
}
|
|
298
|
+
put(owner, { ...d, fields: mine });
|
|
299
|
+
}
|
|
300
|
+
const owner_list = new Set(["", ...defs.keys(), ...exts.keys()]);
|
|
301
|
+
const parts = [];
|
|
302
|
+
for (const owner of owner_list) {
|
|
303
|
+
const own = [...defs.get(owner) ?? []].sort(byName);
|
|
304
|
+
const ext = [...exts.get(owner)?.values() ?? []].map((e) => extensionOf(e.def, e.fields)).sort(byName);
|
|
305
|
+
const body = print2({ kind: Kind2.DOCUMENT, definitions: [...own, ...ext] });
|
|
306
|
+
parts.push({ path: partPath(owner), body: header(owner) + body + `
|
|
307
|
+
` });
|
|
308
|
+
}
|
|
309
|
+
return parts.sort((a, b) => partOrder(a.path, b.path));
|
|
310
|
+
}
|
|
311
|
+
function extensionOf(d, fields) {
|
|
312
|
+
if (d.kind === Kind2.OBJECT_TYPE_DEFINITION)
|
|
313
|
+
return { kind: Kind2.OBJECT_TYPE_EXTENSION, name: d.name, fields };
|
|
314
|
+
if (d.kind === Kind2.INTERFACE_TYPE_DEFINITION)
|
|
315
|
+
return { kind: Kind2.INTERFACE_TYPE_EXTENSION, name: d.name, fields };
|
|
316
|
+
return { kind: Kind2.INPUT_OBJECT_TYPE_EXTENSION, name: d.name, fields };
|
|
317
|
+
}
|
|
318
|
+
function byName(a, b) {
|
|
319
|
+
const name = (d) => ("name" in d) && d.name ? d.name.value : "";
|
|
320
|
+
const rank = (d) => d.kind === Kind2.SCHEMA_DEFINITION ? 0 : d.kind === Kind2.DIRECTIVE_DEFINITION ? 1 : 2;
|
|
321
|
+
return rank(a) - rank(b) || (name(a) < name(b) ? -1 : name(a) > name(b) ? 1 : 0);
|
|
322
|
+
}
|
|
323
|
+
function partOrder(a, b) {
|
|
324
|
+
if (a === b)
|
|
325
|
+
return 0;
|
|
326
|
+
if (a === CORE_PART)
|
|
327
|
+
return -1;
|
|
328
|
+
if (b === CORE_PART)
|
|
329
|
+
return 1;
|
|
330
|
+
return a < b ? -1 : 1;
|
|
331
|
+
}
|
|
332
|
+
function assembleSchema(parts) {
|
|
333
|
+
if (!parts.length)
|
|
334
|
+
throw new Error("no schema parts found");
|
|
335
|
+
const docs = [...parts].sort((a, b) => partOrder(a.path, b.path)).map((p) => {
|
|
336
|
+
try {
|
|
337
|
+
return parse2(p.body, { noLocation: true });
|
|
338
|
+
} catch (e) {
|
|
339
|
+
throw new Error(`${p.path}: ${e.message}`);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
return lexicographicSortSchema(buildASTSchema(concatAST(docs)));
|
|
343
|
+
}
|
|
344
|
+
function assembleSdl(parts) {
|
|
345
|
+
return printSchema(assembleSchema(parts)) + `
|
|
346
|
+
`;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// packages/plugin-kit/src/codegen.ts
|
|
350
|
+
var HEADER2 = `// Generated by @fluxwp/plugin-kit (flx-kit codegen). Do not edit.
|
|
351
|
+
`;
|
|
352
|
+
function graphqlFiles(dir) {
|
|
353
|
+
if (!existsSync(dir))
|
|
354
|
+
return [];
|
|
355
|
+
return readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
|
|
356
|
+
const p = join(dir, e.name);
|
|
357
|
+
if (e.isDirectory())
|
|
358
|
+
return graphqlFiles(p);
|
|
359
|
+
return e.isFile() && e.name.endsWith(".graphql") ? [p] : [];
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
function defaultSchemaPath(from) {
|
|
363
|
+
for (let dir = resolve(from);; dir = dirname(dir)) {
|
|
364
|
+
const p = join(dir, "node_modules/@fluxwp/graphql/schema/full.graphql");
|
|
365
|
+
if (existsSync(p))
|
|
366
|
+
return p;
|
|
367
|
+
if (dirname(dir) === dir)
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
throw new Error("FluxWP schema not found: install @fluxwp/graphql or pass --schema <file>.");
|
|
371
|
+
}
|
|
372
|
+
async function codegen(o) {
|
|
373
|
+
const plugin = resolve(o.plugin);
|
|
374
|
+
const slug = basename(plugin);
|
|
375
|
+
const out = resolve(o.out ?? join(plugin, "screens/generated"));
|
|
376
|
+
const parts = [{ path: CORE_PART, body: readFileSync(o.schema ?? defaultSchemaPath(plugin), "utf8") }];
|
|
377
|
+
const own = join(plugin, "graphql/schema.graphql");
|
|
378
|
+
if (existsSync(own))
|
|
379
|
+
parts.push({ path: `plugins/${slug}/graphql/schema.graphql`, body: readFileSync(own, "utf8") });
|
|
380
|
+
const sdl = assembleSdl(parts);
|
|
381
|
+
const sources = graphqlFiles(join(plugin, "graphql/operations")).sort().map((path) => ({ plugin: slug, path: relative(plugin, path), body: readFileSync(path, "utf8") }));
|
|
382
|
+
const ops = collectOperations(buildSchema(sdl), sources);
|
|
383
|
+
const types = await gqlCodegen({
|
|
384
|
+
filename: "types.ts",
|
|
385
|
+
schema: parse3(sdl),
|
|
386
|
+
documents: ops.map((op) => ({ location: op.path, document: parse3(op.document) })),
|
|
387
|
+
plugins: [{ typescriptOperations: {} }],
|
|
388
|
+
pluginMap: { typescriptOperations },
|
|
389
|
+
config: {
|
|
390
|
+
onlyOperationTypes: true,
|
|
391
|
+
enumsAsTypes: true,
|
|
392
|
+
useTypeImports: true,
|
|
393
|
+
dedupeOperationSuffix: true,
|
|
394
|
+
avoidOptionals: { field: true, inputValue: false, object: true, defaultValue: false },
|
|
395
|
+
maybeValue: "T | null",
|
|
396
|
+
inputMaybeValue: "T | null | undefined",
|
|
397
|
+
strictScalars: true,
|
|
398
|
+
scalars: { ID: "string", FlxJSON: "unknown" },
|
|
399
|
+
defaultScalarType: "unknown",
|
|
400
|
+
skipTypename: false,
|
|
401
|
+
nonOptionalTypename: false
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
const opsTs = operationsModule(ops).replace(/^\/\/ Generated by .*\n/, HEADER2).replace("import type { FlxOperation } from '../operation';", "import type { FlxOperation } from '@fluxwp/graphql';");
|
|
405
|
+
const files = new Map([
|
|
406
|
+
[join(out, "types.ts"), HEADER2 + `/* eslint-disable */
|
|
407
|
+
` + types],
|
|
408
|
+
[join(out, "operations.ts"), opsTs]
|
|
409
|
+
]);
|
|
410
|
+
const manifest = manifests(ops).get(slug);
|
|
411
|
+
const manifestPath = join(plugin, "graphql/persisted-queries.json");
|
|
412
|
+
const result = { operations: ops.length, written: [], stale: [] };
|
|
413
|
+
if (manifest) {
|
|
414
|
+
const cur = existsSync(manifestPath) ? readFileSync(manifestPath, "utf8") : null;
|
|
415
|
+
if (cur !== manifest) {
|
|
416
|
+
if (o.check)
|
|
417
|
+
result.stale.push(manifestPath);
|
|
418
|
+
else
|
|
419
|
+
files.set(manifestPath, manifest);
|
|
420
|
+
}
|
|
421
|
+
} else if (existsSync(manifestPath)) {
|
|
422
|
+
result.stale.push(manifestPath);
|
|
423
|
+
}
|
|
424
|
+
for (const [path, body] of files) {
|
|
425
|
+
if (existsSync(path) && readFileSync(path, "utf8") === body)
|
|
426
|
+
continue;
|
|
427
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
428
|
+
writeFileSync(path, body);
|
|
429
|
+
result.written.push(path);
|
|
430
|
+
}
|
|
431
|
+
return result;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// packages/plugin-kit/bin/flx-kit.ts
|
|
435
|
+
function opt(args, name) {
|
|
436
|
+
const i = args.indexOf(name);
|
|
437
|
+
return i >= 0 ? args[i + 1] : undefined;
|
|
438
|
+
}
|
|
439
|
+
var [cmd, ...args] = process.argv.slice(2);
|
|
440
|
+
try {
|
|
441
|
+
if (cmd === "codegen" && args[0] && !args[0].startsWith("--")) {
|
|
442
|
+
const check = args.includes("--check");
|
|
443
|
+
const out = opt(args, "--out");
|
|
444
|
+
const schema = opt(args, "--schema");
|
|
445
|
+
const r = await codegen({ plugin: args[0], check, ...out ? { out } : {}, ...schema ? { schema } : {} });
|
|
446
|
+
for (const p of r.written)
|
|
447
|
+
console.log(`wrote ${relative2(process.cwd(), p)}`);
|
|
448
|
+
for (const p of r.stale)
|
|
449
|
+
console.error(`out of date: ${relative2(process.cwd(), p)}`);
|
|
450
|
+
console.log(`${r.operations} operations`);
|
|
451
|
+
if (check && r.stale.length) {
|
|
452
|
+
console.error("Run `flx-kit codegen` and commit the result.");
|
|
453
|
+
process.exit(1);
|
|
454
|
+
}
|
|
455
|
+
} else if (cmd === "schema-split" && args.length >= 3) {
|
|
456
|
+
const [schemaFile, ownersFile, pluginDir] = args;
|
|
457
|
+
const owner = `plugins/${basename2(resolve2(pluginDir))}`;
|
|
458
|
+
const owners = JSON.parse(readFileSync2(ownersFile, "utf8"));
|
|
459
|
+
const part = splitSchema(readFileSync2(schemaFile, "utf8"), owners).find((p) => p.path === `${owner}/graphql/schema.graphql`);
|
|
460
|
+
if (!part) {
|
|
461
|
+
console.error(`${owner} registers nothing in this schema (is the plugin active on that site?).`);
|
|
462
|
+
process.exit(1);
|
|
463
|
+
}
|
|
464
|
+
const dest = join2(resolve2(pluginDir), "graphql/schema.graphql");
|
|
465
|
+
mkdirSync2(join2(resolve2(pluginDir), "graphql"), { recursive: true });
|
|
466
|
+
writeFileSync2(dest, part.body);
|
|
467
|
+
console.log(`wrote ${relative2(process.cwd(), dest)}`);
|
|
468
|
+
} else {
|
|
469
|
+
console.error(`usage: flx-kit codegen <plugin dir> [--out <dir>] [--schema <file>] [--check]
|
|
470
|
+
flx-kit schema-split <schema.graphql> <owners.json> <plugin dir>`);
|
|
471
|
+
process.exit(2);
|
|
472
|
+
}
|
|
473
|
+
} catch (e) {
|
|
474
|
+
console.error(e.message);
|
|
475
|
+
process.exit(1);
|
|
476
|
+
}
|
package/dist/modules.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// packages/plugin-kit/src/modules.ts
|
|
2
|
+
var RUNTIME_MODULES = [
|
|
3
|
+
"react",
|
|
4
|
+
"react/jsx-runtime",
|
|
5
|
+
"react-dom",
|
|
6
|
+
"react-dom/client",
|
|
7
|
+
"@tanstack/react-query",
|
|
8
|
+
"i18next",
|
|
9
|
+
"react-i18next",
|
|
10
|
+
"@fluxwp/core",
|
|
11
|
+
"@fluxwp/tokens",
|
|
12
|
+
"@fluxwp/ui",
|
|
13
|
+
"@fluxwp/graphql",
|
|
14
|
+
"@fluxwp/graphql/react",
|
|
15
|
+
"@fluxwp/host"
|
|
16
|
+
];
|
|
17
|
+
export {
|
|
18
|
+
RUNTIME_MODULES
|
|
19
|
+
};
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// packages/plugin-kit/src/vite.ts
|
|
2
|
+
import { readFileSync, readdirSync, writeFileSync, existsSync, rmSync } from "node:fs";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { defineConfig } from "vite";
|
|
6
|
+
import react from "@vitejs/plugin-react";
|
|
7
|
+
import prefixer from "postcss-prefix-selector";
|
|
8
|
+
|
|
9
|
+
// packages/plugin-kit/src/modules.ts
|
|
10
|
+
var RUNTIME_MODULES = [
|
|
11
|
+
"react",
|
|
12
|
+
"react/jsx-runtime",
|
|
13
|
+
"react-dom",
|
|
14
|
+
"react-dom/client",
|
|
15
|
+
"@tanstack/react-query",
|
|
16
|
+
"i18next",
|
|
17
|
+
"react-i18next",
|
|
18
|
+
"@fluxwp/core",
|
|
19
|
+
"@fluxwp/tokens",
|
|
20
|
+
"@fluxwp/ui",
|
|
21
|
+
"@fluxwp/graphql",
|
|
22
|
+
"@fluxwp/graphql/react",
|
|
23
|
+
"@fluxwp/host"
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
// packages/plugin-kit/src/vite.ts
|
|
27
|
+
var kit = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
28
|
+
var EXPORTS = JSON.parse(readFileSync(join(kit, "runtime-exports.json"), "utf8"));
|
|
29
|
+
var SHIM = "\x00flux-runtime:";
|
|
30
|
+
var ENTRY = "\x00flux-screens-entry";
|
|
31
|
+
var RUNTIME_API = 1;
|
|
32
|
+
function runtimeKey(id) {
|
|
33
|
+
if (id === "@fluxwp/modules" || id === "@fluxwp/modules/src/host")
|
|
34
|
+
return "@fluxwp/host";
|
|
35
|
+
return RUNTIME_MODULES.includes(id) ? id : null;
|
|
36
|
+
}
|
|
37
|
+
function shim(key) {
|
|
38
|
+
const names = EXPORTS[key] ?? [];
|
|
39
|
+
const lines = [
|
|
40
|
+
`const rt = globalThis.FluxRuntime;`,
|
|
41
|
+
`if (!rt || rt.api < ${RUNTIME_API}) throw new Error('Flux GraphQL runtime (api ${RUNTIME_API}) is not loaded: update Flux GraphQL.');`,
|
|
42
|
+
`const m = rt.modules[${JSON.stringify(key)}];`,
|
|
43
|
+
`export default (m && 'default' in m) ? m.default : m;`,
|
|
44
|
+
...names.map((n) => `export const ${n} = m.${n};`)
|
|
45
|
+
];
|
|
46
|
+
return lines.join(`
|
|
47
|
+
`);
|
|
48
|
+
}
|
|
49
|
+
function runtimeModules() {
|
|
50
|
+
return {
|
|
51
|
+
name: "flux-runtime-modules",
|
|
52
|
+
enforce: "pre",
|
|
53
|
+
resolveId(id) {
|
|
54
|
+
const key = runtimeKey(id);
|
|
55
|
+
if (key)
|
|
56
|
+
return SHIM + key;
|
|
57
|
+
if (id.startsWith("@fluxwp/") && id.endsWith(".css"))
|
|
58
|
+
return SHIM + "css";
|
|
59
|
+
if (id.startsWith("@fluxwp/") && !id.startsWith("@fluxwp/plugin-kit")) {
|
|
60
|
+
this.error(`${id} is not part of the Flux runtime. Plugins may import: ${RUNTIME_MODULES.join(", ")}.`);
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
},
|
|
64
|
+
load(id) {
|
|
65
|
+
if (id === SHIM + "css")
|
|
66
|
+
return "export {};";
|
|
67
|
+
if (id.startsWith(SHIM))
|
|
68
|
+
return shim(id.slice(SHIM.length));
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function entryModule(entry) {
|
|
74
|
+
return {
|
|
75
|
+
name: "flux-screens-entry",
|
|
76
|
+
resolveId: (id) => id === ENTRY ? ENTRY : null,
|
|
77
|
+
load(id) {
|
|
78
|
+
if (id !== ENTRY)
|
|
79
|
+
return null;
|
|
80
|
+
const target = JSON.stringify(resolve(entry));
|
|
81
|
+
return [
|
|
82
|
+
`import * as mod from ${target};`,
|
|
83
|
+
`const rt = globalThis.FluxRuntime;`,
|
|
84
|
+
`if (!rt || rt.api < ${RUNTIME_API}) throw new Error('Flux GraphQL runtime (api ${RUNTIME_API}) is not loaded: update Flux GraphQL.');`,
|
|
85
|
+
`rt.register(mod.screens, { resources: mod.resources });`,
|
|
86
|
+
`export const screens = mod.screens;`
|
|
87
|
+
].join(`
|
|
88
|
+
`);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function manifest(outDir) {
|
|
93
|
+
return {
|
|
94
|
+
name: "flux-screens-manifest",
|
|
95
|
+
apply: "build",
|
|
96
|
+
closeBundle() {
|
|
97
|
+
const files = readdirSync(outDir).filter((f) => /^flux-screens.*\.(js|css)$/.test(f)).sort();
|
|
98
|
+
const json = { mode: "screens", api: RUNTIME_API, entry: "flux-screens.js", files };
|
|
99
|
+
writeFileSync(join(outDir, "chunks.json"), JSON.stringify(json, null, 2) + `
|
|
100
|
+
`);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function scopeCss(prefix, selector, prefixed) {
|
|
105
|
+
const state = /^((?:html|body)(?:\.[\w-]+)+)(?=$|[\s>+~,*])(.*)$/.exec(selector);
|
|
106
|
+
if (state)
|
|
107
|
+
return state[2].trim() ? `${state[1]} ${prefix} ${state[2].trim()}` : `${state[1]} ${prefix}`;
|
|
108
|
+
if (/^(html|body|:root|#root)\b/.test(selector))
|
|
109
|
+
return selector.replace(/^(html|body|:root|#root)(\.[\w-]+)*/, prefix);
|
|
110
|
+
if (selector.startsWith(".flux-root"))
|
|
111
|
+
return selector;
|
|
112
|
+
if (selector === "*" || selector.startsWith("*"))
|
|
113
|
+
return `${prefix} ${selector}, ${prefix}`;
|
|
114
|
+
return prefixed;
|
|
115
|
+
}
|
|
116
|
+
function fluxScreens(o) {
|
|
117
|
+
const outDir = resolve(o.outDir);
|
|
118
|
+
if (existsSync(outDir)) {
|
|
119
|
+
for (const f of readdirSync(outDir))
|
|
120
|
+
if (/^flux-screens.*\.(js|css)$/.test(f) || f === "chunks.json")
|
|
121
|
+
rmSync(join(outDir, f));
|
|
122
|
+
}
|
|
123
|
+
const base = defineConfig({
|
|
124
|
+
plugins: [runtimeModules(), entryModule(o.entry), react(), manifest(outDir)],
|
|
125
|
+
define: { "process.env.NODE_ENV": '"production"' },
|
|
126
|
+
css: { postcss: { plugins: [prefixer({ prefix: ".flux-root", transform: scopeCss })] } },
|
|
127
|
+
base: "./",
|
|
128
|
+
build: {
|
|
129
|
+
outDir,
|
|
130
|
+
emptyOutDir: false,
|
|
131
|
+
cssCodeSplit: false,
|
|
132
|
+
modulePreload: false,
|
|
133
|
+
assetsInlineLimit: 1e5,
|
|
134
|
+
target: "es2020",
|
|
135
|
+
rollupOptions: {
|
|
136
|
+
input: ENTRY,
|
|
137
|
+
preserveEntrySignatures: "strict",
|
|
138
|
+
output: {
|
|
139
|
+
format: "es",
|
|
140
|
+
entryFileNames: "flux-screens.js",
|
|
141
|
+
chunkFileNames: "flux-screens-[name]-[hash].js",
|
|
142
|
+
assetFileNames: "flux-screens[extname]"
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
return o.vite ? { ...base, ...o.vite, build: { ...base.build, ...o.vite.build } } : base;
|
|
148
|
+
}
|
|
149
|
+
export {
|
|
150
|
+
RUNTIME_API,
|
|
151
|
+
fluxScreens
|
|
152
|
+
};
|