@nightshift-sdk/fiber-vite 0.1.0 → 0.2.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/index.d.ts CHANGED
@@ -2,8 +2,6 @@ import { Plugin } from 'vite';
2
2
  export interface FiberOptions {
3
3
  /** Source root to scan (default "src"). */
4
4
  root?: string;
5
- /** Output file (default "src/fiber.generated.ts"). */
6
- out?: string;
7
5
  /** Override config; otherwise read from VITE_NIGHTSHIFT_* / NIGHTSHIFT_* env. */
8
6
  baseUrl?: string;
9
7
  org?: string;
package/dist/index.js CHANGED
@@ -1,18 +1,42 @@
1
- import { writeFile } from "node:fs/promises";
1
+ import { existsSync } from "node:fs";
2
2
  import { resolve } from "node:path";
3
- import { generate, publish } from "@nightshift-sdk/fiber-cli";
3
+ import { generate, writeFiberCache, fiberCachePath, publish } from "@nightshift-sdk/fiber-cli";
4
4
  import { createFiberProxy } from "@nightshift-sdk/fiber-proxy";
5
5
  import { loadEnv } from "vite";
6
6
  function fiber(options = {}) {
7
7
  let cfg;
8
- let rootDir;
8
+ let projectRoot;
9
+ let scanDir;
9
10
  let outDir;
10
- let outFile;
11
11
  let appId = "";
12
12
  let publishToken = "";
13
13
  let running = false;
14
14
  let queued = false;
15
15
  let timer;
16
+ const run = async () => {
17
+ if (running) {
18
+ queued = true;
19
+ return;
20
+ }
21
+ running = true;
22
+ try {
23
+ const { content, count } = await generate(cfg, scanDir);
24
+ await writeFiberCache(projectRoot, content);
25
+ console.info(`[fiber] typegen: ${count} quer${count === 1 ? "y" : "ies"} → node_modules/.fiber`);
26
+ } catch (e) {
27
+ console.warn(`[fiber] typegen failed: ${e.message}`);
28
+ } finally {
29
+ running = false;
30
+ if (queued) {
31
+ queued = false;
32
+ void run();
33
+ }
34
+ }
35
+ };
36
+ const ensureStub = async () => {
37
+ if (existsSync(fiberCachePath(projectRoot))) return;
38
+ await writeFiberCache(projectRoot, "// AUTO-GENERATED by `fiber typegen`.\nexport {}\n");
39
+ };
16
40
  return {
17
41
  name: "fiber",
18
42
  // Hosted apps serve under apps.nightshift.sh/<id>/, so emit relative asset
@@ -22,18 +46,25 @@ function fiber(options = {}) {
22
46
  config(userConfig) {
23
47
  if (!userConfig.base || userConfig.base === "/") return { base: "./" };
24
48
  },
25
- configResolved(config) {
49
+ async configResolved(config) {
26
50
  const env = loadEnv(config.mode, config.root, "");
27
51
  cfg = {
28
52
  baseUrl: options.baseUrl || env.VITE_NIGHTSHIFT_BASE_URL || env.NIGHTSHIFT_BASE_URL || "https://api.nightshift.sh",
29
53
  org: options.org || env.VITE_NIGHTSHIFT_ORG || env.NIGHTSHIFT_ORG || "",
30
54
  token: options.token || env.VITE_NIGHTSHIFT_TOKEN || env.NIGHTSHIFT_TOKEN || ""
31
55
  };
32
- rootDir = resolve(config.root, options.root ?? "src");
56
+ projectRoot = config.root;
57
+ scanDir = resolve(config.root, options.root ?? "src");
33
58
  outDir = resolve(config.root, config.build.outDir);
34
- outFile = resolve(config.root, options.out ?? "src/fiber.generated.ts");
35
59
  appId = typeof options.publish === "object" && options.publish.appId || env.NIGHTSHIFT_APP_ID || "";
36
60
  publishToken = env.NIGHTSHIFT_PUBLISH_TOKEN || cfg.token;
61
+ await ensureStub();
62
+ },
63
+ // Generate fresh types when the build (or dev server) starts. Runs in both
64
+ // `vite build` and `vite dev`; configResolved already left a stub if creds
65
+ // are absent.
66
+ async buildStart() {
67
+ if (cfg.org && cfg.token) await run();
37
68
  },
38
69
  // Build: publish the manifest (opt-in). Only runs in `vite build`.
39
70
  async closeBundle() {
@@ -47,7 +78,7 @@ function fiber(options = {}) {
47
78
  return;
48
79
  }
49
80
  try {
50
- const { version, count, url } = await publish({ ...cfg, token: publishToken }, rootDir, appId, { dist: outDir });
81
+ const { version, count, url } = await publish({ ...cfg, token: publishToken }, scanDir, appId, { dist: outDir });
51
82
  console.info(`[fiber] publish: ${count} quer${count === 1 ? "y" : "ies"} → ${appId} version ${version}${url ? ` (${url})` : ""}`);
52
83
  } catch (e) {
53
84
  console.error(`[fiber] publish failed: ${e.message}`);
@@ -69,38 +100,16 @@ function fiber(options = {}) {
69
100
  });
70
101
  log(`proxy on ${mount} -> ${cfg.baseUrl}${cfg.org ? "" : " (set NIGHTSHIFT_ORG/TOKEN)"}`);
71
102
  }
72
- const run = async () => {
73
- if (running) {
74
- queued = true;
75
- return;
76
- }
77
- running = true;
78
- try {
79
- const { content, count } = await generate(cfg, rootDir);
80
- await writeFile(outFile, content);
81
- log(`typegen: ${count} quer${count === 1 ? "y" : "ies"}`);
82
- } catch (e) {
83
- log(`typegen failed: ${e.message}`, true);
84
- } finally {
85
- running = false;
86
- if (queued) {
87
- queued = false;
88
- void run();
89
- }
90
- }
91
- };
92
- const schedule = () => {
93
- clearTimeout(timer);
94
- timer = setTimeout(() => void run(), 300);
95
- };
96
103
  if (!cfg.org || !cfg.token) {
97
104
  log("set NIGHTSHIFT_ORG and NIGHTSHIFT_TOKEN (server-side) to enable typegen + proxy", true);
98
105
  return;
99
106
  }
100
- void run();
107
+ const schedule = () => {
108
+ clearTimeout(timer);
109
+ timer = setTimeout(() => void run(), 300);
110
+ };
101
111
  const onChange = (file) => {
102
- if (resolve(file) === outFile) return;
103
- if (/\.tsx?$/.test(file) && !file.endsWith(".generated.ts")) schedule();
112
+ if (/\.tsx?$/.test(file)) schedule();
104
113
  };
105
114
  server.watcher.on("change", onChange);
106
115
  server.watcher.on("add", onChange);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nightshift-sdk/fiber-vite",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Vite plugin — regenerate Nightshift query types on dev start and on save",
5
5
  "license": "MIT",
6
6
  "author": "Nightshift",
@@ -36,8 +36,8 @@
36
36
  "vite": "^5"
37
37
  },
38
38
  "dependencies": {
39
- "@nightshift-sdk/fiber-cli": "^0.1.0",
40
- "@nightshift-sdk/fiber-proxy": "^0.1.0"
39
+ "@nightshift-sdk/fiber-cli": "^0.2.0",
40
+ "@nightshift-sdk/fiber-proxy": "^0.2.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^20.14.0",