This repository was archived by the owner on Nov 6, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathConfig.ts
More file actions
333 lines (287 loc) · 10 KB
/
Copy pathConfig.ts
File metadata and controls
333 lines (287 loc) · 10 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/// <reference types="vitest" />
import { type ManualChunksOption } from 'rollup';
import { fileURLToPath } from 'url';
import { BuildOptions, LibraryOptions, UserConfig, UserConfigExport, defineConfig } from 'vite';
import { type InlineConfig as TestConfig } from 'vitest';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import topLevelAwait from 'vite-plugin-top-level-await';
import wasm from 'vite-plugin-wasm';
import { Paths } from './builder';
import { R, Util, asArray, fs, pc, type t } from './builder/common';
import { displayStartupText } from './plugins/displayStartupText';
export { fs };
/**
* Common configuration setup.
*/
export const Config = {
fs,
defaults: {
/**
* Test runner.
*/
test(): TestConfig {
return {
globals: false,
include: ['src/**/*.v.{mts,ts,tsx}'],
environment: 'happy-dom', // NB: Default, makes DOM available on server/node.
};
},
},
/**
* Build configuration generator (with standard defaults).
*/
vite(modulePath: t.ImportMetaUrl, modify?: t.ModifyViteConfig): UserConfigExport {
const dir = Wrangle.dirname(modulePath);
return defineConfig(async (e) => {
const { command, mode } = e;
const pkg = await Util.PackageJson.load(dir);
const name = pkg.name;
const deps = [
...Wrangle.depsList(false, pkg.dependencies),
...Wrangle.depsList(true, pkg.devDependencies),
];
const targets: t.ViteTarget[] = [];
const plugins: t.VitePlugin[] = [];
/**
* Vite configuration.
*/
const external: string[] = [];
const manualChunks: ManualChunksOption = {};
const build: BuildOptions = {
manifest: Paths.viteBuildManifest,
assetsDir: 'lib',
target: 'esnext',
rollupOptions: {
external,
output: { manualChunks },
onwarn(warning, warn) {
// Use a string match to suppress specific warnings
// Simply return to ignore the warning
warn(warning); // ← default warning handler
},
},
};
const title = pc.gray(`module: ${pc.green(pc.bold(pkg.name))}`);
const titlePlugin =
e.command === 'serve' && e.mode !== 'test' ? displayStartupText(title) : undefined;
let config: UserConfig = {
build,
plugins: [topLevelAwait(), wasm(), titlePlugin],
worker: {
format: 'es',
plugins: () => [topLevelAwait(), wasm()],
},
optimizeDeps: {
/**
* REF: https://automerge.org/docs/quickstart
* Automerge
* This is necessary otherwise `vite dev` includes two separate
* versions of the JS wrapper. This causes problems because the JS
* wrapper has a module level variable to track JS side heap
* allocations, initializing this twice causes horrible breakage
*/
exclude: [
'@automerge/automerge-wasm',
'@automerge/automerge-wasm/bundler/bindgen_bg.wasm',
'@syntect/wasm',
],
},
base: './',
server: { port: 1234 },
};
/**
* TODO 🐷 - Temporary ←[DELETE]
* Temporary requirement of module: ext.lib.privy
* https://privy-developers.slack.com/archives/C059ABLSB47/p1692753366943129?thread_ts=1692753043.728049&cid=C059ABLSB47
*/
const addPolyfill = () => {
if (mode === 'production') return false;
return true;
};
if (addPolyfill()) {
const dirname = fs.basename(fs.dirname(modulePath));
const moduleName = `← ${pc.yellow('vite-plugin-node-polyfills')}`;
console.log(`${pc.yellow('[TMP]')} polyfills added to ${pc.cyan(dirname)} ${moduleName}`);
config.plugins?.push(nodePolyfills({ include: ['process', 'buffer', 'util'] }));
}
/**
* Modification IoC
* (called within each module to perform specific adjustments).
*/
const args: t.ModifyViteConfigArgs = {
ctx: R.clone({ name, command, mode, config, pkg, deps }),
target(...target) {
R.uniq(target)
.filter((name) => !targets.includes(name))
.forEach((name) => targets.push(name));
return args;
},
plugin(...kind) {
R.uniq(kind)
.filter((name) => !plugins.includes(name))
.forEach((name) => plugins.push(name));
return args;
},
externalDependency(moduleName) {
R.uniq(asArray(moduleName))
.filter((name) => !external.includes(name))
.forEach((name) => external.push(name));
return args;
},
chunk(alias, moduleName) {
manualChunks[alias] = R.uniq(asArray(moduleName ?? alias));
return args;
},
lib(options = {}) {
const entry = Wrangle.libEntry(options.entry);
Object.keys(entry).forEach((key) => (entry[key] = fs.join(dir, entry[key])));
const lib: LibraryOptions = { entry, formats: ['es'] };
build.lib = lib;
return args;
},
headers(obj) {
const server = config.server!;
const headers = server.headers || (server.headers = {});
server.headers = { ...headers, ...obj };
return args;
},
};
// Pass execution to the callback.
await modify?.(args);
/**
* Configure plugins.
*/
const hasPlugin = (...items: t.VitePlugin[]) => items.some((name) => plugins.includes(name));
if (hasPlugin('web:react')) {
/**
* https://million.dev/docs
* Million.js → This is a provisional test, only keep if there are
* no problems and there is an apparent speed advantage.
*/
const million = (await import('million/compiler')).default;
const react = (await import('@vitejs/plugin-react-swc')).default;
const hmr = e.mode === 'development';
config.plugins?.push(million.vite({ mode: 'react', hmr }));
config.plugins?.push(react());
}
if (hasPlugin('web:svelte')) {
const svelte = (await import('@sveltejs/vite-plugin-svelte')).svelte;
config.plugins?.push(svelte());
}
if (hasPlugin('rollup:visualizer')) {
const { visualizer } = await import('rollup-plugin-visualizer');
config.plugins?.push(
visualizer({
title: `${pkg.name} - bundle`,
filename: 'tmp.bundle-analysis.html',
open: true,
gzipSize: true,
}),
);
}
if (hasPlugin('ssl')) {
const { devServerSsl } = await import('./plugins/ssl');
await devServerSsl(config);
}
/**
* Complete
*/
config = Config.target.apply(config, ...targets);
return config;
});
},
/**
* Helpers for adjusting a configuration based on the build targets.
*/
target: {
get(config: UserConfig): t.ViteTarget[] {
return (config as any).__targets ?? [];
},
apply(config: UserConfig, ...targets: t.ViteTarget[]) {
config = R.clone(config);
const test = getAndAssignTest(config);
const DEFAULT: t.ViteTarget = 'web';
const isTarget = (...items: t.ViteTarget[]) => {
if (targets.length === 0 && items.includes(DEFAULT)) return true; // NB: Default "web" target if nothing specified.
return items.some((name) => targets.includes(name));
};
if (isTarget('web')) test.environment = 'happy-dom';
if (isTarget('node') && !isTarget('web')) test.environment = 'happy-dom';
if (isTarget('node')) config.build!.ssr = true;
(config as any).__targets = targets;
return config;
},
reset(config: UserConfig) {
config = R.clone(config);
const test = getAndAssignTest(config);
test.environment = undefined;
config.build!.ssr = undefined;
delete (config as any).__targets;
return config;
},
},
/**
* Typescript configuration modifier (tsconfig.json).
*/
ts(modify: t.ModifyTsConfig): t.TsConfigExport {
return async (args) => {
const { kind } = args;
let _current = R.clone(args.config);
const ensureLibs = (input: t.TsConfig, ...items: string[]) => {
let next = input;
let lib = next.compilerOptions.lib || (next.compilerOptions.lib = []);
next.compilerOptions.lib = R.uniq([...lib, ...items]);
return next;
};
const api: t.ModifyTsConfigArgs = {
kind,
get current() {
return { ..._current };
},
edit(fn) {
const next = api.current; // retrieve the clone to mutate.
fn(next);
_current = next;
},
/**
* Named environment(s).
*/
env(...target) {
const env = R.uniq(target);
const is = (...items: t.TsEnv[]) => items.some((name) => env.includes(name));
if (is('web')) {
api.edit((tsconfig) => ensureLibs(tsconfig, 'DOM', 'DOM.Iterable', 'WebWorker'));
}
if (is('web:react')) {
api.edit((tsconfig) => (tsconfig.compilerOptions.jsx = 'react-jsx'));
}
if (is('web:svelte')) {
api.edit((tsconfig) => (tsconfig.extends = '@tsconfig/svelte/tsconfig.json'));
}
},
};
modify(api);
return _current;
};
},
} as const;
/**
* Helpers
*/
function getAndAssignTest(config: UserConfig): TestConfig {
const test = (config as any).test || ((config as any).test = Config.defaults.test());
return test;
}
const Wrangle = {
depsList(isDev: boolean, deps: t.PkgDeps = {}): t.PkgDep[] {
return Object.keys(deps).map((name) => ({ name, version: deps[name], isDev }));
},
dirname(modulePath: t.ImportMetaUrl) {
return fs.dirname(fileURLToPath(modulePath));
},
libEntry(entry?: string | t.ViteLibEntry): t.ViteLibEntry {
if (typeof entry === 'string') return { index: entry };
if (typeof entry === 'object') return entry;
return { index: '/src/index.ts' };
},
};