feat(plugin-vercel-analytics): add new vercel analytics plugin (#9687)

Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
ozaki 2024-02-14 13:25:39 +01:00 committed by GitHub
parent 70ba9d2d01
commit 77723a1121
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 394 additions and 2 deletions

View file

@ -0,0 +1,135 @@
/**
* 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 {normalizePluginOptions} from '@docusaurus/utils-validation';
import {validateOptions, type PluginOptions, type Options} from '../options';
import type {Validate} from '@docusaurus/types';
function testValidateOptions(options: Options) {
return validateOptions({
validate: normalizePluginOptions as Validate<Options, PluginOptions>,
options,
});
}
function validationResult(options: Options) {
return {
id: 'default',
...options,
};
}
describe('validateOptions', () => {
it('accepts for undefined options', () => {
// @ts-expect-error: TS should error
expect(testValidateOptions(undefined)).toEqual(validationResult(undefined));
});
it('throws for custom id', () => {
const config: Options = {id: 'custom', mode: 'auto', debug: false};
expect(() => testValidateOptions(config))
.toThrowErrorMatchingInlineSnapshot(`
"You site uses the Vercel Analytics plugin with a custom plugin id (custom).
But this plugin is only supposed to be used at most once per site. Therefore providing a custom plugin id is unsupported."
`);
});
it('accept for default id', () => {
const config: Options = {id: 'default', mode: 'auto', debug: false};
expect(testValidateOptions(config)).toEqual(validationResult(config));
});
it('throws for null options', () => {
// @ts-expect-error: TS should error
expect(() => testValidateOptions(null)).toThrowErrorMatchingInlineSnapshot(
`""value" must be of type object"`,
);
});
it('accept for empty object options', () => {
const config: Options = {};
expect(testValidateOptions(config)).toEqual(validationResult(config));
});
it('throws for number options', () => {
expect(
// @ts-expect-error: TS should error
() => testValidateOptions(42),
).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`);
});
it('throws for null mode', () => {
expect(
// @ts-expect-error: TS should error
() => testValidateOptions({mode: null}),
).toThrowErrorMatchingInlineSnapshot(
`""mode" must be one of [auto, production, development]"`,
);
});
it('throws for number mode', () => {
expect(
// @ts-expect-error: TS should error
() => testValidateOptions({mode: 42}),
).toThrowErrorMatchingInlineSnapshot(
`""mode" must be one of [auto, production, development]"`,
);
});
it('throws for empty mode', () => {
expect(() =>
// @ts-expect-error: TS should error
testValidateOptions({mode: ''}),
).toThrowErrorMatchingInlineSnapshot(
`""mode" must be one of [auto, production, development]"`,
);
});
it('accepts debug true', () => {
const config: Options = {
debug: true,
};
expect(testValidateOptions(config)).toEqual(validationResult(config));
});
it('accepts debug false', () => {
const config: Options = {
debug: false,
};
expect(testValidateOptions(config)).toEqual(validationResult(config));
});
it('accepts mode prod', () => {
const config: Options = {
mode: 'production',
debug: false,
};
expect(testValidateOptions(config)).toEqual(validationResult(config));
});
it('accepts mode dev', () => {
const config: Options = {
mode: 'development',
debug: false,
};
expect(testValidateOptions(config)).toEqual(validationResult(config));
});
it('accepts mode prod with debug', () => {
const config: Options = {
mode: 'production',
debug: true,
};
expect(testValidateOptions(config)).toEqual(validationResult(config));
});
it('accepts mode dev with debug', () => {
const config: Options = {
mode: 'development',
debug: true,
};
expect(testValidateOptions(config)).toEqual(validationResult(config));
});
});

View file

@ -0,0 +1,17 @@
/**
* 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 {inject} from '@vercel/analytics';
import globalData from '@generated/globalData';
import type {PluginOptions} from './options';
const {debug, mode} = globalData['docusaurus-plugin-vercel-analytics']
?.default as PluginOptions;
inject({
mode,
debug,
});

View file

@ -0,0 +1,32 @@
/**
* 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 type {LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions, Options} from './options';
export default function pluginVercelAnalytics(
context: LoadContext,
options: PluginOptions,
): Plugin {
const isProd = process.env.NODE_ENV === 'production';
return {
name: 'docusaurus-plugin-vercel-analytics',
getClientModules() {
return isProd ? ['./analytics'] : [];
},
contentLoaded({actions}) {
actions.setGlobalData(options);
},
};
}
export {validateOptions} from './options';
export type {PluginOptions, Options};

View 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);
}

View file

@ -0,0 +1,8 @@
/**
* 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.
*/
/// <reference types="@docusaurus/module-type-aliases" />