@babel/plugin-syntax-top-level-await is a Babel syntax plugin that enables parsing of top-level await expressions in JavaScript modules. It provides minimal functionality focused purely on extending Babel's parser to recognize top-level await syntax, making it essential for modern JavaScript applications that use async operations at the module level.
npm install --save-dev @babel/plugin-syntax-top-level-await// Default import (CommonJS)
const syntaxTopLevelAwait = require("@babel/plugin-syntax-top-level-await");
// Default import (ES6)
import syntaxTopLevelAwait from "@babel/plugin-syntax-top-level-await";// babel.config.js
module.exports = {
plugins: ["@babel/plugin-syntax-top-level-await"]
};
// Or with custom options (though this plugin accepts no options)
module.exports = {
plugins: [
["@babel/plugin-syntax-top-level-await", {}]
]
};Once configured, your JavaScript modules can use top-level await:
// module.js - This syntax becomes parseable with the plugin
const data = await fetch('/api/data').then(r => r.json());
const config = await import('./config.js');
export { data, config };The main export is a Babel plugin factory function created using @babel/helper-plugin-utils.
/**
* Default export: Creates a Babel plugin that enables top-level await syntax parsing
* @param {Object} api - Babel API object with version assertion capabilities
* @param {Function} api.assertVersion - Method to assert minimum Babel version (called with version 7)
* @returns {BabelPlugin} Plugin object with name and manipulateOptions method
*/
export default function(api);
interface BabelPlugin {
/** Plugin identifier used by Babel */
name: string;
/**
* Babel plugin hook that modifies parser options to enable top-level await
* @param {Object} opts - Babel transformation options object
* @param {Object} parserOpts - Babel parser options object (modified in-place)
*/
manipulateOptions(opts, parserOpts): void;
}Usage Example:
import syntaxTopLevelAwait from "@babel/plugin-syntax-top-level-await";
// The plugin is typically used via Babel configuration
// but can be instantiated directly for advanced use cases
const babelApi = {
assertVersion: (version) => {
// Asserts minimum required Babel version (throws if < version)
if (parseInt(version) > 7) throw new Error("Babel version too old");
}
};
const plugin = syntaxTopLevelAwait(babelApi);
console.log(plugin.name); // "syntax-top-level-await"The plugin object returned by the factory function contains:
interface BabelPlugin {
/**
* Plugin identifier string constant
* Value: "syntax-top-level-await"
*/
name: "syntax-top-level-await";
/**
* Parser options manipulation method
* Adds "topLevelAwait" to the parser plugins array to enable syntax recognition
* @param {Object} opts - Babel transformation options (unused)
* @param {Object} parserOpts - Parser options object with plugins array
*/
manipulateOptions(opts: Object, parserOpts: { plugins: string[] }): void;
}This plugin follows the minimal Babel syntax plugin pattern:
@babel/helper-plugin-utils declare function for standardized plugin creationThe plugin's manipulateOptions method is called during Babel's initialization phase to configure the parser before any code is processed. This allows the parser to recognize top-level await expressions as valid syntax rather than throwing parsing errors.
The plugin uses the declare utility from @babel/helper-plugin-utils which provides the standardized Babel plugin factory pattern with automatic version checking via api.assertVersion(7).
// External dependency used internally
import { declare } from "@babel/helper-plugin-utils";Runtime Dependencies:
@babel/helper-plugin-utils: Provides the declare utility for creating Babel plugins with version checkingPeer Dependencies:
@babel/core: Babel compiler core (version ^7.0.0-0)This plugin is syntax-only and does not perform transformations, so it typically does not throw runtime errors. However, standard Babel plugin errors may occur:
The plugin itself does not validate await usage - it only enables the parser to recognize the syntax. Actual validation is handled by Babel's parser and other plugins in the transformation pipeline.