Babel syntax plugin that enables parsing of Unicode string names in import and export statements
npx @tessl/cli install tessl/npm-babel--plugin-syntax-module-string-names@7.12.0@babel/plugin-syntax-module-string-names is a Babel syntax plugin that enables parsing of Unicode string names in import and export statements. It allows JavaScript and TypeScript modules to use string literals (including Unicode characters) as import/export identifiers, enabling support for internationalized module names and special characters.
npm install --save-dev @babel/plugin-syntax-module-string-namesimport pluginSyntaxModuleStringNames from "@babel/plugin-syntax-module-string-names";For CommonJS:
const pluginSyntaxModuleStringNames = require("@babel/plugin-syntax-module-string-names");Add the plugin to your Babel configuration to enable module string names syntax:
{
"plugins": ["@babel/plugin-syntax-module-string-names"]
}When using Babel parser directly, include the moduleStringNames plugin:
import { parse } from "@babel/parser";
const code = `import { 'unicode string' as identifier } from 'module';`;
const ast = parse(code, {
sourceType: "module",
plugins: ["moduleStringNames"]
});Once enabled, the plugin allows parsing of the following syntax patterns:
// Named imports with string names
import { 'unicode-string' as identifier } from 'module';
import { 'unicode string with spaces' as identifier } from 'module';
import { '學而時習之,不亦說乎?' as quotation } from 'Confucius';
// Named exports with string names
export { identifier as 'unicode-string' };
export { identifier as 'unicode string with spaces' };
export { quotation as '學而時習之,不亦說乎?' };
// Export from with string names
export { 'string-name' as identifier } from 'module';
export { identifier as 'string-name' } from 'module';
export { 'source-string' as 'target-string' } from 'module';The plugin follows Babel's standard syntax plugin architecture:
@babel/helper-plugin-utils for standard plugin structureThe main plugin function that enables module string names syntax parsing.
/**
* Babel syntax plugin that enables parsing of Unicode string names in import/export statements
* @returns {BabelPlugin} Babel plugin object with name and manipulateOptions method
*/
export default function pluginSyntaxModuleStringNames(): BabelPlugin;
interface BabelPlugin {
name: string;
manipulateOptions(opts: any, parserOpts: ParserOptions): void;
}
interface ParserOptions {
plugins: string[];
}The plugin is created using the declare helper from @babel/helper-plugin-utils:
/**
* Plugin implementation function passed to declare()
* @param {BabelAPI} api - Babel API object with version assertion
* @returns {BabelPlugin} Plugin configuration object
*/
function pluginImplementation(api: BabelAPI): BabelPlugin;
interface BabelAPI {
assertVersion(version: number): void;
}Plugin Name: The internal identifier for the plugin.
/**
* Internal plugin name identifier
* @type {string}
* @constant
*/
name: "syntax-module-string-names"Manipulate Options: Method that modifies parser options to enable the syntax.
/**
* Modifies parser options to add moduleStringNames plugin
* @param {any} opts - Babel transform options object (unused)
* @param {ParserOptions} parserOpts - Parser options object to modify
* @returns {void}
*/
manipulateOptions(opts: any, parserOpts: ParserOptions): void;interface BabelPlugin {
/** Internal plugin name identifier */
name: string;
/** Function to modify parser options and enable syntax */
manipulateOptions(opts: any, parserOpts: ParserOptions): void;
}interface ParserOptions {
/** Array of parser plugin names to enable */
plugins: string[];
}interface BabelAPI {
/** Assert that Babel version meets minimum requirements */
assertVersion(version: number): void;
}The syntax plugin is commonly used alongside transformation plugins:
{
"plugins": [
"@babel/plugin-syntax-module-string-names",
"@babel/plugin-transform-modules-commonjs"
]
}Direct parser usage for AST generation:
import { parse } from "@babel/parser";
const ast = parse(sourceCode, {
sourceType: "module",
plugins: ["moduleStringNames", "typescript", "jsx"]
});Typical usage in a build pipeline:
{
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/plugin-syntax-module-string-names"
]
}This plugin integrates with:
The plugin enables parsing of these specific syntax constructs:
import { 'string-name' as identifier } from 'module';import { regularName, 'string-name' as alias } from 'module';export { identifier as 'string-name' };export { 'string-name' as identifier } from 'module';export { regularName, identifier as 'string-name' };as clause for aliasingThe plugin operates at the parsing stage, so errors are typically parse errors:
BABEL_VERSION_UNSUPPORTED errorThe plugin itself does not throw runtime errors during transformation, as it only modifies parser configuration.
The plugin has minimal dependencies:
declare utility functionNo runtime dependencies are added to the final bundle.