Sith/vite.config.mts

112 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

2024-11-19 00:56:55 +00:00
// biome-ignore lint/correctness/noNodejsModules: this is backend side
import { parse, resolve } from "node:path";
import inject from "@rollup/plugin-inject";
import { glob } from "glob";
2024-11-20 23:33:40 +00:00
import { type AliasOptions, type UserConfig, defineConfig } from "vite";
import type { Rollup } from "vite";
2024-11-19 00:56:55 +00:00
import { viteStaticCopy } from "vite-plugin-static-copy";
import tsconfig from "./tsconfig.json";
const outDir = resolve(__dirname, "./staticfiles/generated/bundled");
const vendored = resolve(outDir, "vendored");
const nodeModules = resolve(__dirname, "node_modules");
const collectedFiles = glob.sync(
"./!(static)/static/bundled/**/*?(-)index.?(m)[j|t]s?(x)",
);
2024-11-19 00:56:55 +00:00
/**
* Grabs import aliases from tsconfig for #module: imports
**/
2024-11-19 00:56:55 +00:00
function getAliases(): AliasOptions {
const aliases: AliasOptions = {};
for (const [key, value] of Object.entries(tsconfig.compilerOptions.paths)) {
aliases[key] = resolve(__dirname, value[0]);
}
return aliases;
}
/**
* Helper function that finds the relative path of an index.js/ts file in django static folders
**/
function getRelativeAssetPath(path: string): string {
let relativePath: string[] = [];
const fullPath = parse(path);
for (const dir of fullPath.dir.split("/").reverse()) {
if (dir === "bundled") {
break;
}
relativePath.push(dir);
}
// We collected folders in reverse order, we put them back in the original order
relativePath = relativePath.reverse();
relativePath.push(fullPath.name);
return relativePath.join("/");
}
2024-11-19 00:56:55 +00:00
// biome-ignore lint/style/noDefaultExport: this is recommended by documentation
2024-11-20 23:33:40 +00:00
export default defineConfig((config: UserConfig) => {
return {
base: "/static/bundled/",
appType: "custom",
build: {
outDir: outDir,
manifest: true, // goes into .vite/manifest.json in the build folder
modulePreload: false, // would require `import 'vite/modulepreload-polyfill'` to always be injected
emptyOutDir: config.mode === "production", // Avoid rebuilding everything in dev mode
rollupOptions: {
input: collectedFiles,
output: {
// Mirror architecture of static folders in generated .js and .css
entryFileNames: (chunkInfo: Rollup.PreRenderedChunk) => {
if (chunkInfo.facadeModuleId !== null) {
return `${getRelativeAssetPath(chunkInfo.facadeModuleId)}.[hash].js`;
}
return "[name].[hash].js";
},
assetFileNames: (chunkInfo: Rollup.PreRenderedAsset) => {
if (
chunkInfo.names?.length === 1 &&
chunkInfo.originalFileNames?.length === 1 &&
collectedFiles.includes(chunkInfo.originalFileNames[0])
) {
return `${getRelativeAssetPath(chunkInfo.originalFileNames[0])}.[hash][extname]`;
}
return "[name].[hash][extname]";
},
chunkFileNames: "[name].[hash].js",
},
2024-11-19 00:56:55 +00:00
},
},
2024-11-20 23:33:40 +00:00
resolve: {
alias: getAliases(),
},
2024-11-19 00:56:55 +00:00
2024-11-20 23:33:40 +00:00
plugins: [
inject({
// biome-ignore lint/style/useNamingConvention: that's how it's called
Alpine: "alpinejs",
htmx: "htmx.org",
2024-11-20 23:33:40 +00:00
}),
viteStaticCopy({
targets: [
{
src: resolve(nodeModules, "jquery/dist/jquery.min.js"),
dest: vendored,
},
{
src: resolve(nodeModules, "jquery-ui/dist/jquery-ui.min.js"),
dest: vendored,
},
{
src: resolve(nodeModules, "jquery.shorten/src/jquery.shorten.min.js"),
dest: vendored,
},
],
}),
],
optimizeDeps: {
include: ["jquery"],
},
} satisfies UserConfig;
});