@zjazj/vite-plugin 0.0.0-stage → 0.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.
package/README.md CHANGED
@@ -1,3 +1,14 @@
1
- # Temporary Holding Version
1
+ # @zjazj/vite-plugin
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ One Vite preset that configures a platform app.
4
+
5
+ ```ts
6
+ import { platform } from '@zjazj/vite-plugin'
7
+ import { defineConfig } from 'vite'
8
+
9
+ export default defineConfig({ plugins: [platform()] })
10
+ ```
11
+
12
+ It sets the base path, adds React, TanStack Router file routes and Tailwind, and runs `service/server.ts` inside the dev server, so `vite` starts frontend and backend on one port.
13
+
14
+ Part of [platform-cli](https://github.com/zjazj/platform-cli). All `@zjazj/*` packages share one version. Install them at the same version.
@@ -0,0 +1,36 @@
1
+ import { PluginOption } from 'vite';
2
+
3
+ interface PlatformOptions {
4
+ /** Backend entry run inside the dev server. `false` turns the backend off. */
5
+ server?: string | false;
6
+ /** Folder of the file routes. */
7
+ routesDirectory?: string;
8
+ /** Turn off the Tailwind plugin, for an app that configures styles itself. */
9
+ tailwind?: boolean;
10
+ /** URL of the portal's Apps page. Defaults to `PORTAL_URL`, then `/`. */
11
+ portalUrl?: string;
12
+ }
13
+ interface Manifest {
14
+ name: string;
15
+ id: string;
16
+ version?: string;
17
+ platform?: {
18
+ title?: string;
19
+ icon?: string;
20
+ };
21
+ }
22
+ declare const BACKEND_PATHS: RegExp;
23
+ /** Read and validate the parts of package.json the platform uses. */
24
+ declare function readManifest(root: string): Manifest;
25
+ /**
26
+ * The Vite preset for a platform app. Use it as the only plugin:
27
+ *
28
+ * export default defineConfig({ plugins: [platform()] })
29
+ *
30
+ * It sets the base path, adds React, file routing and Tailwind, runs `service/server.ts`
31
+ * inside the dev server so one command starts frontend and backend, and tells the page
32
+ * which app it is.
33
+ */
34
+ declare function platform(options?: PlatformOptions): PluginOption[];
35
+
36
+ export { BACKEND_PATHS, type PlatformOptions, platform as default, platform, readManifest };
package/dist/index.js ADDED
@@ -0,0 +1,112 @@
1
+ // src/index.ts
2
+ import { readFileSync } from "fs";
3
+ import path from "path";
4
+ import devServer from "@hono/vite-dev-server";
5
+ import tailwindcss from "@tailwindcss/vite";
6
+ import { tanstackRouter } from "@tanstack/router-plugin/vite";
7
+ import react from "@vitejs/plugin-react";
8
+ import { devIdentity, isDevIdentityEnabled } from "@zjazj/server/dev";
9
+ import { loadEnv } from "vite";
10
+ var APP_NAME_PATTERN = /^[a-z][a-z0-9-]{1,30}$/;
11
+ var APP_ID_PATTERN = /^app[a-z0-9]{6}$/;
12
+ var BACKEND_PATHS = /^\/(?:api|internal)(?:\/|$)|^\/healthz$/;
13
+ function readManifest(root) {
14
+ const file = path.join(root, "package.json");
15
+ const manifest = JSON.parse(readFileSync(file, "utf8"));
16
+ if (typeof manifest.name !== "string" || !APP_NAME_PATTERN.test(manifest.name)) {
17
+ throw new Error(
18
+ `${file}: "name" must be a valid app name (lower-case letters, digits and hyphens, starting with a letter). Got ${JSON.stringify(manifest.name)}.`
19
+ );
20
+ }
21
+ if (typeof manifest.id !== "string" || !APP_ID_PATTERN.test(manifest.id)) {
22
+ throw new Error(
23
+ `${file}: "id" must be "app" followed by six characters from [a-z0-9]. It is written by \`platform create\` and never changes. Got ${JSON.stringify(manifest.id)}.`
24
+ );
25
+ }
26
+ return manifest;
27
+ }
28
+ function inlineJson(value) {
29
+ return JSON.stringify(value).replaceAll("<", "\\u003c");
30
+ }
31
+ function core(options) {
32
+ let manifest;
33
+ let base = "/";
34
+ let serving = false;
35
+ return {
36
+ name: "zjazj:platform",
37
+ config(userConfig, { command, mode }) {
38
+ const root = path.resolve(userConfig.root ?? process.cwd());
39
+ manifest = readManifest(root);
40
+ serving = command === "serve";
41
+ const fileEnv = loadEnv(mode, root, "");
42
+ for (const [key, value] of Object.entries(fileEnv)) {
43
+ if (process.env[key] === void 0) process.env[key] = value;
44
+ }
45
+ if (serving) {
46
+ base = "/";
47
+ process.env.BASE_URL = "/";
48
+ } else {
49
+ base = process.env.BASE_URL || `/apps/${manifest.name}/`;
50
+ if (!base.endsWith("/")) base += "/";
51
+ }
52
+ return {
53
+ base: userConfig.base ?? base,
54
+ build: { outDir: "dist/client", emptyOutDir: true }
55
+ };
56
+ },
57
+ configResolved(config) {
58
+ base = config.base;
59
+ },
60
+ transformIndexHtml() {
61
+ const config = {
62
+ name: manifest.name,
63
+ id: manifest.id,
64
+ title: manifest.platform?.title ?? manifest.name,
65
+ version: manifest.version,
66
+ basePath: base,
67
+ portalUrl: options.portalUrl ?? process.env.PORTAL_URL ?? "/"
68
+ };
69
+ if (serving && isDevIdentityEnabled(process.env)) {
70
+ config.dev = { user: devIdentity(process.env) };
71
+ }
72
+ return [
73
+ {
74
+ tag: "script",
75
+ children: `window.__PLATFORM__ = ${inlineJson(config)}`,
76
+ injectTo: "head-prepend"
77
+ }
78
+ ];
79
+ }
80
+ };
81
+ }
82
+ function platform(options = {}) {
83
+ const plugins = [
84
+ tanstackRouter({
85
+ target: "react",
86
+ autoCodeSplitting: true,
87
+ routesDirectory: options.routesDirectory ?? "src/routes",
88
+ generatedRouteTree: "src/routeTree.gen.ts"
89
+ }),
90
+ react()
91
+ ];
92
+ if (options.tailwind !== false) plugins.push(tailwindcss());
93
+ if (options.server !== false) {
94
+ plugins.push(
95
+ devServer({
96
+ entry: options.server ?? "service/server.ts",
97
+ // Hand the backend only its own paths. A pattern here means "not the backend".
98
+ exclude: [/^(?!\/(?:api|internal)(?:\/|$)|\/healthz$).*/],
99
+ injectClientScript: false
100
+ })
101
+ );
102
+ }
103
+ plugins.push(core(options));
104
+ return plugins;
105
+ }
106
+ var index_default = platform;
107
+ export {
108
+ BACKEND_PATHS,
109
+ index_default as default,
110
+ platform,
111
+ readManifest
112
+ };
package/package.json CHANGED
@@ -1,6 +1,43 @@
1
1
  {
2
2
  "name": "@zjazj/vite-plugin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.1",
4
+ "description": "One Vite preset that configures a platform app: base path, routing, styles, and the backend in the dev server.",
5
+ "type": "module",
6
+ "license": "UNLICENSED",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/zjazj/platform-cli.git",
10
+ "directory": "packages/vite-plugin"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "dependencies": {
22
+ "@hono/vite-dev-server": "^0.26.1",
23
+ "@tailwindcss/vite": "^4.3.5",
24
+ "@tanstack/router-plugin": "^1.169.0",
25
+ "@vitejs/plugin-react": "^5.2.0",
26
+ "@zjazj/server": "^0.0.1"
27
+ },
28
+ "peerDependencies": {
29
+ "vite": "^7.0.0"
30
+ },
31
+ "devDependencies": {
32
+ "vite": "^7.3.8"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public",
36
+ "registry": "https://registry.npmjs.org/"
37
+ },
38
+ "scripts": {
39
+ "build": "tsup",
40
+ "test": "vitest run",
41
+ "typecheck": "tsc --noEmit -p tsconfig.json"
42
+ }
6
43
  }