tonka-vite-plugin 1.11.0 → 1.30.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
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'vite';
2
+ import { TonkaPluginOptions } from '../types';
3
+ export default function tonkaPlugin(options?: TonkaPluginOptions): Plugin;
package/dist/index.js ADDED
@@ -0,0 +1,42 @@
1
+ import path from 'path';
2
+ export default function tonkaPlugin(options = {}) {
3
+ const root = process.cwd();
4
+ return {
5
+ name: 'tonka-vite-plugin',
6
+ config: (userConfig, { command }) => {
7
+ const input = options.input || {
8
+ 'resources/css/app.css': undefined,
9
+ 'resources/js/app.tsx': undefined,
10
+ };
11
+ const outDir = options.outDir || path.resolve(root, 'public/build');
12
+ const manifest = options.manifest !== false ? 'manifest.json' : false;
13
+ const ssrBuild = options.ssr ? options.ssr : false;
14
+ return {
15
+ build: {
16
+ outDir: outDir,
17
+ manifest: manifest,
18
+ assetsDir: options.assetsDir || 'assets',
19
+ rollupOptions: {
20
+ input: input,
21
+ },
22
+ ssr: ssrBuild,
23
+ sourcemap: command === 'serve',
24
+ },
25
+ resolve: {
26
+ alias: options.alias || {
27
+ '@': path.resolve(root, '.'),
28
+ },
29
+ },
30
+ optimizeDeps: {
31
+ include: ['react', 'react-dom', '@inertiajs/react', 'axios'],
32
+ exclude: [],
33
+ },
34
+ };
35
+ },
36
+ transformIndexHtml(html) {
37
+ if (options.refresh) {
38
+ }
39
+ return html;
40
+ },
41
+ };
42
+ }
package/package.json CHANGED
@@ -1,24 +1,35 @@
1
- {
2
- "name": "tonka-vite-plugin",
3
- "version": "1.11.0",
4
- "description": "Tonka vite plugin for Inertia",
5
- "main": "./bin/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.30.0",
4
+ "description": "A Vite plugin for Inertia.js that provides automatic page imports and enhanced development experience.",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": "./dist/index.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "types"
13
+ ],
14
+ "scripts": {
15
+ "build": "tsc"
16
+ },
17
+ "devDependencies": {
18
+ "typescript": "^5.0.0",
19
+ "vite": "^5.0.0"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/clicalmani/inertia-vite-plugin.git"
24
+ },
25
+ "keywords": [
26
+ "Inertia",
27
+ "vite"
28
+ ],
29
+ "author": "@clicalmani",
30
+ "license": "MIT",
31
+ "bugs": {
32
+ "url": "https://github.com/clicalmani/inertia-vite-plugin/issues"
33
+ },
34
+ "homepage": "https://github.com/clicalmani/inertia-vite-plugin#readme"
35
+ }
@@ -0,0 +1,45 @@
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/bin/index.js DELETED
@@ -1,12 +0,0 @@
1
- import fs from 'fs';
2
- export default function() {
3
- return {
4
- name: 'rollup-plugin-tonka-vite',
5
- transformIndexHtml: {
6
- order: 'pre',
7
- async handler() {
8
- return await fs.promises.readFile('./resources/views/app.twig.php', 'utf8');
9
- }
10
- }
11
- }
12
- }
package/lib/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- /**
2
- * Do an index transform
3
- *
4
- * @param string source
5
- * @return Promise<string>
6
- */
7
- export default () => Promise<string>