question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v1] options for plugins

See original GitHub issue

Summary

How are users supposed to pass options for plugins?

Description

For now, we have only one plugin @algolia/autocomplete-plugin-recent-searches. User might want to customize it by overriding the part of source returned by the plugin, or the templates. We do not have an API at the moment.

There are two approaches.

1. Let plugins handle them

We can have a contract that plugins must accept a prop “source” and it will override the part of sources being returned by the plugin.

For example,

import { createRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';

const recentSearches = createRecentSearchesPlugin({
  key: 'recent',
  source: {
    getSuggestionUrl: ...,
    templates: {
      header: ...
    }
  }
});

The burden of merging them are delegated to the plugins, meaning that all the plugins will have the similar code.

2. Let the core handle them

When users add the plugin, they can specify the options.

const recentSearches = createRecentSearchesPlugin({ key: 'recent' });

autocomplete({
  ...
  plugins: [{ plugin: recentSearches, source: { ... } } ]

It’s similar to what Gatsby does:

module.exports = {
  plugins: ['gatsby-plugin-mdx'],
}

// or

module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        defaultLayouts: {
          posts: require.resolve("./src/components/posts-layout.js"),
          default: require.resolve("./src/components/default-page-layout.js"),
        },
      },
    },
  ],
}

User can either pass a plugin itself or an object containing the plugin and the source option. And the core will deal with merging the source.

I’m more in favor the second way. What do you think?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
francoischalifourcommented, Oct 22, 2020

I think that we should remove this concern as a whole by moving this responsibility to plugin authors. If they need to support options, they would export a factory:

const plugin = createPlugin({ ...options, source: {} });

Perhaps the problem you had in mind was rather how to leverage the Autocomplete lifecycle in plugins? That’s a harder one because we need to spec that.

Example of spec with a getSources option

// Notice `sources` here which are the original sources from the plugin.
function pluginGetSources({ query, sources }) {
  if (!query) {
    return [];
  }

  return sources.map(transformOriginalSource);
}

const plugin = createPlugin({ ...options, getSources: pluginGetSources });

If that’s what you meant, solutions like Babel accept an array with the first item being the plugin, and the second being the options:

autocomplete({
  // ...
  plugins: [recentSearches, [customPlugin, { getSources: customPluginGetSources }]]
});
0reactions
francoischalifourcommented, Mar 25, 2021

Closing this since we went for (1) letting plugins handle options themselves.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plugins - webpack
The plugins option is used to customize the webpack build process in a variety of ways. Webpack comes with a variety built-in plugins...
Read more >
containerd/PLUGINS.md at main - GitHub
containerd's Go client gives a user access to many points of extensions from creating their own options on container creation to resolving image...
Read more >
Plugin Config Version 1 of Plugin V2 - Docker Documentation
This document outlines the format of the V0 plugin configuration. The plugin config described herein was introduced in the Docker daemon in the...
Read more >
Plugins 101 — Credo v1.4.0-rc.2 - HexDocs
Configuring plugins. Plugins can be configured via params, just like checks. Each entry consists of a two-element tuple: the plugin's module and a...
Read more >
Plugins - Argo CD - Declarative GitOps CD for Kubernetes
Option 1: Configure plugins via Argo CD configmap¶ · Make sure required binaries are available in argocd-repo-server pod. The binaries can be added...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found