()
| 2 | import type { PluginObj } from '@babel/core'; |
| 3 | |
| 4 | function App() { |
| 5 | |
| 6 | const code1 =` |
| 7 | function add(a, b) { |
| 8 | return a + b; |
| 9 | } |
| 10 | export { add }; |
| 11 | `; |
| 12 | |
| 13 | const url = URL.createObjectURL(new Blob([code1], { type: 'application/javascript' })); |
| 14 | |
| 15 | const transformImportSourcePlugin: PluginObj = { |
| 16 | visitor: { |
| 17 | ImportDeclaration(path) { |
| 18 | path.node.source.value = url; |
| 19 | } |
| 20 | }, |
| 21 | } |
| 22 | |
| 23 | |
| 24 | const code = `import { add } from './add.ts'; |
| 25 | |
| 26 | console.log(add(2, 3)); |
| 27 | ` |
| 28 | |
| 29 | function onClick() { |
| 30 | const res = transform(code, { |
| 31 | presets: ['react', 'typescript'], |
| 32 | filename: 'guang.ts', |
| 33 | plugins: [transformImportSourcePlugin] |
| 34 | }); |
| 35 | console.log(res.code); |
| 36 | } |
| 37 | |
| 38 | return ( |
| 39 | <div> |
| 40 | <button onClick={onClick}>编译</button> |
| 41 | </div> |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | export default App |
nothing calls this directly
no outgoing calls
no test coverage detected