forked from chaosarium/lwt
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathvite.app.config.ts
More file actions
101 lines (93 loc) · 3.29 KB
/
Copy pathvite.app.config.ts
File metadata and controls
101 lines (93 loc) · 3.29 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
import { defineConfig, type PluginOption } from 'vite';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import { cpSync, mkdirSync } from 'fs';
// @ts-expect-error - plain .mjs build helper, no types
import { prerenderPhpView } from './build/php-view-prerender.mjs';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
/**
* Bundled-client ("Model B") build.
*
* Emits standalone HTML pages — connect, library, reader — that boot the
* existing LWT frontend bundle (`src/frontend/js/main.ts`) against a remote
* `/api/v1` with no PHP server in the loop, for packaging into the Lukaisu
* Capacitor/F-Droid app (../lukaisu). Each page's body is prerendered from the
* real PHP view at build time (see build/php-view-prerender.mjs), so the Alpine
* scaffolds never drift from the server templates.
*
* Output: dist-app/ (consumed by ../lukaisu as its Capacitor webDir).
*
* Run: `npm run build:app` (from lwt/).
*/
/**
* Replace `<!--LWT_VIEW:relative/path/to/view.php-->` markers in the page
* wrappers with the prerendered static HTML of that PHP view.
*/
function injectPrerenderedViews(): PluginOption {
return {
name: 'lwt-inject-prerendered-views',
transformIndexHtml: {
order: 'pre' as const,
handler(html: string) {
return html.replace(
/<!--\s*LWT_VIEW:([\w.\-/]+)\s*-->/g,
(_m, rel: string) => {
const abs = resolve(__dirname, rel);
return prerenderPhpView(abs, (msg: string) =>
console.warn(`[prerender] ${rel}: ${msg}`)
);
}
);
}
}
};
}
/**
* Copy the review feedback sounds into dist-app/sounds so the prerendered
* `<audio>` sources (rewritten to ./sounds/*.mp3) resolve in the bundle.
*/
function copyReviewSounds(): PluginOption {
return {
name: 'lwt-copy-review-sounds',
apply: 'build',
closeBundle() {
const dest = resolve(__dirname, 'dist-app/sounds');
mkdirSync(dest, { recursive: true });
for (const file of ['success.mp3', 'failure.mp3']) {
cpSync(resolve(__dirname, 'assets/sounds', file), resolve(dest, file));
}
}
};
}
export default defineConfig({
root: resolve(__dirname, 'src/frontend/app'),
// Relative asset URLs so pages work when served from the bundle root inside
// the WebView (capacitor://localhost / https://localhost).
base: './',
publicDir: false,
resolve: {
alias: {
'@': resolve(__dirname, 'src/frontend/js'),
'@shared': resolve(__dirname, 'src/frontend/js/shared'),
'@modules': resolve(__dirname, 'src/frontend/js/modules'),
'@css': resolve(__dirname, 'src/frontend/css'),
// CSP-compliant Alpine build (matches vite.config.ts; the CSP eval
// restriction is enforced the same way in the bundled client).
'alpinejs': '@alpinejs/csp'
}
},
plugins: [injectPrerenderedViews(), copyReviewSounds()],
build: {
outDir: resolve(__dirname, 'dist-app'),
emptyOutDir: true,
target: 'es2020',
rollupOptions: {
input: {
index: resolve(__dirname, 'src/frontend/app/index.html'),
library: resolve(__dirname, 'src/frontend/app/library.html'),
read: resolve(__dirname, 'src/frontend/app/read.html'),
review: resolve(__dirname, 'src/frontend/app/review.html')
}
}
}
});