tonka-vite-plugin 1.10.0 → 1.21.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.
package/README.md CHANGED
@@ -1 +1,85 @@
1
- # inertia-vite-plugin
1
+ # Tonka Vite Plugin 🔌
2
+
3
+ The official Vite plugin for the [**Tonka Framework**](https://clicalmani.github.io/tonka).
4
+
5
+ This plugin automates the configuration of Vite for applications using **InertiaJS** and **React**. It handles the tedious setup of entry points, build directories, SSR (Server-Side Rendering), and asset manifest generation, ensuring your frontend and backend work together seamlessly.
6
+
7
+ ## 🌟 Features
8
+
9
+ * ⚙️ **Zero-Config Setup**: Automatically configures input files, output directories, and the manifest (`manifest.json`) required by the backend.
10
+ * 🚀 **SSR Support**: Built-in configuration for Server-Side Rendering to enable instant initial page loads.
11
+ * 🔄 **HMR Ready**: Supports React Fast Refresh configuration.
12
+ * 🛣️ **Smart Aliases**: Defaults to a clean `@` alias for your project root or custom paths.
13
+ * 📦 **TypeScript**: Fully typed with TypeScript for a great developer experience.
14
+
15
+ ## 📦 Installation
16
+
17
+ Since this is a local plugin specific to your Tonka setup, copy the plugin file into your project root.
18
+
19
+ 1. Create a file named `tonka-vite-plugin.ts` (or `.js`) in your project root.
20
+ 2. Paste the code provided in the documentation into this file.
21
+
22
+ ## 🚀 Quick Start
23
+
24
+ Import the plugin into your `vite.config.ts` to activate it.
25
+
26
+ ```typescript
27
+ import { defineConfig } from 'vite';
28
+ import react from '@vitejs/plugin-react';
29
+ import tonka from './tonka-vite-plugin';
30
+
31
+ export default defineConfig({
32
+ plugins: [
33
+ tonka({
34
+ // Entry points for the build
35
+ input: ['resources/css/app.css', 'resources/js/app.tsx'],
36
+
37
+ // Enable Server-Side Rendering
38
+ ssr: 'resources/js/ssr.tsx',
39
+
40
+ // Enable React Fast Refresh
41
+ refresh: true,
42
+ }),
43
+ react(),
44
+ ],
45
+ });
46
+ ```
47
+
48
+ ## ⚙️ Configuration Options
49
+
50
+ The plugin accepts a configuration object to customize its behavior.
51
+
52
+ | Option | Type | Default | Description |
53
+ | :--- | :--- | :--- | :--- |
54
+ | `input` | `string \| string[] \| Record` | `['resources/css/app.css', 'resources/js/app.tsx']` | The entry points for the build. Maps to `rollupOptions.input`. |
55
+ | `ssr` | `string \| false` | `false` | The entry point file for the SSR build. Set to the path of your SSR entry file (e.g., `resources/js/ssr.tsx`) to enable SSR. |
56
+ | `refresh` | `boolean` | `false` | Enables support for React Fast Refresh (HMR). Note: The actual script injection is handled by the backend `@vite` directive. |
57
+ | `outDir` | `string` | `'public/build'` | The directory where the compiled assets will be output. |
58
+ | `assetsDir` | `string` | `'assets'` | The directory inside `outDir` to place assets. |
59
+ | `manifest` | `boolean` | `true` | Whether to generate a `manifest.json` file in the output directory. Required for the backend `@vite` directive. |
60
+ | `alias` | `Record<string, string>` | `{ '@': '.' }` | Path aliases for module resolution. |
61
+
62
+ ## 🔍 How it Works
63
+
64
+ ### 1. Build Configuration
65
+ When you run `npm run build`, the plugin automatically configures Rollup (the bundler inside Vite) to output your files to `public/build` (or your specified `outDir`) and generates a `manifest.json` that maps your source files (e.g., `resources/js/app.tsx`) to their hashed filenames (e.g., `app.a1b2c.js`).
66
+
67
+ ### 2. SSR Support
68
+ If you provide an `ssr` entry point, the plugin configures Vite to perform a second build specifically for the server. This allows your backend (Tonka) to render the initial page state on the server for faster load times and SEO.
69
+
70
+ ### 3. Integration with Backend
71
+ The plugin prepares the assets, but the **injection** of the `<script>` tags into the HTML is handled by your backend (Tonka) via the `@vite` Blade directive. This ensures that the correct script source is used:
72
+ * **Dev**: Points to `http://localhost:5173/@vite/client` (for HMR).
73
+ * **Prod**: Points to `/build/assets/xxxxx.js` (using the manifest).
74
+
75
+ ## 🛠️ Development vs Production
76
+
77
+ **In Development:**
78
+ The plugin ensures the dev server runs on port 5173 and resolves aliases correctly. It relies on the backend to proxy requests or inject the client script.
79
+
80
+ **In Production:**
81
+ The plugin is essential to correctly generate the `manifest.json`. Without this plugin generating the manifest, your backend would fail to locate the compiled `.js` files after a build.
82
+
83
+ ## 📝 License
84
+
85
+ MIT
package/bin/index.ts ADDED
@@ -0,0 +1,101 @@
1
+ import type { Plugin, UserConfig } from 'vite';
2
+ import { fileURLToPath, URL } from 'node:url';
3
+ import path from 'path';
4
+ import { TonkaPluginOptions } from '../lib';
5
+
6
+ /**
7
+ * Tonka Vite Plugin
8
+ *
9
+ * A Vite plugin specifically designed for the Tonka Framework.
10
+ * It automates the configuration for Inertia.js, SSR, and asset paths.
11
+ *
12
+ * @param options - Configuration options for the plugin
13
+ * @returns A Vite plugin object
14
+ */
15
+ export default function tonkaPlugin(options: TonkaPluginOptions = {}): Plugin {
16
+ // Determine the base directory of the project
17
+ const root = process.cwd();
18
+
19
+ return {
20
+ name: 'tonka-vite-plugin',
21
+
22
+ /**
23
+ * The `config` hook is called before the user config is resolved.
24
+ * We use it to inject default settings and handle the Tonka-specific structure.
25
+ */
26
+ config: (userConfig: UserConfig, { command }) => {
27
+ // 1. Handle Inputs
28
+ // If the user doesn't provide inputs, we assume the standard structure.
29
+ // Note: We cast to 'any' here to satisfy Rollup's strict typing for input.
30
+ const input: any = options.input || {
31
+ 'resources/css/app.css': undefined, // CSS entry
32
+ 'resources/js/app.tsx': undefined, // Main JS entry
33
+ };
34
+
35
+ // 2. Handle Output Directory
36
+ // Default to 'public/build' if not specified
37
+ const outDir = options.outDir || path.resolve(root, 'public/build');
38
+
39
+ // 3. Handle Manifest
40
+ // Ensure manifest is generated unless explicitly disabled
41
+ const manifest = options.manifest !== false ? 'manifest.json' : false;
42
+
43
+ // 4. Handle SSR
44
+ // If ssr is false, we ensure we don't set the ssr config.
45
+ const ssrBuild = options.ssr ? options.ssr : false;
46
+
47
+ return {
48
+ build: {
49
+ outDir: outDir,
50
+ manifest: manifest,
51
+ assetsDir: options.assetsDir || 'assets',
52
+
53
+ // Rollup options
54
+ rollupOptions: {
55
+ input: input,
56
+ },
57
+
58
+ // Server-Side Rendering configuration
59
+ ssr: ssrBuild,
60
+
61
+ // Source maps: enabled in dev, disabled in prod (optional optimization)
62
+ sourcemap: command === 'serve',
63
+ },
64
+
65
+ // 5. Resolve Aliases
66
+ resolve: {
67
+ // Merge user aliases with defaults
68
+ alias: options.alias || {
69
+ // Default alias: '@' points to the root of the project
70
+ // You can also point it to 'resources' or 'public' based on your preference.
71
+ '@': path.resolve(root, '.'),
72
+ },
73
+ },
74
+
75
+ // 6. Optimize dependencies
76
+ // Pre-bundle common dependencies to speed up dev server start
77
+ optimizeDeps: {
78
+ include: ['react', 'react-dom', '@inertiajs/react', 'axios'],
79
+ exclude: [],
80
+ },
81
+ };
82
+ },
83
+
84
+ /**
85
+ * The `transformIndexHtml` hook allows modifying the HTML index file.
86
+ * This can be used to inject the @vite/client script for HMR if needed.
87
+ */
88
+ transformIndexHtml(html) {
89
+ // If refresh is enabled, we could inject the HMR client here.
90
+ // However, in a typical Inertia setup (like Tonka), this is often
91
+ // handled by the server-side Blade/PHP template (@vite directive).
92
+ // We leave this placeholder for future enhancements.
93
+ if (options.refresh) {
94
+ // Implementation note: Injecting @vite/client usually requires
95
+ // knowing the exact module path, which varies.
96
+ // For now, we trust the server-side directive.
97
+ }
98
+ return html;
99
+ },
100
+ };
101
+ }
package/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ import tonkaPlugin from './bin';
2
+ export default tonkaPlugin;
package/lib/index.d.ts CHANGED
@@ -1 +1,45 @@
1
- /// <reference types="typescript" />
1
+ /**
2
+ * Interface for the options passed to the Tonka plugin.
3
+ * This defines the shape of the configuration object in `vite.config.ts`.
4
+ */
5
+ export interface TonkaPluginOptions {
6
+ /**
7
+ * Entry points for the build.
8
+ * Can be an array of file paths or an object (for named chunks).
9
+ */
10
+ input?: string | string[] | Record<string, string>;
11
+
12
+ /**
13
+ * The entry point for Server-Side Rendering (SSR).
14
+ * Provide a file path string to enable SSR, or false to disable it.
15
+ */
16
+ ssr?: string | false;
17
+
18
+ /**
19
+ * Whether to enable React Fast Refresh (HMR).
20
+ * If true, the plugin may attempt to configure HMR settings.
21
+ */
22
+ refresh?: boolean;
23
+
24
+ /**
25
+ * The output directory for the production build.
26
+ * Defaults to 'public/build'.
27
+ */
28
+ outDir?: string;
29
+
30
+ /**
31
+ * The directory inside `outDir` for assets.
32
+ */
33
+ assetsDir?: string;
34
+
35
+ /**
36
+ * Path aliases (e.g., '@' to point to the root or resources).
37
+ */
38
+ alias?: Record<string, string>;
39
+
40
+ /**
41
+ * Whether to generate a manifest.json file.
42
+ * Defaults to true for production.
43
+ */
44
+ manifest?: boolean;
45
+ }
package/package.json CHANGED
@@ -1,24 +1,23 @@
1
- {
2
- "name": "tonka-vite-plugin",
3
- "version": "1.10.0",
4
- "description": "Tonka vite plugin for Inertia",
5
- "main": "index.js",
6
- "types": "./lib/index.d.ts",
7
- "scripts": {
8
- "test": "echo \"Error: no test specified\" && exit 1"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/clicalmani/inertia-vite-plugin.git"
13
- },
14
- "keywords": [
15
- "Inertia",
16
- "vite"
17
- ],
18
- "author": "@clicalmani",
19
- "license": "MIT",
20
- "bugs": {
21
- "url": "https://github.com/clicalmani/inertia-vite-plugin/issues"
22
- },
23
- "homepage": "https://github.com/clicalmani/inertia-vite-plugin#readme"
24
- }
1
+ {
2
+ "name": "tonka-vite-plugin",
3
+ "version": "1.21.0",
4
+ "description": "A Vite plugin for Inertia.js that provides automatic page imports and enhanced development experience.",
5
+ "main": "index.ts",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/clicalmani/inertia-vite-plugin.git"
12
+ },
13
+ "keywords": [
14
+ "Inertia",
15
+ "vite"
16
+ ],
17
+ "author": "@clicalmani",
18
+ "license": "MIT",
19
+ "bugs": {
20
+ "url": "https://github.com/clicalmani/inertia-vite-plugin/issues"
21
+ },
22
+ "homepage": "https://github.com/clicalmani/inertia-vite-plugin#readme"
23
+ }
package/bin/index.js DELETED
@@ -1,12 +0,0 @@
1
- import fs from 'fs';
2
- export default function(source) {
3
- return {
4
- name: 'rollup-plugin-tonka-vite',
5
- transformIndexHtml: {
6
- order: 'pre',
7
- async handler() {
8
- return await fs.promises.readFile(source, 'utf8');
9
- }
10
- }
11
- }
12
- }