@zappdev/vite 0.5.0-alpha.1 → 0.5.0-alpha.3

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.
Files changed (3) hide show
  1. package/dist/index.js +22 -11
  2. package/package.json +5 -4
  3. package/src/index.ts +36 -12
package/dist/index.js CHANGED
@@ -73,7 +73,7 @@ async function bundleWorker(entry, outDir, aliases) {
73
73
  return false;
74
74
  }
75
75
  }
76
- function zappWorkers() {
76
+ function zappWorkers(options) {
77
77
  let root = "";
78
78
  let srcDir = "";
79
79
  let workers = [];
@@ -81,10 +81,11 @@ function zappWorkers() {
81
81
  let aliases = {};
82
82
  let isDev = false;
83
83
  let outDir = "";
84
+ let backendFromConfig = options?.backend;
84
85
  return {
85
86
  name: "zapp-workers",
86
87
  enforce: "pre",
87
- configResolved(config) {
88
+ async configResolved(config) {
88
89
  root = config.root;
89
90
  srcDir = path.join(root, "src");
90
91
  outDir = path.join(root, "dist", "_workers");
@@ -93,14 +94,24 @@ function zappWorkers() {
93
94
  if (resolvedAlias && typeof resolvedAlias === "object" && !Array.isArray(resolvedAlias)) {
94
95
  aliases = resolvedAlias;
95
96
  }
97
+ if (!backendFromConfig) {
98
+ try {
99
+ const configPath = path.join(root, "zapp.config.ts");
100
+ const mod = await import(configPath);
101
+ const zappConfig = typeof mod.default === "function" ? mod.default() : mod.default;
102
+ if (zappConfig?.backend) {
103
+ backendFromConfig = zappConfig.backend;
104
+ }
105
+ } catch {}
106
+ }
96
107
  },
97
108
  async buildStart() {
98
109
  workers = await discoverWorkers(srcDir);
99
- const backendPath = path.join(srcDir, "backend.ts");
100
- if (existsSync(backendPath)) {
110
+ const backendSrc = backendFromConfig ? path.resolve(root, backendFromConfig) : path.join(srcDir, "backend.ts");
111
+ if (existsSync(backendSrc)) {
101
112
  backendEntry = {
102
- specifier: "./backend.ts",
103
- sourcePath: backendPath,
113
+ specifier: path.relative(srcDir, backendSrc),
114
+ sourcePath: backendSrc,
104
115
  outputName: "backend.mjs",
105
116
  outputUrl: "/_workers/backend.mjs"
106
117
  };
@@ -109,7 +120,7 @@ function zappWorkers() {
109
120
  console.log(`[zapp] discovered ${workers.length} worker(s)`);
110
121
  }
111
122
  if (backendEntry) {
112
- console.log("[zapp] backend worker: src/backend.ts");
123
+ console.log(`[zapp] backend worker: ${path.relative(root, backendSrc)}`);
113
124
  }
114
125
  },
115
126
  transform(code, id) {
@@ -134,11 +145,11 @@ function zappWorkers() {
134
145
  const devOutDir = path.join(root, ".zapp", "workers");
135
146
  await mkdir(devOutDir, { recursive: true });
136
147
  workers = await discoverWorkers(srcDir);
137
- const backendPath = path.join(srcDir, "backend.ts");
138
- if (existsSync(backendPath)) {
148
+ const backendSrc = backendFromConfig ? path.resolve(root, backendFromConfig) : path.join(srcDir, "backend.ts");
149
+ if (existsSync(backendSrc)) {
139
150
  backendEntry = {
140
- specifier: "./backend.ts",
141
- sourcePath: backendPath,
151
+ specifier: path.relative(srcDir, backendSrc),
152
+ sourcePath: backendSrc,
142
153
  outputName: "backend.mjs",
143
154
  outputUrl: "/_workers/backend.mjs"
144
155
  };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@zappdev/vite",
3
- "version": "0.5.0-alpha.1",
3
+ "version": "0.5.0-alpha.3",
4
4
  "type": "module",
5
- "main": "dist/index.js",
6
- "types": "src/index.ts",
7
5
  "exports": {
8
- ".": "./dist/index.js"
6
+ ".": {
7
+ "types": "./src/index.ts",
8
+ "default": "./dist/index.js"
9
+ }
9
10
  },
10
11
  "files": [
11
12
  "dist/",
package/src/index.ts CHANGED
@@ -104,7 +104,12 @@ async function bundleWorker(entry: WorkerEntry, outDir: string, aliases: Record<
104
104
  }
105
105
  }
106
106
 
107
- export function zappWorkers(): Plugin {
107
+ interface ZappWorkersOptions {
108
+ /** Path to backend worker source, relative to project root. Overrides zapp.config.ts. */
109
+ backend?: string;
110
+ }
111
+
112
+ export function zappWorkers(options?: ZappWorkersOptions): Plugin {
108
113
  let root = "";
109
114
  let srcDir = "";
110
115
  let workers: WorkerEntry[] = [];
@@ -112,12 +117,13 @@ export function zappWorkers(): Plugin {
112
117
  let aliases: Record<string, string> = {};
113
118
  let isDev = false;
114
119
  let outDir = "";
120
+ let backendFromConfig = options?.backend;
115
121
 
116
122
  return {
117
123
  name: "zapp-workers",
118
124
  enforce: "pre",
119
125
 
120
- configResolved(config) {
126
+ async configResolved(config) {
121
127
  root = config.root;
122
128
  srcDir = path.join(root, "src");
123
129
  outDir = path.join(root, "dist", "_workers");
@@ -128,18 +134,34 @@ export function zappWorkers(): Plugin {
128
134
  if (resolvedAlias && typeof resolvedAlias === "object" && !Array.isArray(resolvedAlias)) {
129
135
  aliases = resolvedAlias as Record<string, string>;
130
136
  }
137
+
138
+ // Auto-read backend path from zapp.config.ts if not passed as option
139
+ if (!backendFromConfig) {
140
+ try {
141
+ const configPath = path.join(root, "zapp.config.ts");
142
+ const mod = await import(configPath);
143
+ const zappConfig = typeof mod.default === "function" ? mod.default() : mod.default;
144
+ if (zappConfig?.backend) {
145
+ backendFromConfig = zappConfig.backend;
146
+ }
147
+ } catch {
148
+ // No zapp.config.ts or no backend field — that's fine
149
+ }
150
+ }
131
151
  },
132
152
 
133
153
  async buildStart() {
134
154
  // Discover workers from source
135
155
  workers = await discoverWorkers(srcDir);
136
156
 
137
- // Check for backend worker (convention: src/backend.ts)
138
- const backendPath = path.join(srcDir, "backend.ts");
139
- if (existsSync(backendPath)) {
157
+ // Backend worker path from config, or fall back to src/backend.ts convention
158
+ const backendSrc = backendFromConfig
159
+ ? path.resolve(root, backendFromConfig)
160
+ : path.join(srcDir, "backend.ts");
161
+ if (existsSync(backendSrc)) {
140
162
  backendEntry = {
141
- specifier: "./backend.ts",
142
- sourcePath: backendPath,
163
+ specifier: path.relative(srcDir, backendSrc),
164
+ sourcePath: backendSrc,
143
165
  outputName: "backend.mjs",
144
166
  outputUrl: "/_workers/backend.mjs",
145
167
  };
@@ -149,7 +171,7 @@ export function zappWorkers(): Plugin {
149
171
  console.log(`[zapp] discovered ${workers.length} worker(s)`);
150
172
  }
151
173
  if (backendEntry) {
152
- console.log("[zapp] backend worker: src/backend.ts");
174
+ console.log(`[zapp] backend worker: ${path.relative(root, backendSrc)}`);
153
175
  }
154
176
  },
155
177
 
@@ -187,11 +209,13 @@ export function zappWorkers(): Plugin {
187
209
  await mkdir(devOutDir, { recursive: true });
188
210
 
189
211
  workers = await discoverWorkers(srcDir);
190
- const backendPath = path.join(srcDir, "backend.ts");
191
- if (existsSync(backendPath)) {
212
+ const backendSrc = backendFromConfig
213
+ ? path.resolve(root, backendFromConfig)
214
+ : path.join(srcDir, "backend.ts");
215
+ if (existsSync(backendSrc)) {
192
216
  backendEntry = {
193
- specifier: "./backend.ts",
194
- sourcePath: backendPath,
217
+ specifier: path.relative(srcDir, backendSrc),
218
+ sourcePath: backendSrc,
195
219
  outputName: "backend.mjs",
196
220
  outputUrl: "/_workers/backend.mjs",
197
221
  };