elit 2.0.1 → 3.0.1

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 (82) hide show
  1. package/README.md +275 -128
  2. package/dist/build.d.mts +10 -1
  3. package/dist/build.d.ts +10 -1
  4. package/dist/build.js +670 -1
  5. package/dist/build.mjs +641 -1
  6. package/dist/chokidar.d.mts +134 -0
  7. package/dist/chokidar.d.ts +134 -0
  8. package/dist/chokidar.js +240 -0
  9. package/dist/chokidar.mjs +221 -0
  10. package/dist/cli.js +2792 -495
  11. package/dist/dom.d.mts +10 -3
  12. package/dist/dom.d.ts +10 -3
  13. package/dist/dom.js +676 -1
  14. package/dist/dom.mjs +647 -1
  15. package/dist/el.d.mts +16 -36
  16. package/dist/el.d.ts +16 -36
  17. package/dist/el.js +789 -1
  18. package/dist/el.mjs +583 -1
  19. package/dist/fs.d.mts +255 -0
  20. package/dist/fs.d.ts +255 -0
  21. package/dist/fs.js +513 -0
  22. package/dist/fs.mjs +469 -0
  23. package/dist/hmr.js +112 -1
  24. package/dist/hmr.mjs +91 -1
  25. package/dist/http.d.mts +163 -0
  26. package/dist/http.d.ts +163 -0
  27. package/dist/http.js +632 -0
  28. package/dist/http.mjs +605 -0
  29. package/dist/https.d.mts +108 -0
  30. package/dist/https.d.ts +108 -0
  31. package/dist/https.js +907 -0
  32. package/dist/https.mjs +901 -0
  33. package/dist/index.d.mts +613 -33
  34. package/dist/index.d.ts +613 -33
  35. package/dist/index.js +2589 -1
  36. package/dist/index.mjs +2312 -1
  37. package/dist/mime-types.d.mts +48 -0
  38. package/dist/mime-types.d.ts +48 -0
  39. package/dist/mime-types.js +197 -0
  40. package/dist/mime-types.mjs +166 -0
  41. package/dist/path.d.mts +163 -0
  42. package/dist/path.d.ts +163 -0
  43. package/dist/path.js +350 -0
  44. package/dist/path.mjs +310 -0
  45. package/dist/router.d.mts +3 -1
  46. package/dist/router.d.ts +3 -1
  47. package/dist/router.js +830 -1
  48. package/dist/router.mjs +801 -1
  49. package/dist/runtime.d.mts +97 -0
  50. package/dist/runtime.d.ts +97 -0
  51. package/dist/runtime.js +43 -0
  52. package/dist/runtime.mjs +15 -0
  53. package/dist/server.d.mts +5 -1
  54. package/dist/server.d.ts +5 -1
  55. package/dist/server.js +3267 -1
  56. package/dist/server.mjs +3241 -1
  57. package/dist/state.d.mts +3 -1
  58. package/dist/state.d.ts +3 -1
  59. package/dist/state.js +1036 -1
  60. package/dist/state.mjs +992 -1
  61. package/dist/style.d.mts +47 -1
  62. package/dist/style.d.ts +47 -1
  63. package/dist/style.js +551 -1
  64. package/dist/style.mjs +483 -1
  65. package/dist/{types-DOAdFFJB.d.ts → types-C0nGi6MX.d.mts} +29 -13
  66. package/dist/{types-DOAdFFJB.d.mts → types-Du6kfwTm.d.ts} +29 -13
  67. package/dist/types.d.mts +452 -3
  68. package/dist/types.d.ts +452 -3
  69. package/dist/types.js +18 -1
  70. package/dist/ws.d.mts +195 -0
  71. package/dist/ws.d.ts +195 -0
  72. package/dist/ws.js +380 -0
  73. package/dist/ws.mjs +358 -0
  74. package/dist/wss.d.mts +108 -0
  75. package/dist/wss.d.ts +108 -0
  76. package/dist/wss.js +1306 -0
  77. package/dist/wss.mjs +1300 -0
  78. package/package.json +53 -6
  79. package/dist/client.d.mts +0 -9
  80. package/dist/client.d.ts +0 -9
  81. package/dist/client.js +0 -1
  82. package/dist/client.mjs +0 -1
package/dist/build.mjs CHANGED
@@ -1 +1,641 @@
1
- import{build as e}from"esbuild";import{mkdirSync as t,statSync as o,existsSync as n,readFileSync as r,writeFileSync as i,copyFileSync as l}from"fs";import{resolve as s,basename as a,extname as f,join as g,dirname as m}from"path";var c={outDir:"dist",minify:!0,sourcemap:!1,target:"es2020",format:"esm",treeshake:!0,logging:!0,external:[]};async function p(p){let d={...c,...p},y=Date.now();if(!d.entry)throw new Error("Entry file is required");let h=s(d.entry),v=s(d.outDir),b=d.outFile;if(!b){b=a(d.entry,f(d.entry))+("cjs"===d.format?".cjs":".js")}let O=g(v,b);try{t(v,{recursive:!0})}catch{}d.logging&&(console.log("\n🔨 Building..."),console.log(` Entry: ${d.entry}`),console.log(` Output: ${O}`),console.log(` Format: ${d.format}`),console.log(` Target: ${d.target}`));let E={name:"browser-only",setup(e){e.onResolve({filter:/^(node:.*|fs|path|http|https|url|os|child_process|net|tls|crypto|stream|util|events|buffer|zlib|readline|process|assert|constants|dns|domain|punycode|querystring|repl|string_decoder|sys|timers|tty|v8|vm)$/},()=>({path:"node-builtin",external:!0,sideEffects:!1})),e.onResolve({filter:/^(chokidar|esbuild|mime-types|open|ws|fs\/promises)$/},()=>({path:"server-dep",external:!0,sideEffects:!1})),e.onLoad({filter:/[\\/](server|config|cli)\.ts$/},()=>({contents:"export {}",loader:"js"}))}};try{let a=d.platform||("cjs"===d.format?"node":"browser"),f="browser"===a?[E]:[],g={};d.env&&(Object.entries(d.env).forEach(([e,t])=>{e.startsWith("VITE_")&&(g[`import.meta.env.${e}`]=JSON.stringify(t))}),d.env.MODE&&(g["import.meta.env.MODE"]=JSON.stringify(d.env.MODE)),g["import.meta.env.DEV"]=JSON.stringify("production"!==d.env.MODE),g["import.meta.env.PROD"]=JSON.stringify("production"===d.env.MODE));let c=await e({entryPoints:[h],bundle:!0,outfile:O,format:d.format,target:d.target,minify:d.minify,sourcemap:d.sourcemap,external:d.external,treeShaking:d.treeshake,globalName:d.globalName,platform:a,plugins:f,define:g,logLevel:d.logging?"info":"silent",metafile:!0,...d.minify&&{minifyWhitespace:!0,minifyIdentifiers:!0,minifySyntax:!0,legalComments:"none",mangleProps:/^_/,keepNames:!1}}),p=Date.now()-y,b=o(O).size;if(d.logging&&(console.log("\n✅ Build successful!"),console.log(` Time: ${p}ms`),console.log(` Size: ${u(b)}`),c.metafile)){let e=Object.keys(c.metafile.inputs).length;console.log(` Files: ${e} input(s)`);let t=Object.keys(c.metafile.outputs);if(t.length>0){let e=c.metafile.outputs[t[0]];if(e&&e.inputs){let t=Object.entries(e.inputs).sort(([,e],[,t])=>t.bytesInOutput-e.bytesInOutput).slice(0,5);t.length>0&&(console.log("\n 📊 Top 5 largest modules:"),t.forEach(([e,t])=>{let o=e.split(/[/\\]/).pop()||e;console.log(` ${o.padEnd(30)} ${u(t.bytesInOutput)}`)}))}}}let $={outputPath:O,buildTime:p,size:b};if(d.copy&&d.copy.length>0){d.logging&&console.log("\n📦 Copying files...");for(let e of d.copy){let o=s(e.from),a=s(v,e.to),f=m(a);if(n(f)||t(f,{recursive:!0}),n(o)){if(e.transform){let t=r(o,"utf-8"),n=e.transform(t,d);i(a,n)}else l(o,a);d.logging&&console.log(` ✓ ${e.from} → ${e.to}`)}else d.logging&&console.warn(` ⚠ File not found: ${e.from}`)}}return d.onBuildEnd&&await d.onBuildEnd($),d.logging&&console.log(""),$}catch(e){throw d.logging&&(console.error("\n❌ Build failed:"),console.error(e)),e}}function u(e){if(0===e)return"0 B";let t=Math.floor(Math.log(e)/Math.log(1024));return parseFloat((e/Math.pow(1024,t)).toFixed(2))+" "+["B","KB","MB","GB"][t]}export{p as build};
1
+ import {createRequire as __createRequire} from 'module';const require = __createRequire(import.meta.url);
2
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
3
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
4
+ }) : x)(function(x) {
5
+ if (typeof require !== "undefined") return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+
9
+ // src/runtime.ts
10
+ var runtime = (() => {
11
+ if (typeof Deno !== "undefined") return "deno";
12
+ if (typeof Bun !== "undefined") return "bun";
13
+ return "node";
14
+ })();
15
+ var isNode = runtime === "node";
16
+ var isBun = runtime === "bun";
17
+ var isDeno = runtime === "deno";
18
+
19
+ // src/fs.ts
20
+ function parseOptions(options, defaultValue) {
21
+ return typeof options === "string" ? { encoding: options } : options || defaultValue;
22
+ }
23
+ function decodeContent(content, encoding) {
24
+ if (encoding) {
25
+ return new TextDecoder(encoding).decode(content);
26
+ }
27
+ return Buffer.from(content instanceof ArrayBuffer ? new Uint8Array(content) : content);
28
+ }
29
+ function dataToUint8Array(data) {
30
+ if (typeof data === "string") {
31
+ return new TextEncoder().encode(data);
32
+ }
33
+ if (data instanceof Buffer) {
34
+ return new Uint8Array(data);
35
+ }
36
+ return data;
37
+ }
38
+ var fs;
39
+ var fsPromises;
40
+ if (isNode) {
41
+ fs = __require("fs");
42
+ fsPromises = __require("fs/promises");
43
+ }
44
+ function readFileSync(path, options) {
45
+ const opts = parseOptions(options, {});
46
+ if (isNode) {
47
+ return fs.readFileSync(path, opts);
48
+ } else if (isBun) {
49
+ const file = Bun.file(path);
50
+ const content = file.arrayBuffer();
51
+ return decodeContent(content, opts.encoding);
52
+ } else if (isDeno) {
53
+ const content = Deno.readFileSync(path);
54
+ return decodeContent(content, opts.encoding);
55
+ }
56
+ throw new Error("Unsupported runtime");
57
+ }
58
+ function writeFileSync(path, data, options) {
59
+ const opts = parseOptions(options, {});
60
+ if (isNode) {
61
+ fs.writeFileSync(path, data, opts);
62
+ } else if (isBun) {
63
+ Bun.write(path, data);
64
+ } else if (isDeno) {
65
+ Deno.writeFileSync(path, dataToUint8Array(data));
66
+ }
67
+ }
68
+ function existsSync(path) {
69
+ try {
70
+ statSync(path);
71
+ return true;
72
+ } catch {
73
+ return false;
74
+ }
75
+ }
76
+ function statSync(path) {
77
+ if (isNode) {
78
+ return fs.statSync(path);
79
+ } else if (isBun) {
80
+ const file = Bun.file(path);
81
+ const size = file.size;
82
+ try {
83
+ file.arrayBuffer();
84
+ } catch {
85
+ throw new Error(`ENOENT: no such file or directory, stat '${path}'`);
86
+ }
87
+ return createStatsObject(path, size, false);
88
+ } else if (isDeno) {
89
+ const info = Deno.statSync(path);
90
+ return createStatsFromDenoFileInfo(info);
91
+ }
92
+ throw new Error("Unsupported runtime");
93
+ }
94
+ function mkdirSync(path, options) {
95
+ const opts = typeof options === "number" ? { mode: options } : options || {};
96
+ if (isNode) {
97
+ fs.mkdirSync(path, opts);
98
+ } else if (isBun) {
99
+ Deno.mkdirSync(path, { recursive: opts.recursive });
100
+ } else if (isDeno) {
101
+ Deno.mkdirSync(path, { recursive: opts.recursive });
102
+ }
103
+ }
104
+ function copyFileSync(src, dest, flags) {
105
+ if (isNode) {
106
+ fs.copyFileSync(src, dest, flags);
107
+ } else if (isBun) {
108
+ Deno.copyFileSync(src, dest);
109
+ } else if (isDeno) {
110
+ Deno.copyFileSync(src, dest);
111
+ }
112
+ }
113
+ function createStatsObject(_path, size, isDir) {
114
+ const now = Date.now();
115
+ return {
116
+ isFile: () => !isDir,
117
+ isDirectory: () => isDir,
118
+ isBlockDevice: () => false,
119
+ isCharacterDevice: () => false,
120
+ isSymbolicLink: () => false,
121
+ isFIFO: () => false,
122
+ isSocket: () => false,
123
+ dev: 0,
124
+ ino: 0,
125
+ mode: isDir ? 16877 : 33188,
126
+ nlink: 1,
127
+ uid: 0,
128
+ gid: 0,
129
+ rdev: 0,
130
+ size,
131
+ blksize: 4096,
132
+ blocks: Math.ceil(size / 512),
133
+ atimeMs: now,
134
+ mtimeMs: now,
135
+ ctimeMs: now,
136
+ birthtimeMs: now,
137
+ atime: new Date(now),
138
+ mtime: new Date(now),
139
+ ctime: new Date(now),
140
+ birthtime: new Date(now)
141
+ };
142
+ }
143
+ function createStatsFromDenoFileInfo(info) {
144
+ return {
145
+ isFile: () => info.isFile,
146
+ isDirectory: () => info.isDirectory,
147
+ isBlockDevice: () => false,
148
+ isCharacterDevice: () => false,
149
+ isSymbolicLink: () => info.isSymlink || false,
150
+ isFIFO: () => false,
151
+ isSocket: () => false,
152
+ dev: info.dev || 0,
153
+ ino: info.ino || 0,
154
+ mode: info.mode || 0,
155
+ nlink: info.nlink || 1,
156
+ uid: info.uid || 0,
157
+ gid: info.gid || 0,
158
+ rdev: 0,
159
+ size: info.size,
160
+ blksize: info.blksize || 4096,
161
+ blocks: info.blocks || Math.ceil(info.size / 512),
162
+ atimeMs: info.atime?.getTime() || Date.now(),
163
+ mtimeMs: info.mtime?.getTime() || Date.now(),
164
+ ctimeMs: info.birthtime?.getTime() || Date.now(),
165
+ birthtimeMs: info.birthtime?.getTime() || Date.now(),
166
+ atime: info.atime || /* @__PURE__ */ new Date(),
167
+ mtime: info.mtime || /* @__PURE__ */ new Date(),
168
+ ctime: info.birthtime || /* @__PURE__ */ new Date(),
169
+ birthtime: info.birthtime || /* @__PURE__ */ new Date()
170
+ };
171
+ }
172
+
173
+ // src/path.ts
174
+ function getSeparator(isWin) {
175
+ return isWin ? "\\" : "/";
176
+ }
177
+ function getCwd() {
178
+ if (isNode || isBun) {
179
+ return process.cwd();
180
+ } else if (isDeno) {
181
+ return Deno.cwd();
182
+ }
183
+ return "/";
184
+ }
185
+ function findLastSeparator(path) {
186
+ return Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
187
+ }
188
+ function createPathOps(isWin) {
189
+ return {
190
+ sep: getSeparator(isWin),
191
+ delimiter: isWin ? ";" : ":",
192
+ normalize: (path) => normalizePath(path, isWin),
193
+ join: (...paths) => joinPaths(paths, isWin),
194
+ resolve: (...paths) => resolvePaths(paths, isWin),
195
+ isAbsolute: (path) => isWin ? isAbsoluteWin(path) : isAbsolutePosix(path),
196
+ relative: (from, to) => relativePath(from, to, isWin),
197
+ dirname: (path) => getDirname(path, isWin),
198
+ basename: (path, ext) => getBasename(path, ext, isWin),
199
+ extname: (path) => getExtname(path),
200
+ parse: (path) => parsePath(path, isWin),
201
+ format: (pathObject) => formatPath(pathObject, isWin)
202
+ };
203
+ }
204
+ function isAbsolutePosix(path) {
205
+ return path.length > 0 && path[0] === "/";
206
+ }
207
+ function isAbsoluteWin(path) {
208
+ const len = path.length;
209
+ if (len === 0) return false;
210
+ const code = path.charCodeAt(0);
211
+ if (code === 47 || code === 92) {
212
+ return true;
213
+ }
214
+ if (code >= 65 && code <= 90 || code >= 97 && code <= 122) {
215
+ if (len > 2 && path.charCodeAt(1) === 58) {
216
+ const code2 = path.charCodeAt(2);
217
+ if (code2 === 47 || code2 === 92) {
218
+ return true;
219
+ }
220
+ }
221
+ }
222
+ return false;
223
+ }
224
+ var isWindows = (() => {
225
+ if (isNode) {
226
+ return process.platform === "win32";
227
+ } else if (isDeno) {
228
+ return Deno.build.os === "windows";
229
+ }
230
+ return typeof process !== "undefined" && process.platform === "win32";
231
+ })();
232
+ var posix = createPathOps(false);
233
+ var win32 = createPathOps(true);
234
+ function normalizePath(path, isWin) {
235
+ if (path.length === 0) return ".";
236
+ const separator = getSeparator(isWin);
237
+ const isAbsolute = isWin ? isAbsoluteWin(path) : isAbsolutePosix(path);
238
+ const trailingSeparator = path[path.length - 1] === separator || isWin && path[path.length - 1] === "/";
239
+ let normalized = path.replace(isWin ? /[\/\\]+/g : /\/+/g, separator);
240
+ const parts = normalized.split(separator);
241
+ const result = [];
242
+ for (let i = 0; i < parts.length; i++) {
243
+ const part = parts[i];
244
+ if (part === "" || part === ".") {
245
+ if (i === 0 && isAbsolute) result.push("");
246
+ continue;
247
+ }
248
+ if (part === "..") {
249
+ if (result.length > 0 && result[result.length - 1] !== "..") {
250
+ if (!(result.length === 1 && result[0] === "")) {
251
+ result.pop();
252
+ }
253
+ } else if (!isAbsolute) {
254
+ result.push("..");
255
+ }
256
+ } else {
257
+ result.push(part);
258
+ }
259
+ }
260
+ let final = result.join(separator);
261
+ if (final.length === 0) {
262
+ return isAbsolute ? separator : ".";
263
+ }
264
+ if (trailingSeparator && final[final.length - 1] !== separator) {
265
+ final += separator;
266
+ }
267
+ return final;
268
+ }
269
+ function joinPaths(paths, isWin) {
270
+ if (paths.length === 0) return ".";
271
+ const separator = getSeparator(isWin);
272
+ let joined = "";
273
+ for (let i = 0; i < paths.length; i++) {
274
+ const path = paths[i];
275
+ if (path && path.length > 0) {
276
+ if (joined.length === 0) {
277
+ joined = path;
278
+ } else {
279
+ joined += separator + path;
280
+ }
281
+ }
282
+ }
283
+ if (joined.length === 0) return ".";
284
+ return normalizePath(joined, isWin);
285
+ }
286
+ function resolvePaths(paths, isWin) {
287
+ const separator = getSeparator(isWin);
288
+ let resolved = "";
289
+ let isAbsolute = false;
290
+ for (let i = paths.length - 1; i >= 0 && !isAbsolute; i--) {
291
+ const path = paths[i];
292
+ if (path && path.length > 0) {
293
+ resolved = path + (resolved.length > 0 ? separator + resolved : "");
294
+ isAbsolute = isWin ? isAbsoluteWin(resolved) : isAbsolutePosix(resolved);
295
+ }
296
+ }
297
+ if (!isAbsolute) {
298
+ const cwd = getCwd();
299
+ resolved = cwd + (resolved.length > 0 ? separator + resolved : "");
300
+ }
301
+ return normalizePath(resolved, isWin);
302
+ }
303
+ function relativePath(from, to, isWin) {
304
+ from = resolvePaths([from], isWin);
305
+ to = resolvePaths([to], isWin);
306
+ if (from === to) return "";
307
+ const separator = getSeparator(isWin);
308
+ const fromParts = from.split(separator).filter((p) => p.length > 0);
309
+ const toParts = to.split(separator).filter((p) => p.length > 0);
310
+ let commonLength = 0;
311
+ const minLength = Math.min(fromParts.length, toParts.length);
312
+ for (let i = 0; i < minLength; i++) {
313
+ if (fromParts[i] === toParts[i]) {
314
+ commonLength++;
315
+ } else {
316
+ break;
317
+ }
318
+ }
319
+ const upCount = fromParts.length - commonLength;
320
+ const result = [];
321
+ for (let i = 0; i < upCount; i++) {
322
+ result.push("..");
323
+ }
324
+ for (let i = commonLength; i < toParts.length; i++) {
325
+ result.push(toParts[i]);
326
+ }
327
+ return result.join(separator) || ".";
328
+ }
329
+ function getDirname(path, isWin) {
330
+ if (path.length === 0) return ".";
331
+ const separator = getSeparator(isWin);
332
+ const normalized = normalizePath(path, isWin);
333
+ const lastSepIndex = normalized.lastIndexOf(separator);
334
+ if (lastSepIndex === -1) return ".";
335
+ if (lastSepIndex === 0) return separator;
336
+ return normalized.slice(0, lastSepIndex);
337
+ }
338
+ function getBasename(path, ext, isWin) {
339
+ if (path.length === 0) return "";
340
+ const lastSepIndex = isWin ? findLastSeparator(path) : path.lastIndexOf("/");
341
+ let base = lastSepIndex === -1 ? path : path.slice(lastSepIndex + 1);
342
+ if (ext && base.endsWith(ext)) {
343
+ base = base.slice(0, base.length - ext.length);
344
+ }
345
+ return base;
346
+ }
347
+ function getExtname(path) {
348
+ const lastDotIndex = path.lastIndexOf(".");
349
+ const lastSepIndex = findLastSeparator(path);
350
+ if (lastDotIndex === -1 || lastDotIndex < lastSepIndex || lastDotIndex === path.length - 1) {
351
+ return "";
352
+ }
353
+ return path.slice(lastDotIndex);
354
+ }
355
+ function parsePath(path, isWin) {
356
+ let root = "";
357
+ if (isWin) {
358
+ if (path.length >= 2 && path[1] === ":") {
359
+ root = path.slice(0, 2);
360
+ if (path.length > 2 && (path[2] === "\\" || path[2] === "/")) {
361
+ root += "\\";
362
+ }
363
+ } else if (path[0] === "\\" || path[0] === "/") {
364
+ root = "\\";
365
+ }
366
+ } else {
367
+ if (path[0] === "/") {
368
+ root = "/";
369
+ }
370
+ }
371
+ const dir = getDirname(path, isWin);
372
+ const base = getBasename(path, void 0, isWin);
373
+ const ext = getExtname(path);
374
+ const name = ext ? base.slice(0, base.length - ext.length) : base;
375
+ return { root, dir, base, ext, name };
376
+ }
377
+ function formatPath(pathObject, isWin) {
378
+ const separator = getSeparator(isWin);
379
+ const dir = pathObject.dir || pathObject.root || "";
380
+ const base = pathObject.base || (pathObject.name || "") + (pathObject.ext || "");
381
+ if (!dir) return base;
382
+ if (dir === pathObject.root) return dir + base;
383
+ return dir + separator + base;
384
+ }
385
+ function join(...paths) {
386
+ return joinPaths(paths, isWindows);
387
+ }
388
+ function resolve(...paths) {
389
+ return resolvePaths(paths, isWindows);
390
+ }
391
+ function dirname(path) {
392
+ return getDirname(path, isWindows);
393
+ }
394
+ function basename(path, ext) {
395
+ return getBasename(path, ext, isWindows);
396
+ }
397
+ function extname(path) {
398
+ return getExtname(path);
399
+ }
400
+
401
+ // src/build.ts
402
+ function ensureDir(dirPath) {
403
+ try {
404
+ mkdirSync(dirPath, { recursive: true });
405
+ } catch (error) {
406
+ }
407
+ }
408
+ function calculateBuildMetrics(startTime, outputPath) {
409
+ const buildTime = Date.now() - startTime;
410
+ const stats = statSync(outputPath);
411
+ return { buildTime, size: stats.size };
412
+ }
413
+ function readFileAsString(filePath) {
414
+ const contentBuffer = readFileSync(filePath, "utf-8");
415
+ return typeof contentBuffer === "string" ? contentBuffer : contentBuffer.toString("utf-8");
416
+ }
417
+ function getMinifyOptions(minify) {
418
+ return minify ? {
419
+ minifyWhitespace: true,
420
+ minifyIdentifiers: true,
421
+ minifySyntax: true,
422
+ legalComments: "none",
423
+ mangleProps: /^_/,
424
+ keepNames: false
425
+ } : {};
426
+ }
427
+ function logBuildInfo(config, outputPath) {
428
+ console.log("\n\u{1F528} Building...");
429
+ console.log(` Entry: ${config.entry}`);
430
+ console.log(` Output: ${outputPath}`);
431
+ console.log(` Format: ${config.format}`);
432
+ console.log(` Target: ${config.target}`);
433
+ }
434
+ function logBuildSuccess(buildTime, size) {
435
+ console.log(`
436
+ \u2705 Build successful!`);
437
+ console.log(` Time: ${buildTime}ms`);
438
+ console.log(` Size: ${formatBytes(size)}`);
439
+ }
440
+ var defaultOptions = {
441
+ outDir: "dist",
442
+ minify: true,
443
+ sourcemap: false,
444
+ target: "es2020",
445
+ format: "esm",
446
+ treeshake: true,
447
+ logging: true,
448
+ external: []
449
+ };
450
+ async function build(options) {
451
+ const config = { ...defaultOptions, ...options };
452
+ const startTime = Date.now();
453
+ if (!config.entry) {
454
+ throw new Error("Entry file is required");
455
+ }
456
+ const entryPath = resolve(config.entry);
457
+ const outDir = resolve(config.outDir);
458
+ let outFile = config.outFile;
459
+ if (!outFile) {
460
+ const baseName = basename(config.entry, extname(config.entry));
461
+ const ext = config.format === "cjs" ? ".cjs" : ".js";
462
+ outFile = baseName + ext;
463
+ }
464
+ const outputPath = join(outDir, outFile);
465
+ ensureDir(outDir);
466
+ if (config.logging) {
467
+ logBuildInfo(config, outputPath);
468
+ }
469
+ const browserOnlyPlugin = {
470
+ name: "browser-only",
471
+ setup(build2) {
472
+ build2.onResolve({ filter: /^(node:.*|fs|path|http|https|url|os|child_process|net|tls|crypto|stream|util|events|buffer|zlib|readline|process|assert|constants|dns|domain|punycode|querystring|repl|string_decoder|sys|timers|tty|v8|vm)$/ }, () => {
473
+ return { path: "node-builtin", external: true, sideEffects: false };
474
+ });
475
+ build2.onResolve({ filter: /^(chokidar|esbuild|mime-types|open|ws|fs\/promises)$/ }, () => {
476
+ return { path: "server-dep", external: true, sideEffects: false };
477
+ });
478
+ build2.onLoad({ filter: /[\\/](server|config|cli)\.ts$/ }, () => {
479
+ return {
480
+ contents: "export {}",
481
+ loader: "js"
482
+ };
483
+ });
484
+ }
485
+ };
486
+ try {
487
+ const platform = config.platform || (config.format === "cjs" ? "node" : "browser");
488
+ const plugins = platform === "browser" ? [browserOnlyPlugin] : [];
489
+ const define = {};
490
+ if (config.env) {
491
+ Object.entries(config.env).forEach(([key, value]) => {
492
+ if (key.startsWith("VITE_")) {
493
+ define[`import.meta.env.${key}`] = JSON.stringify(value);
494
+ }
495
+ });
496
+ if (config.env.MODE) {
497
+ define["import.meta.env.MODE"] = JSON.stringify(config.env.MODE);
498
+ }
499
+ define["import.meta.env.DEV"] = JSON.stringify(config.env.MODE !== "production");
500
+ define["import.meta.env.PROD"] = JSON.stringify(config.env.MODE === "production");
501
+ }
502
+ let result;
503
+ let buildTime;
504
+ let size;
505
+ if (runtime === "node") {
506
+ const { build: esbuild } = await import("esbuild");
507
+ result = await esbuild({
508
+ entryPoints: [entryPath],
509
+ bundle: true,
510
+ outfile: outputPath,
511
+ format: config.format,
512
+ target: config.target,
513
+ minify: config.minify,
514
+ sourcemap: config.sourcemap,
515
+ external: config.external,
516
+ treeShaking: config.treeshake,
517
+ globalName: config.globalName,
518
+ platform,
519
+ plugins,
520
+ define,
521
+ logLevel: config.logging ? "info" : "silent",
522
+ metafile: true,
523
+ // Additional optimizations
524
+ ...getMinifyOptions(config.minify)
525
+ });
526
+ ({ buildTime, size } = calculateBuildMetrics(startTime, outputPath));
527
+ } else if (runtime === "bun") {
528
+ result = await Bun.build({
529
+ entrypoints: [entryPath],
530
+ outdir: outDir,
531
+ target: "bun",
532
+ format: config.format === "cjs" ? "cjs" : "esm",
533
+ minify: config.minify,
534
+ sourcemap: config.sourcemap ? "external" : "none",
535
+ external: config.external,
536
+ naming: outFile,
537
+ define
538
+ });
539
+ if (!result.success) {
540
+ throw new Error("Bun build failed: " + JSON.stringify(result.logs));
541
+ }
542
+ ({ buildTime, size } = calculateBuildMetrics(startTime, outputPath));
543
+ } else {
544
+ result = await Deno.emit(entryPath, {
545
+ bundle: "module",
546
+ check: false,
547
+ compilerOptions: {
548
+ target: config.target,
549
+ module: config.format === "cjs" ? "commonjs" : "esnext",
550
+ sourceMap: config.sourcemap
551
+ }
552
+ });
553
+ const bundledCode = result.files["deno:///bundle.js"];
554
+ if (bundledCode) {
555
+ await Deno.writeTextFile(outputPath, bundledCode);
556
+ }
557
+ ({ buildTime, size } = calculateBuildMetrics(startTime, outputPath));
558
+ }
559
+ if (config.logging) {
560
+ logBuildSuccess(buildTime, size);
561
+ if (runtime === "node" && result.metafile) {
562
+ const inputs = Object.keys(result.metafile.inputs).length;
563
+ console.log(` Files: ${inputs} input(s)`);
564
+ const outputKeys = Object.keys(result.metafile.outputs);
565
+ if (outputKeys.length > 0) {
566
+ const mainOutput = result.metafile.outputs[outputKeys[0]];
567
+ if (mainOutput && mainOutput.inputs) {
568
+ const sortedInputs = Object.entries(mainOutput.inputs).sort(([, a], [, b]) => {
569
+ const aBytes = a.bytesInOutput || 0;
570
+ const bBytes = b.bytesInOutput || 0;
571
+ return bBytes - aBytes;
572
+ }).slice(0, 5);
573
+ if (sortedInputs.length > 0) {
574
+ console.log("\n \u{1F4CA} Top 5 largest modules:");
575
+ sortedInputs.forEach(([file, info]) => {
576
+ const fileName = file.split(/[/\\]/).pop() || file;
577
+ const infoBytes = info.bytesInOutput || 0;
578
+ console.log(` ${fileName.padEnd(30)} ${formatBytes(infoBytes)}`);
579
+ });
580
+ }
581
+ }
582
+ }
583
+ }
584
+ }
585
+ const buildResult = {
586
+ outputPath,
587
+ buildTime,
588
+ size
589
+ };
590
+ if (config.copy && config.copy.length > 0) {
591
+ if (config.logging) {
592
+ console.log("\n\u{1F4E6} Copying files...");
593
+ }
594
+ for (const copyItem of config.copy) {
595
+ const fromPath = resolve(copyItem.from);
596
+ const toPath = resolve(outDir, copyItem.to);
597
+ const targetDir = dirname(toPath);
598
+ if (!existsSync(targetDir)) {
599
+ ensureDir(targetDir);
600
+ }
601
+ if (existsSync(fromPath)) {
602
+ if (copyItem.transform) {
603
+ const content = readFileAsString(fromPath);
604
+ const transformed = copyItem.transform(content, config);
605
+ writeFileSync(toPath, transformed);
606
+ } else {
607
+ copyFileSync(fromPath, toPath);
608
+ }
609
+ if (config.logging) {
610
+ console.log(` \u2713 ${copyItem.from} \u2192 ${copyItem.to}`);
611
+ }
612
+ } else if (config.logging) {
613
+ console.warn(` \u26A0 File not found: ${copyItem.from}`);
614
+ }
615
+ }
616
+ }
617
+ if (config.onBuildEnd) {
618
+ await config.onBuildEnd(buildResult);
619
+ }
620
+ if (config.logging) {
621
+ console.log("");
622
+ }
623
+ return buildResult;
624
+ } catch (error) {
625
+ if (config.logging) {
626
+ console.error("\n\u274C Build failed:");
627
+ console.error(error);
628
+ }
629
+ throw error;
630
+ }
631
+ }
632
+ function formatBytes(bytes) {
633
+ if (bytes === 0) return "0 B";
634
+ const k = 1024;
635
+ const sizes = ["B", "KB", "MB", "GB"];
636
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
637
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
638
+ }
639
+ export {
640
+ build
641
+ };