How to use the transformAsync function from @babel/core

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

50
51
52
53
54
55
56
57
58
59
  }
}

async function write ({ destDir, filename, code, map }, env) {
  if (env) {
    ;({ code, map } = await babel.transformAsync(code, {
      filename,
      inputSourceMap: map,
      plugins: [[require.resolve('./babel/inline-env-vars'), { env }]],
    }))
fork icon763
star icon0
watch icon188

+ 3 other calls in file

63
64
65
66
67
68
69
70
71
72
else if ('ignore' in options && options.ignore.some(ignoreGlob => multimatch(sourcefile, ignoreGlob).length)) {
        transformed = contents;
        isSkipped = true;
} else {
        try {
                ({ code: transformed } = await babel.transformAsync(
                        contents,
                        Object.assign(
                                localOptions,
                                options
fork icon597
star icon0
watch icon122

+ 3 other calls in file

89
90
91
92
93
94
95
96
97
98
      require.resolve('docsx/src/modules/utils/babel-plugin-jsx-preview'),
      { maxLines: 16, outputFilename: `${tsxPath}.preview` },
    ],
  ]);
}
const { code } = await babel.transformAsync(source, transformOptions);

if (/import \w* from 'prop-types'/.test(code)) {
  throw new Error('TypeScript demo contains prop-types, please remove them');
}
fork icon590
star icon0
watch icon48

+ 4 other calls in file

59
60
61
62
63
64
65
66
67
68
// mark source for omit from build result
taskUtil.setTag(resource, taskUtil.STANDARD_TAGS.OmitFromBuildResult, true);

// transpile the source
config.debug && log.info(`Transpiling resource ${resourcePath}`);
const result = await babel.transformAsync(
	source,
	Object.assign({}, babelConfig, {
		filename: resourcePath // necessary for source map <-> source assoc
	})
fork icon69
star icon161
watch icon16

+ 4 other calls in file

43
44
45
46
47
48
49
50
51
52
  }
  return null;
},
async transform(stdin, id) {
  if (!filter(id)) return;
  const res = await babel.transformAsync(stdin, {
    plugins: [[babelPlugin, restOptions]],
    filename: id,
    sourceMaps: true,
    parserOpts: parserOptions,
fork icon25
star icon494
watch icon6

+ 9 other calls in file

9
10
11
12
13
14
15
16
17
18
async function transformStyle9(
  code,
  id,
  { parserOptions, restOptions, server, cssModules }
) {
  const res = await babel.transformAsync(code, {
    plugins: [[babelPlugin, restOptions]],
    filename: id,
    sourceMaps: true,
    parserOpts: parserOptions,
fork icon25
star icon494
watch icon6

61
62
63
64
65
66
67
68
69
                localOpts = Object.assign({}, localOpts, {
                    presets: [],
                });
            }

            return babelCore.transformAsync(code, localOpts);
        },
    };
};
fork icon2
star icon0
watch icon1

62
63
64
65
66
67
68
69
70
71
72
73
async function transpileFile(tsxPath, program) {
  const jsPath = tsxPath.replace(/\.tsx?$/, '.js');
  try {
    const source = await fse.readFile(tsxPath, 'utf8');


    const { code } = await babel.transformAsync(source, { ...babelConfig, filename: tsxPath });


    if (/import \w* from 'prop-types'/.test(code)) {
      throw new Error('TypeScript demo contains prop-types, please remove them');
    }
fork icon0
star icon0
watch icon1

+ 5 other calls in file

20
21
22
23
24
25
26
27
28
29
let loader = "jsx";
if (args.path.endsWith(".tsx")) {
  plugins.push("typescript");
  loader = "tsx";
}
const result = await babel.transformAsync(code, {
  babelrc: false,
  configFile: false,
  ast: false,
  root,
fork icon0
star icon0
watch icon0

7
8
9
10
11
12
13
14
15
16
const { dirname } = require('path')

let transform = {
  react: async (svg, componentName, format) => {
    let component = await svgr(svg, { ref: true, titleProp: true }, { componentName })
    let { code } = await babel.transformAsync(component, {
      plugins: [[require('@babel/plugin-transform-react-jsx'), { useBuiltIns: true }]],
    })

    if (format === 'esm') {
fork icon0
star icon0
watch icon157

+ 4 other calls in file