forked from vimaec/vim-webgl-component
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrollup.dts.config.mjs
More file actions
133 lines (125 loc) · 5.42 KB
/
rollup.dts.config.mjs
File metadata and controls
133 lines (125 loc) · 5.42 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
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
import dts from 'rollup-plugin-dts'
export default {
input: 'dist/types/index.d.ts',
output: {
file: 'dist/vim-web.d.ts',
format: 'es',
},
plugins: [
// Skip CSS imports
{
name: 'skip-css',
resolveId(source) {
if (source.endsWith('.css')) return { id: source, external: true }
return null
},
},
dts({
// Inline these so the d.ts is self-documenting (no opaque external types)
includeExternal: ['ste-signals', 'ste-simple-events', 'ste-events', 'ste-core'],
}),
// Fix broken self-referential imports from ste-signals.
// ste-signals' ISignal.d.ts has `import { ISignalHandler } from '.'` which
// rollup-plugin-dts can't resolve. Patch the output to remove the broken
// import and inject the missing type definition.
{
name: 'fix-ste-signals',
renderChunk(code) {
// Remove the broken import
code = code.replace(/^import \{ ISignalHandler \} from '\.';\n/m, '')
// If ISignalHandler is referenced but not defined, inject it
if (code.includes('ISignalHandler') && !code.includes('interface ISignalHandler')) {
// ISignalHandler is (ev: IEventManagement) => void
const definition = 'interface ISignalHandler {\n (ev: IEventManagement): void;\n}\n'
// Insert before first usage
code = code.replace(
/^(interface ISignal)/m,
definition + '\n$1'
)
}
return code
},
},
// Replace opaque index_d$N namespace names with semantic names.
// rollup-plugin-dts generates index_d, index_d$1, ... for namespaces.
// We detect each namespace's identity from its exported content.
{
name: 'fix-namespace-names',
renderChunk(code) {
const nameMap = new Map()
// Detect each namespace by peeking at its first export line
for (const m of code.matchAll(/declare namespace (index_d(?:\$\d+)?) \{/g)) {
const id = m[1]
const peek = code.substring(m.index, m.index + 500)
// Names match the access path: Core.Webgl → Core_Webgl, React.Ultra → React_Ultra
if (peek.includes('createCoreWebglViewer')) nameMap.set(id, 'Core_Webgl')
else if (peek.includes('createCoreUltraViewer')) nameMap.set(id, 'Core_Ultra')
else if (peek.includes('createWebglViewer')) nameMap.set(id, 'React_Webgl')
else if (peek.includes('createUltraViewer')) nameMap.set(id, 'React_Ultra')
else if (peek.includes('PointerMode')) nameMap.set(id, 'Core')
else if (peek.includes('controlBarIds')) nameMap.set(id, 'React_ControlBar')
else if (peek.includes('isFalse')) nameMap.set(id, 'React_Settings')
else if (peek.includes('errorStyle')) nameMap.set(id, 'React_Errors')
else if (peek.includes('contextMenuIds')) nameMap.set(id, 'React_ContextMenu')
}
// Bare index_d is the React top-level namespace
if (!nameMap.has('index_d') && code.includes('declare namespace index_d {')) {
nameMap.set('index_d', 'React')
}
// Fix file-derived namespace names (icons.tsx → icons_d, style.ts → style_d)
nameMap.set('icons_d', 'React_Icons')
nameMap.set('style_d', 'React_ControlBar_Style')
nameMap.set('errorStyle_d', 'React_Errors_Style')
// Replace longest names first (index_d$10 before index_d$1 before index_d)
// Use \b prefix to prevent style_d matching inside errorStyle_d
const sorted = [...nameMap.entries()].sort((a, b) => b[0].length - a[0].length)
for (const [from, to] of sorted) {
const escaped = from.replace(/\$/g, '\\$')
code = code.replace(new RegExp('\\b' + escaped + '(?!\\$|\\d)', 'g'), to)
}
return code
},
},
// Strip namespace alias noise generated by rollup-plugin-dts.
// For each namespace, dts generates standalone aliases like:
// type Core_Webgl_IFoo = IFoo;
// declare const Core_Webgl_Bar: typeof Bar;
// and re-exports them as:
// export { Core_Webgl_IFoo as IFoo, Core_Webgl_Bar as Bar };
// This plugin removes the standalone aliases and simplifies
// re-exports to reference the original names directly.
{
name: 'strip-namespace-aliases',
renderChunk(code) {
// Collect all alias names (not namespace names) from standalone declarations
const aliases = new Set()
for (const m of code.matchAll(/^type ((?:Core|React)_\w+)/gm)) {
aliases.add(m[1])
}
for (const m of code.matchAll(/^declare const ((?:Core|React)_\w+):/gm)) {
aliases.add(m[1])
}
// Remove standalone type alias lines
code = code.replace(/^type (?:Core|React)_\w+[^;]*;\n/gm, '')
// Remove standalone const alias lines
code = code.replace(/^declare const (?:Core|React)_\w+: typeof \w+;\n/gm, '')
// In export blocks, replace known aliases with direct references:
// "Core_Webgl_IFoo as IFoo" → "IFoo"
// Namespace references like "Core_Ultra as Ultra" are NOT in aliases set, so kept
code = code.replace(/(?:Core|React)_\w+ as (\w+)/g, (match, realName) => {
const aliasName = match.split(' as ')[0]
return aliases.has(aliasName) ? realName : match
})
return code
},
},
],
external: [
'three',
'react',
'react-dom',
'deepmerge',
'vim-format',
/\.css$/,
],
}