0.1.2 • Published 1 year ago
plugin-importer v0.1.2
Plugin Importer
Recursively imports a plugin tree in order of dependencies
Usage
Simple
import { resolvePlainPlugins } from 'plugin-importer';
const loadedPlugins = await resolvePlainPlugins([
'./test-dependency',
'module-dependency',
], {
meta: import.meta, // Ensures local paths are resolved in relation to this file
});Powerful
import { loadPlugins, resolvePluginsInOrder } from 'plugin-importer';
/**
* @param {unknown} module
* @param {import('plugin-importer').ProcessPluginContext} context
* @returns {SupersetOfPluginDefinition}
*/
function processPlugin (module, { normalizedPluginName, pluginDir }) {
// Whatever other stuff you want to do to resolve the SupersetOfPluginDefinition
}
const pluginLoader = loadPlugins(processPlugin, {
meta: import.meta, // Ensures local paths are resolved in relation to this file
});
// loadedPlugins will be an ordered array of SupersetOfPluginDefinition,in order of who depends on whom
const loadedPlugins = await resolvePluginsInOrder(
[
'./test-dependency',
'module-dependency',
],
pluginLoader
);Exports
Core exports
assertToBePluginDefinition(value)– throws ifvalueisn't a validPluginDefinition(and correctly narrows the type when used with TypeScript)isPluginDefinition(value)– returnstrueifvalueis a validPluginDefinition(and correctly narrows the type when used with TypeScript)loadPlugins(processPlugin, [LoadPluginsOptions])– creates the plugin loader responsible for loading a valid pluginresolvePluginsInOrder(plugins, pluginLoader, [allowOptionalDependencies])– resolves and loads plugins and returns them with the plugin depended upon first and the plugins depending on them last
Plain plugins exports
loadPlainPlugins([LoadPluginsOptions])– likeloadPlugins, but geared to load purePluginDefinitionrather than supersetsprocessPlainPlugin– theprocessPluginthat's used inloadPlainPlugins, should never be needed to be called diretclyresolvePlainPlugins(dependencies, [LoadPluginsOptions])– shortcut for callingresolvePluginsInOrderwithloadPlainPlugins
Utils exports
getExtensionlessBasename(value)– likepath.basename(value)but removes file extensionsimportAbsolutePath(absolutePath)– likeimport(absolutePath)but made to easily work with absolute paths on Windows
Types
LoadPluginsOptions– the optional options forloadPlugins. Contains:cwd– the working directory to load relative plugin paths frommeta– convenience option for settingcwdby giving animport.metaprefix– a prefix that will be added to dependency names. Egexample-prefixwould be added tofooto makeexample-prefix-fooand to@voxpellito make@voxpelli/example-prefix, but eg.example-prefix-foowould not be prefixed as it already has the prefix and@voxpelli/foowould neither get prefixed. This is along the lines of whateslintdoes witheslint-configprefixes
PluginDefinition– the basic definition of a plugin. All loaded plugins are expected to conform to this or a superset of this.ProcessPluginContext– the context given toprocessPlugin