webui/vite.config.ts

110 lines
2.6 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-23 01:41:58 +00:00
import GithubActionsReporter from 'vitest-github-actions-reporter';
2024-12-20 16:16:50 +00:00
// import { visualizer } from 'rollup-plugin-visualizer';
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
}
2024-12-20 16:16:50 +00:00
// visualizer({
// emitFile: true,
// filename: "stats.html",
// })
2023-11-19 19:27:22 +00:00
],
2023-11-17 00:05:35 +00:00
build: {
2024-12-20 16:16:50 +00:00
minify: 'esbuild',
2023-11-17 10:09:06 +00:00
cssCodeSplit: false,
2024-12-20 16:59:52 +00:00
chunkSizeWarningLimit: 550,
2023-11-17 10:09:06 +00:00
rollupOptions: {
output: {
2024-12-20 16:16:50 +00:00
// assetFileNames: '[hash][extname]',
entryFileNames: `[hash][extname]`,
chunkFileNames: `[hash][extname]`,
assetFileNames: `[hash][extname]`,
preserveModules: false,
2024-12-20 16:59:52 +00:00
manualChunks: () => {
return 'app';
2024-12-20 16:16:50 +00:00
}
2023-11-17 10:09:06 +00:00
}
}
2023-11-23 01:04:20 +00:00
},
2024-12-12 22:04:13 +00:00
css: {
preprocessorOptions: {
scss: {
quietDeps: true,
silenceDeprecations: ['import']
}
}
},
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'
},
define: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
2023-11-17 00:05:35 +00:00
}
});