@taserjs/plugin 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.
Files changed (48) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +21 -0
  3. package/dist/esbuild.d.ts +7 -0
  4. package/dist/esbuild.d.ts.map +1 -0
  5. package/dist/esbuild.js +8 -0
  6. package/dist/esbuild.js.map +1 -0
  7. package/dist/index.d.ts +32 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +337 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/next.d.ts +18 -0
  12. package/dist/next.d.ts.map +1 -0
  13. package/dist/next.js +90 -0
  14. package/dist/next.js.map +1 -0
  15. package/dist/nitro.d.ts +17 -0
  16. package/dist/nitro.d.ts.map +1 -0
  17. package/dist/nitro.js +149 -0
  18. package/dist/nitro.js.map +1 -0
  19. package/dist/rolldown.d.ts +6 -0
  20. package/dist/rolldown.d.ts.map +1 -0
  21. package/dist/rolldown.js +7 -0
  22. package/dist/rolldown.js.map +1 -0
  23. package/dist/rollup.d.ts +7 -0
  24. package/dist/rollup.d.ts.map +1 -0
  25. package/dist/rollup.js +8 -0
  26. package/dist/rollup.js.map +1 -0
  27. package/dist/rspack.d.ts +7 -0
  28. package/dist/rspack.d.ts.map +1 -0
  29. package/dist/rspack.js +8 -0
  30. package/dist/rspack.js.map +1 -0
  31. package/dist/vite.d.ts +7 -0
  32. package/dist/vite.d.ts.map +1 -0
  33. package/dist/vite.js +8 -0
  34. package/dist/vite.js.map +1 -0
  35. package/dist/webpack.d.ts +7 -0
  36. package/dist/webpack.d.ts.map +1 -0
  37. package/dist/webpack.js +8 -0
  38. package/dist/webpack.js.map +1 -0
  39. package/package.json +155 -0
  40. package/src/esbuild.ts +5 -0
  41. package/src/index.ts +538 -0
  42. package/src/next.ts +121 -0
  43. package/src/nitro.ts +189 -0
  44. package/src/rolldown.ts +5 -0
  45. package/src/rollup.ts +5 -0
  46. package/src/rspack.ts +5 -0
  47. package/src/vite.ts +20 -0
  48. package/src/webpack.ts +5 -0
package/src/nitro.ts ADDED
@@ -0,0 +1,189 @@
1
+ import { existsSync } from "node:fs";
2
+ import { resolve } from "pathe";
3
+ import {
4
+ generateManifest,
5
+ loadConfig,
6
+ resolveOutputDir,
7
+ resolveRoutesDir,
8
+ resolveServerDir,
9
+ scanRoutes,
10
+ type ResolvedTaserConfig,
11
+ } from "@taserjs/cli";
12
+ import { watch, type FSWatcher } from "chokidar";
13
+ import {
14
+ buildHostFallbackCode,
15
+ getHostServer,
16
+ normalizeImportPath,
17
+ taserPlugin,
18
+ type HostServerInfo,
19
+ type TaserPluginOptions,
20
+ } from "./index.js";
21
+
22
+ export function buildNitroRoutingVirtualSource(): string {
23
+ return [
24
+ "export const findRouteRules = () => ({});",
25
+ "export const findRoute = () => undefined;",
26
+ "export const globalMiddleware = [];",
27
+ "export const findRoutedMiddleware = () => [];",
28
+ ].join("\n");
29
+ }
30
+
31
+ export function buildNitroStandaloneAppSource(
32
+ routesGenPath: string,
33
+ hostServer?: HostServerInfo | null,
34
+ ): string {
35
+ const normalizedPath = normalizeImportPath(routesGenPath);
36
+ const fallback = hostServer
37
+ ? buildHostFallbackCode(normalizeImportPath(hostServer.path), hostServer.type, "taserApp")
38
+ : null;
39
+
40
+ return `// @ts-nocheck
41
+ import { app as taserApp } from "${normalizedPath}";
42
+ import { FastResponse } from "srvx";
43
+ ${fallback ? fallback.imports : ""}
44
+
45
+ globalThis.Response = FastResponse;
46
+
47
+ ${fallback ? fallback.setup : ""}
48
+
49
+ export const handler = (req, ...args) => taserApp.fetch(req, ...args);
50
+
51
+ export function createNitroApp() {
52
+ return {
53
+ fetch: handler,
54
+ captureError: (error) => console.error(error),
55
+ hooks: undefined,
56
+ };
57
+ }
58
+
59
+ export function initNitroPlugins(app) {
60
+ return app;
61
+ }
62
+
63
+ export const app = taserApp;
64
+ export default app;
65
+ `;
66
+ }
67
+
68
+ export function buildNitroMiddlewareHandlerSource(
69
+ routesGenPath: string,
70
+ hostServer?: HostServerInfo | null,
71
+ ): string {
72
+ const normalizedPath = normalizeImportPath(routesGenPath);
73
+ const fallback = hostServer
74
+ ? buildHostFallbackCode(normalizeImportPath(hostServer.path), hostServer.type, "taserApp")
75
+ : null;
76
+
77
+ return `// @ts-nocheck
78
+ import { app as taserApp } from "${normalizedPath}";
79
+ import { toWebRequest } from "h3";
80
+ ${fallback ? fallback.imports : ""}
81
+
82
+ ${fallback ? fallback.setup : ""}
83
+
84
+ export default defineEventHandler((event) => {
85
+ return taserApp.fetch(toWebRequest(event));
86
+ });
87
+ `;
88
+ }
89
+
90
+ export function setupTaserNitro(nitro: any, options: TaserPluginOptions = {}): void {
91
+ const state = nitro.options as unknown as Record<string, unknown>;
92
+ if (state._taserHookRegistered) {
93
+ return;
94
+ }
95
+ state._taserHookRegistered = true;
96
+
97
+ nitro.hooks.hookOnce("build:before", async () => {
98
+ await applyTaserNitro(nitro, options);
99
+ });
100
+ }
101
+
102
+ export async function applyTaserNitro(nitro: any, options: TaserPluginOptions): Promise<void> {
103
+ const cwd = resolve(nitro.options.rootDir || options.cwd || process.cwd());
104
+ const config: ResolvedTaserConfig = await loadConfig(cwd, options.config);
105
+ const routesDir = resolveRoutesDir(config, cwd);
106
+ const serverDir = resolveServerDir(config, cwd);
107
+ const outputDir = resolveOutputDir(config, cwd);
108
+ const routesGenPath = resolve(outputDir, "routes.gen.ts");
109
+
110
+ const executeGeneration = (scaffold = Boolean(nitro.options.dev)) => {
111
+ if (existsSync(routesDir)) {
112
+ const scanResult = scanRoutes({
113
+ routesDir,
114
+ cwd,
115
+ scaffold,
116
+ formatting: config.formatting,
117
+ });
118
+ generateManifest(scanResult, config, cwd);
119
+ }
120
+ };
121
+
122
+ executeGeneration();
123
+
124
+ nitro.options.virtual = nitro.options.virtual || {};
125
+ const isStandalone = options.standalone === true;
126
+
127
+ if (isStandalone) {
128
+ nitro.options.virtual["#nitro/virtual/app"] = () => {
129
+ const currentHost = getHostServer(config, cwd, options);
130
+ return buildNitroStandaloneAppSource(routesGenPath, currentHost);
131
+ };
132
+ nitro.options.virtual["#nitro/virtual/routing"] = () => buildNitroRoutingVirtualSource();
133
+ } else {
134
+ const VIRTUAL_NITRO_HANDLER_ID = "#taserjs/virtual/nitro-handler";
135
+ nitro.options.virtual[VIRTUAL_NITRO_HANDLER_ID] = () => {
136
+ const currentHost = getHostServer(config, cwd, options);
137
+ return buildNitroMiddlewareHandlerSource(routesGenPath, currentHost);
138
+ };
139
+
140
+ nitro.options.handlers = nitro.options.handlers || [];
141
+ nitro.options.handlers.unshift({
142
+ route: "/**",
143
+ lazy: false,
144
+ handler: VIRTUAL_NITRO_HANDLER_ID,
145
+ });
146
+ }
147
+
148
+ let watcher: FSWatcher | undefined;
149
+ if (nitro.options.dev && existsSync(routesDir)) {
150
+ const watchDirs = [routesDir];
151
+ if (existsSync(serverDir) && !watchDirs.includes(serverDir)) {
152
+ watchDirs.push(serverDir);
153
+ }
154
+ watcher = watch(watchDirs, {
155
+ ignoreInitial: true,
156
+ ignored: [/(^|[/\\])\../, /(^|[/\\])-/, /node_modules/, /\.taserjs/],
157
+ });
158
+
159
+ let timer: NodeJS.Timeout | null = null;
160
+ watcher.on("all", () => {
161
+ if (timer) clearTimeout(timer);
162
+ timer = setTimeout(() => {
163
+ executeGeneration();
164
+ }, 50);
165
+ });
166
+ }
167
+
168
+ nitro.hooks.hook("close", async () => {
169
+ await watcher?.close();
170
+ });
171
+ }
172
+
173
+ /**
174
+ * Taser Nitro module.
175
+ *
176
+ * Can be used as a Nitro module (`modules: [taser()]`) in `nitro.config.ts`,
177
+ * or chained as a rollup plugin.
178
+ */
179
+ export function taser(options: TaserPluginOptions = {}) {
180
+ const rollupPlugin = taserPlugin.rollup(options);
181
+ return {
182
+ ...rollupPlugin,
183
+ name: "taserjs:plugin",
184
+ __taserOptions: options,
185
+ setup: (nitro: any) => setupTaserNitro(nitro, options),
186
+ };
187
+ }
188
+
189
+ export default taser;
@@ -0,0 +1,5 @@
1
+ import { taserPlugin, type TaserPluginOptions } from "./index.js";
2
+
3
+ export const taser = taserPlugin.rolldown as typeof taserPlugin.rollup;
4
+ export default taser;
5
+ export type { TaserPluginOptions };
package/src/rollup.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { taserPlugin, type TaserPluginOptions } from "./index.js";
2
+
3
+ export const taser = taserPlugin.rollup;
4
+ export default taserPlugin.rollup;
5
+ export type { TaserPluginOptions };
package/src/rspack.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { taserPlugin, type TaserPluginOptions } from "./index.js";
2
+
3
+ export const taser = taserPlugin.rspack;
4
+ export default taserPlugin.rspack;
5
+ export type { TaserPluginOptions };
package/src/vite.ts ADDED
@@ -0,0 +1,20 @@
1
+ import {
2
+ detectHostServer,
3
+ emitServeShim,
4
+ mountHostFallback,
5
+ resolveHostFetchHandler,
6
+ taserPlugin,
7
+ type HostServerInfo,
8
+ type TaserPluginOptions,
9
+ } from "./index.js";
10
+
11
+ export const taser = taserPlugin.vite;
12
+ export default taserPlugin.vite;
13
+ export {
14
+ detectHostServer,
15
+ emitServeShim,
16
+ mountHostFallback,
17
+ resolveHostFetchHandler,
18
+ type HostServerInfo,
19
+ type TaserPluginOptions,
20
+ };
package/src/webpack.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { taserPlugin, type TaserPluginOptions } from "./index.js";
2
+
3
+ export const taser = taserPlugin.webpack;
4
+ export default taserPlugin.webpack;
5
+ export type { TaserPluginOptions };