-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
58 lines (47 loc) · 1.65 KB
/
convert.js
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
import {Project, SyntaxKind} from "ts-morph"
const project = new Project({
tsConfigFilePath: "./tsconfig.json",
compilerOptions: {
noEmit: false,
outDir: 'dist',
},
})
const t = new Map()
for (const sf of project.getSourceFiles()) {
const imports = sf.getImportDeclarations().map(i => i.getModuleSpecifier().getLiteralValue())
const dyn = sf.getDescendantsOfKind(SyntaxKind.CallExpression).flatMap(call =>
call.getChildren()[0]?.getKind() === SyntaxKind.ImportKeyword
? call.getText()
: []
)
t.set(sf, `${imports.map(i => `import '${i}'`).concat(dyn).join('\n')}\n` + (sf.getBaseName().endsWith('css.ts') ? '' : `
const t: any = 'var${Math.random().toString().slice(2)}'
if (window[t])
throw new Error('Already imported!')
window[t] = t
if (import.meta.hot) {
import.meta.hot.on('vite:beforeUpdate', () => {
delete window[t]
})
}
`))
}
const bigCode = `
import React from 'react'
function SvgChatBubble(props: React.SVGProps<SVGSVGElement>) {
return (
<svg viewBox="0 0 24 24" fill="none" {...props}>
<path
d="M22 11.444a9.311 9.311 0 01-1 4.223 9.445 9.445 0 01-8.444 5.222 9.311 9.311 0 01-4.223-1L2 22l2.111-6.333a9.311 9.311 0 01-1-4.223A9.444 9.444 0 018.333 3a9.311 9.311 0 014.223-1h.555A9.422 9.422 0 0122 10.889v.556z"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
)
}`
for(const [sf, text] of t) {
sf.replaceWithText(text + (sf.getBaseName().endsWith('tsx') ? bigCode : ''))
}
project.saveSync()