@genesiscommunitysuccess/analyzer-import-alias-plugin

中文文档
5.0.3发布于 2024/02/14MITTypeScriptES Module近 30 天下载 2万

Plugin for custom element manifest parser to handle import aliases

原始使用文档
文档目录3 节
  1. Usage
  2. Installation
  3. Configuration

The Genesis Global Community Success initiative is committed to open-sourcing select technologies that we believe the open-source community would benefit from.

NPM version License

Analyzer Import Alias Plugin

This plugin allows you to work around the limitations of import aliasing with the AST that the analyzer uses. Consider the following pattern which you might use if you are using a design system and system of components:

// Imported Button class is a custom element.
import { Button as LibButton } from 'my-library';

// Exported Button has extra styling applied to fit your design system.
export class Button extends LibButton { }

This allows you to apply your custom styling to the library button and export with the same name, and then the consumers of your library can swap the import to get your styling. However, this has a problem...

The AST parser that powers the analyzer will be looking for a class named LibButton in your library dependencies when working out the inheritance tree of your class Button, but because the class is actually called Button in the library, the inheritance tree is broken. This means in your manifest that any members that your Button class inherits from the my-library class (or higher in the inheritance chain) will be lost on the manifest.

This plugin will allow you to fix this issue.

Usage

Installation

Install this package as a dev dependency in the same project that you're using the custom elements manifest analyzer.

npm i @genesiscommunitysuccess/analyzer-import-alias-plugin --save-dev

You then need to apply this plugin in the plugins settings in your custom-elements-manifest.config.mjs, see here.

Configuration

Adding the plugin to the plugins array in the manifest config will not accomplish anything by itself, you must also configure the way the plugin handles the name aliases. You can add a configuration for each npm package such as the following.

import aliasPlugin from '@genesiscommunitysuccess/analyzer-import-alias-plugin';

export default {
    dependencies: true,
    plugins: [
        aliasPlugin({
            'my-library': {
                '*': (name) => name.replace('Lib','')
            }
        })
    ]
}

This will effectively treat any imported superclass from my-library as without the Lib prefix, so in the first example in the readme the local Button class will be correctly inheriting from the package Button class.

The shape of the catch-all function defined as * is (name: string) => string so you may perform more complicated logic than the above example to mutate the names. If you don't want to change a name you should return the input name.

There is also an option to set specific replacements for names such as the following:

aliasPlugin({
    'my-library': {
        '*': (name) => name.replace('Lib', ''),
        LibButton: 'MyButton',
        LibCheckbox: (name) => name.replace('Lib', 'My'),
    }
})

When using this above configuration, the LibButton superclass inheritance will be treated as MyButton. Important, any explicit definition will take precedence over the catch-all * function.

For completeness, you may define as many library imports as you wish.

aliasPlugin({
    'my-library': { 
        LibButton: 'MyButton'
    },
    'another-library': {
        '*': (name) => name.replace('Another', ''),
    }
})

版本与发布记录

@genesiscommunitysuccess/analyzer-import-alias-plugin 共发布 18 个版本,首次发布于 2023/10/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.3latest2024/02/142 年前
5.0.22023/12/202 年前
5.0.12023/12/202 年前
5.0.02023/12/132 年前
4.1.32023/12/122 年前
4.1.22023/12/122 年前

其他发布通道:prerelease → 2.1.1-prerelease.2。

关于 @genesiscommunitysuccess/analyzer-import-alias-plugin 的常见问题

@genesiscommunitysuccess/analyzer-import-alias-plugin 是什么?

Plugin for custom element manifest parser to handle import aliases

如何安装 @genesiscommunitysuccess/analyzer-import-alias-plugin?

在项目目录运行 npm install @genesiscommunitysuccess/analyzer-import-alias-plugin,或使用 pnpm add / yarn add / bun add。

@genesiscommunitysuccess/analyzer-import-alias-plugin 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@genesiscommunitysuccess/analyzer-import-alias-plugin 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@genesiscommunitysuccess/analyzer-import-alias-plugin 可以商用吗?

@genesiscommunitysuccess/analyzer-import-alias-plugin 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@genesiscommunitysuccess/analyzer-import-alias-plugin 的下载量有多少?

根据 npm 官方下载统计,@genesiscommunitysuccess/analyzer-import-alias-plugin 在 2026/10/16 – 2026/11/14 这 30 天内被下载 20,373 次,日均约 679 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@genesiscommunitysuccess/analyzer-import-alias-plugin 还在维护吗?

最新版本 5.0.3 发布于 2024/02/14(2 年前);至今共发布 18 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/10/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。