How to use the transformFromAstSync function from @babel/core

Find comprehensive JavaScript @babel/core.transformFromAstSync code examples handpicked from public code repositorys.

In Babel, the @babel/core.transformFromAstSync function is used to synchronously transform an AST (abstract syntax tree) using a specified set of Babel plugins and options.

171
172
173
174
175
176
177
178
179
180
      return ast;
    },
  },
});

const { ast: transformedAST } = transformFromAstSync(ast, sourceCode, {
  plugins: [[UpdateImportPlugin, options]],
  // allowing it to preserve formatting
  cloneInputAst: false,
  code: false,
fork icon34
star icon253
watch icon13

+ 11 other calls in file

163
164
165
166
167
168
169
170
171
172

const ast = parser.parse(sourceCode, {
    sourceType: 'unambiguous'
});

const { code } = transformFromAstSync(ast, sourceCode, {
    plugins: [[autoTrackPlugin, {
        trackerPath: 'tracker'
    }]]
});
fork icon4
star icon7
watch icon1

+ 5 other calls in file

How does @babel/core.transformFromAstSync work?

@babel/core.transformFromAstSync is a synchronous function in the Babel library that is used to transform an AST (abstract syntax tree) using a specified set of Babel plugins and options.

When @babel/core.transformFromAstSync is called with an AST and a set of options as input, it performs the following operations:

  • It applies a set of Babel plugins and options to the AST, transforming the code as specified by the plugins and options.
  • It returns the transformed code as a string.

By using @babel/core.transformFromAstSync, developers can transform an AST using Babel without having to first generate JavaScript code from the AST. This can be useful for advanced use cases where the AST is already available, such as when implementing custom Babel plugins or using Babel programmatically. Note that @babel/core.transformFromAstSync may not work as expected with certain types of ASTs or plugins, so it is important to test thoroughly before using it in production code.

291
292
293
294
295
296
297
298
299
300
}

plugins.push([metroTransformPlugins.inlinePlugin, babelPluginOpts]);

ast = nullthrows(
  transformFromAstSync(ast, '', {
    ast: true,
    babelrc: false,
    code: false,
    configFile: false,
fork icon617
star icon0
watch icon99

+ 3 other calls in file

218
219
220
221
222
223
224
225
226
227
        babel: true,
        sourceType: babelConfig.sourceType,
      });

const functionMap = generateFunctionMap(sourceAst, {filename});
const result = transformFromAstSync(sourceAst, src, babelConfig);

// The result from `transformFromAstSync` can be null (if the file is ignored)
if (!result) {
  /* $FlowFixMe BabelTransformer specifies that the `ast` can never be null but
fork icon613
star icon0
watch icon99

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const babel = require("@babel/core");
const parser = require("@babel/parser");

// Parsing some code to an AST
const ast = parser.parse("const foo = () => {};");

// Applying Babel transformations to the AST
const outputCode = babel.transformFromAstSync(ast, null, {
  presets: ["@babel/preset-env"],
}).code;

console.log(outputCode); // Outputs: transformed code

In this example, we're using @babel/core.transformFromAstSync to transform an AST generated from the code "const foo = () => {};" using the @babel/preset-env preset. The transformed code is then output to the console. Note that @babel/core.transformFromAstSync does not require a file path as input, as it operates directly on an AST. This can be useful in situations where code is generated programmatically or transformed using custom tools.

90
91
92
93
94
95
96
97
98
99
      }else {
        path.parent.body.push(nameTemplate({ name: types.identifier(name) }))
      }
    }
  })
  const text = babel.transformFromAstSync(ast, tranform.code)
  cache.add(dirPath,text.code)
  cache.addDepend(dirPath,imports)
  return imports
}
fork icon3
star icon0
watch icon1

+ 5 other calls in file

69
70
71
72
73
74
75
76
77
78

// 模块的ID 从 0 开始,相当于一个js文件,可以看成一个模块
const id = ID++;

// ES6 --> ES5
const { code } = babel.transformFromAstSync(ast, null, {
  presets: ["@babel/preset-env"], // 支持被认为是 latest 的Babel所有插件
});
//   console.log(id, filename, dependencies, code);
return {
fork icon1
star icon0
watch icon1

89
90
91
92
93
94
95
96
97
98
99
100
101
    }




    // console.log(babel.transformFromAstSync(ast).code);


    return babel.transformFromAstSync(ast).code
}


// readFromFile(filename, fixReturn);
fork icon0
star icon1
watch icon1

+ 5 other calls in file

80
81
82
83
84
85
86
87
88
89
90
91
92


const path = require("path");


const _require = require("@babel/core"),
  parseSync = _require.parseSync,
  transformFromAstSync = _require.transformFromAstSync;


const _require2 = require("metro-source-map"),
  generateFunctionMap = _require2.generateFunctionMap;

fork icon0
star icon0
watch icon1