How to use the OptionManager function from @babel/core

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

@babel/core.OptionManager is a utility class that manages the options passed to Babel during configuration and compilation.

480
481
482
483
484
485
486
487
488
489
490
491
  }
  return false;
}


function compile(filename, callback) {
  const opts = new babel.OptionManager().init({ filename, ignore, only, configFile, rootMode });


  // If opts is not present, the file is ignored, either by explicit input into
  // babel-watch or by `.babelignore`.
  if (!opts) {
fork icon70
star icon532
watch icon8

How does @babel/core.OptionManager work?

@babel/core.OptionManager is a utility class that is used to manage the options passed to Babel during configuration and compilation. It provides methods for merging default and user-specified options, normalizing option values, and validating that the options are correctly formatted. When @babel/core is initialized, it creates a new OptionManager instance and uses it to process the options passed to Babel. The OptionManager is responsible for merging the default options with any user-specified options, validating that the options are correctly formatted, and normalizing the option values to ensure consistency. The OptionManager class has a number of methods for managing options, including: init(opts: Object): Initializes the OptionManager with the specified options. mergeOptions(opts: Object): Merges the specified options with the existing options. normaliseOptions(): Object: Normalizes the options, converting them to their expected data types. validateOptions(): void: Validates that the options are correctly formatted, throwing an error if any issues are detected. By default, the OptionManager uses the @babel/preset-env preset to handle a variety of configuration options. However, it also allows users to specify their own presets or plugins using the presets and plugins options, respectively. In summary, @babel/core.OptionManager is a crucial component of the Babel compiler that manages the options passed to Babel, ensures they are correctly formatted and normalized, and merges them with default options to produce the final configuration for the compiler.

Ai Example

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

const options = {
  presets: ["@babel/preset-env"],
  plugins: ["@babel/plugin-transform-arrow-functions"],
  sourceMaps: true,
  filename: "example.js",
  minified: true,
};

const optionManager = new OptionManager();
optionManager.init(options);
const normalizedOptions = optionManager.normaliseOptions();
optionManager.validateOptions();

console.log(normalizedOptions);
// Output: {
//   presets: [ [Function], { modules: false } ],
//   plugins: [ [Function] ],
//   sourceMaps: true,
//   filename: "example.js",
//   minified: true
// }

In this example, we create a new OptionManager instance and initialize it with an options object containing presets, plugins, and other configuration options for Babel. We then call the normaliseOptions method to normalize the options and ensure that they are in the expected data types. Finally, we call the validateOptions method to ensure that the options are correctly formatted, throwing an error if any issues are detected. The output shows the normalized options object, with the presets and plugins options transformed into arrays of functions that will be used during compilation.