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";
|
|
|
|
import type { AliasOptions, UserConfig } from "vite";
|
2024-11-19 15:19:13 +00:00
|
|
|
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");
|
2024-11-19 15:19:13 +00:00
|
|
|
const collectedFiles = glob.sync(
|
|
|
|
"./!(static)/static/bundled/**/*?(-)index.?(m)[j|t]s?(x)",
|
|
|
|
);
|
2024-11-19 00:56:55 +00:00
|
|
|
|
2024-11-19 15:19:13 +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;
|
|
|
|
}
|
|
|
|
|
2024-11-19 15:19:13 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
export default {
|
|
|
|
base: "/static/bundled/",
|
|
|
|
appType: "custom",
|
|
|
|
build: {
|
|
|
|
outDir: outDir,
|
2024-11-20 17:24:28 +00:00
|
|
|
manifest: true, // goes into .vite/manifest.json in the build folder
|
2024-11-19 00:56:55 +00:00
|
|
|
modulePreload: false, // would require `import 'vite/modulepreload-polyfill'` to always be injected
|
|
|
|
emptyOutDir: true,
|
|
|
|
rollupOptions: {
|
2024-11-19 15:19:13 +00:00
|
|
|
input: collectedFiles,
|
2024-11-19 00:56:55 +00:00
|
|
|
output: {
|
2024-11-19 15:19:13 +00:00
|
|
|
// Mirror architecture of static folders in generated .js and .css
|
|
|
|
entryFileNames: (chunkInfo: Rollup.PreRenderedChunk) => {
|
|
|
|
if (chunkInfo.facadeModuleId !== null) {
|
2024-11-20 17:24:28 +00:00
|
|
|
return `${getRelativeAssetPath(chunkInfo.facadeModuleId)}.[hash].js`;
|
2024-11-19 15:19:13 +00:00
|
|
|
}
|
2024-11-20 17:24:28 +00:00
|
|
|
return "[name].[hash].js";
|
2024-11-19 15:19:13 +00:00
|
|
|
},
|
|
|
|
assetFileNames: (chunkInfo: Rollup.PreRenderedAsset) => {
|
|
|
|
if (
|
|
|
|
chunkInfo.names?.length === 1 &&
|
|
|
|
chunkInfo.originalFileNames?.length === 1 &&
|
|
|
|
collectedFiles.includes(chunkInfo.originalFileNames[0])
|
|
|
|
) {
|
2024-11-20 17:24:28 +00:00
|
|
|
return `${getRelativeAssetPath(chunkInfo.originalFileNames[0])}.[hash][extname]`;
|
2024-11-19 15:19:13 +00:00
|
|
|
}
|
2024-11-20 17:24:28 +00:00
|
|
|
return "[name].[hash][extname]";
|
2024-11-19 15:19:13 +00:00
|
|
|
},
|
2024-11-20 17:24:28 +00:00
|
|
|
chunkFileNames: "[name].[hash].js",
|
2024-11-19 00:56:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias: getAliases(),
|
|
|
|
},
|
|
|
|
|
|
|
|
plugins: [
|
|
|
|
inject({
|
|
|
|
// biome-ignore lint/style/useNamingConvention: that's how it's called
|
|
|
|
Alpine: "alpinejs",
|
|
|
|
}),
|
|
|
|
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;
|