chore: establish formatting baseline and verification scripts

This commit is contained in:
2026-10-28 12:11:36 +03:30
parent 751af768ce
commit 08b6cd464b
33 changed files with 7264 additions and 5006 deletions
+37 -32
View File
@@ -1,29 +1,37 @@
import {defineConfig} from 'vite';
import {readdirSync, statSync} from 'node:fs';
import {join, relative} from 'node:path';
import {createHash} from 'node:crypto';
import { defineConfig } from 'vite';
import { readdirSync, statSync } from 'node:fs';
import { join, relative } from 'node:path';
import { createHash } from 'node:crypto';
const listFiles = dir => readdirSync(dir).flatMap(name => {
const listFiles = (dir) =>
readdirSync(dir).flatMap((name) => {
const path = join(dir, name);
return statSync(path).isDirectory() ? listFiles(path) : [path];
});
});
// Emits sw.js listing every built file, so the installed game works offline. The cache name is a
// hash of that list: a new build gets a new cache and the page offers to reload into it.
// Satellite tiles (16 MB) are not precached; they are kept as the player passes them.
function offlineServiceWorker() {
return {
name: 'offline-service-worker',
apply: 'build',
generateBundle(_, bundle) {
const files = [...Object.keys(bundle), ...listFiles('public').map(f => relative('public', f).replaceAll('\\', '/'))]
.filter(f => f !== 'sw.js' && !f.endsWith('.map') && !(f.startsWith('imagery/') && f.endsWith('.jpg')));
const version = createHash('sha1').update(files.join('\n')).digest('hex').slice(0, 12);
this.emitFile({
type: 'asset', fileName: 'sw.js', source: `// Generated by vite.config.js — do not edit.
return {
name: 'offline-service-worker',
apply: 'build',
generateBundle(_, bundle) {
const files = [
...Object.keys(bundle),
...listFiles('public').map((f) => relative('public', f).replaceAll('\\', '/')),
].filter(
(f) =>
f !== 'sw.js' && !f.endsWith('.map') && !(f.startsWith('imagery/') && f.endsWith('.jpg')),
);
const version = createHash('sha1').update(files.join('\n')).digest('hex').slice(0, 12);
this.emitFile({
type: 'asset',
fileName: 'sw.js',
source: `// Generated by vite.config.js — do not edit.
const CACHE = 'robot-market-${version}';
const TILES = 'robot-market-imagery';
const FILES = ${JSON.stringify(['./', ...files.map(f => `./${f}`)])};
const FILES = ${JSON.stringify(['./', ...files.map((f) => `./${f}`)])};
self.addEventListener('install', event => event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(FILES))));
self.addEventListener('activate', event => event.waitUntil(caches.keys().then(keys =>
Promise.all(keys.filter(k => k.startsWith('robot-market-') && k !== CACHE && k !== TILES).map(k => caches.delete(k)))).then(() => self.clients.claim())));
@@ -46,23 +54,20 @@ self.addEventListener('fetch', event => {
}
event.respondWith(caches.match(request, {ignoreSearch: true}).then(hit => hit ?? fetch(request)));
});
`
});
}
};
`,
});
},
};
}
export default defineConfig({
// Relative assets also load under a GitHub Pages repository path.
base: './',
plugins: [offlineServiceWorker()],
// Top-level await loads the building and imagery files before the world is built.
build: {target: 'es2022'},
server: {
host: true,
allowedHosts: [
'robotmarkets.ir',
'www.robotmarkets.ir',
],
},
// Relative assets also load under a GitHub Pages repository path.
base: './',
plugins: [offlineServiceWorker()],
// Top-level await loads the building and imagery files before the world is built.
build: { target: 'es2022' },
server: {
host: true,
allowedHosts: ['robotmarkets.ir', 'www.robotmarkets.ir'],
},
});