-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
68 lines (63 loc) · 2.03 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
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import replace, { type RollupReplaceOptions } from '@rollup/plugin-replace'
import react from '@vitejs/plugin-react'
import { defineConfig, type Plugin, type UserConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'
export default defineConfig(async ({ mode }) => {
const isBuild = mode === 'production'
const { version = null } = isBuild ? await readPackageJson() : {}
if (isBuild && version == null) {
throw new Error('Missing version in package.json')
}
const config: UserConfig = {
plugins: [
tsconfigPaths(),
react(),
// UPSTREAM: Rollup plugins have broken type declarations.
// https://github.com/rollup/plugins/issues/1541
(replace as unknown as (options: RollupReplaceOptions) => Plugin)(
isBuild
? {
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify('production'),
'const seamapiReactVersion = null': `const seamapiReactVersion = '${version}'`,
}
: { preventAssignment: true }
),
],
build: {
outDir: fileURLToPath(new URL('./dist', import.meta.url)),
sourcemap: true,
emptyOutDir: false,
lib: {
entry: fileURLToPath(new URL('./src/elements.ts', import.meta.url)),
fileName: 'elements',
formats: ['es'],
},
},
test: {
setupFiles: ['./test/fixtures/api.ts'],
environment: 'happy-dom',
deps: {
interopDefault: false,
web: {
transformCss: false,
transformAssets: false,
},
},
coverage: {
provider: 'v8',
},
},
}
return config
})
export const readPackageJson = async (): Promise<{ version?: string }> => {
const pkgBuff = await readFile(
fileURLToPath(new URL('package.json', import.meta.url))
)
return JSON.parse(pkgBuff.toString())
}
export const versionFiles = ['./README.md']
export const versionPlaceholder = '_________VERSION_________'