-
Notifications
You must be signed in to change notification settings - Fork 81
/
vite.config.ts
152 lines (148 loc) · 4.24 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
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
/// <reference types="vitest" />
import { resolve } from 'path'
import url from 'url'
import { defineConfig } from 'vite'
import { configDefaults } from 'vitest/config'
import dts from 'vite-plugin-dts'
import svgr from 'vite-plugin-svgr'
import libAssetsPlugin from '@laynezh/vite-plugin-lib-assets'
import { checker } from 'vite-plugin-checker'
import react from '@vitejs/plugin-react'
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const uswdsIncludePaths = [
'node_modules/@uswds',
'node_modules/@uswds/uswds/packages',
]
/**
* Pass `--mode uswds` to build uswds assets
*/
export default defineConfig(({ mode: _mode }) => {
const isUswds = _mode === 'uswds'
const isBundles = _mode === 'bundles'
const mode = isUswds ? 'production' : isBundles ? 'test' : _mode
const entryName = isUswds ? 'uswds' : 'index'
const isTest = mode === 'test' || mode === 'benchmark'
return {
mode,
// ignore some plugins if running tests
plugins: [
react(),
!isTest &&
checker({
typescript: true,
}),
!isTest &&
isUswds &&
libAssetsPlugin({
// ignore svg files as they will be inlined via svgr
exclude: /\.svg(\?.*)?$/,
// matches webpack names
name: '[contenthash:20].[ext]',
}),
// default svg url pattern is `*.svg?react`, updated to `*.svg?svgr`
svgr({
svgrOptions: { icon: true, memo: true },
include: '**/*.svg?svgr',
}),
!isTest && dts({ tsconfigPath: 'tsconfig.build.json' }),
],
build: {
outDir: 'lib',
emptyOutDir: !isUswds, // add uswds assets onto lib build
lib: {
entry: isUswds
? resolve(__dirname, 'node_modules/@uswds/uswds/dist/css/uswds.css')
: resolve(__dirname, `src/index.ts`),
name: 'ReactUSWDS',
formats: !isUswds ? ['cjs', 'es', 'iife', 'umd'] : ['es'],
fileName: entryName,
},
cssCodeSplit: isUswds,
assetsDir: './',
rollupOptions: {
external: [
'react',
'react-dom',
'react/jsx-runtime',
'focus-trap-react',
],
output: {
// Support React Server Components
// See: https://react.dev/reference/react/use-client
banner: (assetInfo) =>
assetInfo.fileName.endsWith('.js') ||
assetInfo.fileName.endsWith('.mjs')
? '"use client";'
: '',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'react/jsx-runtime': 'jsxRuntime',
'focus-trap-react': 'FocusTrap',
},
assetFileNames: (assetInfo) => {
if (assetInfo.name === 'style.css') return `${entryName}.css`
return assetInfo.name ?? ''
},
},
},
sourcemap: true,
terserOptions: {
compress: {
// Preserve directives like "use client"
directives: false,
},
},
},
css: {
devSourcemap: true,
preprocessorOptions: {
scss: {
includePaths: uswdsIncludePaths,
},
},
},
resolve: {
alias: [
{
find: 'uswds',
replacement: resolve(__dirname, './node_modules/@uswds/uswds'),
},
],
},
test: {
// bundles test added to default exclude list (so that it can be ran manually after build)
exclude: [
...configDefaults.exclude,
!isBundles ? 'src/bundles.test.tsx' : '',
],
globals: true,
setupFiles: ['src/setupTests.ts'],
environment: 'jsdom',
coverage: {
all: false,
// same as jest default (labeled babel)
provider: 'istanbul',
thresholds: {
global: {
statements: 96,
branches: 87,
functions: 94,
lines: 96,
},
},
},
css: false,
alias: [
{
find: /.+\.svg\?svgr$/,
replacement: resolve(__dirname, './__mocks__/svgrMock.js'),
},
{
find: /.+\.(jpg|jpeg|png|gif|eot|otf|webp|ttf|svg|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$/,
replacement: resolve(__dirname, '__mocks__/fileMock.js'),
},
],
},
}
})