@eventusgo/vite-plugin 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/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import { Plugin } from 'vite';
3
3
  declare const EVENTUS_MANIFEST_FILE = "eventus.manifest.json";
4
4
  declare const EVENTUS_VIRTUAL_MANIFEST_ID = "virtual:eventus-manifest";
5
5
  declare const EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID = "\0virtual:eventus-manifest";
6
+ declare const EVENTUS_DEV_PREVIEW_PATH = "/__eventus__/preview.json";
6
7
  declare const EVENTUS_APP_ID_PATTERN: RegExp;
7
8
  type EventusVitePluginOptions = {
8
9
  manifestPath?: string;
@@ -13,6 +14,16 @@ type EventusManifest = {
13
14
  version: string;
14
15
  permissions?: string[];
15
16
  };
17
+ type EventusDevPreviewDescriptor = {
18
+ type: "eventus-dev-preview";
19
+ version: 1;
20
+ manifest: EventusManifest;
21
+ urls: {
22
+ local: string;
23
+ network: string | null;
24
+ preview: string;
25
+ };
26
+ };
16
27
  declare class EventusManifestError extends Error {
17
28
  constructor(message: string);
18
29
  }
@@ -24,4 +35,4 @@ declare function readEventusManifest(root: string, manifestPath?: string): {
24
35
  declare function validateEventusManifest(value: unknown, filePath?: string): EventusManifest;
25
36
  declare function eventusVitePlugin(options?: EventusVitePluginOptions): Plugin;
26
37
 
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 };
38
+ export { EVENTUS_APP_ID_PATTERN, EVENTUS_DEV_PREVIEW_PATH, EVENTUS_MANIFEST_FILE, EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID, EVENTUS_VIRTUAL_MANIFEST_ID, type EventusDevPreviewDescriptor, type EventusManifest, EventusManifestError, type EventusVitePluginOptions, eventusVitePlugin, readEventusManifest, resolveManifestPath, validateEventusManifest };
package/dist/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  // src/index.ts
2
2
  import { existsSync, readFileSync } from "fs";
3
+ import { networkInterfaces } from "os";
3
4
  import path from "path";
4
5
  var EVENTUS_MANIFEST_FILE = "eventus.manifest.json";
5
6
  var EVENTUS_VIRTUAL_MANIFEST_ID = "virtual:eventus-manifest";
6
7
  var EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID = "\0virtual:eventus-manifest";
8
+ var EVENTUS_DEV_PREVIEW_PATH = "/__eventus__/preview.json";
7
9
  var EVENTUS_APP_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
8
10
  var EventusManifestError = class extends Error {
9
11
  constructor(message) {
@@ -75,6 +77,7 @@ function eventusVitePlugin(options = {}) {
75
77
  let rootDir = process.cwd();
76
78
  let manifestFile = resolveManifestPath(rootDir, options.manifestPath);
77
79
  let manifest;
80
+ let serverHostWasDefined = false;
78
81
  const loadManifest = () => {
79
82
  const result = readEventusManifest(rootDir, options.manifestPath);
80
83
  manifestFile = result.filePath;
@@ -91,9 +94,35 @@ function eventusVitePlugin(options = {}) {
91
94
  manifest ?? loadManifest()
92
95
  )};`
93
96
  });
97
+ const createPreviewDescriptor = (port) => {
98
+ const local = `http://localhost:${port}/`;
99
+ const network = resolveNetworkUrl(port);
100
+ const preview = network ?? local;
101
+ return {
102
+ type: "eventus-dev-preview",
103
+ version: 1,
104
+ manifest: manifest ?? loadManifest(),
105
+ urls: {
106
+ local,
107
+ network,
108
+ preview
109
+ }
110
+ };
111
+ };
94
112
  return {
95
113
  name: "eventus:vite-plugin",
96
114
  enforce: "pre",
115
+ config(config, env) {
116
+ serverHostWasDefined = config.server?.host !== void 0;
117
+ if (env.command !== "serve" || serverHostWasDefined) {
118
+ return void 0;
119
+ }
120
+ return {
121
+ server: {
122
+ host: true
123
+ }
124
+ };
125
+ },
97
126
  configResolved(config) {
98
127
  rootDir = config.root;
99
128
  loadManifest();
@@ -116,6 +145,18 @@ function eventusVitePlugin(options = {}) {
116
145
  transformIndexHtml() {
117
146
  return [createManifestInjectionTag()];
118
147
  },
148
+ configureServer(server) {
149
+ server.middlewares.use((request, response, next) => {
150
+ if (request.url !== EVENTUS_DEV_PREVIEW_PATH) {
151
+ next();
152
+ return;
153
+ }
154
+ const address = server.httpServer?.address();
155
+ const port = typeof address === "object" && address ? address.port : server.config.server.port ?? 5173;
156
+ response.setHeader("Content-Type", "application/json; charset=utf-8");
157
+ response.end(JSON.stringify(createPreviewDescriptor(port)));
158
+ });
159
+ },
119
160
  handleHotUpdate(context) {
120
161
  if (path.resolve(context.file) !== manifestFile) {
121
162
  return;
@@ -137,8 +178,20 @@ function eventusVitePlugin(options = {}) {
137
178
  }
138
179
  };
139
180
  }
181
+ function resolveNetworkUrl(port) {
182
+ for (const addresses of Object.values(networkInterfaces())) {
183
+ for (const address of addresses ?? []) {
184
+ if (address.family !== "IPv4" || address.internal) {
185
+ continue;
186
+ }
187
+ return `http://${address.address}:${port}/`;
188
+ }
189
+ }
190
+ return null;
191
+ }
140
192
  export {
141
193
  EVENTUS_APP_ID_PATTERN,
194
+ EVENTUS_DEV_PREVIEW_PATH,
142
195
  EVENTUS_MANIFEST_FILE,
143
196
  EVENTUS_RESOLVED_VIRTUAL_MANIFEST_ID,
144
197
  EVENTUS_VIRTUAL_MANIFEST_ID,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eventusgo/vite-plugin",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Vite plugin for Eventus mini-app manifests.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",