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, the new specifier is merged into it. Duplicate imports are skipped.

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 (e.g. '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');