46 lines
953 B
JavaScript
Executable File
46 lines
953 B
JavaScript
Executable File
#!/usr/bin/env node
|
|
const fs = require("fs")
|
|
const babel = require("@babel/core")
|
|
|
|
const code = `
|
|
export class A {
|
|
#a: string;
|
|
}
|
|
`
|
|
|
|
const res = babel.transform(code, {
|
|
filename: "a.ts",
|
|
presets: [
|
|
["@babel/preset-env", { targets: { node: 14 } }],
|
|
["@babel/preset-typescript"],
|
|
],
|
|
plugins: [
|
|
({ types: t }) => ({
|
|
visitor: {
|
|
ClassDeclaration(path, state) {
|
|
state.a ??= new WeakSet()
|
|
if (state.a.has(path.node)) {
|
|
return
|
|
}
|
|
state.a.add(path.node)
|
|
|
|
path.parentPath.replaceWithMultiple([
|
|
t.exportNamedDeclaration(
|
|
t.variableDeclaration("let", [
|
|
t.variableDeclarator(path.node.id, {
|
|
...path.node,
|
|
type: "ClassExpression",
|
|
}),
|
|
]),
|
|
[],
|
|
null
|
|
),
|
|
])
|
|
},
|
|
},
|
|
}),
|
|
],
|
|
})
|
|
|
|
console.log(res.code)
|