@babel/plugin-syntax-import-attributes is a Babel syntax plugin that enables parsing of ES2022 import attributes (formerly import assertions) in JavaScript/TypeScript code. It allows the JavaScript parser to understand the 'with' syntax for specifying metadata about imported modules, such as import foo from './module.js' with { type: 'json' }.
npm install --save-dev @babel/plugin-syntax-import-attributes// ES Modules (recommended)
import plugin from "@babel/plugin-syntax-import-attributes";CommonJS (legacy support):
const plugin = require("@babel/plugin-syntax-import-attributes");Note: This plugin is typically not imported directly in user code. Instead, it's configured in Babel configuration files as shown in the Basic Usage section.
// babel.config.js
module.exports = {
plugins: [
"@babel/plugin-syntax-import-attributes"
]
};With options:
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-syntax-import-attributes", {
deprecatedAssertSyntax: true
}]
]
};Once the plugin is configured, you can use import attributes in your code:
// Modern syntax (default)
import config from './config.json' with { type: 'json' };
import styles from './styles.css' with { type: 'css' };
// With deprecatedAssertSyntax: true, also supports legacy syntax
import legacyConfig from './legacy.json' assert { type: 'json' };The plugin operates by:
importAssertions plugin if presentMain plugin function that creates a Babel plugin object with import attributes support.
/**
* Default export: Babel plugin function for parsing import attributes
* Created via declare() from @babel/helper-plugin-utils
* @param api - Babel plugin API object
* @param options - Plugin configuration options (destructured as { deprecatedAssertSyntax })
* @param dirname - Plugin directory path
* @returns Babel plugin object with name and manipulateOptions method
*/
export default function(
api: PluginAPI,
options: Options,
dirname: string
): PluginObject;
interface Options {
/** Enable support for deprecated 'assert' syntax alongside 'with' syntax */
deprecatedAssertSyntax?: boolean;
}
interface PluginObject {
/** Plugin identifier used by Babel */
name: "syntax-import-attributes";
/** Method called to configure parser and generator options */
manipulateOptions(opts: { parserOpts: any; generatorOpts: any }): void;
}Identifier constant for the Babel plugin.
const name: "syntax-import-attributes";Configuration interface for plugin behavior.
interface Options {
/**
* Enable support for deprecated 'assert' syntax alongside 'with' syntax
* When true, allows parsing both:
* - import foo from './file.js' with { type: 'json' }
* - import foo from './file.js' assert { type: 'json' }
* @default false
*/
deprecatedAssertSyntax?: boolean;
}The plugin configures Babel's parser and generator through the manipulateOptions method:
importAttributesKeyword to "with" for output generation"importAttributes""deprecatedImportAssert" and ["importAttributes", { deprecatedAssertSyntax: true }]"importAssertions" plugin if presentThe plugin validates configuration and provides clear error messages:
/**
* Thrown when deprecatedAssertSyntax option is provided but not a boolean
*/
class ConfigurationError extends Error {
message: "'deprecatedAssertSyntax' must be a boolean, if specified.";
}The plugin requires Babel version ^7.22.0 or higher, enforced internally via api.assertVersion() call.
interface PluginAPI {
/** Babel version string */
version: string;
/** Assert minimum Babel version requirement */
assertVersion(range: string): void;
}
interface ParserOptions {
/** Array of parser plugins to enable */
plugins: Array<string | [string, any]>;
}
interface GeneratorOptions {
/** Keyword to use for import attributes in generated code */
importAttributesKeyword?: string;
}