-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvite.config.mts
More file actions
85 lines (81 loc) 路 2.83 KB
/
Copy pathvite.config.mts
File metadata and controls
85 lines (81 loc) 路 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { parse, resolve } from "node:path";
import { glob } from "glob";
import { visualizer } from "rollup-plugin-visualizer";
import {
type AliasOptions,
defineConfig,
type PluginOption,
type Rollup,
type UserConfig,
} from "vite";
import { compilerOptions } from "./tsconfig.bundled.json" with { type: "json" };
const outDir = resolve(import.meta.dirname, "./staticfiles/generated/bundled");
const collectedFiles = glob.sync(
"./!(static)/static/bundled/**/*?(-)index.?(m)[j|t]s?(x)",
);
/**
* Grabs import aliases from tsconfig for #module: imports
**/
function getAliases(): AliasOptions {
const aliases: AliasOptions = {};
for (const [key, value] of Object.entries(compilerOptions.paths)) {
aliases[key] = resolve(import.meta.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("/");
}
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 as string)}.[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",
},
},
},
resolve: {
alias: getAliases(),
},
plugins: [visualizer({ filename: ".bundle-size-report.html" }) as PluginOption],
} satisfies UserConfig;
});