From 19b27ef73bec9e0a1df2ff6d5d167fe7cfbd5f5b Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 22 Sep 2021 17:36:04 +0800 Subject: [PATCH] refactor(plugin-google-gtag, plugin-google-analytics): migrate packages to TS (#5561) * migration * Move to devDeps * Use type assertion --- .../package.json | 10 ++++++++- .../src/{analytics.js => analytics.ts} | 2 +- .../src/{index.js => index.ts} | 17 ++++++++------- .../src/plugin-google-analytics.d.ts | 13 ++++++++++++ .../src/types.d.ts | 12 +++++++++++ .../tsconfig.json | 9 ++++++++ .../package.json | 10 ++++++++- .../src/{gtag.js => gtag.ts} | 11 +++++----- .../src/{index.js => index.ts} | 17 ++++++++------- .../src/plugin-google-gtag.d.ts | 13 ++++++++++++ .../src/types.d.ts | 21 +++++++++++++++++++ .../tsconfig.json | 9 ++++++++ .../src/preset-classic.d.ts | 14 ++++++------- 13 files changed, 128 insertions(+), 30 deletions(-) rename packages/docusaurus-plugin-google-analytics/src/{analytics.js => analytics.ts} (92%) rename packages/docusaurus-plugin-google-analytics/src/{index.js => index.ts} (82%) create mode 100644 packages/docusaurus-plugin-google-analytics/src/plugin-google-analytics.d.ts create mode 100644 packages/docusaurus-plugin-google-analytics/src/types.d.ts create mode 100644 packages/docusaurus-plugin-google-analytics/tsconfig.json rename packages/docusaurus-plugin-google-gtag/src/{gtag.js => gtag.ts} (77%) rename packages/docusaurus-plugin-google-gtag/src/{index.js => index.ts} (85%) create mode 100644 packages/docusaurus-plugin-google-gtag/src/plugin-google-gtag.d.ts create mode 100644 packages/docusaurus-plugin-google-gtag/src/types.d.ts create mode 100644 packages/docusaurus-plugin-google-gtag/tsconfig.json diff --git a/packages/docusaurus-plugin-google-analytics/package.json b/packages/docusaurus-plugin-google-analytics/package.json index c4e65cafc8..9038dc1b8a 100644 --- a/packages/docusaurus-plugin-google-analytics/package.json +++ b/packages/docusaurus-plugin-google-analytics/package.json @@ -2,10 +2,15 @@ "name": "@docusaurus/plugin-google-analytics", "version": "2.0.0-beta.6", "description": "Global analytics (analytics.js) plugin for Docusaurus.", - "main": "src/index.js", + "main": "lib/index.js", + "types": "src/plugin-google-analytics.d.ts", "publishConfig": { "access": "public" }, + "scripts": { + "build": "tsc", + "watch": "tsc --watch" + }, "repository": { "type": "git", "url": "https://github.com/facebook/docusaurus.git", @@ -15,6 +20,9 @@ "dependencies": { "@docusaurus/core": "2.0.0-beta.6" }, + "devDependencies": { + "@docusaurus/types": "2.0.0-beta.6" + }, "peerDependencies": { "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" diff --git a/packages/docusaurus-plugin-google-analytics/src/analytics.js b/packages/docusaurus-plugin-google-analytics/src/analytics.ts similarity index 92% rename from packages/docusaurus-plugin-google-analytics/src/analytics.js rename to packages/docusaurus-plugin-google-analytics/src/analytics.ts index 7370f1f9e7..ae5c6532fe 100644 --- a/packages/docusaurus-plugin-google-analytics/src/analytics.js +++ b/packages/docusaurus-plugin-google-analytics/src/analytics.ts @@ -13,7 +13,7 @@ export default (function () { } return { - onRouteUpdate({location}) { + onRouteUpdate({location}: {location: Location}) { // Set page so that subsequent hits on this page are attributed // to this page. This is recommended for Single-page Applications. window.ga('set', 'page', location.pathname); diff --git a/packages/docusaurus-plugin-google-analytics/src/index.js b/packages/docusaurus-plugin-google-analytics/src/index.ts similarity index 82% rename from packages/docusaurus-plugin-google-analytics/src/index.js rename to packages/docusaurus-plugin-google-analytics/src/index.ts index f30fe359a9..083a1ff7f4 100644 --- a/packages/docusaurus-plugin-google-analytics/src/index.js +++ b/packages/docusaurus-plugin-google-analytics/src/index.ts @@ -5,12 +5,15 @@ * LICENSE file in the root directory of this source tree. */ -const path = require('path'); +import path from 'path'; +import type {LoadContext, Plugin, HtmlTags} from '@docusaurus/types'; +import type {ThemeConfig} from '@docusaurus/plugin-google-analytics'; -module.exports = function (context) { - const {siteConfig} = context; - const {themeConfig} = siteConfig; - const {googleAnalytics} = themeConfig || {}; +export default function pluginGoogleAnalytics(context: LoadContext): Plugin { + const { + siteConfig: {themeConfig}, + } = context; + const {googleAnalytics} = themeConfig as ThemeConfig; if (!googleAnalytics) { throw new Error( @@ -66,8 +69,8 @@ module.exports = function (context) { src: 'https://www.google-analytics.com/analytics.js', }, }, - ], + ] as HtmlTags, }; }, }; -}; +} diff --git a/packages/docusaurus-plugin-google-analytics/src/plugin-google-analytics.d.ts b/packages/docusaurus-plugin-google-analytics/src/plugin-google-analytics.d.ts new file mode 100644 index 0000000000..fb0e0626f7 --- /dev/null +++ b/packages/docusaurus-plugin-google-analytics/src/plugin-google-analytics.d.ts @@ -0,0 +1,13 @@ +/** + * 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 interface ThemeConfig { + googleAnalytics?: { + trackingID: string; + anonymizeIP?: boolean; + }; +} diff --git a/packages/docusaurus-plugin-google-analytics/src/types.d.ts b/packages/docusaurus-plugin-google-analytics/src/types.d.ts new file mode 100644 index 0000000000..5a68f46904 --- /dev/null +++ b/packages/docusaurus-plugin-google-analytics/src/types.d.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. + */ + +/// + +interface Window { + ga: (command: string, ...fields: string[]) => void; +} diff --git a/packages/docusaurus-plugin-google-analytics/tsconfig.json b/packages/docusaurus-plugin-google-analytics/tsconfig.json new file mode 100644 index 0000000000..f5902ba108 --- /dev/null +++ b/packages/docusaurus-plugin-google-analytics/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo", + "rootDir": "src", + "outDir": "lib" + } +} diff --git a/packages/docusaurus-plugin-google-gtag/package.json b/packages/docusaurus-plugin-google-gtag/package.json index fabac2318d..c8fd375006 100644 --- a/packages/docusaurus-plugin-google-gtag/package.json +++ b/packages/docusaurus-plugin-google-gtag/package.json @@ -2,7 +2,12 @@ "name": "@docusaurus/plugin-google-gtag", "version": "2.0.0-beta.6", "description": "Global Site Tag (gtag.js) plugin for Docusaurus.", - "main": "src/index.js", + "main": "lib/index.js", + "types": "src/plugin-google-gtag.d.ts", + "scripts": { + "build": "tsc", + "watch": "tsc --watch" + }, "publishConfig": { "access": "public" }, @@ -15,6 +20,9 @@ "dependencies": { "@docusaurus/core": "2.0.0-beta.6" }, + "devDependencies": { + "@docusaurus/types": "2.0.0-beta.6" + }, "peerDependencies": { "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" diff --git a/packages/docusaurus-plugin-google-gtag/src/gtag.js b/packages/docusaurus-plugin-google-gtag/src/gtag.ts similarity index 77% rename from packages/docusaurus-plugin-google-gtag/src/gtag.js rename to packages/docusaurus-plugin-google-gtag/src/gtag.ts index 852d0392e5..c448b112d8 100644 --- a/packages/docusaurus-plugin-google-gtag/src/gtag.js +++ b/packages/docusaurus-plugin-google-gtag/src/gtag.ts @@ -7,20 +7,19 @@ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment'; import siteConfig from '@generated/docusaurus.config'; +import type {ThemeConfig} from '@docusaurus/plugin-google-gtag'; export default (function () { if (!ExecutionEnvironment.canUseDOM) { return null; } - const { - themeConfig: { - gtag: {trackingID}, - }, - } = siteConfig; + const {themeConfig} = siteConfig; + const {gtag} = themeConfig as ThemeConfig; + const {trackingID} = gtag!; return { - onRouteUpdate({location}) { + onRouteUpdate({location}: {location: Location}) { // Always refer to the variable on window in-case it gets overridden elsewhere. window.gtag('config', trackingID, { page_path: location.pathname, diff --git a/packages/docusaurus-plugin-google-gtag/src/index.js b/packages/docusaurus-plugin-google-gtag/src/index.ts similarity index 85% rename from packages/docusaurus-plugin-google-gtag/src/index.js rename to packages/docusaurus-plugin-google-gtag/src/index.ts index 263e56a9fb..09a4d467e5 100644 --- a/packages/docusaurus-plugin-google-gtag/src/index.js +++ b/packages/docusaurus-plugin-google-gtag/src/index.ts @@ -5,12 +5,15 @@ * LICENSE file in the root directory of this source tree. */ -const path = require('path'); +import path from 'path'; +import type {LoadContext, Plugin, HtmlTags} from '@docusaurus/types'; +import type {ThemeConfig} from '@docusaurus/plugin-google-gtag'; -module.exports = function (context) { - const {siteConfig} = context; - const {themeConfig} = siteConfig; - const {gtag} = themeConfig || {}; +export default function pluginGoogleGtag(context: LoadContext): Plugin { + const { + siteConfig: {themeConfig}, + } = context; + const {gtag} = themeConfig as ThemeConfig; if (!gtag) { throw new Error( @@ -75,8 +78,8 @@ module.exports = function (context) { anonymizeIP ? "'anonymize_ip': true" : '' } });`, }, - ], + ] as HtmlTags, }; }, }; -}; +} diff --git a/packages/docusaurus-plugin-google-gtag/src/plugin-google-gtag.d.ts b/packages/docusaurus-plugin-google-gtag/src/plugin-google-gtag.d.ts new file mode 100644 index 0000000000..18d997bd81 --- /dev/null +++ b/packages/docusaurus-plugin-google-gtag/src/plugin-google-gtag.d.ts @@ -0,0 +1,13 @@ +/** + * 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 interface ThemeConfig { + gtag?: { + trackingID: string; + anonymizeIP?: boolean; + }; +} diff --git a/packages/docusaurus-plugin-google-gtag/src/types.d.ts b/packages/docusaurus-plugin-google-gtag/src/types.d.ts new file mode 100644 index 0000000000..6e58d2ecb8 --- /dev/null +++ b/packages/docusaurus-plugin-google-gtag/src/types.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. + */ + +/* eslint-disable camelcase */ +/// + +interface Window { + gtag: ( + command: string, + fields: string, + params: { + page_title?: string; + page_location?: string; + page_path?: string; + }, + ) => void; +} diff --git a/packages/docusaurus-plugin-google-gtag/tsconfig.json b/packages/docusaurus-plugin-google-gtag/tsconfig.json new file mode 100644 index 0000000000..f5902ba108 --- /dev/null +++ b/packages/docusaurus-plugin-google-gtag/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo", + "rootDir": "src", + "outDir": "lib" + } +} diff --git a/packages/docusaurus-preset-classic/src/preset-classic.d.ts b/packages/docusaurus-preset-classic/src/preset-classic.d.ts index 11c8babb58..96ab487358 100644 --- a/packages/docusaurus-preset-classic/src/preset-classic.d.ts +++ b/packages/docusaurus-preset-classic/src/preset-classic.d.ts @@ -14,10 +14,10 @@ export type Options = { theme?: import('@docusaurus/theme-classic').Options; }; -export type ThemeConfig = import('@docusaurus/theme-common').ThemeConfig & { - // Those themeConfigs should rather be moved to preset/plugin options - // Plugin data can be made available to browser thank to the globalData api - algolia?: unknown; // TODO type plugin - googleAnalytics?: unknown; // TODO type plugin - gtag?: unknown; // TODO type plugin -}; +export type ThemeConfig = import('@docusaurus/theme-common').ThemeConfig & + import('@docusaurus/plugin-google-analytics').ThemeConfig & + import('@docusaurus/plugin-google-gtag').ThemeConfig & { + // Those themeConfigs should rather be moved to preset/plugin options + // Plugin data can be made available to browser thank to the globalData api + algolia?: unknown; // TODO type plugin + };