-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
93 lines (90 loc) · 2.57 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
import react from '@vitejs/plugin-react';
import glob from 'glob';
import { resolve } from 'path';
import path from 'path';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { viteStaticCopy } from 'vite-plugin-static-copy';
// https://vitejs.dev/guide/build.html#library-mode
// https://onderonur.netlify.app/blog/creating-a-typescript-library-with-vite/
const entriesMask = 'lib/**/*.{ts,tsx}';
export default defineConfig({
resolve: {
// allow absolute path imports. see tsconfig.json compilerOptions.paths
alias: [
{
find: 'lib',
replacement: resolve(__dirname, './lib'),
},
],
},
build: {
lib: {
// include independent modules https://github.com/vitejs/vite/discussions/8098
entry: glob.sync(path.resolve(__dirname, entriesMask)),
// esm - default format. cjs - for node require api (single bundle)
fileName: (format, entryName) => `${entryName}.${format === 'esm' ? 'js' : format}`,
},
rollupOptions: {
// exclude react from dist:
external: ['react'],
output: [
{
// Keep directory structure and files:
preserveModules: true,
sourcemap: true,
// link *.js.map files to copied lib files. see viteStaticCopy
sourcemapPathTransform: (relativeSourcePath) => {
return `./${relativeSourcePath.split('/').pop()}`;
},
// sources already linked in map files. no reason to include sources as strings
sourcemapExcludeSources: true,
format: 'esm',
globals: {
react: 'React',
},
},
// support legacy node require api
{
preserveModules: true,
format: 'cjs',
globals: {
react: 'React',
},
},
],
},
},
plugins: [
// allow jsx/tsx:
react({
// exclude react/jsx-runtime (30kb+) from dist
jsxRuntime: 'classic',
}),
// export types:
// rollupTypes - put all types into one file
dts(),
// copy sources to dist
viteStaticCopy({
flatten: false,
targets: [
{
// path of sources which will be copied to dist
src: glob.sync(entriesMask),
dest: './',
},
{
src: ['./README.md', 'LICENSE'],
dest: './',
},
{
// set package.json for npm
// https://webpack.js.org/guides/package-exports/
src: './package.dist.json',
dest: './',
rename: 'package.json',
},
],
}),
],
});