How to use the transformFileSync function from @babel/core

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

@babel/core.transformFileSync is a function that transforms a file using Babel, a JavaScript compiler, and returns the transformed code.

89
90
91
92
93
94
95
96
97
98
spawnSync('mkdir', ['-p', path.dirname(destPath)]);
if (!minimatch(file, IGNORE_PATTERN)) {
  if (!minimatch(file, JS_FILES_PATTERN)) {
    fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
  } else {
    const transformed = babel.transformFileSync(file, babelOptions).code;
    spawnSync('mkdir', ['-p', path.dirname(destPath)]);
    fs.writeFileSync(destPath, transformed);
  }
}
fork icon663
star icon0
watch icon204

11
12
13
14
15
16
17
18
19
20

// 获取 import {a, b, c} from '@xxx/yyy', a, b, c 对应的模块真实路径,只适用于nanachi ui 组件库
const getImportSpecifierFilePath = (function () {
    const ret = {};
    return function (ImportSpecifierIdentifier: string, entryFilePath: string) {
        babel.transformFileSync(entryFilePath, {
            configFile: false,
            babelrc: false,
            comments: false,
            ast: true,
fork icon330
star icon44
watch icon4

How does @babel/core.transformFileSync work?

The @babel/core.transformFileSync function is used to transform a JavaScript file using Babel, a popular compiler for modern JavaScript code. This function is a part of the @babel/core package, which is the core of the Babel toolchain. To use @babel/core.transformFileSync, you pass the path to a JavaScript file that you want to transform, along with any configuration options that you want to use. Babel will then apply any configured transformations to the file and return the transformed code as a string. Here's an example of how to use @babel/core.transformFileSync: javascript Copy code {{{{{{{ const babel = require('@babel/core'); const fs = require('fs'); const inputFile = 'src/index.js'; const outputFile = 'dist/index.js'; const options = { presets: ['@babel/preset-env'] }; const inputCode = fs.readFileSync(inputFile, 'utf8'); const outputCode = babel.transformFileSync(inputFile, options).code; fs.writeFileSync(outputFile, outputCode, 'utf8'); In this example, we're using @babel/core.transformFileSync to transform a file located at src/index.js using the @babel/preset-env preset. We then write the transformed code to a file located at dist/index.js. This code demonstrates how to use @babel/core.transformFileSync to transform a JavaScript file using Babel, and to save the transformed code to a new file.

31
32
33
34
35
36
37
38
39
40
// their own locales files and taken care separatedly in this script
glob('src/**/*.js?(x)', {
  ignore: ['src/customizations/**', 'src/addons/**'],
}),
(filename) => {
  babel.transformFileSync(filename, {}, (err) => {
    if (err) {
      console.log(err);
    }
  });
fork icon215
star icon232
watch icon182

+ 2 other calls in file

42
43
44
45
46
47
48
49
50
51
const porps = {}
const ext = this.getPathExt(path)
if (ext === '.ts') {
  porps.presets = ['@babel/preset-typescript']
}
const transForm = babel.transformFileSync(path, porps)
// this.log('path2Ast transForm:', transForm)

const { code, options } = transForm
const { root } = options
fork icon3
star icon0
watch icon4

+ 7 other calls in file

Ai Example

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

const inputFile = "src/index.js";
const outputFile = "dist/index.js";

const options = {
  presets: ["@babel/preset-env"],
};

const inputCode = fs.readFileSync(inputFile, "utf8");
const outputCode = babel.transformFileSync(inputFile, options).code;

fs.writeFileSync(outputFile, outputCode, "utf8");

In this example, we're using @babel/core.transformFileSync to transform a file located at src/index.js using the @babel/preset-env preset. We then write the transformed code to a file located at dist/index.js.

19
20
21
22
23
24
25
26
27
28
29
 */
function transformCode(path, source, config = {}) {
  const options = getBabelOptions(path, config);
  const result =
    source === undefined
      ? babel.transformFileSync(path, options)
      : babel.transformSync(source, options);


  if (!result || !result.code) {
    throw new Error(`babel failed to transpile [${path}]`);
fork icon0
star icon0
watch icon1

+ 2 other calls in file

32
33
34
35
36
37
38
39
40
41
42
43
44
function removeDEV() {


    const suite = makeSuite('removeDEV');


    suite.eachSrcFile(({fileName, filePath}) => {
        let result = babel.transformFileSync(filePath, {
            plugins: [removeDEVPlugin]
        });


        suite.writeToExpectFile(fileName, result.code);
fork icon0
star icon0
watch icon0

+ 3 other calls in file

161
162
163
164
165
166
167
168
169
170
let plugins = [];

!reserveDEV && plugins.push(removeDEVPlugin);
plugins.push(esm2cjsPlugin);

let {code} = babel.transformFileSync(inputPath, {
    plugins: plugins
});

!reserveDEV && removeDEVPlugin.recheckDEV(code);
fork icon0
star icon0
watch icon0

5
6
7
8
9
10
11
12
13
14
15
16
const babel = require("@babel/core");
const url = "http://localhost"


const html = fs.readFileSync(path.resolve(__dirname, '..', 'index.html'), 'utf-8')


const babelResult = babel.transformFileSync(
  path.resolve(__dirname, '..', 'index.js'), {
    presets: ['@babel/env']
  }
);
fork icon0
star icon0
watch icon0

249
250
251
252
253
254
255
256
257
258
This code has been generated by resources/buildConfigDefinitions.js
Do not edit manually, but update Options/index.js
`;

const babel = require('@babel/core');
const res = babel.transformFileSync('./src/Options/index.js', {
  plugins: [plugin, '@babel/transform-flow-strip-types'],
  babelrc: false,
  auxiliaryCommentBefore,
  sourceMaps: false,
fork icon0
star icon0
watch icon591