-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
171 lines (157 loc) · 4.83 KB
/
vite.config.js
File metadata and controls
171 lines (157 loc) · 4.83 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { sveltekit } from '@sveltejs/kit/vite';
import { loadEnv, defineConfig } from 'vite';
import fs from 'fs';
import path from 'path';
import { collectLicenses } from './scripts/generate-licenses.js';
const env = loadEnv('', process.cwd());
// Global flag to ensure licenses are generated only once per build
let licensesGenerated = false;
function copyLangFolderPlugin() {
let outDir = '';
return {
name: 'copy-lang-folder',
apply: 'build', // Run only during build
configResolved(config) {
// Get the output directory from Vite config
outDir = 'build/';
},
async closeBundle() {
const srcDir = path.resolve(process.cwd(), 'lang');
const destDir = path.resolve(process.cwd(), outDir, 'lang');
if (!fs.existsSync(srcDir)) {
console.warn(`Source folder "lang" not found at: ${srcDir}`);
return;
}
try {
// Copy the "lang" folder recursively to the destination
await fs.promises.cp(srcDir, destDir, { recursive: true });
console.log(`Copied "lang" folder from ${srcDir} to ${destDir}`);
} catch (error) {
console.error('Error copying "lang" folder:', error);
}
},
};
}
function copyManifestPlugin(filename = 'manifest.json') {
let outDir = '';
return {
name: 'copy-manifest-json',
apply: 'build',
configResolved(config) {
outDir = 'build/';
},
async closeBundle() {
const srcPath = path.resolve(process.cwd(), filename);
const destPath = path.resolve(process.cwd(), outDir, filename);
if (!fs.existsSync(srcPath)) {
console.warn(`Manifest file not found at: ${srcPath}`);
return;
}
try {
await fs.promises.copyFile(srcPath, destPath);
console.log(`Copied manifest from ${srcPath} to ${destPath}`);
} catch (err) {
console.error('Failed to copy manifest.json:', err);
}
},
};
}
function generateLicensesPlugin() {
let outDir = '';
return {
name: 'generate-licenses',
apply: 'build',
configResolved(config) {
outDir = path.resolve(process.cwd(), 'build');
},
async closeBundle() {
// Don't regenerate if file already exists (SSR and client build run in separate processes)
const licensesPath = path.join(outDir, 'licenses.json');
if (fs.existsSync(licensesPath)) {
return;
}
// Run only once (closeBundle is called for both SSR and client builds)
if (!licensesGenerated) {
licensesGenerated = true;
try {
console.log('Generating licenses...');
// Ensure the build directory exists
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true });
}
collectLicenses(outDir);
console.log('Licenses generated successfully.');
} catch (err) {
console.error('Failed to generate licenses:', err);
// Don't fail the build, just warn
}
}
},
};
}
export default defineConfig(({ isSsrBuild, command }) => {
return {
clearScreen: false,
plugins: [sveltekit(), generateLicensesPlugin(), copyLangFolderPlugin(), copyManifestPlugin()],
ssr: {
noExternal:
command === 'build'
? true
: [
'chart.js',
'@tiptap/**',
'prosemirror-**',
'@tiptap/pm',
'@jill64/universal-sanitizer',
'@panomc/sdk',
'svelte-i18n',
],
},
css: {
preprocessorOptions: {
scss: {
api: 'modern-compiler',
loadPaths: [process.cwd(), path.resolve(process.cwd(), 'node_modules')],
quietDeps: true,
silenceDeprecations: ['mixed-decls', 'color-functions', 'global-builtin', 'import'],
},
},
},
optimizeDeps: {
include: ['deepmerge', 'svelte-i18n'],
exclude: ['@panomc/sdk', 'svelte'],
},
server: {
proxy: {
'/api': env.VITE_API_URL.replace('/api', ''),
'/panel/api': env.VITE_API_URL.replace('/api', ''),
},
allowedHosts: true,
hmr: {
path: '/panel/',
},
},
resolve: {
alias: {
'@theme-style':
command === 'serve'
? path.resolve(process.cwd(), 'src/styles/_empty.scss')
: path.resolve(process.cwd(), 'src/styles/style.scss'),
},
preserveSymlinks: true,
dedupe: ['svelte', '@panomc/sdk', 'svelte-i18n'],
},
build: {
manifest: true,
rollupOptions: {
// Only externalize in the client-side build to support the importmap.
// We let SSR build handle dependencies normally to avoid node_modules resolution issues.
...(isSsrBuild
? {}
: {
external: (id) => id.startsWith('svelte') || id.startsWith('@panomc/sdk'),
}),
},
},
};
});