-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
108 lines (95 loc) · 3.77 KB
/
vite.config.ts
File metadata and controls
108 lines (95 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { readdirSync, statSync, existsSync } from 'fs';
const MODULE_NAME = 'game-tables';
/**
* Auto-discover Vue files in a directory.
* Returns an object mapping entry names to their file paths.
*
* @param dir Absolute path to the directory to scan
* @param basePath The base path prefix for entry keys (e.g. 'resources/js/components')
*/
function discoverEntries(dir: string, basePath: string): Record<string, string> {
if (!existsSync(dir)) {
return {};
}
const entries: Record<string, string> = {};
function scanDir(currentDir: string, prefix = ''): void {
const files = readdirSync(currentDir);
for (const file of files) {
const filePath = resolve(currentDir, file);
const stat = statSync(filePath);
if (stat.isDirectory()) {
scanDir(filePath, prefix ? `${prefix}/${file}` : file);
} else if (file.endsWith('.vue')) {
const relativePath = prefix ? `${prefix}/${file}` : file;
entries[`${basePath}/${relativePath}`] = filePath;
}
}
}
scanDir(dir);
return entries;
}
const componentsDir = resolve(__dirname, 'resources/js/components');
const pagesDir = resolve(__dirname, 'resources/js/pages');
const componentEntries = discoverEntries(componentsDir, 'resources/js/components');
const pageEntries = discoverEntries(pagesDir, 'resources/js/pages');
const allEntries = { ...componentEntries, ...pageEntries };
if (Object.keys(allEntries).length === 0) {
console.warn(`[${MODULE_NAME}] No Vue files found in resources/js/components/ or resources/js/pages/`);
}
// Detect if running in standalone repo or inside main app.
// Dev: src/modules/X → ../../public. Prod Docker: modules/X → ../public.
const mainPublicDir = [
resolve(__dirname, '../../public'),
resolve(__dirname, '../public'),
].find((d) => existsSync(d));
const isStandalone = !mainPublicDir;
const forceStandaloneOutput = process.env.MODULE_STANDALONE_OUTPUT === 'true';
const outDir = (isStandalone || forceStandaloneOutput)
? 'public/build'
: `${mainPublicDir}/build/modules/${MODULE_NAME}`;
export default defineConfig({
base: (isStandalone || forceStandaloneOutput)
? '/'
: `/build/modules/${MODULE_NAME}/`,
plugins: [vue()],
publicDir: isStandalone ? false : 'public',
build: {
outDir,
emptyOutDir: true,
manifest: 'manifest.json',
rollupOptions: {
input: allEntries,
output: {
format: 'es',
entryFileNames: 'assets/[name]-[hash].js',
chunkFileNames: 'assets/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash].[ext]',
preserveModules: false,
},
preserveEntrySignatures: 'exports-only',
// Framework singletons must remain external (shared runtime instances via import maps).
// @/ imports are bundled when inside the main app, external only in standalone mode.
external: (id) => {
if (['vue', 'vue-i18n', 'pinia', '@inertiajs/vue3', '@unhead/vue'].includes(id)) {
return true;
}
if (isStandalone && id.startsWith('@/')) {
return true;
}
return false;
},
},
},
resolve: {
alias: {
'@': isStandalone
? resolve(__dirname, 'resources/js')
: existsSync(resolve(__dirname, '../../resources/js'))
? resolve(__dirname, '../../resources/js')
: resolve(__dirname, '../resources/js'),
},
},
});