Repository navigation
Expand file tree
/
Copy pathbuildfront.php
More file actions
208 lines (165 loc) · 6.54 KB
/
Copy pathbuildfront.php
File metadata and controls
208 lines (165 loc) · 6.54 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
/**
* Build every sifront SPA (each a pnpm workspace package under sifronts/)
* into a ZIP bundle: dist/<name>.sifront.
*
* The bundle has exactly two entries:
* meta.json identity + version + theme keys (require_keys, …)
* bundle.js the raw app bundle with its CSS/fonts inlined
*
* Usage:
*
* php buildfront.php dev bundle (unminified + embedded sourcemap)
* php buildfront.php release release bundle (minified)
*
* Shares the build/inlining helpers with build.php (build_common.php).
* This script does NOT touch the PHP artifact (dist/index.php /
* dist/sifpress.php) — use build.php for that.
*
* A sifront is served inside a small HTML shell generated by the backend
* (see src/spa.php), which supplies the theme bootstrap and the shared
* ui-sdk module; the ZIP itself carries no HTML.
*/
$root = __DIR__;
$outputDir = $root . '/dist';
$mode = $argv[1] ?? 'dev';
if ($mode === 'release' || $mode === 'rel') {
$isRelease = true;
} else {
$isRelease = false;
$mode = 'dev';
}
$buildCommand = $isRelease ? 'pnpm run build:release' : 'pnpm run build:dev';
require __DIR__ . '/build_common.php';
/**
* Build one sifront SPA and package its Vite output into a two-entry ZIP
* (`meta.json` + `bundle.js`) written to <outputDir>/<name>.sifront.
*/
function build_sifront(string $appDir, string $outputDir, string $buildCommand, bool $dev): void
{
$name = basename($appDir);
$dist = $appDir . '/dist';
if (!is_dir($appDir . '/node_modules')) {
run('pnpm install', $appDir);
}
run($buildCommand, $appDir);
$html = file_get_contents($dist . '/index.html');
if ($html === false) {
throw new RuntimeException("Could not read $name/dist/index.html");
}
/*
* Locate the Vite app bundle + stylesheet. The ZIP carries JS and CSS
* as a single `bundle.js`: the stylesheet is prepended as a tiny runtime
* injector and its fonts are inlined as data: URIs so the file stays
* self-contained.
*/
if (preg_match('#<script[^>]*\bsrc="((?:\./)?assets/[^"]+\.js)"#i', $html, $jsMatch) !== 1) {
throw new RuntimeException("No app bundle found in $name/dist/index.html");
}
$jsFile = $dist . '/' . ltrim(html_entity_decode($jsMatch[1]), './');
$js = file_get_contents($jsFile);
if ($js === false) {
throw new RuntimeException("Could not read $jsFile");
}
$css = '';
if (preg_match('#<link[^>]*\bhref="((?:\./)?assets/[^"]+\.css)"#i', $html, $cssMatch) === 1) {
$cssFile = $dist . '/' . ltrim(html_entity_decode($cssMatch[1]), './');
$rawCss = file_get_contents($cssFile);
if ($rawCss === false) {
throw new RuntimeException("Could not read $cssFile");
}
$css = inline_css_urls($rawCss, $dist, $cssFile);
}
/*
* Dev bundles carry a source map: embed it as a data URI so the ZIP
* still has exactly two entries while stack traces stay readable.
*/
if ($dev && preg_match('/\/\/# sourceMappingURL=(index-[A-Za-z0-9_-]+\.js\.map)/', $js, $mapMatch) === 1) {
$mapFile = $dist . '/assets/' . $mapMatch[1];
if (is_file($mapFile)) {
$mapData = file_get_contents($mapFile);
if ($mapData !== false) {
$uri = 'data:application/json;charset=utf-8;base64,' . base64_encode($mapData);
$js = str_replace('//# sourceMappingURL=' . $mapMatch[1], '//# sourceMappingURL=' . $uri, $js);
}
}
}
$cssLiteral = json_encode(
$css,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
| JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
);
if ($cssLiteral === false) {
throw new RuntimeException("Could not encode the $name stylesheet");
}
$bundle = '(function(){var s=document.createElement("style");'
. 's.setAttribute("data-sifront","' . $name . '");s.textContent='
. $cssLiteral . ';document.head.appendChild(s);})();'
. "\n" . $js;
/*
* meta.json is the sifront's identity + theme contract. Author keys
* (require_keys, …) are kept; identity fields are injected from
* package.json / the build environment.
*/
$meta = [];
$metaFile = $appDir . '/meta.json';
if (is_file($metaFile)) {
$decoded = json_decode((string) file_get_contents($metaFile), true);
if (!is_array($decoded)) {
throw new RuntimeException("Invalid JSON in $metaFile");
}
$meta = $decoded;
}
$pkgFile = $appDir . '/package.json';
$pkg = is_file($pkgFile) ? json_decode((string) file_get_contents($pkgFile), true) : null;
$version = is_array($pkg) && is_string($pkg['version'] ?? null) ? $pkg['version'] : '0.0.0';
$title = '';
if (is_string($meta['title'] ?? null) && $meta['title'] !== '') {
$title = $meta['title'];
} elseif (preg_match('#<title>([^<]*)</title>#i', $html, $titleMatch) === 1) {
$title = trim($titleMatch[1]);
}
$meta = array_merge($meta, [
'schema' => 1,
'name' => $name,
'title' => $title !== '' ? $title : $name,
'version' => $version,
'built_at' => gmdate('c'),
'entry' => 'bundle.js',
]);
$metaJson = json_encode(
$meta,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT
);
if ($metaJson === false) {
throw new RuntimeException("Could not encode meta for $name");
}
$output = $outputDir . '/' . $name . '.sifront';
zip_create($output, [
'meta.json' => $metaJson,
'bundle.js' => $bundle,
]);
/*
* Dev-serving companions: the backend reads these plain files (never the
* ZIP), so the in-repo `sifpress1` can be iterated on without a re-upload.
*/
file_put_contents($outputDir . '/' . $name . '.meta.json', $metaJson);
file_put_contents($outputDir . '/' . $name . '.bundle.js', $bundle);
echo "Sifront bundle: $output (v$version)" . PHP_EOL;
}
$sifrontApps = glob($root . '/sifronts/*', GLOB_ONLYDIR);
if ($sifrontApps === false || $sifrontApps === []) {
throw new RuntimeException('No sifront packages found under sifronts/');
}
foreach ($sifrontApps as $sifrontApp) {
if (!is_file($sifrontApp . '/package.json')) {
continue;
}
build_sifront($sifrontApp, $outputDir, $buildCommand, !$isRelease);
}
echo PHP_EOL;
echo 'Build complete.' . PHP_EOL;
echo 'Mode: ' . $mode . PHP_EOL;
echo PHP_EOL;
echo 'Bundles are ZIP archives (meta.json + bundle.js), ready to be stored/served as a sifront.' . PHP_EOL;