Skip to content

chore: plugin developer can reference to plugin name & module path in code #371

@louis-jan

Description

@louis-jan

Problem

  • Plugin developers often face the inconvenience of having to redefine the plugin name in their code and keeping it in sync across multiple files.
// index.ts
const PLUGIN_NAME = "@janhq/inference-plugin";
const MODULE_PATH = `${PluginName}/dist/module.js`;

function onStart() {
    return core.invokePluginFunc(MODULE_PATH, onStart.name, {})
}

function init(register: Function) {
  register(PluginService.OnStart, PLUGIN_NAME, onStart);
}

This redundancy exists even though the plugin name is already defined in the package.json.

Solution

To streamline this process, we can employ Webpack's DefinePlugin to read the package.json and automatically pass the required variables.

Developers can now simply define the plugin name and module entry in their package.json file

// package.json
{
  "name": "@janhq/monitoring-plugin",
  "main": "index.js",
  "module": "module.js",
}

With the provided Webpack configuration (already predefined in the plugin template), the plugin code becomes much cleaner:

plugins: [
    new webpack.DefinePlugin({
      PLUGIN_NAME: JSON.stringify(packageJson.name),
      MODULE_PATH: JSON.stringify(`${packageJson.name}/${packageJson.module}`),
    }),
  ],

This solution not only minimizes code redundancy but also makes it more user-friendly and maintainable.

register(PluginService.OnStart, PLUGIN_NAME, onStart);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions