-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
57 lines (51 loc) · 1.55 KB
/
vite.config.ts
File metadata and controls
57 lines (51 loc) · 1.55 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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import path from 'path'
import type { Plugin } from 'vite'
/**
* Inline source-location transform for the dev app.
* Adds data-direct-edit-source attributes to lowercase JSX tags
* so the DirectEdit panel can locate source files.
*
* Adapted from vite/index.ts but avoids the issues with
* require.resolve('made-refine') and path-based filtering.
*/
function devSourceLocations(): Plugin {
let root = ''
return {
name: 'dev-source-locations',
enforce: 'pre',
configResolved(config) {
root = config.root
},
transform(code, id) {
if (!/\.[jt]sx$/.test(id)) return null
if (id.includes('node_modules')) return null
// Only instrument the dev app files, not the library source
if (!id.startsWith(path.resolve(root))) return null
const relativePath = path.relative(root, id)
let line = 1
let lastIndex = 0
const result = code.replace(/<([a-z][a-zA-Z0-9]*)\b/g, (match, _tag, offset) => {
const slice = code.slice(lastIndex, offset)
line += (slice.match(/\n/g) || []).length
lastIndex = offset
const col = offset - code.lastIndexOf('\n', offset)
return `${match} data-direct-edit-source="${relativePath}:${line}:${col}"`
})
return { code: result, map: null }
},
}
}
export default defineConfig({
root: 'dev',
plugins: [
tailwindcss(),
devSourceLocations(),
react(),
],
server: {
port: 3000,
},
})