-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvite.config.ts
More file actions
90 lines (86 loc) · 2.7 KB
/
vite.config.ts
File metadata and controls
90 lines (86 loc) · 2.7 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
import eslintPlugin from '@nabla/vite-plugin-eslint';
import storybookTest from '@storybook/addon-vitest/vitest-plugin';
import pluginReact from '@vitejs/plugin-react';
import path from 'node:path';
import removeAttributes from 'rollup-plugin-jsx-remove-attributes';
import type { Plugin } from 'vite';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import svgr from 'vite-plugin-svgr';
import { name } from './package.json';
import { svgRawLoaderPlugin } from './vite/plugins/svg-raw-loader';
const __dirname = import.meta.dirname;
const { resolve } = path;
// Auto-detect Storybook from the CLI command.
const isStorybook = process.argv.some((arg) => arg.includes('storybook'));
export default defineConfig(async ({ mode }) => {
const isStorybookTest = Boolean(process.env.STORYBOOK_CONFIG_DIR);
const storybookPlugins = isStorybookTest ? await storybookTest() : [];
const plugins: Plugin[] = [
eslintPlugin(),
svgRawLoaderPlugin(),
svgr({
include: '**/*.svg?react',
}),
pluginReact(),
dts({
insertTypesEntry: true,
}),
...storybookPlugins,
mode === 'test'
? null
: removeAttributes({
attributes: ['data-testid'],
usage: 'vite',
}),
].filter(Boolean) as Plugin[];
return {
publicDir: false,
resolve: {
tsconfigPaths: true,
alias: {
'~': resolve(__dirname),
// Catch-all for internal library paths to bypass restrictive "exports" in package.json
'@cfpb/cfpb-design-system/src': resolve(
__dirname,
'node_modules/@cfpb/cfpb-design-system/src',
),
},
},
plugins,
build: {
lib: {
entry: resolve('src', 'index.ts'),
name,
formats: ['es', 'cjs'],
fileName: (format): string => `index.${format === 'es' ? 'mjs' : 'js'}`,
},
rollupOptions: {
// Only externalize in production/library build, not in Storybook dev mode.
external: isStorybook
? []
: [
'react',
'react-dom',
'react-router',
'react/jsx-runtime',
'react/jsx-dev-runtime',
],
output: {
// This prevents the "flat" file explosion for icons/assets in the root
assetFileNames: 'index.css',
chunkFileNames: 'chunks/[name]-[hash].js',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'react-router': 'ReactRouter',
},
},
},
},
optimizeDeps: {
// Only exclude in production/library build, not in Storybook dev mode.
exclude: isStorybook ? [] : ['react', 'react-dom', 'react-router'],
},
};
});