How to use the loadOptions function from @babel/core

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

@babel/core.loadOptions is a function that loads and validates configuration options for Babel compiler.

38
39
40
41
42
43
44
45
46
47
48
  }
  return ''
}


function getBabelConfig () {
  const { plugins = [] } = babel.loadOptions()
  const partialConfig = babel.loadPartialConfig()
  const targets = getTargetByConfig(partialConfig) || browserslist.loadConfig({ path: process.cwd() })
  const { list = [] } = coreJsCompat({ targets })
  return {
fork icon1
star icon17
watch icon0

27
28
29
30
31
32
33
34
35
36
37
    peggyVersion: peggy.version,
    // get a config for a fake js, ts, and tsx file to make sure we
    // capture conditional config portions based on the file extension
    js: babel.loadOptions(getBabelOptions(Path.resolve('foo.js'))),
    ts: babel.loadOptions(getBabelOptions(Path.resolve('foo.ts'))),
    tsx: babel.loadOptions(getBabelOptions(Path.resolve('foo.tsx'))),
  });


  return Crypto.createHash('sha256').update(json).digest('hex').slice(0, 10);
}
fork icon0
star icon0
watch icon1

+ 11 other calls in file

How does @babel/core.loadOptions work?

@babel/core.loadOptions is a function that loads and validates the configuration options for Babel using various sources, including babel.config.js files, .babelrc files, and CLI options. It returns a validated options object that can be used to configure a Babel transform.

11
12
13
14
15
16
17
18
19
20
name: 'babel',
setup(build, { transform } = {}) {
  const { filter = /.*/, namespace = '', config = {} } = options

  const transformContents = ({ args, contents }) => {
    const babelOptions = babel.loadOptions({
      ...config,
      filename: args.path,
      caller: {
        name: 'esbuild-plugin-babel',
fork icon0
star icon0
watch icon0

547
548
549
550
551
552
553
554
555
556
const fileCss = await jssCache.get(hash);
if (fileCss) {
  return fileCss;
}

const babelOptions = babel.loadOptions({caller: {name: 'jss'}});
if (!babelOptions) {
  throw new Error('Could not find babel config for jss');
}
babelOptions['filename'] = jssFile;
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
const babel = require("@babel/core");

const options = babel.loadOptions({
  presets: ["@babel/preset-env"],
  plugins: ["@babel/plugin-transform-arrow-functions"],
});

console.log(options);

This example loads the Babel options with the presets @babel/preset-env and plugins @babel/plugin-transform-arrow-functions, and logs the resulting options to the console.

89
90
91
92
93
94
95
96
97
98
build.onLoad(
  {filter: /\.(cjs|mjs|js|jsx|ts|tsx)$/, namespace: ''},
  async (file) => {
    const filename = file.path;
    const babelOptions = /** @type {*} */ (
      babel.loadOptions({caller: {name: callerName}, filename}) || {}
    );
    babelOptions.sourceMaps = true;
    if (plugins) {
      babelOptions.plugins = [...babelOptions.plugins, ...plugins];
fork icon0
star icon0
watch icon0

67
68
69
70
71
72
73
74
75
76
  return;
}

debug(CompilationLifecycles['closured-pre-babel'], file);
/** @type {?babel.TransformOptions} */
const babelOptions = babel.loadOptions({caller: {name: 'post-closure'}});
const {code, map: babelMap} =
  (await babel.transformFileAsync(file, babelOptions ?? undefined)) || {};
if (!code || !babelMap) {
  throw new Error(`Error transforming contents of ${file}`);
fork icon0
star icon0
watch icon0

82
83
84
85
86
87
88
89
90
91
  return transformedFile;
}
try {
  debug(CompilationLifecycles['pre-babel'], file);
  const babelOptions =
    babel.loadOptions({caller: {name: 'pre-closure'}}) || {};
  const optionsHash = md5(
    JSON.stringify({babelOptions, argv: process.argv.slice(2)})
  );
  const {contents, hash} = await batchedRead(file, optionsHash);
fork icon0
star icon0
watch icon0

62
63
64
65
66
67
68
69
70

async setup(build) {
  preSetup();

  const babelOptions =
    babel.loadOptions({caller: {name: callerName}}) || {};
  const optionsHash = md5(
    JSON.stringify({babelOptions, argv: process.argv.slice(2)})
  );
fork icon0
star icon0
watch icon0