-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
224 lines (208 loc) · 6.09 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* eslint-disable import/no-extraneous-dependencies */
/// <reference types="vitest/config" />
import {
BuildOptions,
Plugin,
ServerOptions,
UserConfig,
build,
defineConfig,
} from 'vite';
import { resolve } from 'path';
import { existsSync, readFileSync } from 'fs';
import react from '@vitejs/plugin-react-swc';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import { viteSingleFile } from 'vite-plugin-singlefile';
import { writeFile } from 'fs/promises';
import packageInfo from './package.json';
const PORT = 3000;
const clientRoot = './src/client';
const outDir = './dist';
const serverEntry = './src/server/index.ts';
const copyAppscriptEntry = './appsscript.json';
const devServerWrapper = './dev/dev-server-wrapper.html';
type DialogEntry = {
name: string;
filename: string;
template: string;
};
const clientEntrypoints: Array<DialogEntry> = [
{
name: 'CLIENT:about',
filename: 'about-dialog',
template: 'about-dialog/index.html',
},
{
name: 'CLIENT:import',
filename: 'import-dialog',
template: 'import-dialog/index.html',
},
{
name: 'CLIENT:settings',
filename: 'settings-dialog',
template: 'settings-dialog/index.html',
},
];
const sharedConfig = defineConfig({
define: {
__APP_VERSION__: JSON.stringify(packageInfo.version),
},
resolve: {
alias: {
'@': resolve(__dirname, './src'),
},
},
});
const keyPath = resolve(__dirname, './certs/key.pem');
const certPath = resolve(__dirname, './certs/cert.pem');
const devServerOptions: ServerOptions = {
port: PORT,
};
// use key and cert settings only if they are found
if (existsSync(keyPath) && existsSync(certPath)) {
devServerOptions.https = {
key: readFileSync(resolve(__dirname, './certs/key.pem')),
cert: readFileSync(resolve(__dirname, './certs/cert.pem')),
};
}
const clientServeConfig: UserConfig = {
...sharedConfig,
plugins: [react()],
server: devServerOptions,
root: clientRoot,
};
const clientBuildConfig = ({ filename, template }: DialogEntry) =>
defineConfig({
...sharedConfig,
plugins: [react(), viteSingleFile({ useRecommendedBuildConfig: true })],
root: resolve(__dirname, clientRoot, filename),
build: {
sourcemap: false,
write: false, // don't write to disk
outDir,
emptyOutDir: true,
minify: true,
rollupOptions: {
input: resolve(__dirname, clientRoot, template),
external: [
'react',
'react-dom',
'react-bootstrap',
'@mui/material',
'@emotion/react',
'@emotion/styled',
'gas-client',
'@types/react',
],
output: {
format: 'iife', // needed to use globals from UMD builds
dir: outDir,
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'@mui/material': 'MaterialUI',
'@emotion/react': 'emotionReact',
'@emotion/styled': 'emotionStyled',
'gas-client': 'GASClient',
},
},
},
},
});
const serverBuildConfig: BuildOptions = {
emptyOutDir: true,
minify: false, // needed to work with footer
lib: {
entry: resolve(__dirname, serverEntry),
fileName: 'server',
name: 'globalThis',
formats: ['iife'],
},
rollupOptions: {
output: {
entryFileNames: 'server.js',
extend: true,
footer: (chunk) =>
chunk.exports
.map((exportedFunction) => `function ${exportedFunction}() {};`)
.join('\n'),
},
},
};
const buildIFrame = (entrypoint: DialogEntry) => ({
src: devServerWrapper,
dest: './',
rename: `${entrypoint.filename}.html`,
transform: (contents: string) =>
contents
.toString()
.replace(/__PORT__/g, String(PORT))
.replace(/__FILE_NAME__/g, entrypoint.template),
});
/**
* This builds the client react app bundles for production, and writes them to disk.
* Because multiple client entrypoints (dialogs) are built, we need to loop through
* each entrypoint and build the client bundle for each. Vite doesn't have great tooling for
* building multiple single-page apps in one project, so we have to do this manually with a
* post-build closeBundle hook (https://rollupjs.org/guide/en/#closebundle).
*/
function buildFrontendBundlesPlugin(): Plugin {
return {
name: 'build-client-production-bundles',
async closeBundle() {
this.info('Building client production bundles...');
// eslint-disable-next-line no-restricted-syntax
for (const clientEntrypoint of clientEntrypoints) {
this.info(`Building client bundle for ${clientEntrypoint.name}`);
// eslint-disable-next-line no-await-in-loop
const buildOutput = await build(clientBuildConfig(clientEntrypoint));
// eslint-disable-next-line no-await-in-loop
await writeFile(
resolve(__dirname, outDir, `${clientEntrypoint.filename}.html`),
// @ts-expect-error - output is an array of RollupOutput
buildOutput?.output[0].source
);
}
this.info('Finished building client bundles!');
},
};
}
const buildConfig = defineConfig(({ mode }) => {
const targets = [{ src: copyAppscriptEntry, dest: './' }];
if (mode === 'development') {
targets.push(...clientEntrypoints.map(buildIFrame));
}
return {
...sharedConfig,
plugins: [
viteStaticCopy({
targets,
}),
mode === 'production' && buildFrontendBundlesPlugin(),
],
build: serverBuildConfig,
esbuild: {
keepNames: true,
},
};
});
const testConfig: UserConfig = {
...sharedConfig,
test: {
globals: true,
setupFiles: ['./test-setup.ts'],
},
};
// https://vitejs.dev/config/
export default defineConfig(async ({ command, mode }) => {
if (mode === 'test') {
return testConfig;
} else if (command === 'serve') {
// for 'serve' mode, we only want to serve the client bundle locally
return clientServeConfig;
} else if (command === 'build') {
// for 'build' mode, we have two paths: build assets for local development, and build for production
return buildConfig({ command, mode });
}
return {};
});