@pracht/vite-plugin 0.0.1 → 0.1.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.
package/dist/index.mjs CHANGED
@@ -100,6 +100,15 @@ async function pracht(options = {}) {
100
100
  });
101
101
  return null;
102
102
  },
103
+ transform(code, id) {
104
+ if (id !== resolve(root, resolved.appFile.slice(1))) return null;
105
+ const transformed = code.replace(/\(\)\s*=>\s*import\(\s*(['"])([^'"]+)\1\s*\)/g, "$1$2$1");
106
+ if (transformed === code) return null;
107
+ return {
108
+ code: transformed,
109
+ map: null
110
+ };
111
+ },
103
112
  configureServer(server) {
104
113
  if (isPagesMode) {
105
114
  const abs = resolve(root, resolved.pagesDir.slice(1));
@@ -265,6 +274,7 @@ function createDevSSRMiddleware(server, _pluginOptions) {
265
274
  app: serverMod.resolvedApp,
266
275
  registry: serverMod.registry,
267
276
  request: webRequest,
277
+ debugErrors: true,
268
278
  clientEntryUrl: CLIENT_BROWSER_PATH,
269
279
  apiRoutes: serverMod.apiRoutes
270
280
  });
@@ -307,7 +317,7 @@ function createDevSSRMiddleware(server, _pluginOptions) {
307
317
  }
308
318
  const BODYLESS_METHODS = new Set(["GET", "HEAD"]);
309
319
  async function nodeToWebRequest(req) {
310
- const protocol = (Array.isArray(req.headers["x-forwarded-proto"]) ? req.headers["x-forwarded-proto"][0] : req.headers["x-forwarded-proto"]) ?? "http";
320
+ const protocol = "http";
311
321
  const host = req.headers.host ?? "localhost";
312
322
  const url = new URL(req.url ?? "/", `${protocol}://${host}`);
313
323
  const method = req.method ?? "GET";
@@ -17,6 +17,7 @@ declare function filePathToRoutePath(relativePath: string): string;
17
17
  declare function sortRoutes(pages: ScannedPage[]): ScannedPage[];
18
18
  declare function generatePagesManifestSource(pages: ScannedPage[], options: PagesRouterOptions & {
19
19
  pagesDirPrefix?: string;
20
+ useImportSyntax?: boolean;
20
21
  }): string;
21
22
  declare function generateRoutesFile(pagesDir: string, outputPath: string, options: PagesRouterOptions): void;
22
23
  //#endregion
@@ -71,19 +71,22 @@ function generatePagesManifestSource(pages, options) {
71
71
  const pagesDir = options.pagesDir;
72
72
  const defaultRender = options.pagesDefaultRender ?? "ssr";
73
73
  const prefix = options.pagesDirPrefix;
74
+ const useImport = options.useImportSyntax ?? false;
74
75
  const appFile = scanAllFiles(pagesDir).find((f) => basename(f, extname(f)) === "_app" && PAGE_EXTENSIONS.has(extname(f)));
75
76
  const lines = ["import { defineApp, group, route } from \"@pracht/core\";", ""];
76
77
  const routeEntries = [];
77
78
  for (const page of pages) {
78
79
  const render = page.renderMode ?? defaultRender;
79
80
  const filePath = prefix ? `${prefix}/${page.relativePath.replace(/\\/g, "/")}` : `./${page.relativePath.replace(/\\/g, "/")}`;
80
- routeEntries.push(` route(${JSON.stringify(page.routePath)}, ${JSON.stringify(filePath)}, { render: ${JSON.stringify(render)} })`);
81
+ const fileRef = useImport ? `() => import(${JSON.stringify(filePath)})` : JSON.stringify(filePath);
82
+ routeEntries.push(` route(${JSON.stringify(page.routePath)}, ${fileRef}, { render: ${JSON.stringify(render)} })`);
81
83
  }
82
84
  if (appFile) {
83
85
  const appPath = prefix ? `${prefix}/_app.${extname(appFile).slice(1)}` : `./${relative(join(pagesDir, ".."), appFile).replace(/\\/g, "/")}`;
86
+ const shellRef = useImport ? `() => import(${JSON.stringify(appPath)})` : JSON.stringify(appPath);
84
87
  lines.push("const app = defineApp({");
85
88
  lines.push(" shells: {");
86
- lines.push(` pages: ${JSON.stringify(appPath)},`);
89
+ lines.push(` pages: ${shellRef},`);
87
90
  lines.push(" },");
88
91
  lines.push(" routes: [");
89
92
  lines.push(` group({ shell: "pages" }, [`);
@@ -121,7 +124,10 @@ function generateRoutesFile(pagesDir, outputPath, options) {
121
124
  "// Auto-generated from pages/ directory by @pracht/vite-plugin.",
122
125
  "// Customize this file and remove `pagesDir` from pracht config to use it directly.",
123
126
  "",
124
- generatePagesManifestSource(scanPagesDirectory(pagesDir), options).replace("const app = defineApp(", "export const app = defineApp(")
127
+ generatePagesManifestSource(scanPagesDirectory(pagesDir), {
128
+ ...options,
129
+ useImportSyntax: true
130
+ }).replace("const app = defineApp(", "export const app = defineApp(")
125
131
  ].join("\n"), "utf-8");
126
132
  }
127
133
  //#endregion
package/package.json CHANGED
@@ -1,22 +1,19 @@
1
1
  {
2
2
  "name": "@pracht/vite-plugin",
3
- "version": "0.0.1",
3
+ "version": "0.1.1",
4
+ "homepage": "https://github.com/JoviDeCroock/pracht/tree/main/packages/vite-plugin",
5
+ "bugs": {
6
+ "url": "https://github.com/JoviDeCroock/pracht/issues"
7
+ },
4
8
  "repository": {
5
9
  "type": "git",
6
10
  "url": "https://github.com/JoviDeCroock/pracht",
7
11
  "directory": "packages/vite-plugin"
8
12
  },
9
- "homepage": "https://github.com/JoviDeCroock/pracht/tree/main/packages/vite-plugin",
10
- "bugs": {
11
- "url": "https://github.com/JoviDeCroock/pracht/issues"
12
- },
13
13
  "files": [
14
14
  "dist"
15
15
  ],
16
16
  "type": "module",
17
- "publishConfig": {
18
- "provenance": true
19
- },
20
17
  "exports": {
21
18
  ".": {
22
19
  "types": "./dist/index.d.mts",
@@ -27,11 +24,14 @@
27
24
  "default": "./dist/pages-router.mjs"
28
25
  }
29
26
  },
27
+ "publishConfig": {
28
+ "provenance": true
29
+ },
30
30
  "dependencies": {
31
31
  "@preact/preset-vite": "^2.10.5",
32
- "@pracht/adapter-cloudflare": "0.0.1",
33
- "@pracht/core": "0.0.1",
34
- "@pracht/adapter-vercel": "0.0.1"
32
+ "@pracht/adapter-cloudflare": "0.0.3",
33
+ "@pracht/adapter-vercel": "0.0.3",
34
+ "@pracht/core": "0.2.0"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "@cloudflare/vite-plugin": "^1.0.0",