-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvite.config.ts
More file actions
157 lines (150 loc) · 4.49 KB
/
vite.config.ts
File metadata and controls
157 lines (150 loc) · 4.49 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
import { BuildOptions, defineConfig, mergeConfig } from 'vite';
import { defineConfig as defineVitestConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';
import path from 'path';
import dts from 'vite-plugin-dts';
import { externalizeDeps } from 'vite-plugin-externalize-deps';
import tsconfigPaths from 'vite-tsconfig-paths';
import { visualizer } from 'rollup-plugin-visualizer';
import { cssColocatePlugin } from './plugins/css-colocate';
import { generateScopedName } from './plugins/css-colocate/utils';
const srcDir = path.resolve(__dirname, 'src').replace(/\\/g, '/');
const createEntryFileNames = (ext: 'js' | 'cjs') => {
return (chunkInfo: { name: string }) => {
const parts = chunkInfo.name.split('/');
if (parts.length >= 2) {
const fileName = parts[parts.length - 1];
const dirName = parts[parts.length - 2];
// NOTE: Solve import name redundancy for better API
// by preferring default build name as index
// e.g. Button/Button -> Button
if (fileName === dirName) {
parts[parts.length - 1] = 'index';
return `${parts.join('/')}.${ext}`;
}
}
return `${chunkInfo.name}.${ext}`;
};
};
const buildOptions: BuildOptions = {
target: 'esnext',
emptyOutDir: true,
// WARNING: Do not minify unbundled builds
// Consumer will perform a final minification of app
// Minifiers often modify var names and collapse logic
// which makes static analysis challenging
minify: false,
lib: {
entry: {
index: path.resolve(srcDir, 'index.ts'),
'hooks/index': path.resolve(srcDir, 'hooks/index.ts'),
},
},
rollupOptions: {
output: [
{
format: 'es',
dir: 'dist/esm',
preserveModules: true,
preserveModulesRoot: 'src',
entryFileNames: createEntryFileNames('js'),
chunkFileNames: '[name].js',
// NOTE: assetFileNames is set at build level (Vite requires same pattern for all outputs)
banner: chunk =>
chunk.name === 'index' || chunk.name === 'hooks/index' ? `'use client';` : '',
interop: 'auto',
},
{
format: 'cjs',
dir: 'dist/cjs',
preserveModules: true,
preserveModulesRoot: 'src',
entryFileNames: createEntryFileNames('cjs'),
chunkFileNames: '[name].cjs',
// NOTE: assetFileNames is set at build level (Vite requires same pattern for all outputs)
banner: chunk =>
chunk.name === 'index' || chunk.name === 'hooks/index' ? `'use client';` : '',
interop: 'auto',
exports: 'named',
},
],
},
sourcemap: true,
};
const viteConfig = defineConfig({
publicDir: false,
css: {
modules: {
// Generate predictable class names for debugging in dev
generateScopedName,
},
},
plugins: [
react({
// TODO: On styled-components migration completion
// remove styled-components babel plugins
babel: {
plugins: [['babel-plugin-styled-components', { displayName: false }]],
env: {
development: {
plugins: [['babel-plugin-styled-components', { displayName: true }]],
},
},
},
}),
dts({
outDir: 'dist/types',
include: ['src/**/*'],
exclude: [
'**/*.stories.*',
'**/*.test.*',
'**/*.mdx',
'src/App.tsx',
'src/main.tsx',
'src/examples/**',
'src/assets/**',
'src/stories/**',
],
}),
externalizeDeps({
deps: true,
devDeps: false,
nodeBuiltins: true,
optionalDeps: true,
peerDeps: true,
useFile: path.join(process.cwd(), 'package.json'),
}),
tsconfigPaths(),
cssColocatePlugin(),
// WARNING: Keep the visualizer last
...(process.env.ANALYZE === 'true'
? [
visualizer({
open: true,
gzipSize: true,
brotliSize: true,
filename: './tmp/stats.html',
}),
]
: []),
],
resolve: {
alias: {
'@': srcDir,
},
},
build: buildOptions,
});
const vitestConfig = defineVitestConfig({
test: {
environment: 'jsdom',
// TODO: Note that currently, the pw visual regression tests
// are kept separate, see ./tests
include: ['src/**/*.{test,spec}.{ts,tsx}'],
exclude: ['node_modules', 'dist', 'build', 'storybook-static', '.storybook'],
globals: true,
watch: false,
setupFiles: ['@testing-library/jest-dom', './setupTests.ts'],
},
});
export default mergeConfig(viteConfig, vitestConfig);