1 | # import dd from 'ddeyes'
|
2 | import {
|
3 | transformSync
|
4 | transformFromAst
|
5 | } from '@babel/core'
|
6 |
|
7 | export default (
|
8 | codeOrAst
|
9 | opts = {}
|
10 | ) =>
|
11 | needRuntime = true
|
12 | astOpts = opts
|
13 | {
|
14 | needRuntime
|
15 | astOpts...
|
16 | } = opts if opts.needRuntime?
|
17 |
|
18 | transform =
|
19 | if typeof codeOrAst is 'string'
|
20 | then transformSync
|
21 | else (ast, options) =>
|
22 | transformFromAst ast, ''
|
23 | , options
|
24 |
|
25 | options =
|
26 | presets: [
|
27 | "@babel/preset-env"
|
28 | ]
|
29 | plugins: [
|
30 | (
|
31 | if needRuntime? and (
|
32 | needRuntime is true
|
33 | )
|
34 | then [ '@babel/plugin-transform-runtime' ]
|
35 | else []
|
36 | )...
|
37 | ]
|
38 |
|
39 | { code } = transform codeOrAst
|
40 | , {
|
41 | options...
|
42 | astOpts...
|
43 | }
|
44 |
|
45 | code
|