Files
doona/vite.config.ts
Zakk edbcf9c2e6 chore(license): relicense doona under AGPL-3.0-only (#699)
## Problem / 问题

Under GPL-3.0, a service that runs a modified doona panel for its
customers does not have to publish its changes, because it never
distributes the program.

在 GPL-3.0 下,把修改过的 doona 面板架成服务给客户用,因为没有散布程序,不必公开修改。

## How I fixed it / 解决方法

doona is licensed AGPL-3.0-only. LICENSE, LICENSES/, REUSE.toml,
package.json, the READMEs, NOTICE and the Alpine, Gentoo, nfpm, Nix and
OpenWrt metadata say so; the About box reads the licence from
package.json and already links the source repository. Lines still
contributed by eries (14 files) and qiuzi (4 files) stay GPL-3.0-only,
combined under GPL-3.0 section 13, and REUSE.toml records them. The
pinned rc.7 archive keeps its original licence in the packaging.

doona 改用 AGPL-3.0-only。eries 与 qiuzi 留下的代码仍按 GPL-3.0-only,依 GPL-3.0 第 13
条合并;已发布的 rc.7 源码包保持原授权。

## Verified / 验证

- `pnpm check`, coverage (4,336 tests), build, size, and Chromium/WebKit
E2E (2,356 passed, 35 skipped, in the Playwright container).
- LICENSE matches the gnu.org AGPL-3.0 text; LICENSES/AGPL-3.0-only.txt
matches SPDX license-list-data v3.28.0 byte for byte.
- Notice staging and the Alpine, Gentoo and OpenWrt metadata checks.
- `reuse lint` reports only the existing lite gaps (230 licence, 218
copyright entries), unchanged by this branch.
- Not verified: package builds on Alpine, Gentoo, OpenWrt and Nix.

---

- [x] I understand this change and answer for every line
- [x] This branch is based on the current main
- [x] I ran the repository's gates and recorded the results in Verified
- [x] Each commit is one coherent change
- [x] If behaviour changed, I updated the documentation
- [ ] If interface text changed, I updated `en.json` and every complete
language's catalogue
- [ ] If interface appearance changed, I attached light and dark
screenshots
- [ ] If the API contract changed, I ran `pnpm gen:api` and `pnpm
check:gen`
- [ ] I checked the boxes above without reading them
- [ ] I am an AI agent, and this content was submitted without human
review
2026-11-25 12:04:39 +11:00

221 lines
11 KiB
TypeScript

import {defineConfig} from 'vite';
import {fileURLToPath} from 'node:url';
import {createHash} from 'node:crypto';
import {readFileSync} from 'node:fs';
import {createRequire} from 'node:module';
import {version, license, repository, config} from './package.json';
import react from '@vitejs/plugin-react';
import optimizeLocales from '@react-aria/optimize-locales-plugin';
import {DEFAULT_PALETTE, palettes} from './src/shell/palettes';
import {rtlScripts} from './src/i18n/direction';
import {languages, REFERENCE_LANG} from './src/i18n/languages';
import {languageFiles} from './src/i18n/offline';
import {startupTextPlugin} from './tools/startup-text.mjs';
import {cataloguePart, cataloguePartsPlugin} from './tools/catalogue-parts.mjs';
import {stampScript} from './tools/stamp.mjs';
import {bundledPackagesPlugin} from './tools/bundled-packages.mjs';
// lightningcss ships native binaries for x86_64, aarch64 and armv7; on any other architecture the build
// minifies CSS with esbuild instead, so a packager on riscv64 or loong64 is not stopped by it.
const cssMinify = (() => {
try {
createRequire(import.meta.url)('lightningcss');
return 'lightningcss' as const;
} catch {
return 'esbuild' as const;
}
})();
// The mock backend, loaded only by the demo and development profiles.
const mockEntry = /\/mock\/index\.ts$/;
// The modules a module reaches by static imports; the Activity chunk takes those the sign-in path does not reach.
const statics = (start: string, getModuleInfo: (id: string) => {importedIds: readonly string[]} | null) => {
const seen = new Set<string>();
const queue = [start];
while (queue.length) {
const id = queue.pop()!;
if (seen.has(id)) continue;
seen.add(id);
queue.push(...(getModuleInfo(id)?.importedIds ?? []));
}
return seen;
};
let reach: {activity: Set<string>; shell: Set<string>} | undefined;
function activityOnly(id: string, getModuleInfo: (id: string) => {importedIds: readonly string[]} | null) {
if (!reach) {
// The desktop panel and the dashboard's cards load with the page, and so does the signed-in frame around it, so
// their code shares its chunk.
const starts = ['widgets/Dashboard', 'widgets/WidgetContent', 'widgets/Widgets', 'Frame'].map(name =>
fileURLToPath(new URL(`src/shell/${name}.tsx`, import.meta.url))
);
// The sign-in path loads the entry, the sign-in forms and, on the demo, the mock backend, so what they reach stays
// out of the chunk.
const login = ['src/main.tsx', 'src/shell/Login.tsx', 'mock/index.ts'].map(name => fileURLToPath(new URL(name, import.meta.url)));
reach = {
activity: new Set(starts.flatMap(start => [...statics(start, getModuleInfo)])),
shell: new Set(login.flatMap(start => [...statics(start, getModuleInfo)]))
};
}
return reach.activity.has(id) && !reach.shell.has(id);
}
export default defineConfig({
base: './',
define: {
'import.meta.env.VITE_DOONA_VERSION': JSON.stringify(version),
'import.meta.env.VITE_DOONA_LICENSE': JSON.stringify(license),
// The links the about box and the sidebar open: doona's own repository, and the organisation whose engine
// (honk today, dae later) the backend names in its version; neither is written in a component.
'import.meta.env.VITE_DOONA_REPO': JSON.stringify(repository.url.replace(/\.git$/, '')),
'import.meta.env.VITE_ENGINE_ORG': JSON.stringify(config.engineOrg),
'import.meta.env.VITE_DOONA_CONTRACT_COMMIT': JSON.stringify(
readFileSync(new URL('contract/api-standardize/SOURCE.md', import.meta.url), 'utf8').match(/^Pin: (.+)$/m)![1]
)
},
plugins: [
bundledPackagesPlugin(['vite', 'rollup', '@react-aria/optimize-locales-plugin']),
react(),
startupTextPlugin(),
cataloguePartsPlugin(),
{
name: 'font-notices',
generateBundle() {
for (const [fileName, path] of [
['fonts/OFL.txt', 'LICENSES/OFL-1.1.txt'],
['fonts/README', 'docs/fonts.md']
]) {
this.emitFile({type: 'asset', fileName, source: readFileSync(new URL(path, import.meta.url), 'utf8')});
}
}
},
{
// The stored theme and language are stamped on <html> by an inline script before the stylesheet can paint,
// so a returning dark-theme reader never sees a light first frame. The CSP allows that one script by hash.
// Every palette family's stylesheet but the default's is linked from the script too (Glass's is glass.css, which
// brings its tokens), so the build's names for them are filled in once the bundle exists.
name: 'first-paint-stamp',
transformIndexHtml: {
order: 'post',
handler(html, {bundle}) {
const defaultFamily = DEFAULT_PALETTE.split('/')[0];
const ownSheets = [...new Set(palettes.map(palette => palette.id.split('/')[0]))]
.filter(family => family !== defaultFamily)
.map(family => [family, family === 'glass' ? 'src/ui/styles/glass.css' : `src/ui/styles/palettes/${family}.css`]);
const paletteStylesheets = Object.fromEntries(
ownSheets.map(([family, source]) => {
const file = bundle
? Object.values(bundle).find(file => file.type === 'asset' && file.originalFileNames.some(name => name.endsWith(source)))?.fileName
: source;
if (!file) throw new Error(`The build has no ${family} stylesheet to link before first paint`);
return [family, `./${file}`];
})
);
const stamp = stampScript({
palettes: palettes.map(palette => palette.id),
defaultPalette: DEFAULT_PALETTE,
locales: Object.fromEntries(languages.map(language => [language.id, language.locale])),
referenceLocale: languages.find(language => language.id === REFERENCE_LANG)!.locale,
rtlScripts,
paletteStylesheets
});
const digest = createHash('sha256').update(stamp).digest('base64');
return html
.replace("default-src 'self';", `default-src 'self'; script-src 'self' 'sha256-${digest}';`)
.replace('<head>\n', `<head>\n<script>${stamp}</script>\n`);
}
}
},
{
...optimizeLocales.vite({locales: languages.map(language => language.locale)}),
enforce: 'pre'
},
{
name: 'offline-shell',
apply: 'build',
enforce: 'post',
generateBundle(_, bundle) {
const files = Object.keys(bundle)
.filter(name => name === 'index.html' || name.startsWith('assets/'))
.sort();
// A reader needs one language, so no catalogue or its stylesheet is installed up front. The worker caches the
// language a page it controls reports, and any it loads later. Every file still counts towards the build hash.
const perLanguage = languageFiles(
files.map(name => {
const entry = bundle[name];
return entry.type === 'chunk'
? {name, catalogue: cataloguePart(entry.facadeModuleId)?.lang ?? null, sources: []}
: {name, catalogue: null, sources: entry.originalFileNames};
})
);
const languageOnly = new Set(Object.values(perLanguage).flat());
// The mock backend serves the demo and development only; the worker caches it once a page reports running on it.
const mock = files.filter(name => {
const entry = bundle[name];
return entry.type === 'chunk' && entry.facadeModuleId !== null && mockEntry.test(entry.facadeModuleId);
});
const precache = files.filter(name => !languageOnly.has(name) && !mock.includes(name));
const template = readFileSync(new URL('public/sw.js', import.meta.url), 'utf8');
const hash = createHash('sha256').update(template);
for (const name of files) {
const entry = bundle[name];
hash.update(name).update(entry.type === 'chunk' ? entry.code : entry.source);
}
const build = hash.digest('hex').slice(0, 16);
// index.html carries the build too, so a page can tell a worker of a new build from one of its own.
const html = bundle['index.html'];
if (html?.type !== 'asset' || !String(html.source).includes('<head>\n')) this.error('index.html has no <head> to stamp the build on');
html.source = String(html.source).replace('<head>\n', `<head>\n<meta name="doona-build" content="${build}">\n`);
this.emitFile({
type: 'asset',
fileName: 'sw.js',
source: template
.replace('__BUILD_HASH__', build)
.replace("'__PRECACHE__'", JSON.stringify(precache))
.replace("'__LANGUAGES__'", JSON.stringify(perLanguage))
.replace("'__MOCK__'", JSON.stringify(mock))
});
}
}
],
// Catalogues are read whole as their default export, so each key needs no named export of its own.
json: {namedExports: false},
build: {
// Even small slices stay separate so unicode-range controls their downloads.
assetsInlineLimit: file => (/noto-sans-(tc|sc)-.*\.woff2$/.test(file) ? false : undefined),
manifest: true,
target: ['es2022'],
// Terser reduces total gzip size without moving lazy modules into the shell.
minify: 'terser',
cssTarget: ['chrome120', 'safari17', 'firefox121', 'edge120'],
cssMinify,
rollupOptions: {
input: {
index: fileURLToPath(new URL('index.html', import.meta.url))
},
output: {
// Keep optional CJK subsets out of the shell precache and in the separate font archive.
assetFileNames: asset =>
asset.names.some(name => /^noto-sans-(tc|sc)-.*\.woff2$/.test(name)) ? 'fonts/[name]-[hash][extname]' : 'assets/[name]-[hash][extname]',
// Catalogue parts are named apart, so the service worker can leave them out of its precache.
chunkFileNames: chunk => {
const part = cataloguePart(chunk.facadeModuleId);
return part ? `assets/locale-${part.lang}-${part.stage}-[hash].js` : 'assets/[name]-[hash].js';
},
// A manual chunk holds only the modules named for it; their other dependencies stay where Rollup puts them, so
// the shell's modules are not pulled into the Activity chunk.
onlyExplicitManualChunks: true,
manualChunks(id, {getModuleInfo}) {
// Keep the React runtime and react-aria shared utilities in the startup vendor chunk.
if (/\/node_modules\/(react|react-dom|scheduler|clsx|use-sync-external-store)\//.test(id)) return 'vendor-react';
// react-aria is left to Rollup so the components only lazy pages use (drag and drop, grids) stay out of the
// startup chunk.
if (/\/node_modules\/(@codemirror|@lezer|style-mod|w3c-keyname|crelt)\//.test(id)) return 'vendor-editor';
// The Activity page and the signed-in frame are one chunk, requested as the shell starts. Code it shares with
// later pages stays in it rather than in many small shared chunks, since it is always loaded by then.
if (activityOnly(id, getModuleInfo)) return 'activity';
}
}
}
}
});