* Registers a plugin with Core.
(
Plugin: T,
// We want to let the plugin decide whether `opts` is optional or not
// so we spread the argument rather than defining `opts:` ourselves.
...args: OmitFirstArg<ConstructorParameters<T>>
)
| 1889 | * Registers a plugin with Core. |
| 1890 | */ |
| 1891 | use<T extends typeof BasePlugin<any, M, B>>( |
| 1892 | Plugin: T, |
| 1893 | // We want to let the plugin decide whether `opts` is optional or not |
| 1894 | // so we spread the argument rather than defining `opts:` ourselves. |
| 1895 | ...args: OmitFirstArg<ConstructorParameters<T>> |
| 1896 | ): this { |
| 1897 | if (typeof Plugin !== 'function') { |
| 1898 | const msg = |
| 1899 | `Expected a plugin class, but got ${ |
| 1900 | Plugin === null ? 'null' : typeof Plugin |
| 1901 | }.` + |
| 1902 | ' Please verify that the plugin was imported and spelled correctly.' |
| 1903 | throw new TypeError(msg) |
| 1904 | } |
| 1905 | |
| 1906 | // Instantiate |
| 1907 | const plugin = new Plugin(this, ...args) |
| 1908 | const pluginId = plugin.id |
| 1909 | |
| 1910 | if (!pluginId) { |
| 1911 | throw new Error('Your plugin must have an id') |
| 1912 | } |
| 1913 | |
| 1914 | if (!plugin.type) { |
| 1915 | throw new Error('Your plugin must have a type') |
| 1916 | } |
| 1917 | |
| 1918 | const existsPluginAlready = this.getPlugin(pluginId) |
| 1919 | if (existsPluginAlready) { |
| 1920 | const msg = |
| 1921 | `Already found a plugin named '${existsPluginAlready.id}'. ` + |
| 1922 | `Tried to use: '${pluginId}'.\n` + |
| 1923 | 'Uppy plugins must have unique `id` options.' |
| 1924 | throw new Error(msg) |
| 1925 | } |
| 1926 | |
| 1927 | // @ts-expect-error does exist |
| 1928 | if (Plugin.VERSION) { |
| 1929 | // @ts-expect-error does exist |
| 1930 | this.log(`Using ${pluginId} v${Plugin.VERSION}`) |
| 1931 | } |
| 1932 | |
| 1933 | if (plugin.type in this.#plugins) { |
| 1934 | this.#plugins[plugin.type].push(plugin) |
| 1935 | } else { |
| 1936 | this.#plugins[plugin.type] = [plugin] |
| 1937 | } |
| 1938 | plugin.install() |
| 1939 | |
| 1940 | this.emit('plugin-added', plugin) |
| 1941 | |
| 1942 | return this |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * Find one Plugin by name. |