rastack 0.0.50 → 0.0.52
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/CHANGELOG.md +4 -0
- package/auth/context.ts +73 -0
- package/auth/core.ts +293 -0
- package/auth/index.ts +23 -0
- package/components/auto-form/AutoForm.tsx +32 -12
- package/components/auto-form/use-auto-form.ts +75 -6
- package/components/data-table/DataTable.tsx +72 -45
- package/components/data-table/use-data-table.ts +57 -3
- package/components/index.ts +2 -0
- package/db.ts +9 -0
- package/dist/admin.js +13 -13
- package/dist/compile/analyze.d.ts +1 -7
- package/dist/compile/analyze.js +19 -70
- package/dist/compile/entities.js +163 -16
- package/dist/compile/model.d.ts +7 -3
- package/dist/compile/openapi.js +13 -3
- package/dist/compile/program.d.ts +2 -1
- package/dist/compile/program.js +7 -2
- package/dist/define/auth.d.ts +27 -0
- package/dist/define/auth.js +42 -0
- package/dist/define/db.d.ts +24 -1
- package/dist/define/db.js +1 -1
- package/dist/define/index.d.ts +82 -18
- package/dist/define/index.js +27 -21
- package/dist/define/manifest.d.ts +64 -0
- package/dist/define/manifest.js +259 -0
- package/dist/define/markers.d.ts +80 -0
- package/dist/define/markers.js +37 -0
- package/dist/define/types.d.ts +10 -0
- package/dist/define/types.js +26 -0
- package/dist/plugin/core.d.ts +108 -0
- package/dist/plugin/core.js +198 -0
- package/dist/plugin/index.d.ts +112 -0
- package/dist/plugin/index.js +203 -0
- package/dist/wasm/rastack_wasm.js +1 -1
- package/dist/wasm/rastack_wasm_bg.wasm +0 -0
- package/hooks/data.ts +18 -6
- package/hooks/entity.ts +11 -5
- package/hooks/form/entity-form.ts +12 -8
- package/hooks/manifest.ts +50 -14
- package/hooks/registry.ts +25 -8
- package/package.json +1 -1
- package/plugin.ts +8 -0
- package/provider/index.ts +1 -5
- package/provider/provider.tsx +61 -16
- package/provider/types.ts +26 -7
- package/provider/warehouse.ts +4 -3
- package/provider/wasm.ts +3 -1
- package/runtime.ts +1 -0
- package/src/compile/analyze.ts +27 -65
- package/src/compile/entities.ts +193 -19
- package/src/compile/model.ts +8 -9
- package/src/compile/openapi.ts +11 -3
- package/src/compile/program.ts +10 -3
- package/src/define/auth.ts +28 -0
- package/src/define/db.ts +26 -4
- package/src/define/index.ts +118 -24
- package/src/define/manifest.ts +287 -0
- package/src/define/markers.ts +83 -0
- package/src/define/types.ts +11 -0
- package/src/plugin/core.ts +236 -0
- package/src/plugin/index.ts +243 -0
- package/test/auth.spec.ts +207 -0
- package/test/compile.spec.ts +18 -7
- package/test/components.spec.ts +116 -0
- package/test/entities.spec.ts +123 -6
- package/test/plugin.spec.ts +315 -0
- package/test/runtime-manifest.spec.ts +309 -0
- package/test/typed-hooks.spec.ts +7 -2
- package/types.ts +7 -2
- package/wasm/rastack_wasm.js +1 -1
- package/wasm/rastack_wasm_bg.wasm +0 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Entity-level authorization markers — auth as *TypeScript types*.
|
|
4
|
+
*
|
|
5
|
+
* Class and interface entities have no options bag, so their access policy is
|
|
6
|
+
* expressed the same way their columns are: in the type system. Attach a
|
|
7
|
+
* marker with `implements` (classes) or `extends` (interfaces) and the
|
|
8
|
+
* compiler reads the policy straight off the heritage clause:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { AdminGroups, IsAuthenticated, Owner, Group } from "rastack/auth";
|
|
12
|
+
*
|
|
13
|
+
* // Per-user rows: RLS on `owner`, staff sees everything.
|
|
14
|
+
* export class Itinerary implements IsAuthenticated, AdminGroups<"staff"> {
|
|
15
|
+
* owner!: string & Owner;
|
|
16
|
+
* notes!: string;
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Group-owned dataset: rows belong to a team and are *physically stored*
|
|
20
|
+
* // under that team's own storage prefix (groups/{team}/…).
|
|
21
|
+
* export class Report implements IsAuthenticated, AdminGroups<"staff"> {
|
|
22
|
+
* team!: string & Group;
|
|
23
|
+
* title!: string;
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* The markers are phantom types: every property is optional and never exists
|
|
28
|
+
* at runtime, so `implements` needs no members and instances stay plain data.
|
|
29
|
+
* Each marker family uses its own phantom key, so markers combine freely —
|
|
30
|
+
* while two *permission* markers on one entity conflict in the type checker,
|
|
31
|
+
* which is exactly right.
|
|
32
|
+
*
|
|
33
|
+
* JSDoc equivalents exist for every marker (`@rastackPermission`,
|
|
34
|
+
* `@rastackAdminGroups staff, ops`, `@rastackScope owner|group|shared`, and
|
|
35
|
+
* field-level `@owner` / `@group`), mirroring the column-constraint tags.
|
|
36
|
+
*/
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `rastack/types` compatibility surface — one module aliasing the two
|
|
3
|
+
* split authoring subpaths: `rastack/db` (the `DbConfig` column brands) and
|
|
4
|
+
* `rastack/auth` (the entity-level auth markers + `Owner`/`Group` brands).
|
|
5
|
+
* Prefer those; everything stays re-exported here so existing imports keep
|
|
6
|
+
* working. The compiler maps all three specifiers (see `compile/program.ts`);
|
|
7
|
+
* the published package re-exports the same modules from `tools/types.ts`.
|
|
8
|
+
*/
|
|
9
|
+
export * from "./db";
|
|
10
|
+
export * from "./markers";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The `rastack/types` compatibility surface — one module aliasing the two
|
|
4
|
+
* split authoring subpaths: `rastack/db` (the `DbConfig` column brands) and
|
|
5
|
+
* `rastack/auth` (the entity-level auth markers + `Owner`/`Group` brands).
|
|
6
|
+
* Prefer those; everything stays re-exported here so existing imports keep
|
|
7
|
+
* working. The compiler maps all three specifiers (see `compile/program.ts`);
|
|
8
|
+
* the published package re-exports the same modules from `tools/types.ts`.
|
|
9
|
+
*/
|
|
10
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
13
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
14
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
17
|
+
}) : (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
o[k2] = m[k];
|
|
20
|
+
}));
|
|
21
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
22
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
__exportStar(require("./db"), exports);
|
|
26
|
+
__exportStar(require("./markers"), exports);
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dev-server schema compiler — the way around "classes need a compile
|
|
3
|
+
* step".
|
|
4
|
+
*
|
|
5
|
+
* Class/interface entities have no runtime shape (TypeScript erases their
|
|
6
|
+
* types), so *something* must run the `ts.Program` + `TypeChecker` to read
|
|
7
|
+
* fields, `DbConfig` brands and FK types off them. The insight is that it
|
|
8
|
+
* doesn't have to be a command the developer runs: this module packages the
|
|
9
|
+
* same `analyzeProject`/`compile` pass the CLI uses so it can live **inside
|
|
10
|
+
* the dev server that is already running** — a bundler plugin
|
|
11
|
+
* (`rastackPlugin`, Vite/Rollup-compatible) or a bare filesystem watcher
|
|
12
|
+
* (`startRastackWatcher`, for anything else). The schema recompiles on every
|
|
13
|
+
* save, the app imports it as the `virtual:rastack-manifest` module, and
|
|
14
|
+
* `rastack-env.d.ts` is rewritten so string-key row types stay fresh — the
|
|
15
|
+
* developer never runs `rastack compile`.
|
|
16
|
+
*
|
|
17
|
+
* Design mirrors the rest of `tools/src`: the decisions live here as pure
|
|
18
|
+
* (or thin-IO) functions, tested in `tools/test/plugin.spec.ts`; the plugin
|
|
19
|
+
* object in `./index.ts` is a shell over {@link createSchemaState}.
|
|
20
|
+
*/
|
|
21
|
+
import type { EntityDiagnostic, Manifest } from "../compile";
|
|
22
|
+
/** The module id apps import the live manifest from. */
|
|
23
|
+
export declare const VIRTUAL_MANIFEST_ID = "virtual:rastack-manifest";
|
|
24
|
+
/** The resolved (bundler-internal) id — Rollup convention: `\0` prefix. */
|
|
25
|
+
export declare const RESOLVED_MANIFEST_ID: string;
|
|
26
|
+
/**
|
|
27
|
+
* The auto-registration module the plugin injects into `index.html`: it sets
|
|
28
|
+
* `globalThis.__RASTACK_MANIFEST__` before the app boots, which is what lets
|
|
29
|
+
* `<RAStackProvider mode="local">` (and bare `useData(Airport)`) run with
|
|
30
|
+
* **no manifest wiring in app code at all**. Kept un-prefixed (no `\0`) so
|
|
31
|
+
* its dev-server URL is a plain `/@id/…`.
|
|
32
|
+
*/
|
|
33
|
+
export declare const VIRTUAL_REGISTER_ID = "virtual:rastack-manifest/register";
|
|
34
|
+
/** The key the register module assigns (mirrored in `rastack/hooks`). */
|
|
35
|
+
export declare const GLOBAL_MANIFEST_KEY = "__RASTACK_MANIFEST__";
|
|
36
|
+
export interface RastackPluginOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Directory (or single file) scanned for entities — classes, interfaces
|
|
39
|
+
* and `resource()` calls alike, recursively (co-located definitions work).
|
|
40
|
+
* Default `"resources"`; pass `"src"` (or `"."`) for co-located entities.
|
|
41
|
+
*/
|
|
42
|
+
resourcesDir?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Where the artifacts land: `schema.rastack.json`, `openapi.json`,
|
|
45
|
+
* `rastack-env.d.ts` (the string-key type registry) and
|
|
46
|
+
* `rastack-plugin.d.ts` (types for the virtual module). Default
|
|
47
|
+
* `".rastack"` — include it in `tsconfig.json` once.
|
|
48
|
+
*/
|
|
49
|
+
outDir?: string;
|
|
50
|
+
/** Skip writing the artifacts to disk (virtual module only). Default false. */
|
|
51
|
+
emitFiles?: boolean;
|
|
52
|
+
/** Where compile problems are reported. Default `console.error`. */
|
|
53
|
+
onError?: (error: unknown) => void;
|
|
54
|
+
}
|
|
55
|
+
export interface ResolvedPluginOptions {
|
|
56
|
+
resourcesDir: string;
|
|
57
|
+
outDir: string;
|
|
58
|
+
emitFiles: boolean;
|
|
59
|
+
onError: (error: unknown) => void;
|
|
60
|
+
}
|
|
61
|
+
export declare function resolveOptions(options?: RastackPluginOptions, root?: string): ResolvedPluginOptions;
|
|
62
|
+
/**
|
|
63
|
+
* Whether a changed file should trigger a schema recompile: a `.ts`/`.tsx`
|
|
64
|
+
* source under the scanned directory that is not itself an emitted artifact.
|
|
65
|
+
*/
|
|
66
|
+
export declare function isEntitySource(file: string, options: Pick<ResolvedPluginOptions, "resourcesDir" | "outDir">): boolean;
|
|
67
|
+
/** The virtual module's source — the manifest as a plain ES module. */
|
|
68
|
+
export declare function manifestModuleCode(manifest: Manifest): string;
|
|
69
|
+
/**
|
|
70
|
+
* The register module's source: import the live manifest, publish it on the
|
|
71
|
+
* global the runtime falls back to. A separate module (rather than a side
|
|
72
|
+
* effect of the manifest module) so explicit `virtual:rastack-manifest`
|
|
73
|
+
* importers stay side-effect-free.
|
|
74
|
+
*/
|
|
75
|
+
export declare function registerModuleCode(): string;
|
|
76
|
+
/** Types for the virtual module, written once beside `rastack-env.d.ts`. */
|
|
77
|
+
export declare function virtualModuleDts(): string;
|
|
78
|
+
export interface SchemaState {
|
|
79
|
+
/**
|
|
80
|
+
* The virtual module's current source. Compiles (lazily) when stale; on a
|
|
81
|
+
* compile error reports via `onError` and keeps serving the last good
|
|
82
|
+
* manifest — a half-typed entity must not blank the running app.
|
|
83
|
+
*/
|
|
84
|
+
code(): string;
|
|
85
|
+
/** The current manifest (same laziness/fallback as {@link code}). */
|
|
86
|
+
manifest(): Manifest;
|
|
87
|
+
/** Mark the schema stale — the next `code()`/`manifest()` recompiles. */
|
|
88
|
+
invalidate(): void;
|
|
89
|
+
/** The error from the most recent failed compile, if it is still failing. */
|
|
90
|
+
error(): unknown;
|
|
91
|
+
/** Non-error diagnostics from the last successful compile. */
|
|
92
|
+
diagnostics(): EntityDiagnostic[];
|
|
93
|
+
options: ResolvedPluginOptions;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The plugin's whole brain: lazy compile, invalidation, keep-last-good.
|
|
97
|
+
* Both the bundler plugin and the bare watcher drive this one object.
|
|
98
|
+
*/
|
|
99
|
+
export declare function createSchemaState(options?: RastackPluginOptions, root?: string): SchemaState;
|
|
100
|
+
/**
|
|
101
|
+
* One compile pass: the same artifacts `rastack compile` writes (manifest,
|
|
102
|
+
* OpenAPI, `rastack-env.d.ts`), plus the virtual module's type declaration.
|
|
103
|
+
* With `emitFiles: false` it only analyses (nothing touches disk).
|
|
104
|
+
*/
|
|
105
|
+
export declare function runCompile(options: ResolvedPluginOptions): {
|
|
106
|
+
manifest: Manifest;
|
|
107
|
+
diagnostics: EntityDiagnostic[];
|
|
108
|
+
};
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The dev-server schema compiler — the way around "classes need a compile
|
|
4
|
+
* step".
|
|
5
|
+
*
|
|
6
|
+
* Class/interface entities have no runtime shape (TypeScript erases their
|
|
7
|
+
* types), so *something* must run the `ts.Program` + `TypeChecker` to read
|
|
8
|
+
* fields, `DbConfig` brands and FK types off them. The insight is that it
|
|
9
|
+
* doesn't have to be a command the developer runs: this module packages the
|
|
10
|
+
* same `analyzeProject`/`compile` pass the CLI uses so it can live **inside
|
|
11
|
+
* the dev server that is already running** — a bundler plugin
|
|
12
|
+
* (`rastackPlugin`, Vite/Rollup-compatible) or a bare filesystem watcher
|
|
13
|
+
* (`startRastackWatcher`, for anything else). The schema recompiles on every
|
|
14
|
+
* save, the app imports it as the `virtual:rastack-manifest` module, and
|
|
15
|
+
* `rastack-env.d.ts` is rewritten so string-key row types stay fresh — the
|
|
16
|
+
* developer never runs `rastack compile`.
|
|
17
|
+
*
|
|
18
|
+
* Design mirrors the rest of `tools/src`: the decisions live here as pure
|
|
19
|
+
* (or thin-IO) functions, tested in `tools/test/plugin.spec.ts`; the plugin
|
|
20
|
+
* object in `./index.ts` is a shell over {@link createSchemaState}.
|
|
21
|
+
*/
|
|
22
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
23
|
+
if (k2 === undefined) k2 = k;
|
|
24
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
25
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
26
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
27
|
+
}
|
|
28
|
+
Object.defineProperty(o, k2, desc);
|
|
29
|
+
}) : (function(o, m, k, k2) {
|
|
30
|
+
if (k2 === undefined) k2 = k;
|
|
31
|
+
o[k2] = m[k];
|
|
32
|
+
}));
|
|
33
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
34
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
35
|
+
}) : function(o, v) {
|
|
36
|
+
o["default"] = v;
|
|
37
|
+
});
|
|
38
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
39
|
+
var ownKeys = function(o) {
|
|
40
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
41
|
+
var ar = [];
|
|
42
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
43
|
+
return ar;
|
|
44
|
+
};
|
|
45
|
+
return ownKeys(o);
|
|
46
|
+
};
|
|
47
|
+
return function (mod) {
|
|
48
|
+
if (mod && mod.__esModule) return mod;
|
|
49
|
+
var result = {};
|
|
50
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
51
|
+
__setModuleDefault(result, mod);
|
|
52
|
+
return result;
|
|
53
|
+
};
|
|
54
|
+
})();
|
|
55
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
56
|
+
exports.GLOBAL_MANIFEST_KEY = exports.VIRTUAL_REGISTER_ID = exports.RESOLVED_MANIFEST_ID = exports.VIRTUAL_MANIFEST_ID = void 0;
|
|
57
|
+
exports.resolveOptions = resolveOptions;
|
|
58
|
+
exports.isEntitySource = isEntitySource;
|
|
59
|
+
exports.manifestModuleCode = manifestModuleCode;
|
|
60
|
+
exports.registerModuleCode = registerModuleCode;
|
|
61
|
+
exports.virtualModuleDts = virtualModuleDts;
|
|
62
|
+
exports.createSchemaState = createSchemaState;
|
|
63
|
+
exports.runCompile = runCompile;
|
|
64
|
+
const fs = __importStar(require("fs"));
|
|
65
|
+
const path = __importStar(require("path"));
|
|
66
|
+
const compile_1 = require("../compile");
|
|
67
|
+
/** The module id apps import the live manifest from. */
|
|
68
|
+
exports.VIRTUAL_MANIFEST_ID = "virtual:rastack-manifest";
|
|
69
|
+
/** The resolved (bundler-internal) id — Rollup convention: `\0` prefix. */
|
|
70
|
+
exports.RESOLVED_MANIFEST_ID = "\0" + exports.VIRTUAL_MANIFEST_ID;
|
|
71
|
+
/**
|
|
72
|
+
* The auto-registration module the plugin injects into `index.html`: it sets
|
|
73
|
+
* `globalThis.__RASTACK_MANIFEST__` before the app boots, which is what lets
|
|
74
|
+
* `<RAStackProvider mode="local">` (and bare `useData(Airport)`) run with
|
|
75
|
+
* **no manifest wiring in app code at all**. Kept un-prefixed (no `\0`) so
|
|
76
|
+
* its dev-server URL is a plain `/@id/…`.
|
|
77
|
+
*/
|
|
78
|
+
exports.VIRTUAL_REGISTER_ID = "virtual:rastack-manifest/register";
|
|
79
|
+
/** The key the register module assigns (mirrored in `rastack/hooks`). */
|
|
80
|
+
exports.GLOBAL_MANIFEST_KEY = "__RASTACK_MANIFEST__";
|
|
81
|
+
function resolveOptions(options = {}, root = process.cwd()) {
|
|
82
|
+
return {
|
|
83
|
+
resourcesDir: path.resolve(root, options.resourcesDir ?? "resources"),
|
|
84
|
+
outDir: path.resolve(root, options.outDir ?? ".rastack"),
|
|
85
|
+
emitFiles: options.emitFiles !== false,
|
|
86
|
+
onError: options.onError ??
|
|
87
|
+
((error) => console.error(`[rastack] schema compile failed — serving the last good manifest\n` +
|
|
88
|
+
`${error instanceof Error ? error.message : String(error)}`)),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Whether a changed file should trigger a schema recompile: a `.ts`/`.tsx`
|
|
93
|
+
* source under the scanned directory that is not itself an emitted artifact.
|
|
94
|
+
*/
|
|
95
|
+
function isEntitySource(file, options) {
|
|
96
|
+
const resolved = path.resolve(file);
|
|
97
|
+
if (!/\.tsx?$/.test(resolved) || resolved.endsWith(".d.ts"))
|
|
98
|
+
return false;
|
|
99
|
+
if (within(resolved, options.outDir))
|
|
100
|
+
return false;
|
|
101
|
+
return (within(resolved, options.resourcesDir) || resolved === options.resourcesDir);
|
|
102
|
+
}
|
|
103
|
+
function within(file, dir) {
|
|
104
|
+
const rel = path.relative(dir, file);
|
|
105
|
+
return rel !== "" && !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
106
|
+
}
|
|
107
|
+
/** The virtual module's source — the manifest as a plain ES module. */
|
|
108
|
+
function manifestModuleCode(manifest) {
|
|
109
|
+
const json = JSON.stringify(manifest);
|
|
110
|
+
return (`// virtual:rastack-manifest — compiled by the rastack dev-server plugin.\n` +
|
|
111
|
+
`const manifest = ${json};\n` +
|
|
112
|
+
`export default manifest;\n` +
|
|
113
|
+
`export { manifest };\n`);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The register module's source: import the live manifest, publish it on the
|
|
117
|
+
* global the runtime falls back to. A separate module (rather than a side
|
|
118
|
+
* effect of the manifest module) so explicit `virtual:rastack-manifest`
|
|
119
|
+
* importers stay side-effect-free.
|
|
120
|
+
*/
|
|
121
|
+
function registerModuleCode() {
|
|
122
|
+
return (`// ${exports.VIRTUAL_REGISTER_ID} — auto-injected by the rastack plugin.\n` +
|
|
123
|
+
`import manifest from ${JSON.stringify(exports.VIRTUAL_MANIFEST_ID)};\n` +
|
|
124
|
+
`globalThis.${exports.GLOBAL_MANIFEST_KEY} = manifest;\n` +
|
|
125
|
+
`export default manifest;\n`);
|
|
126
|
+
}
|
|
127
|
+
/** Types for the virtual module, written once beside `rastack-env.d.ts`. */
|
|
128
|
+
function virtualModuleDts() {
|
|
129
|
+
return ("// Generated by the rastack plugin — do not edit.\n" +
|
|
130
|
+
`declare module "${exports.VIRTUAL_MANIFEST_ID}" {\n` +
|
|
131
|
+
' const manifest: import("rastack/define").Manifest;\n' +
|
|
132
|
+
" export default manifest;\n" +
|
|
133
|
+
" export { manifest };\n" +
|
|
134
|
+
"}\n");
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The plugin's whole brain: lazy compile, invalidation, keep-last-good.
|
|
138
|
+
* Both the bundler plugin and the bare watcher drive this one object.
|
|
139
|
+
*/
|
|
140
|
+
function createSchemaState(options = {}, root) {
|
|
141
|
+
const resolved = resolveOptions(options, root);
|
|
142
|
+
let current = null;
|
|
143
|
+
let stale = true;
|
|
144
|
+
let lastError = undefined;
|
|
145
|
+
const recompile = () => {
|
|
146
|
+
try {
|
|
147
|
+
const result = runCompile(resolved);
|
|
148
|
+
current = { manifest: result.manifest, diagnostics: result.diagnostics };
|
|
149
|
+
lastError = undefined;
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
lastError = error;
|
|
153
|
+
resolved.onError(error);
|
|
154
|
+
if (!current)
|
|
155
|
+
throw error; // no last-good manifest to fall back to
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
stale = false;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const ensure = () => {
|
|
162
|
+
if (stale || !current)
|
|
163
|
+
recompile();
|
|
164
|
+
return current;
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
code: () => manifestModuleCode(ensure().manifest),
|
|
168
|
+
manifest: () => ensure().manifest,
|
|
169
|
+
invalidate: () => {
|
|
170
|
+
stale = true;
|
|
171
|
+
},
|
|
172
|
+
error: () => lastError,
|
|
173
|
+
diagnostics: () => ensure().diagnostics,
|
|
174
|
+
options: resolved,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* One compile pass: the same artifacts `rastack compile` writes (manifest,
|
|
179
|
+
* OpenAPI, `rastack-env.d.ts`), plus the virtual module's type declaration.
|
|
180
|
+
* With `emitFiles: false` it only analyses (nothing touches disk).
|
|
181
|
+
*/
|
|
182
|
+
function runCompile(options) {
|
|
183
|
+
if (!options.emitFiles) {
|
|
184
|
+
// compile() always writes to disk; the analyse-only path applies the
|
|
185
|
+
// same error gate without touching the filesystem.
|
|
186
|
+
const { resources, diagnostics } = (0, compile_1.analyzeProject)(options.resourcesDir);
|
|
187
|
+
const errors = diagnostics.filter((d) => d.severity === "error");
|
|
188
|
+
if (errors.length)
|
|
189
|
+
throw new Error(errors.map((e) => e.message).join("\n"));
|
|
190
|
+
return { manifest: (0, compile_1.buildManifest)(resources), diagnostics };
|
|
191
|
+
}
|
|
192
|
+
const result = (0, compile_1.compile)(options.resourcesDir, options.outDir);
|
|
193
|
+
const dtsPath = path.join(options.outDir, "rastack-plugin.d.ts");
|
|
194
|
+
if (!fs.existsSync(dtsPath)) {
|
|
195
|
+
fs.writeFileSync(dtsPath, virtualModuleDts());
|
|
196
|
+
}
|
|
197
|
+
return { manifest: result.manifest, diagnostics: result.diagnostics };
|
|
198
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `rastack/plugin` — the schema compiles itself inside your dev server.
|
|
3
|
+
*
|
|
4
|
+
* Class and interface entities are pure types, so the manifest has to be
|
|
5
|
+
* derived by the TypeScript checker — but not by a command you run. Add the
|
|
6
|
+
* plugin to the bundler you already use and the whole `rastack compile`
|
|
7
|
+
* pass disappears into the dev loop:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* // vite.config.ts
|
|
11
|
+
* import { rastackPlugin } from "rastack/plugin";
|
|
12
|
+
* export default defineConfig({
|
|
13
|
+
* plugins: [rastackPlugin({ resourcesDir: "src/entities" })],
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ```tsx
|
|
18
|
+
* // app — the manifest is a live import, recompiled on every save
|
|
19
|
+
* import manifest from "virtual:rastack-manifest";
|
|
20
|
+
* <RAStackProvider mode="local" manifest={manifest}>…</RAStackProvider>
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* What it does on your behalf, on every relevant file save:
|
|
24
|
+
* - recompiles the entity graph (classes, interfaces, `resource()` calls);
|
|
25
|
+
* - serves the manifest as the `virtual:rastack-manifest` module and
|
|
26
|
+
* hot-invalidates it;
|
|
27
|
+
* - rewrites `.rastack/rastack-env.d.ts` (string-key row types) and
|
|
28
|
+
* `schema.rastack.json`/`openapi.json`, so the committed artifacts and the
|
|
29
|
+
* registry never go stale;
|
|
30
|
+
* - on a broken in-progress edit, reports the error and keeps serving the
|
|
31
|
+
* last good manifest.
|
|
32
|
+
*
|
|
33
|
+
* The plugin object is Vite/Rollup-compatible (Vite-specific hooks are
|
|
34
|
+
* ignored by plain Rollup). For dev servers with other plugin systems
|
|
35
|
+
* (Next/webpack, Metro/Expo), `startRastackWatcher` does the same
|
|
36
|
+
* recompile-on-save against the filesystem — call it once from your config —
|
|
37
|
+
* and `rastack dev` remains the zero-config path.
|
|
38
|
+
*/
|
|
39
|
+
import { type RastackPluginOptions, type SchemaState } from "./core";
|
|
40
|
+
export { createSchemaState, GLOBAL_MANIFEST_KEY, isEntitySource, manifestModuleCode, registerModuleCode, resolveOptions, RESOLVED_MANIFEST_ID, runCompile, VIRTUAL_MANIFEST_ID, VIRTUAL_REGISTER_ID, virtualModuleDts, } from "./core";
|
|
41
|
+
export type { RastackPluginOptions, ResolvedPluginOptions, SchemaState, } from "./core";
|
|
42
|
+
/** One HTML tag descriptor in Vite's `transformIndexHtml` shape. */
|
|
43
|
+
export interface HtmlTagDescriptor {
|
|
44
|
+
tag: string;
|
|
45
|
+
attrs?: Record<string, string>;
|
|
46
|
+
children?: string;
|
|
47
|
+
injectTo?: "head" | "head-prepend" | "body" | "body-prepend";
|
|
48
|
+
}
|
|
49
|
+
/** The minimal bundler-plugin surface we emit (Vite superset of Rollup). */
|
|
50
|
+
export interface RastackBundlerPlugin {
|
|
51
|
+
name: string;
|
|
52
|
+
buildStart(): void;
|
|
53
|
+
resolveId(id: string): string | undefined;
|
|
54
|
+
load(id: string): string | undefined;
|
|
55
|
+
watchChange(id: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* Vite-only: auto-register the schema. Injects a module script that sets
|
|
58
|
+
* `globalThis.__RASTACK_MANIFEST__` before the app boots, so the provider
|
|
59
|
+
* and hooks need no `manifest` wiring at all. Object form with
|
|
60
|
+
* `order: "pre"` — the tag must be injected *before* Vite scans the HTML
|
|
61
|
+
* entry, or the build ships the virtual import verbatim.
|
|
62
|
+
*/
|
|
63
|
+
transformIndexHtml: {
|
|
64
|
+
order: "pre";
|
|
65
|
+
handler(html: string, ctx?: {
|
|
66
|
+
server?: unknown;
|
|
67
|
+
}): HtmlTagDescriptor[];
|
|
68
|
+
};
|
|
69
|
+
/** Vite-only: recompile + hot-invalidate the virtual manifest module. */
|
|
70
|
+
handleHotUpdate(ctx: {
|
|
71
|
+
file: string;
|
|
72
|
+
server: {
|
|
73
|
+
moduleGraph: {
|
|
74
|
+
getModuleById(id: string): unknown;
|
|
75
|
+
invalidateModule(mod: any): void;
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
modules: unknown[];
|
|
79
|
+
}): unknown[] | void;
|
|
80
|
+
/** The live schema state — exposed for tests and advanced callers. */
|
|
81
|
+
api: SchemaState;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The bundler plugin. Works as-is in Vite and Rollup; the returned object is
|
|
85
|
+
* plain data + closures, so any bundler with resolve/load/watch hooks can
|
|
86
|
+
* adapt it.
|
|
87
|
+
*/
|
|
88
|
+
export declare function rastackPlugin(options?: RastackPluginOptions): RastackBundlerPlugin;
|
|
89
|
+
/** Alias so `import { rastack } from "rastack/plugin"` reads naturally. */
|
|
90
|
+
export declare const rastack: typeof rastackPlugin;
|
|
91
|
+
export interface RastackWatcher {
|
|
92
|
+
/** Stop watching. */
|
|
93
|
+
close(): void;
|
|
94
|
+
/** The live schema state (same object the recompiles flow through). */
|
|
95
|
+
state: SchemaState;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The plugin for everything that isn't Rollup-shaped: watch the resources
|
|
99
|
+
* directory and rewrite the artifacts (`schema.rastack.json`,
|
|
100
|
+
* `openapi.json`, `rastack-env.d.ts`) on every save. Call once from any
|
|
101
|
+
* Node-side config (next.config.js, metro.config.js, a dev script):
|
|
102
|
+
*
|
|
103
|
+
* ```js
|
|
104
|
+
* // next.config.js
|
|
105
|
+
* const { startRastackWatcher } = require("rastack/plugin");
|
|
106
|
+
* if (process.env.NODE_ENV === "development") startRastackWatcher();
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* The app then imports `.rastack/schema.rastack.json` directly (a plain JSON
|
|
110
|
+
* import every bundler hot-reloads) instead of the virtual module.
|
|
111
|
+
*/
|
|
112
|
+
export declare function startRastackWatcher(options?: RastackPluginOptions): RastackWatcher;
|