btclock-webui/vite.config.ts

86 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2023-11-17 01:05:35 +01:00
import { sveltekit } from '@sveltejs/kit/vite';
2023-11-23 02:04:20 +01:00
import { defineConfig } from 'vitest/config';
2023-11-23 02:41:58 +01:00
import GithubActionsReporter from 'vitest-github-actions-reporter';
2023-11-17 01:05:35 +01:00
2023-11-19 20:27:22 +01:00
import * as fs from 'fs';
import * as path from 'path';
2023-11-17 11:09:06 +01:00
const doRewrap = ({ cssClass }) => {
try {
if (fs.existsSync(path.resolve(__dirname, 'dist/bundle.js'))) {
2023-11-19 20:27:22 +01:00
return;
2023-11-17 11:09:06 +01:00
}
2024-09-03 12:11:14 +02:00
} catch {
// do nothing
}
2023-11-19 20:27:22 +01:00
console.log('\nStart re-wrapping...');
fs.readFile(path.resolve(__dirname, 'dist/bundle.html'), 'utf8', function (err, data) {
2023-11-17 11:09:06 +01:00
if (!data) {
2023-11-19 20:27:22 +01:00
console.log(
`[Error]: No bundle.html generated, check svelte.config.js -> config.kit.adapter -> fallback: "bundle.html"`
);
return;
2023-11-17 11:09:06 +01:00
}
2023-11-19 20:27:22 +01:00
const matchData = data.match(/(?<=<script\b[^>]*>)([\s\S]*?)(?=<\/script>)/gm);
2023-11-17 11:09:06 +01:00
if (matchData) {
2023-11-19 20:27:22 +01:00
const cleanData = matchData[0]
.trim()
.replace(
`document.querySelector('[data-sveltekit-hydrate="45h"]').parentNode`,
`document.querySelector(".${cssClass}")`
);
2023-11-17 11:09:06 +01:00
fs.writeFile(path.resolve(__dirname, 'dist/bundle.js'), cleanData, (err) => {
2023-11-19 20:27:22 +01:00
if (err) console.log(err);
2023-11-17 11:09:06 +01:00
else {
try {
2023-11-19 20:27:22 +01:00
fs.rename(
path.resolve(__dirname, 'dist/index.page'),
path.resolve(__dirname, 'dist/index.html'),
() => {}
);
2024-09-03 12:11:14 +02:00
} catch {
// do nothing
}
2023-11-17 11:09:06 +01:00
try {
2023-11-19 20:27:22 +01:00
fs.unlinkSync(path.resolve(__dirname, 'dist/bundle.html'));
2024-09-03 12:11:14 +02:00
} catch {
// do nothing
}
2023-11-19 20:27:22 +01:00
console.log('Finished: bundle.js + index.html have been regenerated.\n');
2023-11-17 11:09:06 +01:00
}
2023-11-19 20:27:22 +01:00
});
} else console.log(`[Error]: No proper <script> tag found in bundle.html`);
});
};
2023-11-17 11:09:06 +01:00
2023-11-17 01:05:35 +01:00
export default defineConfig({
2023-11-19 20:27:22 +01:00
plugins: [
sveltekit(),
{
name: 'postbuild-command',
2023-11-17 11:09:06 +01:00
closeBundle: {
order: 'post',
handler() {
2023-11-19 20:27:22 +01:00
setTimeout(() => doRewrap({ cssClass: 'overlay' }), Math.random() * 500 + 500);
2023-11-17 11:09:06 +01:00
}
}
2023-11-19 20:27:22 +01:00
}
],
2023-11-17 01:05:35 +01:00
build: {
2023-11-17 11:09:06 +01:00
minify: true,
cssCodeSplit: false,
rollupOptions: {
output: {
manualChunks: () => 'app',
2023-11-19 20:27:22 +01:00
assetFileNames: '[name][extname]'
2023-11-17 11:09:06 +01:00
}
}
2023-11-23 02:04:20 +01:00
},
test: {
include: ['src/**/*.{test,spec}.{js,ts}'],
globals: true,
2023-11-23 02:41:58 +01:00
environment: 'jsdom',
reporters: process.env.GITHUB_ACTIONS ? ['default', new GithubActionsReporter()] : 'default'
2023-11-17 01:05:35 +01:00
}
});