-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
executable file
·86 lines (78 loc) · 2.57 KB
/
vite.config.ts
File metadata and controls
executable file
·86 lines (78 loc) · 2.57 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
86
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { defineConfig, Plugin } from "vite";
import path, { resolve } from "path";
import ts from "typescript";
import { writeFile } from "fs/promises";
import { nodePolyfills } from "vite-plugin-node-polyfills";
// https://vite.dev/config/
export default defineConfig({
plugins: [
svelte(),
GenerateGlobalTypesPlugin(),
nodePolyfills(),
{
name: "inject-source-path",
transform(code, id) {
return code.replace(/__SOURCE__/g, JSON.stringify(id));
},
},
],
envPrefix: "DW_",
resolve: {
alias: {
$assets: resolve(__dirname, "./src/assets"),
$state: resolve(__dirname, "./src/state"),
$apps: resolve(__dirname, "./src/apps"),
$css: resolve(__dirname, "./src/css"),
$types: resolve(__dirname, "./src/types"),
$ts: resolve(__dirname, "./src/ts"),
$lib: resolve(__dirname, "./src/lib"),
$interfaces: resolve(__dirname, "./src/interfaces"),
},
},
build: {
rollupOptions: {
output: {
manualChunks: (id) => {
if (id.includes("node_modules")) return "vendor";
},
},
},
assetsInlineLimit: 1,
minify: true,
},
base: "",
});
function GenerateGlobalTypesPlugin(): Plugin {
return {
name: "generate-global-types",
apply: "build",
async buildStart() {
const thirdPartyPath = path.resolve(__dirname, "src/ts/server/user/thirdparty.ts");
// Load and parse the source file
const program = ts.createProgram([thirdPartyPath], {
allowJs: false,
module: ts.ModuleKind.ESNext,
target: ts.ScriptTarget.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
noEmit: true,
});
const checker = program.getTypeChecker();
const source = program.getSourceFile(thirdPartyPath);
if (!source) return;
const typeAlias = [...source.statements].find(
(stmt): stmt is ts.TypeAliasDeclaration => ts.isTypeAliasDeclaration(stmt) && stmt.name.text === "ThirdPartyPropMap"
);
if (!typeAlias) return;
const type = checker.getTypeAtLocation(typeAlias);
const props = type.getProperties().map((symbol) => {
const name = symbol.getName();
return `declare const ${name}: ThirdPartyPropMap["${name}"];`;
});
const content = `// AUTO-GENERATED BY VITE PLUGIN\nimport type { ThirdPartyPropMap } from "./ts/server/user/thirdparty";\n\n${props.join(
"\n"
)}\n\nexport {};`;
await writeFile(path.resolve(__dirname, "dist/globals.d.ts"), content);
},
};
}