-
Notifications
You must be signed in to change notification settings - Fork 5
/
vite.config.ts
174 lines (167 loc) · 5.21 KB
/
vite.config.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import fs from "fs-extra";
import * as Vite from "vite";
// import { LightningCSSOptions } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
// eslint-disable-next-line import/default
import checker from "vite-plugin-checker";
import path from "path";
import packageJSON from "./package.json";
import esbuild from "esbuild";
// import { LengthValue, TransformOptions } from "lightningcss";
const config = Vite.defineConfig(({ command, mode }): Vite.UserConfig => {
const buildMode = mode === "production" ? "production" : "development";
const outDir = "dist";
const plugins = [
checker({ typescript: true }),
tsconfigPaths(),
];
// Handle minification after build to allow for tree-shaking and whitespace minification
// "Note the build.minify option does not minify whitespaces when using the 'es' format in lib mode, as it removes
// pure annotations and breaks tree-shaking."
if (buildMode === "production") {
plugins.push({
name: "minify",
renderChunk: {
order: "post",
async handler(code, chunk) {
const opts = {
keepNames: true,
minifyIdentifiers: false,
minifySyntax: true,
minifyWhitespace: true,
sourcemap: true,
};
return chunk.fileName.endsWith(".mjs") ? esbuild.transform(code, opts) : code;
},
},
});
} else {
plugins.push(
// Foundry expects all esm files listed in system.json to exist: create empty vendor module when in dev mode
{
name: "touch-vendor-mjs",
apply: "build",
writeBundle: {
async handler() {
fs.closeSync(fs.openSync(path.resolve(outDir, "vendor.mjs"), "w"));
},
},
},
// Vite HMR is only preconfigured for css files: add handler for HBS templates
{
name: "hmr-handler",
apply: "serve",
handleHotUpdate(context) {
if (context.file.startsWith(outDir)) return;
if (context.file.endsWith("en.json")) {
const basePath = context.file.slice(context.file.indexOf("lang/"));
console.log(`Updating lang file at ${basePath}`);
fs.promises.copyFile(context.file, `${outDir}/${basePath}`).then(() => {
context.server.ws.send({
type: "custom",
event: "lang-update",
data: { path: `modules/pf2e-mobile-sheet/${basePath}` },
});
});
} else if (context.file.endsWith(".hbs")) {
const basePath = context.file.slice(context.file.indexOf("templates/"));
console.log(`Updating template at ${basePath}`);
fs.promises.copyFile(context.file, `${outDir}/${basePath}`).then(() => {
context.server.ws.send({
type: "custom",
event: "template-update",
data: { path: `modules/pf2e-mobile-sheet/${basePath}` },
});
});
} else if (context.file.endsWith(".scss")) {
const basePath = context.file.slice(context.file.indexOf("styles/"));
console.log(`Updating scss at ${basePath}`);
(async()=>{})().then(() => {
context.server.ws.send({
type: "custom",
event: "scss-update",
data: { path: `modules/pf2e-mobile-sheet/${basePath}` },
});
});
}
},
},
);
}
// Create dummy files for vite dev server
if (command === "serve") {
const message = "This file is for a running vite dev server and is not copied to a build";
fs.writeFileSync("./index.html", `<h1>${message}</h1>\n`);
if (!fs.existsSync("./styles")) fs.mkdirSync("./styles");
fs.writeFileSync("./styles/pf2e-mobile-sheet.css", `/** ${message} */\n`);
fs.writeFileSync("./pf2e-mobile-sheet.mjs", `/** ${message} */\n\nimport "./src/module/pf2e-mobile-sheet.ts";\n`);
fs.writeFileSync("./vendor.mjs", `/** ${message} */\n`);
}
return {
root: "./",
base: command === "build" ? "./" : "/modules/pf2e-mobile-sheet/",
publicDir: "static",
define: {
BUILD_MODE: JSON.stringify(buildMode),
fu: "foundry.utils",
},
esbuild: { keepNames: true },
build: {
outDir,
emptyOutDir: false, //build-packs.ts handles this
minify: false,
sourcemap: true,
lib: {
name: "pf2e-mobile-sheet",
entry: "src/module/pf2e-mobile-sheet.ts",
formats: ["es"],
fileName: "pf2e-mobile-sheet",
},
rollupOptions: {
output: {
assetFileNames: ({ name }): string =>
name === "style.css" ? "styles/pf2e-mobile-sheet.css" : name!,
chunkFileNames: "[name].mjs",
entryFileNames: "pf2e-mobile-sheet.mjs",
manualChunks: {
vendor: buildMode === "production" ? Object.keys(packageJSON.dependencies) : [],
},
},
watch: { buildDelay: 100 },
},
target: "es2022",
},
server: {
port: 30001,
open: false,
proxy: {
"^(?!/modules/pf2e-mobile-sheet/)": "http://localhost:32456/",
"/socket.io": {
target: "ws://localhost:32456",
ws: true,
},
},
},
plugins,
css: {
// transformer: "lightningcss",
// lightningcss: (<Partial<TransformOptions<never>>>{
// visitor: {
// Length(length: LengthValue): LengthValue | void {
// return { unit: length.unit, value: length.value };
// },
// },
// }) as LightningCSSOptions,
// devSourcemap: true,
preprocessorOptions: {
scss: {
sourceMap: true,
sourceMapEmbed: true,
sourceEmbed: true
}
},
},
};
});
// eslint-disable-next-line import/no-default-export
export default config;