This is the outsourced home of the underlying module for the rollup plugin: rollup-plugin-import-manager. To have the ability to analyze and modify JavaScript source code for import statements (cjs/es6/dynamic) without having to deal with rollup or rollup dependencies, the source code for the ImportManager class itself has its own repository. It can be downloaded and used independently from the rollup plugin and or a building process.
Using npm:
npm install import-manager
ImportManager analyzes a given source code for import statements. Those are collected (in the imports object) as so called unit objects, on which the user can interact with. Also the creation of new units → import statements is possible.
import ImportManager from "import-manager"
const manager = new ImportManager(<sourceCodeAsString>, <filename>)
- source - The unmodified source code.
- filename (optional) - The path/name of the input file (used for hash generation only).
- warnSpamProtection (empty
Set()
by default) - A Set which contains all previously printed warning hashes. - warnings (default
true
) - Pass false to suppress warning messages. - pluginInstance (optional) - Rollup plugin instance if used as a plugin.
this.imports
contains all units (if analyze
was called) which are objects for every import statement. It has three sub-objects for the import statement types:
- cjs
- dynamic
- es6
Methods, callable from manager instance.
Analyzes the source and stores all import statements as unit objects in the imports object.
Searches this.imports
for the given module name. If type is provided (cjs
/dynamic
/es6
), it only searches for the module in that category. If allowNull false
the module must be found or a MatchError
is thrown.
Searches this.imports
for the given module id. If allowNull false
the module must be found or a MatchError
is thrown.
Searches this.imports
for the given module hash. If allowNull false
the module must be found or a MatchError
is thrown.
Generates a CJS Import Statement String from the module, declarator (const
/let
/var
/global
) and the varname.
<declarator> <varname> = require(<module>)
Generates a Dynamic Import Statement String including with a await
call from the module, declarator (const
/let
/var
/global
) and the varname.
<declarator> <varname> = await import(<module>)
Generates an ES6 Import Statement String from the module and defaultMember and/or members if provided.
import <defaultMembers>, { <members> } from <module>
Inserts an import statement to the position top
of the file or the bottom
which is after the last found import statement.
Inserts an import statement at a given unit-object. There are three different modes available:
append
- statement gets inserted after the given unitprepend
- statement gets inserted before the given unitreplace
- statement replaces the given unit
Debugging method to stop the building process to list all import units with its id
, hash
and module
.
Debugging method to stop the building process to list the complete import object with all its units, which is much more verbose than logUnits
.
Removes a unit from the code instance.
All manipulation done via a unit method is made on the code slice of the unit. This methods finally writes it to the main code instance.
Methods callable from a unit object.
Changes the name -> module (path). modType can be "string"
which adds quotation marks around name or "raw"
, which doesn't and can be used to pass variables if valid for the import type.
names is an array of strings (even for the most common case of a single member) of default members to add to the unit. [es6 only]
names is an array of strings of members to add to the unit. [es6 only]
Removes a singular defaultMember
if memberType is set to it or a member
with the specified name. [es6 only]
Removes the group of defaultMember(s)
if memberType is set to it or all member(s)
. [es6 only]
Renames a singular member of memberType defaultMember
/member
matching the given name to a newName. It is possible to keepAlias if it should not be changed. [es6 only]
Sets a alias of memberType defaultMember
/member
of the given member name. You can either set (pass a string) a new name or don't define set to delete the alias. [es6 only]
Method to call after a unit was completely removed or replaced, to prevent matching it again afterwards.
Debugging method to stop the building process and list the unit properties.
If multiple changes should be performed on a es6
unit, this method should be called after a change. If called the unit gets generated again with the updates code.
Extends the generic JavaScript Error
. An error to inform, that it is not possible to select a specific unit.
Extends the generic JavaScript Error
. An error to deliberately abort the building process for retrieving information about the imports/units.
Copyright (c) 2022-2023, UmamiAppearance