1 |
|
2 |
|
3 | import {
|
4 | transformSync,
|
5 | transformFromAst
|
6 | } from '@babel/core';
|
7 |
|
8 | export default (codeOrAst, opts = {}) => {
|
9 | var astOpts, code, needRuntime, options, transform;
|
10 | needRuntime = true;
|
11 | astOpts = opts;
|
12 | if (opts.needRuntime != null) {
|
13 | ({needRuntime, ...astOpts} = opts);
|
14 | }
|
15 | transform = typeof codeOrAst === 'string' ? transformSync : (ast, options) => {
|
16 | return transformFromAst(ast, '', options);
|
17 | };
|
18 | options = {
|
19 | presets: ["@babel/preset-env"],
|
20 | plugins: [...((needRuntime != null) && (needRuntime === true) ? ['@babel/plugin-transform-runtime'] : [])]
|
21 | };
|
22 | ({code} = transform(codeOrAst, {...options, ...astOpts}));
|
23 | return code;
|
24 | };
|