@eventusgo/vite-plugin 0.1.0

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/dist/client.js ADDED
File without changes
@@ -0,0 +1,27 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ declare const EVENTUS_MANIFEST_FILE = "eventus.manifest.json";
4
+ declare const EVENTUS_VIRTUAL_MANIFEST_ID = "virtual:eventus-manifest";
5
+ declare const EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID = "\0virtual:eventus-manifest";
6
+ declare const EVENTUS_APP_ID_PATTERN: RegExp;
7
+ type EventusVitePluginOptions = {
8
+ manifestPath?: string;
9
+ };
10
+ type EventusManifest = {
11
+ name: string;
12
+ appId: string;
13
+ version: string;
14
+ permissions?: string[];
15
+ };
16
+ declare class EventusManifestError extends Error {
17
+ constructor(message: string);
18
+ }
19
+ declare function resolveManifestPath(root: string, manifestPath?: string): string;
20
+ declare function readEventusManifest(root: string, manifestPath?: string): {
21
+ filePath: string;
22
+ manifest: EventusManifest;
23
+ };
24
+ declare function validateEventusManifest(value: unknown, filePath?: string): EventusManifest;
25
+ declare function eventusVitePlugin(options?: EventusVitePluginOptions): Plugin;
26
+
27
+ export { EVENTUS_APP_ID_PATTERN, EVENTUS_MANIFEST_FILE, EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID, EVENTUS_VIRTUAL_MANIFEST_ID, type EventusManifest, EventusManifestError, type EventusVitePluginOptions, eventusVitePlugin, readEventusManifest, resolveManifestPath, validateEventusManifest };
package/dist/index.js ADDED
@@ -0,0 +1,150 @@
1
+ // src/index.ts
2
+ import { existsSync, readFileSync } from "fs";
3
+ import path from "path";
4
+ var EVENTUS_MANIFEST_FILE = "eventus.manifest.json";
5
+ var EVENTUS_VIRTUAL_MANIFEST_ID = "virtual:eventus-manifest";
6
+ var EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID = "\0virtual:eventus-manifest";
7
+ var EVENTUS_APP_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
8
+ var EventusManifestError = class extends Error {
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = "EventusManifestError";
12
+ }
13
+ };
14
+ function resolveManifestPath(root, manifestPath) {
15
+ return path.resolve(root, manifestPath ?? EVENTUS_MANIFEST_FILE);
16
+ }
17
+ function readEventusManifest(root, manifestPath) {
18
+ const filePath = resolveManifestPath(root, manifestPath);
19
+ if (!existsSync(filePath)) {
20
+ throw new EventusManifestError(
21
+ `Missing Eventus manifest: expected ${path.basename(filePath)} at ${filePath}.`
22
+ );
23
+ }
24
+ let parsed;
25
+ try {
26
+ parsed = JSON.parse(readFileSync(filePath, "utf8"));
27
+ } catch (error) {
28
+ throw new EventusManifestError(
29
+ `Invalid JSON in ${filePath}: ${error.message}`
30
+ );
31
+ }
32
+ return {
33
+ filePath,
34
+ manifest: validateEventusManifest(parsed, filePath)
35
+ };
36
+ }
37
+ function validateEventusManifest(value, filePath = EVENTUS_MANIFEST_FILE) {
38
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
39
+ throw new EventusManifestError(
40
+ `Invalid Eventus manifest in ${filePath}: expected a JSON object.`
41
+ );
42
+ }
43
+ const manifest = value;
44
+ const name = readRequiredString(manifest, "name", filePath);
45
+ const appId = readRequiredString(manifest, "appId", filePath);
46
+ const version = readRequiredString(manifest, "version", filePath);
47
+ if (!EVENTUS_APP_ID_PATTERN.test(appId)) {
48
+ throw new EventusManifestError(
49
+ `Invalid Eventus manifest in ${filePath}: "appId" must use lowercase letters, numbers, and hyphens.`
50
+ );
51
+ }
52
+ const permissions = manifest.permissions;
53
+ if (permissions !== void 0 && (!Array.isArray(permissions) || permissions.some((permission) => typeof permission !== "string"))) {
54
+ throw new EventusManifestError(
55
+ `Invalid Eventus manifest in ${filePath}: "permissions" must be an array of strings.`
56
+ );
57
+ }
58
+ return {
59
+ name,
60
+ appId,
61
+ version,
62
+ ...permissions ? { permissions } : {}
63
+ };
64
+ }
65
+ function readRequiredString(manifest, key, filePath) {
66
+ const value = manifest[key];
67
+ if (typeof value !== "string" || !value.trim()) {
68
+ throw new EventusManifestError(
69
+ `Invalid Eventus manifest in ${filePath}: "${key}" is required and must be a non-empty string.`
70
+ );
71
+ }
72
+ return value;
73
+ }
74
+ function eventusVitePlugin(options = {}) {
75
+ let rootDir = process.cwd();
76
+ let manifestFile = resolveManifestPath(rootDir, options.manifestPath);
77
+ let manifest;
78
+ const loadManifest = () => {
79
+ const result = readEventusManifest(rootDir, options.manifestPath);
80
+ manifestFile = result.filePath;
81
+ manifest = result.manifest;
82
+ return result.manifest;
83
+ };
84
+ const createManifestInjectionTag = () => ({
85
+ tag: "script",
86
+ attrs: {
87
+ type: "application/javascript"
88
+ },
89
+ injectTo: "head",
90
+ children: `globalThis.__EVENTUS_MANIFEST__ = ${JSON.stringify(
91
+ manifest ?? loadManifest()
92
+ )};`
93
+ });
94
+ return {
95
+ name: "eventus:vite-plugin",
96
+ enforce: "pre",
97
+ configResolved(config) {
98
+ rootDir = config.root;
99
+ loadManifest();
100
+ },
101
+ buildStart() {
102
+ loadManifest();
103
+ },
104
+ resolveId(id) {
105
+ if (id === EVENTUS_VIRTUAL_MANIFEST_ID) {
106
+ return EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID;
107
+ }
108
+ return void 0;
109
+ },
110
+ load(id) {
111
+ if (id === EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID) {
112
+ return `export default ${JSON.stringify(manifest ?? loadManifest(), null, 2)};`;
113
+ }
114
+ return void 0;
115
+ },
116
+ transformIndexHtml() {
117
+ return [createManifestInjectionTag()];
118
+ },
119
+ handleHotUpdate(context) {
120
+ if (path.resolve(context.file) !== manifestFile) {
121
+ return;
122
+ }
123
+ try {
124
+ loadManifest();
125
+ const module = context.server.moduleGraph.getModuleById(
126
+ EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID
127
+ );
128
+ if (module) {
129
+ context.server.moduleGraph.invalidateModule(module);
130
+ }
131
+ context.server.ws.send({ type: "full-reload" });
132
+ } catch (error) {
133
+ manifest = void 0;
134
+ context.server.config.logger.error(error.message);
135
+ }
136
+ return [];
137
+ }
138
+ };
139
+ }
140
+ export {
141
+ EVENTUS_APP_ID_PATTERN,
142
+ EVENTUS_MANIFEST_FILE,
143
+ EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID,
144
+ EVENTUS_VIRTUAL_MANIFEST_ID,
145
+ EventusManifestError,
146
+ eventusVitePlugin,
147
+ readEventusManifest,
148
+ resolveManifestPath,
149
+ validateEventusManifest
150
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@eventusgo/vite-plugin",
3
+ "version": "0.1.0",
4
+ "description": "Vite plugin for Eventus mini-app manifests.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js"
16
+ },
17
+ "./client": {
18
+ "types": "./src/client.d.ts",
19
+ "import": "./dist/client.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "src/client.d.ts"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsup",
28
+ "check": "tsc -p tsconfig.json --noEmit",
29
+ "test": "vitest run"
30
+ },
31
+ "peerDependencies": {
32
+ "vite": "^5.0.0"
33
+ }
34
+ }
@@ -0,0 +1,17 @@
1
+ type EventusManifest = {
2
+ name: string;
3
+ appId: string;
4
+ version: string;
5
+ permissions?: string[];
6
+ };
7
+
8
+ declare module "virtual:eventus-manifest" {
9
+ const manifest: EventusManifest;
10
+ export default manifest;
11
+ }
12
+
13
+ declare global {
14
+ const __EVENTUS_MANIFEST__: EventusManifest;
15
+ }
16
+
17
+ export {};