mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-23 22:17:00 +02:00
feat(plugin-vercel-analytics): add new vercel analytics plugin (#9687)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
parent
70ba9d2d01
commit
77723a1121
13 changed files with 394 additions and 2 deletions
42
packages/docusaurus-plugin-vercel-analytics/src/options.ts
Normal file
42
packages/docusaurus-plugin-vercel-analytics/src/options.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
|
||||
import logger from '@docusaurus/logger';
|
||||
import {Joi} from '@docusaurus/utils-validation';
|
||||
import type {OptionValidationContext} from '@docusaurus/types';
|
||||
|
||||
export type PluginOptions = {
|
||||
id: string;
|
||||
mode: 'auto' | 'production' | 'development' | undefined;
|
||||
debug: boolean | undefined;
|
||||
};
|
||||
|
||||
export type Options = Partial<PluginOptions>;
|
||||
|
||||
const pluginOptionsSchema = Joi.object<PluginOptions>({
|
||||
mode: Joi.string().valid('auto', 'production', 'development').optional(),
|
||||
debug: Joi.boolean().optional(),
|
||||
});
|
||||
|
||||
// We can't validate this through the schema
|
||||
// Docusaurus core auto registers the id field to the schema already
|
||||
function ensureNoMultiInstance(options: Options) {
|
||||
if (options?.id && options.id !== DEFAULT_PLUGIN_ID) {
|
||||
throw new Error(
|
||||
logger.interpolate`You site uses the Vercel Analytics plugin with a custom plugin id (name=${options.id}).
|
||||
But this plugin is only supposed to be used at most once per site. Therefore providing a custom plugin id is unsupported.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function validateOptions({
|
||||
validate,
|
||||
options,
|
||||
}: OptionValidationContext<Options, PluginOptions>): PluginOptions {
|
||||
ensureNoMultiInstance(options);
|
||||
return validate(pluginOptionsSchema, options);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue