{"id":25925303,"url":"https://github.com/umamiappearance/importmanager","last_synced_at":"2025-10-25T16:12:53.281Z","repository":{"id":57818940,"uuid":"527899316","full_name":"UmamiAppearance/ImportManager","owner":"UmamiAppearance","description":"A class to analyze and manipulate JavaScript import statements from source code files.","archived":false,"fork":false,"pushed_at":"2023-07-05T19:41:00.000Z","size":366,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-11T04:48:07.783Z","etag":null,"topics":["cjs","code-manipulation","dynamic","es6","import","modules","rollup-plugin-import-manager"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/UmamiAppearance.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-08-23T08:28:24.000Z","updated_at":"2023-03-12T04:39:39.000Z","dependencies_parsed_at":"2023-12-26T08:45:31.153Z","dependency_job_id":null,"html_url":"https://github.com/UmamiAppearance/ImportManager","commit_stats":{"total_commits":57,"total_committers":4,"mean_commits":14.25,"dds":"0.10526315789473684","last_synced_commit":"2a333199b1fc18c75c2e3d2b3dd03e0dbb53e4b5"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UmamiAppearance%2FImportManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UmamiAppearance%2FImportManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UmamiAppearance%2FImportManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UmamiAppearance%2FImportManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UmamiAppearance","download_url":"https://codeload.github.com/UmamiAppearance/ImportManager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241723132,"owners_count":20009412,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cjs","code-manipulation","dynamic","es6","import","modules","rollup-plugin-import-manager"],"created_at":"2025-03-03T18:47:53.974Z","updated_at":"2025-10-25T16:12:48.230Z","avatar_url":"https://github.com/UmamiAppearance.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Import Manager\r\n\r\n[![License](https://img.shields.io/github/license/UmamiAppearance/ImportManager?color=009911\u0026style=for-the-badge)](./LICENSE)\r\n[![npm](https://img.shields.io/npm/v/import-manager?color=009911\u0026style=for-the-badge)](https://www.npmjs.com/package/import-manager)\r\n\r\nThis is the outsourced home of the underlying module for the rollup plugin: [rollup-plugin-import-manager](https://github.com/UmamiAppearance/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.\r\n\r\n\r\n## Install\r\nUsing npm:\r\n```console\r\nnpm install import-manager\r\n```\r\n\r\n## How it works\r\n**ImportManager** analyzes a given source code for import statements. Those are collected (in the [imports object](#imports-object)) as so called unit objects, on which the user can interact with. Also the creation of new units \u0026rarr; import statements is possible. \r\n\r\n\r\n## Usage\r\n\r\n### Importing\r\n```js\r\nimport ImportManager from \"import-manager\"\r\n```\r\n\r\n### Initializing\r\n```js\r\nconst manager = new ImportManager(\u003csourceCodeAsString\u003e, \u003cfilename\u003e)\r\n```\r\n#### `constructor(source, filename, warnSpamProtection=new Set(), warnings=true, pluginInstance=null)`\r\n* _source_ - The unmodified source code.\r\n* _filename_ (optional) - The path/name of the input file (used for hash generation only). \r\n* _warnSpamProtection_ (empty `Set()` by default) - A Set which contains all previously printed warning hashes.\r\n* _warnings_ (default `true`) - Pass false to suppress warning messages.\r\n* _pluginInstance_ (optional) - Rollup plugin instance if used as a plugin.\r\n\r\n### `imports` [object]\r\n`this.imports` contains all _units_ (if [`analyze`](#analyze) was called) which are objects for every import statement. It has three sub-objects for the import statement types:\r\n* cjs\r\n* dynamic\r\n* es6\r\n\r\n### Methods\r\n\r\n#### Global Methods\r\nMethods, callable from manager instance.\r\n\r\n##### `analyze()`\r\nAnalyzes the source and stores all import statements as unit objects in the [imports object](#imports-object).\r\n\r\n##### `selectModByName(name, type, allowNull, rawName=false)`\r\nSearches `this.imports` for the given module _name_ (`String`|`RegExp`). If _type_ is provided (`cjs`/`dynamic`/`es6`), it only searches for the module in that category. If _allowNull_ is set to `false` the module must be found or a [`MatchError`](#matcherror) is thrown.  \r\nBy setting `rawName` to true the name is searched in the whole raw module-name string. For the statement `import foo from \"/path/to/bar.js\"` the module name will be `bar.js` while the raw name is `\"/path/to/bar.js\"`. It is therefore possible to search for the whole path if it is necessary.\r\n\r\n##### `selectModById(id, allowNull)`\r\nSearches `this.imports` for the given module _id_. If _allowNull_ `false` the module must be found or a [`MatchError`](#matcherror) is thrown.\r\n\r\n##### `selectModByHash(hash, allowNull)`\r\nSearches `this.imports` for the given module _hash_. If _allowNull_ `false` the module must be found or a [`MatchError`](#matcherror) is thrown.\r\n\r\n##### `makeCJSStatement(module, declarator, varname)`\r\nGenerates a CJS Import Statement String from the _module_, _declarator_ (`const`/`let`/`var`/`global`) and the _varname_.  \r\n`\u003cdeclarator\u003e \u003cvarname\u003e = require(\u003cmodule\u003e)`\r\n\r\n##### `makeDynamicStatement(module, declarator, varname)`\r\nGenerates a Dynamic Import Statement String including with a `await` call from the _module_, _declarator_ (`const`/`let`/`var`/`global`) and the _varname_.  \r\n`\u003cdeclarator\u003e \u003cvarname\u003e = await import(\u003cmodule\u003e)`\r\n\r\n##### `makeES6Statement(module, defaultMembers, members)`\r\nGenerates an ES6 Import Statement String from the _module_ and _defaultMember_ and/or _members_ if provided.  \r\n`import \u003cdefaultMembers\u003e, { \u003cmembers\u003e } from \u003cmodule\u003e`\r\n\r\n##### `insertStatement(statement, pos, type)`\r\nInserts an import _statement_ to the  \u003ci\u003epos\u003c/i\u003eition `top` of the file or the `bottom` which is after the last found import statement.\r\n\r\n##### `insertAtUnit(unit, mode, statement)`\r\nInserts an import _statement_ at a given _unit_-object. There are three different \u003ci\u003emode\u003c/i\u003es available:\r\n* `append` - _statement_ gets inserted after the given _unit_\r\n* `prepend` - _statement_ gets inserted before the given _unit_\r\n* `replace` - _statement_ replaces the given _unit_\r\n\r\n##### `logUnits()`\r\nDebugging method to stop the building process to list all import units with its `id`, `hash` and `module`.\r\n\r\n##### `logUnitObjects()`\r\nDebugging method to stop the building process to list the complete import object with all its units, which is much more verbose than [`logUnits`](#logunits).\r\n\r\n##### `remove(unit)`\r\nRemoves a unit from the code instance.\r\n\r\n##### `commitChanges(unit)`\r\nAll manipulation done via a [unit method](#unit-methods) is made on the code slice of the _unit_. This methods finally writes it to the main code instance.\r\n\r\n\r\n#### Unit Methods\r\nMethods callable from a unit object.\r\n\r\n##### `renameModule(name, modType)`\r\nChanges the _name_ -\u003e module (path). _modType_ can either 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.\r\n\r\nName can also be a function which passes the original raw module-name (including quotes if present) as a first parameter and must return a raw module name as a string. (eg. `rawName =\u003e rawName.replace(\"foo\", \"bar\")`)\r\n\r\n\r\n##### `addDefaultMembers(names)`\r\n_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]_\r\n\r\n##### `addMembers(names)`\r\n_names_ is an array of strings of members to add to the unit. _[es6 only]_\r\n\r\n##### `removeMember(memberType, name)`\r\nRemoves a singular `defaultMember`/`member` (distinguished by _memberType_) with the specified _name_. _[es6 only]_\r\n\r\n##### `removeMembers(membersType)`\r\nRemoves all `defaultMember(s)` or `member(s)` (distinguished by _memberType_). _[es6 only]_\r\n\r\n##### `renameMember(memberType, name, newName, keepAlias)`\r\nRenames 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]_\r\n\r\n##### `setAlias(memberType, name, set)`\r\nSets 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]_\r\n\r\n##### `makeUntraceable()`\r\nMethod to call after a unit was completely removed or replaced, to prevent matching it again afterwards.\r\n\r\n##### `log()`\r\nDebugging method to stop the building process and list the unit properties.\r\n\r\n##### `updateUnit()`\r\nIf 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 updated code.\r\n\r\n\r\n### Errors\r\n\r\n#### `MatchError`\r\nExtends the generic JavaScript `Error`. An error to inform, that it is not possible to select a specific unit.\r\n\r\n#### `DebuggingError`\r\nExtends the generic JavaScript `Error`. An error to deliberately abort the building process for retrieving information about the imports/units.\r\n\r\n\r\n## License\r\n\r\n[MIT](https://opensource.org/licenses/MIT)\r\n\r\nCopyright (c) 2022-2023, UmamiAppearance\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fumamiappearance%2Fimportmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fumamiappearance%2Fimportmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fumamiappearance%2Fimportmanager/lists"}