Files
wordpress-theme-aksrt/tools/build-public-assets.mjs
T
kate522 1f4a39d8f8 feat(runtime): 全站前端脚本统一接入 AksrtFrontend 全局 DOM 生命周期
- rehydrate.js 新增 boot()/aksrt:ready 事件,支持 boot 后晚注册立即以 init 触发
- aksrt-runtime.js 初始加载改走 AksrtFrontend.boot,tooltip/倒计时注册为组件
- 约 30 个前端脚本(首页、评论、支付、路由、页面叶子、widgets)
  从直接 DOMContentLoaded 迁移为 AksrtFrontend.register,并加幂等标记
- section-navs 依赖改为 aksrt-runtime,保证注册时序
- 清理死代码:删除孤儿 loader.js/main.js 及 runtime/page-navs 遗留副本
- loader.js 移除已迁移模块的 tbquire 路径映射,main.js 相应瘦身
- build-public-assets.mjs 增加 runtime/main.min.js 自动压缩步骤
- 新增 4 个契约测试(homepage-dom/rehydrate-runtime/global-dom-init/
  public-bundle-parity),3 个既有 harness 适配注册表初始化方式
2026-09-02 21:48:56 +08:00

218 lines
7.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const require = createRequire(path.join(repoRoot, 'assets/admin/package.json'));
const { transform } = require('esbuild');
const cssBuildDir = path.join(repoRoot, 'assets/css/build');
const jsBuildDir = path.join(repoRoot, 'assets/js/build');
const cssBundles = {
'public.css': [
'assets/css/bootstrap.min.css',
'assets/fontawesome5/css/all.min.css',
'assets/css/main.min.css',
'style.css',
'assets/css/core/tokens.css',
'assets/css/core/reset.css',
'assets/css/core/typography.css',
'assets/css/core/buttons.css',
'assets/css/core/forms.css',
'assets/css/core/alerts.css',
'assets/css/core/animations.css',
'assets/css/core/components-ext.css',
'assets/css/core/color-utilities.css',
'assets/css/frontend/base.css',
'assets/css/frontend/header-userbox.css',
'assets/css/frontend/card-primitives.css',
'assets/css/frontend/overlay-primitives.css',
'assets/css/frontend/layout.css',
'assets/css/frontend/footer.css',
'assets/css/frontend/floating-buttons.css',
'assets/css/frontend/enhancements.css',
'assets/css/frontend/skeleton.css',
'assets/css/frontend/empty-state.css',
'assets/css/frontend/pagination.css',
'assets/css/frontend/ajax-spinner.css',
'assets/css/frontend/modal-runtime.css',
'assets/css/frontend/notyf.css',
'assets/css/shared/captcha.css',
],
'homepage.css': [
'assets/css/frontend/post-card.css',
'assets/css/frontend/sidebar.css',
'assets/css/frontend/breadcrumb.css',
'assets/css/frontend/homepage-fixed/base.css',
'assets/css/frontend/homepage-fixed/ajax-tab-skeleton-placeholder.css',
'assets/css/frontend/homepage-fixed/ajax-tab-error-state.css',
],
'single.css': [
'assets/css/frontend/legacy-utilities.css',
'assets/css/frontend/single-post/shell.css',
'assets/css/frontend/single-post/content.css',
'assets/css/frontend/single-post/author-actions.css',
],
'comments.css': [
'assets/css/frontend/comments/layout.css',
'assets/css/frontend/comments/shell.css',
'assets/css/frontend/comments/mobile-item.css',
],
};
const publicScripts = [
'assets/js/runtime/libs/bootstrap.min.js',
'assets/js/runtime/loader.js',
'assets/js/lazy-image-attr.js',
'assets/js/rehydrate.js',
'assets/js/notyf.js',
'assets/js/aksrt-runtime.js',
'assets/js/grade.min.js',
'assets/js/core/modal-telemetry.js',
'assets/js/core/request.js',
'assets/js/core/modal-cache.js',
'assets/js/core/modal-state.js',
'assets/js/core/modal-runtime.js',
'assets/js/core/modal-triggers.js',
'assets/js/core/article.js',
'assets/js/core/comments.js',
'assets/js/core/comment-fixed-input.js',
'assets/js/core/comment-move-form.js',
'assets/js/core/ajax-list.js',
'assets/js/core/pagination.js',
'assets/js/core/archive.js',
'assets/js/core/footer-tabbar.js',
'assets/js/core/misc.js',
'assets/js/follow.js',
'assets/js/enhancements.js',
'assets/js/enhancements2.js',
'assets/js/color-extract.js',
'assets/js/effects.js',
'assets/js/runtime/main.min.js',
'assets/js/captcha-front.js',
'assets/js/instant-page.js',
];
function normalizePath(filePath) {
return filePath.split(path.sep).join('/');
}
function rewriteCssUrls(css, sourcePath, outputPath) {
const outputDir = path.dirname(outputPath);
return css.replace(/url\(\s*(['"]?)([^)'"\s]+)\1\s*\)/gi, (match, quote, rawUrl) => {
if (/^(?:data:|https?:|\/\/|\/|#|var\()/i.test(rawUrl)) {
return match;
}
const suffixIndex = rawUrl.search(/[?#]/);
const pathname = suffixIndex === -1 ? rawUrl : rawUrl.slice(0, suffixIndex);
const suffix = suffixIndex === -1 ? '' : rawUrl.slice(suffixIndex);
const absoluteAsset = path.resolve(path.dirname(sourcePath), pathname);
let relativeAsset = normalizePath(path.relative(outputDir, absoluteAsset));
if (!relativeAsset.startsWith('.')) {
relativeAsset = `./${relativeAsset}`;
}
const wrapper = quote || '"';
return `url(${wrapper}${relativeAsset}${suffix}${wrapper})`;
});
}
async function expandCss(sourcePath, outputPath, stack = []) {
const absoluteSource = path.resolve(sourcePath);
if (stack.includes(absoluteSource)) {
throw new Error(`Circular CSS import: ${normalizePath(path.relative(repoRoot, absoluteSource))}`);
}
const source = await readFile(absoluteSource, 'utf8');
const importPattern = /@import\s+(?:url\(\s*)?['"]([^'"]+\.css)['"]\s*\)?\s*;/gi;
let expanded = '';
let cursor = 0;
let match;
while ((match = importPattern.exec(source)) !== null) {
expanded += rewriteCssUrls(source.slice(cursor, match.index), absoluteSource, outputPath);
const importedPath = path.resolve(path.dirname(absoluteSource), match[1]);
expanded += await expandCss(importedPath, outputPath, [...stack, absoluteSource]);
cursor = importPattern.lastIndex;
}
expanded += rewriteCssUrls(source.slice(cursor), absoluteSource, outputPath);
return expanded.trimEnd();
}
async function buildCssBundle(outputName, sources) {
const outputPath = path.join(cssBuildDir, outputName);
const sections = [];
for (const source of sources) {
const sourcePath = path.join(repoRoot, source);
sections.push(`/* source: ${source} */\n${await expandCss(sourcePath, outputPath)}`);
}
const bundle = `${sections.join('\n\n')}\n`;
const minified = await transform(bundle, {
charset: 'utf8',
legalComments: 'inline',
loader: 'css',
minify: true,
sourcefile: outputName,
});
const minifiedPath = outputPath.replace(/\.css$/, '.min.css');
await Promise.all([
writeFile(outputPath, bundle, 'utf8'),
writeFile(minifiedPath, minified.code, 'utf8'),
]);
}
async function buildScriptBundle() {
const sections = [];
for (const source of publicScripts) {
const content = (await readFile(path.join(repoRoot, source), 'utf8')).trimEnd();
sections.push(`/* source: ${source} */\n${content}\n;`);
}
await writeFile(path.join(jsBuildDir, 'public.js'), `${sections.join('\n\n')}\n`, 'utf8');
}
async function buildRuntimeMainMin() {
const sourcePath = path.join(repoRoot, 'assets/js/runtime/main.js');
const outputPath = path.join(repoRoot, 'assets/js/runtime/main.min.js');
const source = await readFile(sourcePath, 'utf8');
const minified = await transform(source, {
charset: 'utf8',
legalComments: 'inline',
loader: 'js',
minifyIdentifiers: false,
minifySyntax: true,
minifyWhitespace: true,
sourcefile: 'runtime/main.js',
});
await writeFile(outputPath, minified.code, 'utf8');
}
// 评论区硬切换后 main.min.css 由 main.css 重新生成,保持两者同步。
async function buildMainMin() {
const sourcePath = path.join(repoRoot, 'assets/css/main.css');
const outputPath = path.join(repoRoot, 'assets/css/main.min.css');
const source = await readFile(sourcePath, 'utf8');
const minified = await transform(source, {
charset: 'utf8',
legalComments: 'inline',
loader: 'css',
minify: true,
sourcefile: 'main.css',
});
await writeFile(outputPath, minified.code, 'utf8');
}
await mkdir(cssBuildDir, { recursive: true });
await mkdir(jsBuildDir, { recursive: true });
await buildRuntimeMainMin();
// 先刷新 main.min.css,public.css 打包时才能读到最新内容。
await buildMainMin();
for (const [outputName, sources] of Object.entries(cssBundles)) {
await buildCssBundle(outputName, sources);
}
await buildScriptBundle();
console.log('Built public CSS and JS bundles.');