1
0
Fork 0
forked from btclock/webui
webui/vite.config.ts

86 lines
2.1 KiB
TypeScript
Raw Normal View History

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