From aace0bc3595a437badcf78a787f5d672fb99c068 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Tue, 2 Jan 2024 11:30:13 +0100 Subject: [PATCH 01/23] wip: vercel analytics --- .../.npmignore | 3 ++ .../README.md | 9 ++++ .../package.json | 34 ++++++++++++++ .../src/index.ts | 45 +++++++++++++++++++ .../src/options.ts | 12 +++++ .../src/types.d.ts | 8 ++++ .../tsconfig.client.json | 16 +++++++ .../tsconfig.json | 13 ++++++ .../docusaurus-preset-classic/package.json | 1 + .../docusaurus-preset-classic/src/index.ts | 4 ++ .../docusaurus-preset-classic/src/options.ts | 2 + website/docusaurus.config.ts | 3 ++ yarn.lock | 12 +++++ 13 files changed, 162 insertions(+) create mode 100644 packages/docusaurus-plugin-vercel-analytics/.npmignore create mode 100644 packages/docusaurus-plugin-vercel-analytics/README.md create mode 100644 packages/docusaurus-plugin-vercel-analytics/package.json create mode 100644 packages/docusaurus-plugin-vercel-analytics/src/index.ts create mode 100644 packages/docusaurus-plugin-vercel-analytics/src/options.ts create mode 100644 packages/docusaurus-plugin-vercel-analytics/src/types.d.ts create mode 100644 packages/docusaurus-plugin-vercel-analytics/tsconfig.client.json create mode 100644 packages/docusaurus-plugin-vercel-analytics/tsconfig.json diff --git a/packages/docusaurus-plugin-vercel-analytics/.npmignore b/packages/docusaurus-plugin-vercel-analytics/.npmignore new file mode 100644 index 000000000000..03c9ae1e1b54 --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/.npmignore @@ -0,0 +1,3 @@ +.tsbuildinfo* +tsconfig* +__tests__ diff --git a/packages/docusaurus-plugin-vercel-analytics/README.md b/packages/docusaurus-plugin-vercel-analytics/README.md new file mode 100644 index 000000000000..cd307c496475 --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/README.md @@ -0,0 +1,9 @@ +# `@docusaurus/plugin-vercel-analytics` + +Vercel analytics plugin for Docusaurus. + +## Usage + +TODO: add documentation + +See [plugin-vercel-analytics documentation](). diff --git a/packages/docusaurus-plugin-vercel-analytics/package.json b/packages/docusaurus-plugin-vercel-analytics/package.json new file mode 100644 index 000000000000..47ce927b8d46 --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/package.json @@ -0,0 +1,34 @@ +{ + "name": "@docusaurus/plugin-vercel-analytics", + "version": "3.0.0", + "description": "Global vercel analytics plugin for Docusaurus.", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "publishConfig": { + "access": "public" + }, + "scripts": { + "build": "tsc --build", + "watch": "tsc --build --watch" + }, + "repository": { + "type": "git", + "url": "https://github.com/facebook/docusaurus.git", + "directory": "packages/docusaurus-plugin-vercel-analytics" + }, + "license": "MIT", + "dependencies": { + "@docusaurus/core": "3.0.0", + "@docusaurus/types": "3.0.0", + "@docusaurus/utils-validation": "3.0.0", + "@vercel/analytics": "^1.1.1", + "tslib": "^2.6.0" + }, + "peerDependencies": { + "react": "^18.0.0", + "react-dom": "^18.0.0" + }, + "engines": { + "node": ">=18.0" + } +} diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts new file mode 100644 index 000000000000..ad45e836e325 --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -0,0 +1,45 @@ +/** + * 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 {Joi} from '@docusaurus/utils-validation'; +import type {Plugin, OptionValidationContext} from '@docusaurus/types'; +import type {PluginOptions, Options} from './options'; + +export default function pluginVercelAnalytics(): Plugin { + const isProd = process.env.NODE_ENV === 'production'; + + return { + name: 'docusaurus-plugin-vercel-analytics', + + injectHtmlTags() { + if (!isProd) { + return {}; + } + return { + headTags: [ + { + tagName: 'script', + innerHTML: ``, + }, + ], + }; + }, + }; +} + +const pluginOptionsSchema = Joi.object({ + enabled: Joi.boolean().required(), +}); + +export function validateOptions({ + validate, + options, +}: OptionValidationContext): PluginOptions { + return validate(pluginOptionsSchema, options); +} + +export type {PluginOptions, Options}; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts new file mode 100644 index 000000000000..120999b029cf --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -0,0 +1,12 @@ +/** + * 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. + */ + +export type PluginOptions = { + enabled: boolean; +}; + +export type Options = Partial; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/types.d.ts b/packages/docusaurus-plugin-vercel-analytics/src/types.d.ts new file mode 100644 index 000000000000..6f6f99f12793 --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/src/types.d.ts @@ -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. + */ + +/// diff --git a/packages/docusaurus-plugin-vercel-analytics/tsconfig.client.json b/packages/docusaurus-plugin-vercel-analytics/tsconfig.client.json new file mode 100644 index 000000000000..7cf373d92f92 --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/tsconfig.client.json @@ -0,0 +1,16 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "noEmit": false, + "composite": true, + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo-client", + "moduleResolution": "bundler", + "module": "esnext", + "target": "esnext", + "rootDir": "src", + "outDir": "lib" + }, + "include": ["src/analytics.ts", "src/*.d.ts"], + "exclude": ["**/__tests__/**"] +} diff --git a/packages/docusaurus-plugin-vercel-analytics/tsconfig.json b/packages/docusaurus-plugin-vercel-analytics/tsconfig.json new file mode 100644 index 000000000000..c7fda37effc4 --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.json", + "references": [{"path": "./tsconfig.client.json"}], + "compilerOptions": { + "noEmit": false, + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo", + "rootDir": "src", + "outDir": "lib" + }, + "include": ["src"], + "exclude": ["src/analytics.ts", "**/__tests__/**"] +} diff --git a/packages/docusaurus-preset-classic/package.json b/packages/docusaurus-preset-classic/package.json index f665e6d97024..7bfee0ccb318 100644 --- a/packages/docusaurus-preset-classic/package.json +++ b/packages/docusaurus-preset-classic/package.json @@ -26,6 +26,7 @@ "@docusaurus/plugin-google-analytics": "3.0.0", "@docusaurus/plugin-google-gtag": "3.0.0", "@docusaurus/plugin-google-tag-manager": "3.0.0", + "@docusaurus/plugin-vercel-analytics": "3.0.0", "@docusaurus/plugin-sitemap": "3.0.0", "@docusaurus/theme-classic": "3.0.0", "@docusaurus/theme-common": "3.0.0", diff --git a/packages/docusaurus-preset-classic/src/index.ts b/packages/docusaurus-preset-classic/src/index.ts index ff4f70602ae0..a0e1ff660ee1 100644 --- a/packages/docusaurus-preset-classic/src/index.ts +++ b/packages/docusaurus-preset-classic/src/index.ts @@ -41,6 +41,7 @@ export default function preset( googleAnalytics, gtag, googleTagManager, + vercelAnalytics, ...rest } = opts; @@ -89,6 +90,9 @@ export default function preset( ), ); } + if (vercelAnalytics) { + plugins.push(makePluginConfig('@docusaurus/plugin-vercel-analytics')); + } if (isProd && sitemap !== false) { plugins.push(makePluginConfig('@docusaurus/plugin-sitemap', sitemap)); } diff --git a/packages/docusaurus-preset-classic/src/options.ts b/packages/docusaurus-preset-classic/src/options.ts index a8fdb68125f2..58b6b1306ff7 100644 --- a/packages/docusaurus-preset-classic/src/options.ts +++ b/packages/docusaurus-preset-classic/src/options.ts @@ -12,6 +12,7 @@ import type {Options as SitemapPluginOptions} from '@docusaurus/plugin-sitemap'; import type {Options as GAPluginOptions} from '@docusaurus/plugin-google-analytics'; import type {Options as GtagPluginOptions} from '@docusaurus/plugin-google-gtag'; import type {Options as GTMPluginOptions} from '@docusaurus/plugin-google-tag-manager'; +import type {Options as VercelPluginOptions} from '@docusaurus/plugin-vercel-analytics'; import type {Options as ThemeOptions} from '@docusaurus/theme-classic'; import type {ThemeConfig as BaseThemeConfig} from '@docusaurus/types'; import type {UserThemeConfig as ClassicThemeConfig} from '@docusaurus/theme-common'; @@ -44,6 +45,7 @@ export type Options = { */ gtag?: GtagPluginOptions; googleTagManager?: GTMPluginOptions; + vercelAnalytics?: VercelPluginOptions; }; export type ThemeConfig = BaseThemeConfig & diff --git a/website/docusaurus.config.ts b/website/docusaurus.config.ts index 3f88352dd6e5..d7fba05f6650 100644 --- a/website/docusaurus.config.ts +++ b/website/docusaurus.config.ts @@ -376,6 +376,9 @@ export default async function createConfigAsync() { [ 'classic', { + vercelAnalytics: { + enabled: true, + }, debug: true, // force debug plugin usage docs: { // routeBasePath: '/', diff --git a/yarn.lock b/yarn.lock index ab5360ca4a31..0537649d74fd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3863,6 +3863,13 @@ resolved "https://registry.yarnpkg.com/@ungap/structured-clone/-/structured-clone-1.2.0.tgz#756641adb587851b5ccb3e095daf27ae581c8406" integrity sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ== +"@vercel/analytics@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@vercel/analytics/-/analytics-1.1.1.tgz#2a712378a95014a548b4f9d2ae1ea0721433908d" + integrity sha512-+NqgNmSabg3IFfxYhrWCfB/H+RCUOCR5ExRudNG2+pcRehq628DJB5e1u1xqwpLtn4pAYii4D98w7kofORAGQA== + dependencies: + server-only "^0.0.1" + "@webassemblyjs/ast@1.11.6", "@webassemblyjs/ast@^1.11.5": version "1.11.6" resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.11.6.tgz#db046555d3c413f8966ca50a95176a0e2c642e24" @@ -14718,6 +14725,11 @@ serve-static@1.15.0: parseurl "~1.3.3" send "0.18.0" +server-only@^0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/server-only/-/server-only-0.0.1.tgz#0f366bb6afb618c37c9255a314535dc412cd1c9e" + integrity sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA== + set-blocking@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7" From e6d2bf8d3e989e48fdbb39cf8f6241c9e0f0ce0b Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 4 Jan 2024 09:12:53 +0100 Subject: [PATCH 02/23] wip --- .../src/analytics.ts | 12 +++++++ .../src/index.ts | 34 +++---------------- .../src/options.ts | 6 ---- .../docusaurus-preset-classic/src/index.ts | 4 --- .../docusaurus-preset-classic/src/options.ts | 2 -- website/docusaurus.config.ts | 3 -- 6 files changed, 17 insertions(+), 44 deletions(-) create mode 100644 packages/docusaurus-plugin-vercel-analytics/src/analytics.ts diff --git a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts new file mode 100644 index 000000000000..bc5078582137 --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts @@ -0,0 +1,12 @@ +/** + * 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'; + +inject({ + mode: 'production', + debug: true, +}); diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index ad45e836e325..589b9543a81e 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -5,41 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -import {Joi} from '@docusaurus/utils-validation'; -import type {Plugin, OptionValidationContext} from '@docusaurus/types'; -import type {PluginOptions, Options} from './options'; +import type {Plugin} from '@docusaurus/types'; export default function pluginVercelAnalytics(): Plugin { - const isProd = process.env.NODE_ENV === 'production'; + // const isProd = process.env.NODE_ENV === 'production'; return { name: 'docusaurus-plugin-vercel-analytics', - injectHtmlTags() { - if (!isProd) { - return {}; - } - return { - headTags: [ - { - tagName: 'script', - innerHTML: ``, - }, - ], - }; + getClientModules() { + // return isProd ? ['./analytics'] : []; + return ['./analytics']; }, }; } - -const pluginOptionsSchema = Joi.object({ - enabled: Joi.boolean().required(), -}); - -export function validateOptions({ - validate, - options, -}: OptionValidationContext): PluginOptions { - return validate(pluginOptionsSchema, options); -} - -export type {PluginOptions, Options}; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index 120999b029cf..b5c0e33b4a5b 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -4,9 +4,3 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ - -export type PluginOptions = { - enabled: boolean; -}; - -export type Options = Partial; diff --git a/packages/docusaurus-preset-classic/src/index.ts b/packages/docusaurus-preset-classic/src/index.ts index a0e1ff660ee1..ff4f70602ae0 100644 --- a/packages/docusaurus-preset-classic/src/index.ts +++ b/packages/docusaurus-preset-classic/src/index.ts @@ -41,7 +41,6 @@ export default function preset( googleAnalytics, gtag, googleTagManager, - vercelAnalytics, ...rest } = opts; @@ -90,9 +89,6 @@ export default function preset( ), ); } - if (vercelAnalytics) { - plugins.push(makePluginConfig('@docusaurus/plugin-vercel-analytics')); - } if (isProd && sitemap !== false) { plugins.push(makePluginConfig('@docusaurus/plugin-sitemap', sitemap)); } diff --git a/packages/docusaurus-preset-classic/src/options.ts b/packages/docusaurus-preset-classic/src/options.ts index 58b6b1306ff7..a8fdb68125f2 100644 --- a/packages/docusaurus-preset-classic/src/options.ts +++ b/packages/docusaurus-preset-classic/src/options.ts @@ -12,7 +12,6 @@ import type {Options as SitemapPluginOptions} from '@docusaurus/plugin-sitemap'; import type {Options as GAPluginOptions} from '@docusaurus/plugin-google-analytics'; import type {Options as GtagPluginOptions} from '@docusaurus/plugin-google-gtag'; import type {Options as GTMPluginOptions} from '@docusaurus/plugin-google-tag-manager'; -import type {Options as VercelPluginOptions} from '@docusaurus/plugin-vercel-analytics'; import type {Options as ThemeOptions} from '@docusaurus/theme-classic'; import type {ThemeConfig as BaseThemeConfig} from '@docusaurus/types'; import type {UserThemeConfig as ClassicThemeConfig} from '@docusaurus/theme-common'; @@ -45,7 +44,6 @@ export type Options = { */ gtag?: GtagPluginOptions; googleTagManager?: GTMPluginOptions; - vercelAnalytics?: VercelPluginOptions; }; export type ThemeConfig = BaseThemeConfig & diff --git a/website/docusaurus.config.ts b/website/docusaurus.config.ts index d7fba05f6650..3f88352dd6e5 100644 --- a/website/docusaurus.config.ts +++ b/website/docusaurus.config.ts @@ -376,9 +376,6 @@ export default async function createConfigAsync() { [ 'classic', { - vercelAnalytics: { - enabled: true, - }, debug: true, // force debug plugin usage docs: { // routeBasePath: '/', From 9dfea26c3f919ac50682fb65295c1c36bf0e3f49 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 4 Jan 2024 10:52:56 +0100 Subject: [PATCH 03/23] enable plugin without options --- packages/docusaurus-plugin-vercel-analytics/src/analytics.ts | 2 +- packages/docusaurus-plugin-vercel-analytics/src/index.ts | 5 ++--- packages/docusaurus-preset-classic/src/index.ts | 4 ++++ packages/docusaurus-preset-classic/src/options.ts | 4 ++++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts index bc5078582137..9e28346d73f6 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts @@ -8,5 +8,5 @@ import {inject} from '@vercel/analytics'; inject({ mode: 'production', - debug: true, + debug: false, }); diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 589b9543a81e..909b70af2380 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -8,14 +8,13 @@ import type {Plugin} from '@docusaurus/types'; export default function pluginVercelAnalytics(): Plugin { - // const isProd = process.env.NODE_ENV === 'production'; + const isProd = process.env.NODE_ENV === 'production'; return { name: 'docusaurus-plugin-vercel-analytics', getClientModules() { - // return isProd ? ['./analytics'] : []; - return ['./analytics']; + return isProd ? ['./analytics'] : []; }, }; } diff --git a/packages/docusaurus-preset-classic/src/index.ts b/packages/docusaurus-preset-classic/src/index.ts index ff4f70602ae0..8468daeab671 100644 --- a/packages/docusaurus-preset-classic/src/index.ts +++ b/packages/docusaurus-preset-classic/src/index.ts @@ -41,6 +41,7 @@ export default function preset( googleAnalytics, gtag, googleTagManager, + vercelAnalytics, ...rest } = opts; @@ -78,6 +79,9 @@ export default function preset( if (debug || (debug === undefined && !isProd)) { plugins.push(require.resolve('@docusaurus/plugin-debug')); } + if (vercelAnalytics) { + plugins.push(require.resolve('@docusaurus/plugin-vercel-analytics')); + } if (gtag) { plugins.push(makePluginConfig('@docusaurus/plugin-google-gtag', gtag)); } diff --git a/packages/docusaurus-preset-classic/src/options.ts b/packages/docusaurus-preset-classic/src/options.ts index a8fdb68125f2..fb0f7d5cd557 100644 --- a/packages/docusaurus-preset-classic/src/options.ts +++ b/packages/docusaurus-preset-classic/src/options.ts @@ -44,6 +44,10 @@ export type Options = { */ gtag?: GtagPluginOptions; googleTagManager?: GTMPluginOptions; + /** + * Options for `@docusaurus/plugin-vercel-analytics`. Use `false` to disable. + */ + vercelAnalytics?: boolean; }; export type ThemeConfig = BaseThemeConfig & From 76dd26cad669b6bdac4a36f6bbbe5ce35b77289a Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 4 Jan 2024 17:30:43 +0100 Subject: [PATCH 04/23] add vercel documentation --- packages/docusaurus-plugin-vercel-analytics/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/README.md b/packages/docusaurus-plugin-vercel-analytics/README.md index cd307c496475..64857993e09d 100644 --- a/packages/docusaurus-plugin-vercel-analytics/README.md +++ b/packages/docusaurus-plugin-vercel-analytics/README.md @@ -1,6 +1,6 @@ # `@docusaurus/plugin-vercel-analytics` -Vercel analytics plugin for Docusaurus. +Vercel analytics plugin for Docusaurus. [Official documentation](https://vercel.com/docs/analytics) ## Usage From 0164c908720ce3d7cdf4b24f53520f1545e2fc50 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 4 Jan 2024 17:30:50 +0100 Subject: [PATCH 05/23] remove from preset --- packages/docusaurus-preset-classic/src/index.ts | 4 ---- packages/docusaurus-preset-classic/src/options.ts | 4 ---- 2 files changed, 8 deletions(-) diff --git a/packages/docusaurus-preset-classic/src/index.ts b/packages/docusaurus-preset-classic/src/index.ts index 8468daeab671..ff4f70602ae0 100644 --- a/packages/docusaurus-preset-classic/src/index.ts +++ b/packages/docusaurus-preset-classic/src/index.ts @@ -41,7 +41,6 @@ export default function preset( googleAnalytics, gtag, googleTagManager, - vercelAnalytics, ...rest } = opts; @@ -79,9 +78,6 @@ export default function preset( if (debug || (debug === undefined && !isProd)) { plugins.push(require.resolve('@docusaurus/plugin-debug')); } - if (vercelAnalytics) { - plugins.push(require.resolve('@docusaurus/plugin-vercel-analytics')); - } if (gtag) { plugins.push(makePluginConfig('@docusaurus/plugin-google-gtag', gtag)); } diff --git a/packages/docusaurus-preset-classic/src/options.ts b/packages/docusaurus-preset-classic/src/options.ts index fb0f7d5cd557..a8fdb68125f2 100644 --- a/packages/docusaurus-preset-classic/src/options.ts +++ b/packages/docusaurus-preset-classic/src/options.ts @@ -44,10 +44,6 @@ export type Options = { */ gtag?: GtagPluginOptions; googleTagManager?: GTMPluginOptions; - /** - * Options for `@docusaurus/plugin-vercel-analytics`. Use `false` to disable. - */ - vercelAnalytics?: boolean; }; export type ThemeConfig = BaseThemeConfig & From 522f73e22d2a99f70716ccbb8505e30d7903656d Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 4 Jan 2024 18:05:10 +0100 Subject: [PATCH 06/23] wip --- .../package.json | 1 + .../src/analytics.ts | 9 +++++-- .../src/index.ts | 26 +++++++++++++++++-- .../src/plugin-vercel-analytics.d.ts | 21 +++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 packages/docusaurus-plugin-vercel-analytics/src/plugin-vercel-analytics.d.ts diff --git a/packages/docusaurus-plugin-vercel-analytics/package.json b/packages/docusaurus-plugin-vercel-analytics/package.json index 47ce927b8d46..b3b7c659dea3 100644 --- a/packages/docusaurus-plugin-vercel-analytics/package.json +++ b/packages/docusaurus-plugin-vercel-analytics/package.json @@ -22,6 +22,7 @@ "@docusaurus/types": "3.0.0", "@docusaurus/utils-validation": "3.0.0", "@vercel/analytics": "^1.1.1", + "webpack": "^5.88.1", "tslib": "^2.6.0" }, "peerDependencies": { diff --git a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts index 9e28346d73f6..1d89bb1b5004 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts @@ -6,7 +6,12 @@ */ import {inject} from '@vercel/analytics'; +/* eslint-disable prefer-destructuring */ +const VERCEL_ANALYTICS_DEBUG = process.env.VERCEL_ANALYTICS_DEBUG; +const VERCEL_ANALYTICS_MODE = process.env.VERCEL_ANALYTICS_MODE; + +// todo fix type error inject({ - mode: 'production', - debug: false, + mode: VERCEL_ANALYTICS_MODE ?? 'production', + debug: VERCEL_ANALYTICS_DEBUG ?? false, }); diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 909b70af2380..839c0b8d8b17 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -5,16 +5,38 @@ * LICENSE file in the root directory of this source tree. */ -import type {Plugin} from '@docusaurus/types'; +import webpack from 'webpack'; +import type {LoadContext, Plugin} from '@docusaurus/types'; +import type {PluginOptions} from '@docusaurus/plugin-vercel-analytics'; -export default function pluginVercelAnalytics(): Plugin { +export default function pluginVercelAnalytics( + context: LoadContext, + options: PluginOptions, +): Plugin { const isProd = process.env.NODE_ENV === 'production'; + const {debug, mode} = options; + return { name: 'docusaurus-plugin-vercel-analytics', getClientModules() { return isProd ? ['./analytics'] : []; }, + + configureWebpack() { + if (!isProd) { + return {}; + } + + return { + plugins: [ + new webpack.EnvironmentPlugin({ + VERCEL_ANALYTICS_DEBUG: debug, + VERCEL_ANALYTICS_MODE: mode, + }), + ], + }; + }, }; } diff --git a/packages/docusaurus-plugin-vercel-analytics/src/plugin-vercel-analytics.d.ts b/packages/docusaurus-plugin-vercel-analytics/src/plugin-vercel-analytics.d.ts new file mode 100644 index 000000000000..c44afecb2e3a --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/src/plugin-vercel-analytics.d.ts @@ -0,0 +1,21 @@ +/** + * 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. + */ + +declare module '@docusaurus/plugin-vercel-analytics' { + export type PluginOptions = { + /** + * Turn debug mode on + */ + debug?: boolean; + /** + * TODO add description + */ + mode: 'auto' | 'development' | 'production'; + }; + + export type Options = Partial; +} From fcabc25bc107ce9a8573bd9a837015d72c33d802 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 4 Jan 2024 18:05:53 +0100 Subject: [PATCH 07/23] remove from preset --- packages/docusaurus-preset-classic/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/docusaurus-preset-classic/package.json b/packages/docusaurus-preset-classic/package.json index 7bfee0ccb318..f665e6d97024 100644 --- a/packages/docusaurus-preset-classic/package.json +++ b/packages/docusaurus-preset-classic/package.json @@ -26,7 +26,6 @@ "@docusaurus/plugin-google-analytics": "3.0.0", "@docusaurus/plugin-google-gtag": "3.0.0", "@docusaurus/plugin-google-tag-manager": "3.0.0", - "@docusaurus/plugin-vercel-analytics": "3.0.0", "@docusaurus/plugin-sitemap": "3.0.0", "@docusaurus/theme-classic": "3.0.0", "@docusaurus/theme-common": "3.0.0", From 16242ffa9b7b3092301c19e23e6d86871550b2a5 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 16:29:22 +0100 Subject: [PATCH 08/23] feat: plugin options in config --- .../src/analytics.ts | 58 +++++++++++++++++-- .../src/index.ts | 18 +----- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts index 1d89bb1b5004..860c506fb0b4 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts @@ -5,13 +5,59 @@ * LICENSE file in the root directory of this source tree. */ import {inject} from '@vercel/analytics'; +import globalData from '@generated/globalData'; +import type {PluginOptions} from '@docusaurus/plugin-vercel-analytics'; +// import {Joi} from '@docusaurus/utils-validation'; +// import type { +// OptionValidationContext, +// ThemeConfig, +// ThemeConfigValidationContext, +// } from '@docusaurus/types'; -/* eslint-disable prefer-destructuring */ -const VERCEL_ANALYTICS_DEBUG = process.env.VERCEL_ANALYTICS_DEBUG; -const VERCEL_ANALYTICS_MODE = process.env.VERCEL_ANALYTICS_MODE; +const {debug, mode} = globalData['docusaurus-plugin-vercel-analytics'] + ?.default as PluginOptions; -// todo fix type error inject({ - mode: VERCEL_ANALYTICS_MODE ?? 'production', - debug: VERCEL_ANALYTICS_DEBUG ?? false, + mode, + debug, }); + +// export type Options = { +// trackingID: string | [string, ...string[]]; +// anonymizeIP?: boolean; +// }; + +// export const DEFAULT_OPTIONS: Partial = { +// anonymizeIP: false, +// }; + +// const pluginOptionsSchema = Joi.object({ +// // We normalize trackingID as a string[] +// trackingID: Joi.alternatives() +// .try( +// Joi.alternatives().conditional(Joi.string().required(), { +// then: Joi.custom((val: boolean) => [val]), +// }), +// Joi.array().items(Joi.string().required()), +// ) +// .required(), +// anonymizeIP: Joi.boolean().default(DEFAULT_OPTIONS.anonymizeIP), +// }); + +// export function validateOptions({ +// validate, +// options, +// }: OptionValidationContext): PluginOptions { +// return validate(pluginOptionsSchema, options); +// } + +// export function validateThemeConfig({ +// themeConfig, +// }: ThemeConfigValidationContext): ThemeConfig { +// if ('gtag' in themeConfig) { +// throw new Error( +// 'The "gtag" field in themeConfig should now be specified as option for plugin-google-gtag. More information at https://github.com/facebook/docusaurus/pull/5832.', +// ); +// } +// return themeConfig; +// } diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 839c0b8d8b17..c495ce6ef64c 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import webpack from 'webpack'; import type {LoadContext, Plugin} from '@docusaurus/types'; import type {PluginOptions} from '@docusaurus/plugin-vercel-analytics'; @@ -15,8 +14,6 @@ export default function pluginVercelAnalytics( ): Plugin { const isProd = process.env.NODE_ENV === 'production'; - const {debug, mode} = options; - return { name: 'docusaurus-plugin-vercel-analytics', @@ -24,19 +21,8 @@ export default function pluginVercelAnalytics( return isProd ? ['./analytics'] : []; }, - configureWebpack() { - if (!isProd) { - return {}; - } - - return { - plugins: [ - new webpack.EnvironmentPlugin({ - VERCEL_ANALYTICS_DEBUG: debug, - VERCEL_ANALYTICS_MODE: mode, - }), - ], - }; + contentLoaded({actions}) { + actions.setGlobalData(options); }, }; } From 6dc660eab7f2f6aa9c5ef84ab32b415ba39dd920 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 17:05:17 +0100 Subject: [PATCH 09/23] feat: config validation --- .../package.json | 1 - .../src/analytics.ts | 46 ------------------- .../src/index.ts | 35 ++++++++++++-- .../src/options.ts | 7 +++ 4 files changed, 38 insertions(+), 51 deletions(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/package.json b/packages/docusaurus-plugin-vercel-analytics/package.json index b3b7c659dea3..47ce927b8d46 100644 --- a/packages/docusaurus-plugin-vercel-analytics/package.json +++ b/packages/docusaurus-plugin-vercel-analytics/package.json @@ -22,7 +22,6 @@ "@docusaurus/types": "3.0.0", "@docusaurus/utils-validation": "3.0.0", "@vercel/analytics": "^1.1.1", - "webpack": "^5.88.1", "tslib": "^2.6.0" }, "peerDependencies": { diff --git a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts index 860c506fb0b4..cdc320b86bc8 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts @@ -7,12 +7,6 @@ import {inject} from '@vercel/analytics'; import globalData from '@generated/globalData'; import type {PluginOptions} from '@docusaurus/plugin-vercel-analytics'; -// import {Joi} from '@docusaurus/utils-validation'; -// import type { -// OptionValidationContext, -// ThemeConfig, -// ThemeConfigValidationContext, -// } from '@docusaurus/types'; const {debug, mode} = globalData['docusaurus-plugin-vercel-analytics'] ?.default as PluginOptions; @@ -21,43 +15,3 @@ inject({ mode, debug, }); - -// export type Options = { -// trackingID: string | [string, ...string[]]; -// anonymizeIP?: boolean; -// }; - -// export const DEFAULT_OPTIONS: Partial = { -// anonymizeIP: false, -// }; - -// const pluginOptionsSchema = Joi.object({ -// // We normalize trackingID as a string[] -// trackingID: Joi.alternatives() -// .try( -// Joi.alternatives().conditional(Joi.string().required(), { -// then: Joi.custom((val: boolean) => [val]), -// }), -// Joi.array().items(Joi.string().required()), -// ) -// .required(), -// anonymizeIP: Joi.boolean().default(DEFAULT_OPTIONS.anonymizeIP), -// }); - -// export function validateOptions({ -// validate, -// options, -// }: OptionValidationContext): PluginOptions { -// return validate(pluginOptionsSchema, options); -// } - -// export function validateThemeConfig({ -// themeConfig, -// }: ThemeConfigValidationContext): ThemeConfig { -// if ('gtag' in themeConfig) { -// throw new Error( -// 'The "gtag" field in themeConfig should now be specified as option for plugin-google-gtag. More information at https://github.com/facebook/docusaurus/pull/5832.', -// ); -// } -// return themeConfig; -// } diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index c495ce6ef64c..872164b9ab47 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -5,20 +5,26 @@ * LICENSE file in the root directory of this source tree. */ -import type {LoadContext, Plugin} from '@docusaurus/types'; -import type {PluginOptions} from '@docusaurus/plugin-vercel-analytics'; +import {Joi} from '@docusaurus/utils-validation'; +import type { + LoadContext, + Plugin, + OptionValidationContext, +} 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'; + // const isProd = process.env.NODE_ENV === 'production'; return { name: 'docusaurus-plugin-vercel-analytics', getClientModules() { - return isProd ? ['./analytics'] : []; + // return isProd ? ['./analytics'] : []; + return ['./analytics']; }, contentLoaded({actions}) { @@ -26,3 +32,24 @@ export default function pluginVercelAnalytics( }, }; } + +export const DEFAULT_OPTIONS: Partial = { + mode: 'production', + debug: false, +}; + +const pluginOptionsSchema = Joi.object({ + mode: Joi.string() + .valid('auto', 'production', 'development') + .default(DEFAULT_OPTIONS.mode), + debug: Joi.boolean().default(DEFAULT_OPTIONS.debug), +}); + +export function validateOptions({ + validate, + options, +}: OptionValidationContext): PluginOptions { + return validate(pluginOptionsSchema, options); +} + +export type {PluginOptions, Options}; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index b5c0e33b4a5b..315fd5a287a8 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -4,3 +4,10 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + +export type PluginOptions = { + mode: 'auto' | 'production' | 'development'; + debug: boolean; +}; + +export type Options = Partial; From 772f1bf3beaaf6cc9c8c22a120588407ac26ba33 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 18:13:45 +0100 Subject: [PATCH 10/23] feat: multi instance check --- .../docusaurus-plugin-vercel-analytics/README.md | 2 +- .../docusaurus-plugin-vercel-analytics/package.json | 1 + .../docusaurus-plugin-vercel-analytics/src/index.ts | 12 +++++++++--- .../src/options.ts | 1 + 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/README.md b/packages/docusaurus-plugin-vercel-analytics/README.md index 64857993e09d..76c9027a2841 100644 --- a/packages/docusaurus-plugin-vercel-analytics/README.md +++ b/packages/docusaurus-plugin-vercel-analytics/README.md @@ -1,6 +1,6 @@ # `@docusaurus/plugin-vercel-analytics` -Vercel analytics plugin for Docusaurus. [Official documentation](https://vercel.com/docs/analytics) +[Vercel analytics](https://vercel.com/docs/analytics) plugin for Docusaurus. ## Usage diff --git a/packages/docusaurus-plugin-vercel-analytics/package.json b/packages/docusaurus-plugin-vercel-analytics/package.json index 47ce927b8d46..271be8d36325 100644 --- a/packages/docusaurus-plugin-vercel-analytics/package.json +++ b/packages/docusaurus-plugin-vercel-analytics/package.json @@ -21,6 +21,7 @@ "@docusaurus/core": "3.0.0", "@docusaurus/types": "3.0.0", "@docusaurus/utils-validation": "3.0.0", + "@docusaurus/utils": "3.0.0", "@vercel/analytics": "^1.1.1", "tslib": "^2.6.0" }, diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 872164b9ab47..c2bb2eb0a776 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -6,6 +6,7 @@ */ import {Joi} from '@docusaurus/utils-validation'; +import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import type { LoadContext, Plugin, @@ -17,17 +18,22 @@ export default function pluginVercelAnalytics( context: LoadContext, options: PluginOptions, ): Plugin { - // const isProd = process.env.NODE_ENV === 'production'; + const isProd = process.env.NODE_ENV === 'production'; return { name: 'docusaurus-plugin-vercel-analytics', getClientModules() { - // return isProd ? ['./analytics'] : []; - return ['./analytics']; + return isProd ? ['./analytics'] : []; }, contentLoaded({actions}) { + console.log('options:', options); + if (options.id !== DEFAULT_PLUGIN_ID) { + throw new Error( + `You cannot use a custom plugin id option with the Vercel Analytics plugin`, + ); + } actions.setGlobalData(options); }, }; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index 315fd5a287a8..24d29573c761 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -6,6 +6,7 @@ */ export type PluginOptions = { + id: string; mode: 'auto' | 'production' | 'development'; debug: boolean; }; From 9461ad7b1cf4d009bbf01443f0b5618e71529041 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 18:15:43 +0100 Subject: [PATCH 11/23] remove log --- packages/docusaurus-plugin-vercel-analytics/src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index c2bb2eb0a776..160285d2a68c 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -28,7 +28,6 @@ export default function pluginVercelAnalytics( }, contentLoaded({actions}) { - console.log('options:', options); if (options.id !== DEFAULT_PLUGIN_ID) { throw new Error( `You cannot use a custom plugin id option with the Vercel Analytics plugin`, From 9d761ee3c5cf48f4d6fa8c3ce598b6bcf7a2f37d Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 18:29:42 +0100 Subject: [PATCH 12/23] review --- .../docusaurus-plugin-vercel-analytics/src/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 160285d2a68c..16f0a5b8c150 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -18,6 +18,12 @@ export default function pluginVercelAnalytics( context: LoadContext, options: PluginOptions, ): Plugin { + if (options.id !== DEFAULT_PLUGIN_ID) { + throw new Error( + `You cannot use a custom plugin id option with the Vercel Analytics plugin`, + ); + } + const isProd = process.env.NODE_ENV === 'production'; return { @@ -28,11 +34,6 @@ export default function pluginVercelAnalytics( }, contentLoaded({actions}) { - if (options.id !== DEFAULT_PLUGIN_ID) { - throw new Error( - `You cannot use a custom plugin id option with the Vercel Analytics plugin`, - ); - } actions.setGlobalData(options); }, }; From 38478831f5df3180bdd7ecf14f3debf0c572b395 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 18:35:56 +0100 Subject: [PATCH 13/23] review --- .../src/index.ts | 26 +------------------ .../src/options.ts | 21 +++++++++++++++ 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 16f0a5b8c150..51b42e200187 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -5,13 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -import {Joi} from '@docusaurus/utils-validation'; import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; -import type { - LoadContext, - Plugin, - OptionValidationContext, -} from '@docusaurus/types'; +import type {LoadContext, Plugin} from '@docusaurus/types'; import type {PluginOptions, Options} from './options'; export default function pluginVercelAnalytics( @@ -39,23 +34,4 @@ export default function pluginVercelAnalytics( }; } -export const DEFAULT_OPTIONS: Partial = { - mode: 'production', - debug: false, -}; - -const pluginOptionsSchema = Joi.object({ - mode: Joi.string() - .valid('auto', 'production', 'development') - .default(DEFAULT_OPTIONS.mode), - debug: Joi.boolean().default(DEFAULT_OPTIONS.debug), -}); - -export function validateOptions({ - validate, - options, -}: OptionValidationContext): PluginOptions { - return validate(pluginOptionsSchema, options); -} - export type {PluginOptions, Options}; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index 24d29573c761..e74558615c01 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -4,6 +4,8 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ +import {Joi} from '@docusaurus/utils-validation'; +import type {OptionValidationContext} from '@docusaurus/types'; export type PluginOptions = { id: string; @@ -12,3 +14,22 @@ export type PluginOptions = { }; export type Options = Partial; + +export const DEFAULT_OPTIONS: Partial = { + mode: 'production', + debug: false, +}; + +const pluginOptionsSchema = Joi.object({ + mode: Joi.string() + .valid('auto', 'production', 'development') + .default(DEFAULT_OPTIONS.mode), + debug: Joi.boolean().default(DEFAULT_OPTIONS.debug), +}); + +export function validateOptions({ + validate, + options, +}: OptionValidationContext): PluginOptions { + return validate(pluginOptionsSchema, options); +} From 67576cbea2f6cd43c4cca31c72c0a87e828e6d18 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 18:37:40 +0100 Subject: [PATCH 14/23] export validate options --- packages/docusaurus-plugin-vercel-analytics/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 51b42e200187..50040afdf82b 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -34,4 +34,6 @@ export default function pluginVercelAnalytics( }; } +export {validateOptions} from './options'; + export type {PluginOptions, Options}; From 097f2b4798e567954e2ff9732a9cf62a14dbde00 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 19:18:27 +0100 Subject: [PATCH 15/23] adds doc --- .../README.md | 4 +- .../api/plugins/plugin-vercel-analytics.mdx | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 website/docs/api/plugins/plugin-vercel-analytics.mdx diff --git a/packages/docusaurus-plugin-vercel-analytics/README.md b/packages/docusaurus-plugin-vercel-analytics/README.md index 76c9027a2841..0234cb5fdba5 100644 --- a/packages/docusaurus-plugin-vercel-analytics/README.md +++ b/packages/docusaurus-plugin-vercel-analytics/README.md @@ -4,6 +4,4 @@ ## Usage -TODO: add documentation - -See [plugin-vercel-analytics documentation](). +See [plugin-vercel-analytics documentation](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-vercel-analytics). diff --git a/website/docs/api/plugins/plugin-vercel-analytics.mdx b/website/docs/api/plugins/plugin-vercel-analytics.mdx new file mode 100644 index 000000000000..0a3a356c1bdf --- /dev/null +++ b/website/docs/api/plugins/plugin-vercel-analytics.mdx @@ -0,0 +1,52 @@ +--- +sidebar_position: 11 +slug: /api/plugins/@docusaurus/plugin-vercel-analytics +--- + +# 📦 plugin-vercel-analytics + +import APITable from '@site/src/components/APITable'; + +[Vercel Analytics](https://vercel.com/docs/analytics) provides comprehensive insights into your website's visitors, tracking top pages, referrers, and demographics like location, operating systems, and browser info. + +:::warning production only + +This plugin is always inactive in development and **only active in production** to avoid polluting the analytics statistics. + +::: + +## Installation {#installation} + +```bash npm2yarn +npm install --save @docusaurus/plugin-vercel-analytics +``` + +## Configuration {#configuration} + +Accepted fields: + +```mdx-code-block + +``` + +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| `mode` | string | 'production' | Sets the environment mode ('auto' or 'development' or 'production') | +| `debug` | `boolean` | `false` | Enable debug logging | + +```mdx-code-block + +``` + +### Example configuration {#ex-config} + +You can configure this plugin through plugin options. + +```js config-tabs +// Plugin Options: @docusaurus/plugin-vercel-analytics + +const config = { + mode: 'development', + debug: true, +}; +``` From b243a961cdc3f719eb06be395078f1d3af9d0b01 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 5 Jan 2024 21:12:49 +0100 Subject: [PATCH 16/23] fix doc --- .../api/plugins/plugin-vercel-analytics.mdx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/website/docs/api/plugins/plugin-vercel-analytics.mdx b/website/docs/api/plugins/plugin-vercel-analytics.mdx index 0a3a356c1bdf..ed166bacbbff 100644 --- a/website/docs/api/plugins/plugin-vercel-analytics.mdx +++ b/website/docs/api/plugins/plugin-vercel-analytics.mdx @@ -42,11 +42,16 @@ Accepted fields: You can configure this plugin through plugin options. -```js config-tabs -// Plugin Options: @docusaurus/plugin-vercel-analytics - -const config = { - mode: 'development', - debug: true, +```js title="docusaurus.config.js" +export default { + plugins: [ + [ + 'vercel-analytics', + { + debug: true, + mode: 'auto', + }, + ], + ], }; ``` From 3c1d5b14d83ba2cdee8595da5d6f7e06219c5ccc Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Mon, 12 Feb 2024 19:07:05 +0100 Subject: [PATCH 17/23] refactor: review --- .../src/analytics.ts | 2 +- .../src/options.ts | 6 +++--- .../src/plugin-vercel-analytics.d.ts | 21 ------------------- .../api/plugins/plugin-vercel-analytics.mdx | 6 +++--- 4 files changed, 7 insertions(+), 28 deletions(-) delete mode 100644 packages/docusaurus-plugin-vercel-analytics/src/plugin-vercel-analytics.d.ts diff --git a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts index cdc320b86bc8..0af51ce9269a 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/analytics.ts @@ -6,7 +6,7 @@ */ import {inject} from '@vercel/analytics'; import globalData from '@generated/globalData'; -import type {PluginOptions} from '@docusaurus/plugin-vercel-analytics'; +import type {PluginOptions} from './options'; const {debug, mode} = globalData['docusaurus-plugin-vercel-analytics'] ?.default as PluginOptions; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index e74558615c01..d6ad1fc7ad0d 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -10,14 +10,14 @@ import type {OptionValidationContext} from '@docusaurus/types'; export type PluginOptions = { id: string; mode: 'auto' | 'production' | 'development'; - debug: boolean; + debug: boolean | undefined; }; export type Options = Partial; export const DEFAULT_OPTIONS: Partial = { - mode: 'production', - debug: false, + mode: 'auto', + debug: undefined, }; const pluginOptionsSchema = Joi.object({ diff --git a/packages/docusaurus-plugin-vercel-analytics/src/plugin-vercel-analytics.d.ts b/packages/docusaurus-plugin-vercel-analytics/src/plugin-vercel-analytics.d.ts deleted file mode 100644 index c44afecb2e3a..000000000000 --- a/packages/docusaurus-plugin-vercel-analytics/src/plugin-vercel-analytics.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * 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. - */ - -declare module '@docusaurus/plugin-vercel-analytics' { - export type PluginOptions = { - /** - * Turn debug mode on - */ - debug?: boolean; - /** - * TODO add description - */ - mode: 'auto' | 'development' | 'production'; - }; - - export type Options = Partial; -} diff --git a/website/docs/api/plugins/plugin-vercel-analytics.mdx b/website/docs/api/plugins/plugin-vercel-analytics.mdx index ed166bacbbff..98dd747ae838 100644 --- a/website/docs/api/plugins/plugin-vercel-analytics.mdx +++ b/website/docs/api/plugins/plugin-vercel-analytics.mdx @@ -30,9 +30,9 @@ Accepted fields: ``` | Name | Type | Default | Description | -| --- | --- | --- | --- | -| `mode` | string | 'production' | Sets the environment mode ('auto' or 'development' or 'production') | -| `debug` | `boolean` | `false` | Enable debug logging | +| --- | --- | --- | --- | --- | +| `mode` | string | 'auto' | Sets the environment mode ('auto' or 'development' or 'production') see [vercel doc](https://vercel.com/docs/analytics/package#mode) | +| `debug` | `boolean | undefined` | `undefined` | Enable debug logging see [vercel doc](https://vercel.com/docs/analytics/package#debug) | ```mdx-code-block From 9730346568e4c0f30b5af5cda7bce2593ee775a9 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Mon, 12 Feb 2024 20:20:07 +0100 Subject: [PATCH 18/23] feat: tests --- .../src/__tests__/options.test.ts | 133 ++++++++++++++++++ .../src/options.ts | 2 +- .../tsconfig.client.json | 2 +- 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts diff --git a/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts b/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts new file mode 100644 index 000000000000..55d67357acfe --- /dev/null +++ b/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts @@ -0,0 +1,133 @@ +/** + * 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, + DEFAULT_OPTIONS, +} from '../options'; +import type {Validate} from '@docusaurus/types'; + +function testValidateOptions(options: Options) { + return validateOptions({ + validate: normalizePluginOptions as Validate, + options, + }); +} + +function validationResult(options: Options) { + return { + id: 'default', + ...DEFAULT_OPTIONS, + ...options, + }; +} + +// const MinimalConfig: Options = { +// trackingID: 'G-XYZ12345', +// }; + +describe('validateOptions', () => { + it('throws for undefined options', () => { + expect(() => testValidateOptions(undefined)).toEqual( + validationResult(undefined), + ); + }); + + it('throws for custom id', () => { + const config: Options = {id: 'custom', mode: 'auto', debug: false}; + expect(() => testValidateOptions(config)).toEqual(validationResult(config)); + }); + + 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', () => { + expect( + // @ts-expect-error: TS should error + () => testValidateOptions(null), + ).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); + }); + + it('throw for empty object options', () => { + expect(() => testValidateOptions({})).toThrowErrorMatchingInlineSnapshot( + `""value" must be of type object"`, + ); + }); + + 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 minimal config', () => { + // expect(testValidateOptions(MinimalConfig)).toEqual( + // validationResult(MinimalConfig), + // ); + // }); + + 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)); + }); +}); diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index d6ad1fc7ad0d..e8d63d1887b5 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -24,7 +24,7 @@ const pluginOptionsSchema = Joi.object({ mode: Joi.string() .valid('auto', 'production', 'development') .default(DEFAULT_OPTIONS.mode), - debug: Joi.boolean().default(DEFAULT_OPTIONS.debug), + debug: Joi.boolean().default(false).optional(), }); export function validateOptions({ diff --git a/packages/docusaurus-plugin-vercel-analytics/tsconfig.client.json b/packages/docusaurus-plugin-vercel-analytics/tsconfig.client.json index 7cf373d92f92..e77a5fd904ff 100644 --- a/packages/docusaurus-plugin-vercel-analytics/tsconfig.client.json +++ b/packages/docusaurus-plugin-vercel-analytics/tsconfig.client.json @@ -11,6 +11,6 @@ "rootDir": "src", "outDir": "lib" }, - "include": ["src/analytics.ts", "src/*.d.ts"], + "include": ["src/analytics.ts", "src/options.ts", "src/*.d.ts"], "exclude": ["**/__tests__/**"] } From db11c27199293bbb818d083d8e3e10b75e529439 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Tue, 13 Feb 2024 15:20:13 +0100 Subject: [PATCH 19/23] wip: tests --- .../src/__tests__/options.test.ts | 69 ++++++++++--------- .../src/options.ts | 14 ++-- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts b/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts index 55d67357acfe..14dd087b7404 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts @@ -6,12 +6,7 @@ */ import {normalizePluginOptions} from '@docusaurus/utils-validation'; -import { - validateOptions, - type PluginOptions, - type Options, - DEFAULT_OPTIONS, -} from '../options'; +import {validateOptions, type PluginOptions, type Options} from '../options'; import type {Validate} from '@docusaurus/types'; function testValidateOptions(options: Options) { @@ -24,45 +19,41 @@ function testValidateOptions(options: Options) { function validationResult(options: Options) { return { id: 'default', - ...DEFAULT_OPTIONS, ...options, }; } -// const MinimalConfig: Options = { -// trackingID: 'G-XYZ12345', -// }; - describe('validateOptions', () => { - it('throws for undefined options', () => { - expect(() => testValidateOptions(undefined)).toEqual( - validationResult(undefined), - ); + 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)).toEqual(validationResult(config)); - }); + // TODO: fix this test + // it('throws for custom id', () => { + // const config: Options = {id: 'custom', mode: 'auto', debug: false}; + // expect(() => + // testValidateOptions(config), + // ).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); + // }); it('accept for default id', () => { const config: Options = {id: 'default', mode: 'auto', debug: false}; - expect(() => testValidateOptions(config)).toEqual(validationResult(config)); + expect(testValidateOptions(config)).toEqual(validationResult(config)); }); it('throws for null options', () => { - expect( - // @ts-expect-error: TS should error - () => testValidateOptions(null), - ).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); - }); - - it('throw for empty object options', () => { - expect(() => testValidateOptions({})).toThrowErrorMatchingInlineSnapshot( + // @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 @@ -95,12 +86,6 @@ describe('validateOptions', () => { ); }); - // it('accepts minimal config', () => { - // expect(testValidateOptions(MinimalConfig)).toEqual( - // validationResult(MinimalConfig), - // ); - // }); - it('accepts debug true', () => { const config: Options = { debug: true, @@ -130,4 +115,20 @@ describe('validateOptions', () => { }; 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)); + }); }); diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index e8d63d1887b5..cb2878321bf7 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -9,22 +9,16 @@ import type {OptionValidationContext} from '@docusaurus/types'; export type PluginOptions = { id: string; - mode: 'auto' | 'production' | 'development'; + mode: 'auto' | 'production' | 'development' | undefined; debug: boolean | undefined; }; export type Options = Partial; -export const DEFAULT_OPTIONS: Partial = { - mode: 'auto', - debug: undefined, -}; - const pluginOptionsSchema = Joi.object({ - mode: Joi.string() - .valid('auto', 'production', 'development') - .default(DEFAULT_OPTIONS.mode), - debug: Joi.boolean().default(false).optional(), + id: Joi.string().valid('default').default('default'), + mode: Joi.string().valid('auto', 'production', 'development').optional(), + debug: Joi.boolean().optional(), }); export function validateOptions({ From 065f686ac274d3841d2e11778abce1370cc54d7c Mon Sep 17 00:00:00 2001 From: sebastien Date: Wed, 14 Feb 2024 11:59:57 +0100 Subject: [PATCH 20/23] fix docs table issues --- website/docs/api/plugins/plugin-vercel-analytics.mdx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/docs/api/plugins/plugin-vercel-analytics.mdx b/website/docs/api/plugins/plugin-vercel-analytics.mdx index 98dd747ae838..1285a776f61d 100644 --- a/website/docs/api/plugins/plugin-vercel-analytics.mdx +++ b/website/docs/api/plugins/plugin-vercel-analytics.mdx @@ -11,7 +11,7 @@ import APITable from '@site/src/components/APITable'; :::warning production only -This plugin is always inactive in development and **only active in production** to avoid polluting the analytics statistics. +This plugin is always inactive in development and **only active in production** (`docusaurus build`) to avoid polluting the analytics statistics. ::: @@ -30,9 +30,9 @@ Accepted fields: ``` | Name | Type | Default | Description | -| --- | --- | --- | --- | --- | -| `mode` | string | 'auto' | Sets the environment mode ('auto' or 'development' or 'production') see [vercel doc](https://vercel.com/docs/analytics/package#mode) | -| `debug` | `boolean | undefined` | `undefined` | Enable debug logging see [vercel doc](https://vercel.com/docs/analytics/package#debug) | +| --- | --- | --- | --- | +| `mode` | `string` | `'auto'` | Override the automatic environment detection. Read the [official docs](https://vercel.com/docs/analytics/package#mode) for details. | +| `debug` | `boolean` | `undefined` | Enable browser console logging of analytics events. SRead the [official docs](https://vercel.com/docs/analytics/package#debug) for details. | ```mdx-code-block From b5a2efb0ebdf847304372458848be60df27b7d84 Mon Sep 17 00:00:00 2001 From: sebastien Date: Wed, 14 Feb 2024 12:00:18 +0100 Subject: [PATCH 21/23] use DEFAULT_PLUGIN_ID constant --- packages/docusaurus-plugin-vercel-analytics/src/options.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index cb2878321bf7..253aca21dbac 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -4,6 +4,7 @@ * 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 {Joi} from '@docusaurus/utils-validation'; import type {OptionValidationContext} from '@docusaurus/types'; @@ -16,7 +17,7 @@ export type PluginOptions = { export type Options = Partial; const pluginOptionsSchema = Joi.object({ - id: Joi.string().valid('default').default('default'), + id: Joi.string().valid(DEFAULT_PLUGIN_ID).default(DEFAULT_PLUGIN_ID), mode: Joi.string().valid('auto', 'production', 'development').optional(), debug: Joi.boolean().optional(), }); From f85345bc0a7e2d9126f3a7082f9533e41eae02e6 Mon Sep 17 00:00:00 2001 From: sebastien Date: Wed, 14 Feb 2024 12:23:59 +0100 Subject: [PATCH 22/23] Improve APITable error message in case of invalid Markdown table --- website/src/components/APITable/index.tsx | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/website/src/components/APITable/index.tsx b/website/src/components/APITable/index.tsx index 049e2d4bcfd4..0772f163badd 100644 --- a/website/src/components/APITable/index.tsx +++ b/website/src/components/APITable/index.tsx @@ -22,11 +22,20 @@ interface Props { } // ReactNode equivalent of HTMLElement#innerText -function getText(node: ReactElement): string { +function getRowName(node: ReactElement): string { let curNode: ReactNode = node; while (isValidElement(curNode)) { [curNode] = React.Children.toArray(curNode.props.children); } + if (typeof curNode !== 'string') { + throw new Error( + `Could not extract APITable row name from JSX tree:\n${JSON.stringify( + node, + null, + 2, + )}`, + ); + } return curNode as string; } @@ -37,7 +46,7 @@ function APITableRow( }: {name: string | undefined; children: ReactElement>}, ref: React.ForwardedRef, ) { - const entryName = getText(children); + const entryName = getRowName(children); const id = name ? `${name}-${entryName}` : entryName; const anchor = `#${id}`; const history = useHistory(); @@ -71,6 +80,11 @@ const APITableRowComp = React.forwardRef(APITableRow); * should be generally correct in the MDX context. */ export default function APITable({children, name}: Props): JSX.Element { + if (children.type !== 'table') { + throw new Error( + 'Bad usage of APITable component.\nIt is probably that your Markdown table is malformed.\nMake sure to double-check you have the appropriate number of columns for each table row.', + ); + } const [thead, tbody] = React.Children.toArray(children.props.children) as [ ReactElement<{children: ReactElement[]}>, ReactElement<{children: ReactElement[]}>, From 42bb53b5f082b60922b742da0f38648511f05fae Mon Sep 17 00:00:00 2001 From: sebastien Date: Wed, 14 Feb 2024 12:49:09 +0100 Subject: [PATCH 23/23] Change way to reject usage of custom plugin id with Vercel Analytics plugin --- .../package.json | 1 + .../src/__tests__/options.test.ts | 15 ++++++++------- .../src/index.ts | 7 ------- .../src/options.ts | 14 +++++++++++++- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/packages/docusaurus-plugin-vercel-analytics/package.json b/packages/docusaurus-plugin-vercel-analytics/package.json index 271be8d36325..e4b8ef4f780b 100644 --- a/packages/docusaurus-plugin-vercel-analytics/package.json +++ b/packages/docusaurus-plugin-vercel-analytics/package.json @@ -19,6 +19,7 @@ "license": "MIT", "dependencies": { "@docusaurus/core": "3.0.0", + "@docusaurus/logger": "3.0.0", "@docusaurus/types": "3.0.0", "@docusaurus/utils-validation": "3.0.0", "@docusaurus/utils": "3.0.0", diff --git a/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts b/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts index 14dd087b7404..41af7e32d0b7 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts @@ -29,13 +29,14 @@ describe('validateOptions', () => { expect(testValidateOptions(undefined)).toEqual(validationResult(undefined)); }); - // TODO: fix this test - // it('throws for custom id', () => { - // const config: Options = {id: 'custom', mode: 'auto', debug: false}; - // expect(() => - // testValidateOptions(config), - // ).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); - // }); + 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}; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 50040afdf82b..c05d1cefe83a 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import type {LoadContext, Plugin} from '@docusaurus/types'; import type {PluginOptions, Options} from './options'; @@ -13,12 +12,6 @@ export default function pluginVercelAnalytics( context: LoadContext, options: PluginOptions, ): Plugin { - if (options.id !== DEFAULT_PLUGIN_ID) { - throw new Error( - `You cannot use a custom plugin id option with the Vercel Analytics plugin`, - ); - } - const isProd = process.env.NODE_ENV === 'production'; return { diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index 253aca21dbac..754866ab0c2d 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -5,6 +5,7 @@ * 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'; @@ -17,14 +18,25 @@ export type PluginOptions = { export type Options = Partial; const pluginOptionsSchema = Joi.object({ - id: Joi.string().valid(DEFAULT_PLUGIN_ID).default(DEFAULT_PLUGIN_ID), 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): PluginOptions { + ensureNoMultiInstance(options); return validate(pluginOptionsSchema, options); }