auth.andert.me/vite.config.ts

133 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-07-19 09:42:47 +00:00
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import * as fs from 'fs';
import { resolve } from 'path';
2023-07-19 09:42:47 +00:00
// Utility function to get all .svelte files from a directory recursively
function getRecursiveSvelteFiles(dir) {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
const files = Array.from(dirents).flatMap((dirent) => {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
return getRecursiveSvelteFiles(res);
} else if (res.endsWith('.svelte')) {
return [res];
} else {
return [];
}
});
return files;
}
function getRecursiveServiceFiles(dir) {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
const files = Array.from(dirents).flatMap((dirent) => {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
return getRecursiveServiceFiles(res);
} else if (res.endsWith('.ts')) {
return [res];
} else {
return [];
}
});
return files;
}
function getRecursiveDataFiles(dir) {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
const files = Array.from(dirents).flatMap((dirent) => {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
return getRecursiveDataFiles(res);
} else if (res.endsWith('.ts')) {
return [res];
} else {
return [];
}
});
return files;
}
2023-07-19 09:42:47 +00:00
export default defineConfig({
plugins: [
sveltekit(),
{
name: 'components-resolver',
resolveId(source) {
if (source === 'virtual:components-list') return source;
return null;
},
load(id) {
if (id === 'virtual:components-list') {
const componentsDir = resolve(__dirname, 'src/lib/components');
const componentsFiles = getRecursiveSvelteFiles(componentsDir);
const components = componentsFiles.map(file =>
file
.replace(componentsDir, '')
.replace(/\.svelte$/, '')
.replace(/\\/g, '/') // Fix Windows path separators
.slice(1) // Remove leading "/"
);
return `export default ${JSON.stringify(components)};`;
}
return null;
}
},
{
name: 'services-resolver',
resolveId(source) {
if (source === 'virtual:services-list') return source;
return null;
},
load(id) {
if (id === 'virtual:services-list') {
const servicesDir = resolve(__dirname, 'src/lib/services');
const serviceFiles = getRecursiveServiceFiles(servicesDir);
const services = serviceFiles.map(file =>
file
.replace(servicesDir, '')
.replace(/\.ts$/, '')
.replace(/\\/g, '/') // Fix Windows path separators
.slice(1) // Remove leading "/"
);
return `export default ${JSON.stringify(services)};`;
}
return null;
}
},
{
name: 'data-sources-resolver',
resolveId(source) {
if (source === 'virtual:data-sources-list') return source;
return null;
},
load(id) {
if (id === 'virtual:data-sources-list') {
const dataDir = resolve(__dirname, 'src/lib/data');
const dataFiles = getRecursiveServiceFiles(dataDir); // Use the same function as before
const dataSources = dataFiles.map(file =>
file
.replace(dataDir, '')
.replace(/\.ts$/, '')
.replace(/\\/g, '/') // Fix Windows path separators
.slice(1) // Remove leading "/"
);
return `export default ${JSON.stringify(dataSources)};`;
}
return null;
}
}
],
2023-07-19 09:42:47 +00:00
test: {
include: ['src/**/*.{test,spec}.{js,ts}']
}
});