webui/vite.config.ts

73 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-11-17 00:05:35 +00:00
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
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
}
2023-11-19 19:27:22 +00:00
} catch (e) {}
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'),
() => {}
);
} catch (e) {}
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'));
} catch (e) {}
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-17 00:05:35 +00:00
}
});