generated from Nhaaro/FoundryVTT-Module-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
137 lines (130 loc) · 5.24 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
import esbuild from 'esbuild';
import fs from 'fs-extra';
import path, { resolve } from 'path';
import { defineConfig } from 'vite';
import checker from 'vite-plugin-checker';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import tsconfigPaths from 'vite-tsconfig-paths';
import packageJSON from './package.json' assert { type: 'json' };
import { PACKAGE_ID } from './src/constants.ts';
import handlebarsHMR from './utils/vite/handlebars-hmr.ts';
import languagesHMR from './utils/vite/languages-hmr.ts';
const [outDir] = (() => {
const configPath = resolve(process.cwd(), 'foundryconfig.json');
const config = fs.readJSONSync(configPath, { throws: false });
const outDir =
config instanceof Object
? path.join(config.dataPath, 'modules', config.systemName ?? PACKAGE_ID)
: path.join(__dirname, 'dist/');
return [outDir] as const;
})();
const config = defineConfig(({ command, mode }) => {
const buildMode = mode === 'production' ? 'production' : 'development';
const plugins = [checker({ typescript: true }), tsconfigPaths(), handlebarsHMR(), languagesHMR()];
// 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) {
return chunk.fileName.endsWith('.mjs')
? esbuild.transform(code, {
keepNames: true,
minifyIdentifiers: false,
minifySyntax: true,
minifyWhitespace: true,
})
: code;
},
},
},
...viteStaticCopy({
targets: [
{ src: 'README.md', dest: '.' },
{ src: 'CHANGELOG.md', dest: '.' },
{ src: 'CONTRIBUTING.md', dest: '.' },
{ src: 'UNLICENSE', dest: '.' },
],
})
);
} 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'));
},
},
}
);
}
// 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`);
fs.writeFileSync(`./${PACKAGE_ID}.css`, `/** ${message} */\n\n@import "./src/styles/module.css"`);
fs.writeFileSync(`./${PACKAGE_ID}.mjs`, `/** ${message} */\n\nimport "./src/index.ts";\n`);
fs.writeFileSync('./vendor.mjs', `/** ${message} */\n`);
}
return {
publicDir: 'static',
base: command === 'build' ? './' : `/modules/${PACKAGE_ID}/`,
define: {
BUILD_MODE: JSON.stringify(buildMode),
},
server: {
port: 30001,
open: false,
proxy: {
[`^(?!/modules/${PACKAGE_ID})`]: 'http://localhost:30000/',
'/socket.io': {
target: `ws://localhost:30000`,
ws: true,
},
},
},
esbuild: { keepNames: true },
build: {
outDir,
emptyOutDir: true,
sourcemap: buildMode === 'development',
minify: buildMode === 'production',
lib: {
name: PACKAGE_ID,
entry: 'src/index.ts',
formats: ['es'],
fileName: PACKAGE_ID,
},
rollupOptions: {
output: {
// vite is wrong about this type, if we return undefined it cannot build
assetFileNames: ({ name }): string =>
// Forcibly rename style file so that it does not share Foundry's CSS file name
name === 'style.css' ? `${PACKAGE_ID}.css` : name!,
chunkFileNames: '[name].mjs',
entryFileNames: `${PACKAGE_ID}.mjs`,
manualChunks:
buildMode === 'production' && 'dependencies' in packageJSON
? {
vendor: Object.keys(packageJSON.dependencies as object),
}
: undefined,
},
watch: { buildDelay: 100 },
},
},
clearScreen: false,
plugins,
css: {
devSourcemap: buildMode === 'development',
},
};
});
export default config;