Skip to content

Home > vue-metamorph > astHelpers > createNamedImport

astHelpers.createNamedImport() function ​

Adds a named import to a script AST. If an import declaration for the module already exists, this function merges the new specifier into that declaration. It doesn't create duplicate imports.

Signature:

typescript
export declare function createNamedImport(ast: namedTypes.Program, moduleSpecifier: string, importName: string, localName?: string): void;

Parameters ​

Parameter

Type

Description

ast

namedTypes.Program

The script AST.

moduleSpecifier

string

The module name to import from, such as 'vue'.

importName

string

The exported name of the import.

localName

string

(Optional) The local alias. Defaults to importName.

Returns:

void

Example ​

ts
// import { defineComponent } from 'vue';
createNamedImport(scriptAST, 'vue', 'defineComponent');

// import { map as lodashMap } from 'lodash-es';
createNamedImport(scriptAST, 'lodash-es', 'map', 'lodashMap');