-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathvite.config.ts
More file actions
73 lines (67 loc) · 1.57 KB
/
vite.config.ts
File metadata and controls
73 lines (67 loc) · 1.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
import { resolve } from 'node:path';
import { defineConfig } from 'vite';
import type { LibraryFormats } from 'vite';
const ENTRY = resolve(__dirname, 'src/index.ts');
const LOGGER_LEVEL = JSON.stringify('');
const EXTERNALS = ['axios'];
const UMD_GLOBALS = { axios: 'axios' };
type BuildMode = 'cjs' | 'esm' | 'umd' | 'umd-min';
function createBaseBuild(outDir: 'lib' | 'esm' | 'dist', minify: boolean) {
return {
emptyOutDir: false,
sourcemap: true,
outDir,
minify,
rollupOptions: {
external: EXTERNALS,
},
};
}
function createModuleConfig(format: LibraryFormats, outDir: 'lib' | 'esm') {
return {
build: {
...createBaseBuild(outDir, false),
lib: {
entry: ENTRY,
formats: [format],
fileName: () => 'index.js',
},
},
};
}
function createUmdConfig(fileName: string, minify: boolean) {
return {
define: {
'process.env.LOGGER_LEVEL': LOGGER_LEVEL,
},
build: {
...createBaseBuild('dist', minify),
lib: {
entry: ENTRY,
name: 'axios-extensions',
formats: ['umd' as LibraryFormats],
fileName: () => fileName,
},
rollupOptions: {
external: EXTERNALS,
output: {
globals: UMD_GLOBALS,
},
},
},
};
}
export default defineConfig(({ mode }) => {
switch (mode as BuildMode) {
case 'cjs':
return createModuleConfig('cjs', 'lib');
case 'esm':
return createModuleConfig('es', 'esm');
case 'umd':
return createUmdConfig('axios-extensions.js', false);
case 'umd-min':
return createUmdConfig('axios-extensions.min.js', true);
default:
throw new Error(`Unknown build mode: ${mode}`);
}
});