tonka-vite-plugin 1.32.0 → 1.32.7

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.js CHANGED
@@ -1,43 +1,100 @@
1
- import path from 'path';
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
2
3
  export default function tonkaPlugin(options = {}) {
3
4
  const root = process.cwd();
5
+ const publicDir = options.publicDirectory
6
+ ? path.resolve(root, options.publicDirectory)
7
+ : path.resolve(root, 'public');
8
+ const hotFile = path.resolve(publicDir, 'hot');
9
+ const refreshPaths = options.refresh === true
10
+ ? ['resources/views/**', 'app/**/*.php', 'routes/**']
11
+ : Array.isArray(options.refresh)
12
+ ? options.refresh
13
+ : typeof options.refresh === 'string'
14
+ ? [options.refresh]
15
+ : [];
16
+ let viteDevServerUrl;
17
+ let resolvedConfig;
4
18
  return {
5
19
  name: 'tonka-vite-plugin',
6
- config: (userConfig, { command }) => {
20
+ config(userConfig, { command, mode }) {
7
21
  const input = options.input || {
8
- 'resources/css/app.css': undefined,
9
- 'resources/js/app.tsx': undefined,
22
+ app: 'resources/css/app.css',
23
+ main: 'resources/js/app.tsx',
10
24
  };
11
25
  const outDir = options.outDir || path.resolve(root, 'public/build');
12
26
  const manifest = options.manifest !== false ? 'manifest.json' : false;
13
- const ssrBuild = options.ssr ? options.ssr : false;
14
- return {
27
+ resolvedConfig = {
28
+ base: command === 'build'
29
+ ? (options.buildBase ?? '/build/')
30
+ : '',
15
31
  build: {
16
- outDir: outDir,
17
- manifest: manifest,
32
+ outDir,
33
+ manifest,
18
34
  assetsDir: options.assetsDir || 'assets',
19
- rollupOptions: {
20
- input: input,
21
- },
22
- ssr: ssrBuild,
23
- sourcemap: command === 'serve',
35
+ rollupOptions: { input },
36
+ ssr: options.ssr || false,
37
+ sourcemap: command === 'serve' ? true : (options.sourcemap ?? false),
24
38
  copyPublicDir: false,
39
+ emptyOutDir: command === 'build',
25
40
  },
26
41
  resolve: {
27
- alias: options.alias || {
28
- '@': path.resolve(root, '.'),
42
+ alias: {
43
+ '@': path.resolve(root, 'resources/js'),
44
+ ...(options.alias || {}),
29
45
  },
30
46
  },
31
47
  optimizeDeps: {
32
- include: ['react', 'react-dom', '@inertiajs/react', 'axios'],
48
+ include: ['react', 'react-dom', '@inertiajs/react'],
33
49
  exclude: [],
34
50
  },
51
+ server: {
52
+ origin: userConfig.server?.origin,
53
+ cors: true,
54
+ watch: {
55
+ ignored: [
56
+ '**/public/build/**',
57
+ '**/public/hot',
58
+ '**/node_modules/**',
59
+ '**/vendor/**',
60
+ '**/storage/**',
61
+ ],
62
+ },
63
+ },
35
64
  };
65
+ return resolvedConfig;
36
66
  },
37
- transformIndexHtml(html) {
38
- if (options.refresh) {
67
+ configureServer(server) {
68
+ const cleanupHotFile = () => {
69
+ if (fs.existsSync(hotFile))
70
+ fs.rmSync(hotFile);
71
+ };
72
+ cleanupHotFile();
73
+ server.httpServer?.once('listening', () => {
74
+ const address = server.httpServer?.address();
75
+ const protocol = server.config.server.https ? 'https' : 'http';
76
+ if (address && typeof address === 'object') {
77
+ viteDevServerUrl =
78
+ resolvedConfig.server?.origin ??
79
+ `${protocol}://${address.address === '::' ? 'localhost' : address.address}:${address.port}`;
80
+ fs.mkdirSync(publicDir, { recursive: true });
81
+ fs.writeFileSync(hotFile, viteDevServerUrl);
82
+ }
83
+ });
84
+ const exitHandler = () => cleanupHotFile();
85
+ process.on('exit', exitHandler);
86
+ process.on('SIGINT', () => process.exit());
87
+ process.on('SIGTERM', () => process.exit());
88
+ server.httpServer?.once('close', cleanupHotFile);
89
+ if (refreshPaths.length > 0) {
90
+ const watcher = server.watcher.add(refreshPaths);
91
+ watcher.on('change', (file) => {
92
+ server.ws.send({ type: 'full-reload', path: '*' });
93
+ });
39
94
  }
40
- return html;
95
+ },
96
+ transformIndexHtml() {
97
+ return undefined;
41
98
  },
42
99
  };
43
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tonka-vite-plugin",
3
- "version": "1.32.0",
3
+ "version": "1.32.7",
4
4
  "description": "A Vite plugin for Inertia.js that provides automatic page imports and enhanced development experience.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/types/index.d.ts CHANGED
@@ -16,10 +16,17 @@ export interface TonkaPluginOptions {
16
16
  ssr?: string | false;
17
17
 
18
18
  /**
19
- * Whether to enable React Fast Refresh (HMR).
20
- * If true, the plugin may attempt to configure HMR settings.
19
+ * Whether to watch server-rendered files (views, routes, PHP, ...) and
20
+ * trigger a full browser reload when they change, since Vite's module
21
+ * graph can't HMR server-rendered templates.
22
+ *
23
+ * - `true` → watch a sensible default set (`resources/views/**`,
24
+ * `app/**\/*.php`, `routes/**`)
25
+ * - string → a single glob pattern to watch
26
+ * - string[] → an explicit list of glob patterns to watch
27
+ * - false → disabled (default)
21
28
  */
22
- refresh?: boolean;
29
+ refresh?: boolean | string | string[];
23
30
 
24
31
  /**
25
32
  * The output directory for the production build.
@@ -34,12 +41,34 @@ export interface TonkaPluginOptions {
34
41
 
35
42
  /**
36
43
  * Path aliases (e.g., '@' to point to the root or resources).
44
+ * Merged with the plugin's default alias ('@' -> 'resources/js').
37
45
  */
38
46
  alias?: Record<string, string>;
39
-
47
+
40
48
  /**
41
49
  * Whether to generate a manifest.json file.
42
50
  * Defaults to true for production.
43
51
  */
44
52
  manifest?: boolean;
53
+
54
+ /**
55
+ * The public directory of the project, used to locate/write the `hot`
56
+ * file that signals the dev server's URL to the backend.
57
+ * Defaults to 'public'.
58
+ */
59
+ publicDirectory?: string;
60
+
61
+ /**
62
+ * The `base` path used for built asset URLs in production
63
+ * (i.e. what the backend should prefix asset paths with).
64
+ * Defaults to '/build/'.
65
+ */
66
+ buildBase?: string;
67
+
68
+ /**
69
+ * Whether to emit sourcemaps for the production build.
70
+ * Dev builds always emit sourcemaps regardless of this setting.
71
+ * Defaults to false.
72
+ */
73
+ sourcemap?: boolean;
45
74
  }