How to use the loadPartialConfigAsync function from @babel/core

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

The @babel/core.loadPartialConfigAsync function asynchronously loads partial Babel configuration.

43
44
45
46
47
48
49
50
51
52
let partialConfig;
if (path.basename(configFile) === "package.json") {
	// for the package.json, we need to extract the babel config
	partialConfig = require(configFile);
	if (partialConfig.babel) {
		partialConfig = await babel.loadPartialConfigAsync(
			Object.assign({ filename: "src/dummy.js" }, partialConfig.babel)
		);
	} else {
		partialConfig = configFile = undefined;
fork icon69
star icon160
watch icon16

How does @babel/core.loadPartialConfigAsync work?

@babel/core.loadPartialConfigAsync() is an async function that loads Babel configuration files, searches for the nearest configuration file, merges multiple configurations together, and returns the merged configuration. It can be used to load and validate Babel configuration from a .babelrc file or from a package.json file.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const { loadPartialConfigAsync } = require("@babel/core");

async function loadBabelConfig(filePath) {
  const { options, babelrc } = await loadPartialConfigAsync({
    cwd: process.cwd(),
    filename: filePath,
    envName: process.env.BABEL_ENV || process.env.NODE_ENV || "development",
    caller: {
      name: "loadBabelConfig",
      supportsStaticESM: true,
    },
  });

  return {
    options,
    babelrc,
  };
}

In this example, loadBabelConfig is an asynchronous function that takes a file path as input and returns a Promise that resolves to an object containing the parsed Babel configuration options and any .babelrc files found in the parent directories of the specified file. loadPartialConfigAsync is used to asynchronously load and parse the Babel configuration files. It takes an options object as input, which includes the cwd (current working directory), filename (the file path for which the configuration is being loaded), envName (the name of the environment for which to load the configuration), and caller (an object that identifies the caller of the function). The function returns a Promise that resolves to an object containing the parsed configuration options and any .babelrc files found. These options can then be passed to @babel/core.transform to transform JavaScript code using Babel.