Repository navigation
Expand file tree
/
Copy pathbuild_common.php
More file actions
225 lines (176 loc) · 6.51 KB
/
Copy pathbuild_common.php
File metadata and controls
225 lines (176 loc) · 6.51 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
/**
* Shared helpers for the build scripts (build.php, buildfront.php).
* No main flow here — each script drives its own pipeline.
*/
function run(string $command, string $cwd): void
{
echo '$ ' . $command . PHP_EOL;
$descriptor = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];
$process = proc_open($command, $descriptor, $pipes, $cwd);
if (!is_resource($process)) {
throw new RuntimeException("Could not execute: $command");
}
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$exitCode = proc_close($process);
echo $stdout;
if ($stderr !== '') {
fwrite(STDERR, $stderr);
}
if ($exitCode !== 0) {
throw new RuntimeException(
"Command failed with exit code $exitCode: $command"
);
}
}
/**
* Rewrite every relative url() in a stylesheet to a data: URI so the
* referencing artifact stays fully self-contained (Vite emits fonts as
* separate files that cannot be fetched once the CSS is inlined).
* `$cssFile` is the on-disk path the CSS was read from (resolves `./`
* refs); `assets/` refs resolve against `$dist`.
*/
function inline_css_urls(string $css, string $dist, string $cssFile): string
{
$inlined = preg_replace_callback(
'/url\(\s*(?:"([^"]+)"|\'([^\']+)\'|([^)\s]*?))\s*\)/i',
function (array $m) use ($dist, $cssFile): string {
$url = $m[1] !== '' ? $m[1] : ($m[2] !== '' ? $m[2] : trim($m[3]));
if ($url === '' || str_starts_with($url, 'data:') || str_starts_with($url, 'http')) {
return $m[0];
}
$candidate = null;
if (str_starts_with($url, './')) {
$candidate = dirname($cssFile) . '/' . substr($url, 2);
} elseif (str_starts_with($url, 'assets/')) {
$candidate = $dist . '/' . $url;
}
$candidate = $candidate !== null ? realpath($candidate) : false;
if ($candidate === false || !is_file($candidate)) {
return $m[0];
}
$mime = match (pathinfo($candidate, PATHINFO_EXTENSION)) {
'woff2' => 'font/woff2',
'woff' => 'font/woff',
'ttf' => 'font/ttf',
'otf' => 'font/otf',
'eot' => 'application/vnd.ms-fontobject',
'svg' => 'image/svg+xml',
'png' => 'image/png',
'jpg', 'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'webp' => 'image/webp',
default => 'application/octet-stream',
};
$data = file_get_contents($candidate);
if ($data === false) {
return $m[0];
}
return 'url("data:' . $mime . ';base64,' . base64_encode($data) . '")';
},
$css
);
return $inlined ?? $css;
}
/**
* Write a ZIP archive with `name => bytes` entries, overwriting any
* existing file. Uses PharData (the `zip` extension is not assumed).
*/
function zip_create(string $path, array $entries): void
{
if (is_file($path) && !unlink($path)) {
throw new RuntimeException("Could not remove existing $path");
}
$zip = new PharData($path, 0, null, Phar::ZIP);
foreach ($entries as $name => $data) {
$zip[$name] = $data;
}
unset($zip);
if (!is_file($path)) {
throw new RuntimeException("Could not write $path");
}
}
/**
* Inline every stylesheet and script that references a built asset
* in dist/. The result is a fully self-contained HTML document.
*/
function inline_assets(string $html, string $dist, bool $dev): string
{
$html = preg_replace_callback(
'/<link\b[^>]*\bhref="([^"]+)"[^>]*>/i',
function (array $match) use ($dist): string {
$tag = $match[0];
$href = $match[1];
// Only inline stylesheets that point into dist/assets/.
if (!str_contains($tag, 'stylesheet') || !str_contains($href, 'assets/')) {
return $tag;
}
$file = $dist . '/' . ltrim(html_entity_decode($href), './');
if (!is_file($file)) {
return $tag;
}
$css = file_get_contents($file);
if ($css === false) {
return $tag;
}
$css = inline_css_urls($css, $dist, $file);
return '<style>' . $css . '</style>';
},
$html
);
if ($html === null) {
throw new RuntimeException('Could not inline stylesheets');
}
$html = preg_replace_callback(
'/<script\b[^>]*\bsrc="([^"]+)"[^>]*>/i',
function (array $match) use ($dist, $dev): string {
$tag = $match[0];
$src = $match[1];
// Only inline scripts that point into dist/assets/.
if (!str_contains($src, 'assets/')) {
return $tag;
}
$file = $dist . '/' . ltrim(html_entity_decode($src), './');
if (!is_file($file)) {
return $tag;
}
$js = file_get_contents($file);
if ($js === false) {
return $tag;
}
// Keep type/crossorigin attributes; drop the src attribute.
// The opening tag has no closing part, so append the JS and
// a single closing tag.
$cleaned = preg_replace('/\bsrc="[^"]*"/i', '', $tag);
$js = $cleaned . $js . '</script>';
// Dev builds carry a source map. Embed it as a data URI so the
// inlined bundle still resolves readable stack traces no matter
// where the single file is served from.
if ($dev && preg_match('/\/\/# sourceMappingURL=(index-[A-Za-z0-9_-]+\.js\.map)/', $js, $m)) {
$map = $dist . '/assets/' . $m[1];
if (is_file($map)) {
$data = file_get_contents($map);
if ($data !== false) {
$uri = 'data:application/json;charset=utf-8;base64,' . base64_encode($data);
$js = str_replace('//# sourceMappingURL=' . $m[1], '//# sourceMappingURL=' . $uri, $js);
}
}
}
return $js;
},
$html
);
if ($html === null) {
throw new RuntimeException('Could not inline scripts');
}
return $html;
}