-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathvite.config.ts
More file actions
63 lines (61 loc) · 1.91 KB
/
vite.config.ts
File metadata and controls
63 lines (61 loc) · 1.91 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
import { defineConfig, transformWithEsbuild } from 'vite'
import * as path from 'node:path'
import * as fs from 'node:fs'
export default defineConfig({
build: {
outDir: 'packages/fiber/react-reconciler',
target: 'esnext',
lib: {
formats: ['es'],
entry: {
index: 'packages/fiber/node_modules/react-reconciler/index.js',
constants: 'packages/fiber/node_modules/react-reconciler/cjs/react-reconciler-constants.production.js',
},
fileName: '[name]',
},
rollupOptions: {
treeshake: false,
external: (id: string) => !id.startsWith('.') && !path.isAbsolute(id),
output: {
entryFileNames: '[name].js',
chunkFileNames: '[name].js',
},
},
},
plugins: [
{
name: 'vite-ts',
enforce: 'pre',
transform(code, id) {
// Vite does not preserve static exports which breaks ESM transpilation
// Instead, we manually transpile constants from CJS -> ESM
if (id.includes('react-reconciler-constants')) {
return (
code
// Export top-level constants
.replace(/exports\.(\w+)\s*=\s*(.*?);/g, 'export const $1 = $2;')
// Remove "use strict"
.replace(/"use strict";\s*/g, '')
)
}
},
generateBundle(_options, bundle) {
for (const key in bundle) {
if (!('isEntry' in bundle[key]) || !bundle[key].isEntry) continue
const name = bundle[key].name
const source = fs.readFileSync(`packages/fiber/node_modules/@types/react-reconciler/${name}.d.ts`, 'utf-8')
this.emitFile({ type: 'asset', fileName: `${name}.d.ts`, source })
}
},
},
{
name: 'vite-minify',
renderChunk: {
order: 'post',
handler(code, { fileName }) {
return transformWithEsbuild(code, fileName, { minify: true })
},
},
},
],
})