8a48765a46
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
import { readFile, access } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
export async function verifyBuild(directory) {
|
|
const root = path.resolve(directory);
|
|
const html = await readFile(path.join(root, 'index.html'), 'utf8');
|
|
// Inspect actual resource tags, not URLs embedded in comments or inline scripts.
|
|
const markup = html
|
|
.replace(/<!--[\s\S]*?-->/g, '')
|
|
.replace(/(<script\b[^>]*>)[\s\S]*?<\/script\s*>/gi, '$1</script>');
|
|
const tags = [
|
|
...markup.matchAll(/<(?:script|link|img|source)\b[^>]*>/gi),
|
|
].map((match) => match[0]);
|
|
const assets = tags
|
|
.flatMap((tag) => [...tag.matchAll(/\s(?:src|href)\s*=["']([^"']+)["']/g)])
|
|
.map((match) => match[1].split(/[?#]/)[0])
|
|
// The inline brand bootstrap resolves this server placeholder to /logo.svg.
|
|
.map((value) => (value === '__WL_BRAND_FAVICON__' ? '/logo.svg' : value))
|
|
.filter((value) => value && !/^(?:[a-z]+:|\/\/|#)/i.test(value));
|
|
if (!assets.some((value) => /\.js$/.test(value)))
|
|
throw new Error('Build has no JavaScript entry');
|
|
for (const asset of assets) {
|
|
const filename = path.resolve(root, asset.replace(/^\//, ''));
|
|
if (!filename.startsWith(root + path.sep))
|
|
throw new Error(`Build asset escapes output directory: ${asset}`);
|
|
await access(filename);
|
|
}
|
|
return { root, assets: assets.length };
|
|
}
|
|
|
|
if (
|
|
process.argv[1] &&
|
|
path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
|
|
) {
|
|
console.log(await verifyBuild(process.argv[2] || 'dist'));
|
|
}
|