Files
dawnline/web/tsup.config.ts
T
Aemeath 2f2aaf6c34
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
Dawnline 0.11.0
- Russian translation. The admin pages, the LuCI menu and the router's
  messages (errors, Health checks, update steps, the service log) come in
  Russian with the new luci-i18n-dawnline-ru package, released beside the
  app. LuCI shows it when its language is Russian, or "auto" with a Russian
  browser
- The translation installs with install.sh when LuCI is in Russian (or with
  --lang ru), follows every Dawnline update and rollback, and can be added
  or removed in Components → Dawnline → Translation
- The portal speaks the visitor's language: it opens in the system's
  language and switches between English and Russian in place, from the
  language pill beside its QR code. It carries its own translation and needs
  no package
- Set-up steps quote the apps' own menus word for word, in Russian too:
  AmneziaVPN is "+ → Insert → Continue" (it has no "Add server"), and the
  Mac and Windows menus of WireGuard and AmneziaWG have their real names
- Questions in the portal (Reconnect, Remove) read from the left edge and
  close with Cancel, without ×
- Numbers, sizes and dates follow the page's language. A server that can't
  start says why in one sentence; event details and service log lines read
  as whole sentences
- README in Russian (README_ru.md). rules/i18n.md: how strings are written
  and translated; CI checks the catalogs (npm run i18n:check)
2026-10-20 19:33:52 +03:00

134 lines
4.6 KiB
TypeScript

import { defineConfig } from 'tsup';
import { build, transform, type Plugin } from 'esbuild';
import fs from 'node:fs';
import path from 'node:path';
import { catalogOf, parsePo } from './i18n/po.ts';
const PKG = '../package/luci-app-dawnline';
const ADMIN_OUT = `${PKG}/htdocs/luci-static/resources/dawnline`;
// Outside the web root: the portal CGI serves them only with a valid code.
const PORTAL_OUT = `${PKG}/root/usr/share/dawnline/portal/assets`;
// LuCI evaluates a module's source as a function body whose parameters are
// the `'require x'` directives; it must `return` its class. tsup emits ESM,
// so the trailing `export { ... }` is rewritten into a baseclass.
function patchForLuci(file: string) {
const code = fs.readFileSync(file, 'utf8');
const patched = code.replace(/export\s*{([\s\S]*?)};?\s*$/, (_m, body: string) => {
const props = body
.split(',')
.map((s) => s.trim())
.filter(Boolean)
.map((s) => {
const [local, exported] = s.split(/\s+as\s+/);
return exported ? `${exported}: ${local}` : local;
});
return `return baseclass.extend({ ${props.join(', ')} });\n`;
});
if (patched === code) throw new Error(`LuCI patch failed: no export block in ${file}`);
fs.writeFileSync(file, patched);
}
/** Minify CSS with esbuild (the same engine that bundles the scripts). */
async function minifyCss(css: string): Promise<string> {
return (await transform(css, { loader: 'css', minify: true, legalComments: 'none' })).code.trim();
}
/** The portal's stylesheet: its parts (src/portal/styles, joined by @import) as one minified file. */
async function portalCss(): Promise<string> {
const r = await build({
entryPoints: ['src/portal/styles/index.css'],
bundle: true,
minify: true,
write: false,
legalComments: 'none',
// The fonts sit next to the stylesheet on the router: their URLs stay as written.
external: ['*.woff2'],
logLevel: 'silent',
});
return r.outputFiles[0].text.trim();
}
/** The portal's translations: a .po it imports becomes its lookup table (i18n/po.ts). */
export const poCatalog: Plugin = {
name: 'po-catalog',
setup(build) {
build.onLoad({ filter: /\.po$/ }, (args) => ({
contents: JSON.stringify(catalogOf(parsePo(fs.readFileSync(args.path, 'utf8')))),
loader: 'json',
}));
},
};
// The admin styles are plain .css files under src/admin/styles, imported as
// text and joined in styles/index.ts (rendered inside each view): each is
// minified here. They get a namespace (and a path not ending in .css) of
// their own, so tsup's CSS handling, which would emit a stylesheet, never sees them.
const adminCss: Plugin = {
name: 'admin-css-text',
setup(build) {
build.onResolve({ filter: /\.css$/ }, (args) =>
/admin[\\/]styles$/.test(args.resolveDir)
? { path: `${path.join(args.resolveDir, args.path)}#text`, namespace: 'dl-css' }
: undefined,
);
build.onLoad({ filter: /.*/, namespace: 'dl-css' }, async (args) => ({
contents: await minifyCss(fs.readFileSync(args.path.replace(/#text$/, ''), 'utf8')),
loader: 'text',
}));
},
};
export default defineConfig([
{
entry: { app: 'src/admin/main.ts' },
format: ['esm'],
outDir: ADMIN_OUT,
outExtension: () => ({ js: '.js' }),
target: 'es2020',
clean: false,
sourcemap: false,
// Identifiers only: LuCI's require directives in the banner must stay as written.
minify: true,
noExternal: ['qrcode-generator'],
esbuildPlugins: [adminCss],
banner: {
js: [
'// Generated from dawnline/web — do not edit by hand.',
"'use strict';",
"'require baseclass';",
"'require dom';",
"'require poll';",
"'require rpc';",
"'require ui';",
].join('\n'),
},
esbuildOptions(o) {
o.legalComments = 'none';
o.charset = 'utf8';
},
onSuccess: async () => patchForLuci(path.join(ADMIN_OUT, 'app.js')),
},
{
entry: { portal: 'src/portal/main.ts' },
format: ['iife'],
outDir: PORTAL_OUT,
outExtension: () => ({ js: '.js' }),
target: 'es2019',
clean: false,
sourcemap: false,
minify: true,
noExternal: ['qrcode-generator'],
esbuildPlugins: [poCatalog],
esbuildOptions(o) {
o.legalComments = 'none';
o.charset = 'utf8';
},
onSuccess: async () => {
fs.writeFileSync(path.join(PORTAL_OUT, 'portal.css'), (await portalCss()) + '\n');
fs.copyFileSync('src/portal/icon.svg', path.join(PORTAL_OUT, 'icon.svg'));
for (const f of fs.readdirSync('src/portal/fonts')) fs.copyFileSync(path.join('src/portal/fonts', f), path.join(PORTAL_OUT, f));
},
},
]);