From 40636315c5240885a0aada859cd650977a68d1d5 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 11 Jun 2022 19:31:07 +0800 Subject: [PATCH 1/7] chore: upgrade to TS 4.7, compile with NodeNext --- .eslintrc.js | 6 +- __tests__/validate-tsconfig.test.ts | 2 - package.json | 2 +- .../templates/classic-typescript/package.json | 2 +- .../create-docusaurus/tsconfig.build.json | 1 - packages/create-docusaurus/tsconfig.json | 1 - .../docusaurus-cssnano-preset/tsconfig.json | 1 - packages/docusaurus-logger/tsconfig.json | 1 - packages/docusaurus-mdx-loader/tsconfig.json | 1 - .../docusaurus-migrate/tsconfig.build.json | 1 - packages/docusaurus-migrate/tsconfig.json | 1 - .../src/deps.d.ts | 16 ++++ .../tsconfig.json | 1 - .../tsconfig.json | 1 - .../package.json | 15 +++- .../src/client/index.ts | 68 +++++++++++++-- .../src/plugin-content-docs.d.ts | 86 ------------------- .../tsconfig.json | 1 - .../tsconfig.json | 1 - .../docusaurus-plugin-debug/tsconfig.json | 1 - .../tsconfig.json | 1 - .../tsconfig.json | 1 - .../tsconfig.json | 1 - .../docusaurus-plugin-pwa/src/registerSw.ts | 2 +- .../src/renderReloadPopup.tsx | 2 +- packages/docusaurus-plugin-pwa/tsconfig.json | 1 - .../docusaurus-plugin-sitemap/tsconfig.json | 1 - .../docusaurus-preset-classic/tsconfig.json | 1 - .../tsconfig.json | 1 - .../src/theme/CodeBlock/Container/index.tsx | 2 +- .../src/theme/CodeBlock/CopyButton/index.tsx | 1 + .../src/theme/NavbarItem/index.tsx | 2 +- .../docusaurus-theme-classic/tsconfig.json | 1 - packages/docusaurus-theme-common/Details.d.ts | 14 --- .../tsconfig.json | 1 - .../package.json | 10 ++- .../src/deps.d.ts | 10 +++ .../tsconfig.json | 1 - .../tsconfig.build.json | 1 - .../tsconfig.json | 1 - .../docusaurus-utils-common/tsconfig.json | 1 - .../docusaurus-utils-validation/tsconfig.json | 1 - packages/docusaurus-utils/tsconfig.json | 1 - .../src/client/exports/ComponentCreator.tsx | 2 +- packages/docusaurus/src/deps.d.ts | 19 ++++ packages/docusaurus/tsconfig.json | 3 +- packages/docusaurus/tsconfig.server.json | 1 - packages/eslint-plugin/tsconfig.json | 1 - packages/lqip-loader/tsconfig.json | 1 - packages/stylelint-copyright/tsconfig.json | 1 - tsconfig.json | 6 +- yarn.lock | 7 +- 52 files changed, 142 insertions(+), 166 deletions(-) create mode 100644 packages/docusaurus-plugin-client-redirects/src/deps.d.ts delete mode 100644 packages/docusaurus-theme-common/Details.d.ts diff --git a/.eslintrc.js b/.eslintrc.js index 03a67afe1e1e..6708ef87f1f9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -211,10 +211,12 @@ module.exports = { ], 'import/extensions': OFF, - // Ignore certain webpack aliases because they can't be resolved + // This rule doesn't yet support resolving .js imports when the actual file + // is .ts. Plus it's not all that useful when our code is fully TS-covered. 'import/no-unresolved': [ - ERROR, + OFF, { + // Ignore certain webpack aliases because they can't be resolved ignore: [ '^@theme', '^@docusaurus', diff --git a/__tests__/validate-tsconfig.test.ts b/__tests__/validate-tsconfig.test.ts index e69d170502ba..8038a04ae824 100644 --- a/__tests__/validate-tsconfig.test.ts +++ b/__tests__/validate-tsconfig.test.ts @@ -40,14 +40,12 @@ const tsconfigSchema = Joi.object({ incremental: Joi.forbidden(), tsBuildInfoFile: Joi.forbidden(), outDir: Joi.forbidden(), - module: Joi.valid('commonjs', 'es2020', 'esnext').required(), }).unknown(), otherwise: Joi.object({ noEmit: Joi.valid(false).required(), incremental: Joi.valid(true).required(), rootDir: Joi.valid('src').required(), outDir: Joi.valid('lib').required(), - module: Joi.valid('commonjs', 'es2020', 'esnext').required(), }).unknown(), }, ), diff --git a/package.json b/package.json index c381b4dfcae0..f77d8f05c928 100644 --- a/package.json +++ b/package.json @@ -111,6 +111,6 @@ "stylelint": "^14.9.1", "stylelint-config-prettier": "^9.0.3", "stylelint-config-standard": "^26.0.0", - "typescript": "~4.6.4" + "typescript": "~4.7.3" } } diff --git a/packages/create-docusaurus/templates/classic-typescript/package.json b/packages/create-docusaurus/templates/classic-typescript/package.json index 6f7971bf2757..c08d521416b2 100644 --- a/packages/create-docusaurus/templates/classic-typescript/package.json +++ b/packages/create-docusaurus/templates/classic-typescript/package.json @@ -26,7 +26,7 @@ "devDependencies": { "@docusaurus/module-type-aliases": "2.0.0-beta.21", "@tsconfig/docusaurus": "^1.0.5", - "typescript": "^4.6.4" + "typescript": "^4.7.3" }, "browserslist": { "production": [ diff --git a/packages/create-docusaurus/tsconfig.build.json b/packages/create-docusaurus/tsconfig.build.json index 6f46335ae9d0..36377cdd0cbe 100644 --- a/packages/create-docusaurus/tsconfig.build.json +++ b/packages/create-docusaurus/tsconfig.build.json @@ -3,7 +3,6 @@ "compilerOptions": { "noEmit": false, "composite": true, - "module": "es2020", "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", "rootDir": "src", diff --git a/packages/create-docusaurus/tsconfig.json b/packages/create-docusaurus/tsconfig.json index 5c73d3ac4eca..13e35c5a957c 100644 --- a/packages/create-docusaurus/tsconfig.json +++ b/packages/create-docusaurus/tsconfig.json @@ -3,7 +3,6 @@ "references": [{"path": "./tsconfig.build.json"}], "compilerOptions": { "noEmit": true, - "module": "esnext", "rootDir": "." }, "include": ["bin"], diff --git a/packages/docusaurus-cssnano-preset/tsconfig.json b/packages/docusaurus-cssnano-preset/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/docusaurus-cssnano-preset/tsconfig.json +++ b/packages/docusaurus-cssnano-preset/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-logger/tsconfig.json b/packages/docusaurus-logger/tsconfig.json index 7807612b51a5..69bd9341b47e 100644 --- a/packages/docusaurus-logger/tsconfig.json +++ b/packages/docusaurus-logger/tsconfig.json @@ -6,7 +6,6 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "sourceMap": true, "declarationMap": true, - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-mdx-loader/tsconfig.json b/packages/docusaurus-mdx-loader/tsconfig.json index 3cdfe2a88c91..670db9ce9168 100644 --- a/packages/docusaurus-mdx-loader/tsconfig.json +++ b/packages/docusaurus-mdx-loader/tsconfig.json @@ -6,7 +6,6 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "sourceMap": true, "declarationMap": true, - "module": "commonjs", "rootDir": "src", "outDir": "lib", "types": [] diff --git a/packages/docusaurus-migrate/tsconfig.build.json b/packages/docusaurus-migrate/tsconfig.build.json index e43e58accad2..bc03b5edf231 100644 --- a/packages/docusaurus-migrate/tsconfig.build.json +++ b/packages/docusaurus-migrate/tsconfig.build.json @@ -5,7 +5,6 @@ "composite": true, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-migrate/tsconfig.json b/packages/docusaurus-migrate/tsconfig.json index 5c73d3ac4eca..13e35c5a957c 100644 --- a/packages/docusaurus-migrate/tsconfig.json +++ b/packages/docusaurus-migrate/tsconfig.json @@ -3,7 +3,6 @@ "references": [{"path": "./tsconfig.build.json"}], "compilerOptions": { "noEmit": true, - "module": "esnext", "rootDir": "." }, "include": ["bin"], diff --git a/packages/docusaurus-plugin-client-redirects/src/deps.d.ts b/packages/docusaurus-plugin-client-redirects/src/deps.d.ts new file mode 100644 index 000000000000..90ace5ffd5e8 --- /dev/null +++ b/packages/docusaurus-plugin-client-redirects/src/deps.d.ts @@ -0,0 +1,16 @@ +/** + * 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. + */ + +// TODO incompatible declaration file +declare module 'eta' { + export const defaultConfig: object; + + export function compile( + template: string, + options?: object, + ): (data: object, config: object) => string; +} diff --git a/packages/docusaurus-plugin-client-redirects/tsconfig.json b/packages/docusaurus-plugin-client-redirects/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/docusaurus-plugin-client-redirects/tsconfig.json +++ b/packages/docusaurus-plugin-client-redirects/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-content-blog/tsconfig.json b/packages/docusaurus-plugin-content-blog/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/docusaurus-plugin-content-blog/tsconfig.json +++ b/packages/docusaurus-plugin-content-blog/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-content-docs/package.json b/packages/docusaurus-plugin-content-docs/package.json index 903a6c374544..28d79211769a 100644 --- a/packages/docusaurus-plugin-content-docs/package.json +++ b/packages/docusaurus-plugin-content-docs/package.json @@ -6,9 +6,18 @@ "sideEffects": false, "exports": { "./src/*": "./src/*", - "./client": "./lib/client/index.js", - "./server": "./lib/server-export.js", - ".": "./lib/index.js" + "./client": { + "type": "./lib/client/index.d.ts", + "default": "./lib/client/index.js" + }, + "./server": { + "type": "./lib/server-export.d.ts", + "default": "./lib/server-export.js" + }, + ".": { + "types": "./src/plugin-content-docs.d.ts", + "default": "./lib/index.js" + } }, "types": "src/plugin-content-docs.d.ts", "scripts": { diff --git a/packages/docusaurus-plugin-content-docs/src/client/index.ts b/packages/docusaurus-plugin-content-docs/src/client/index.ts index bfc2bfd78cb0..bf475ee00e9d 100644 --- a/packages/docusaurus-plugin-content-docs/src/client/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/client/index.ts @@ -18,15 +18,60 @@ import { getActiveDocContext, getDocVersionSuggestions, } from './docsClientUtils'; -import type { - GlobalPluginData, - GlobalVersion, - ActivePlugin, - ActiveDocContext, - DocVersionSuggestions, -} from '@docusaurus/plugin-content-docs/client'; import type {UseDataOptions} from '@docusaurus/types'; +export type ActivePlugin = { + pluginId: string; + pluginData: GlobalPluginData; +}; +export type ActiveDocContext = { + activeVersion?: GlobalVersion; + activeDoc?: GlobalDoc; + alternateDocVersions: {[versionName: string]: GlobalDoc}; +}; +export type GlobalDoc = { + /** + * For generated index pages, this is the `slug`, **not** `permalink` + * (without base URL). Because slugs have leading slashes but IDs don't, + * there won't be clashes. + */ + id: string; + path: string; + sidebar: string | undefined; +}; + +export type GlobalVersion = { + name: string; + label: string; + isLast: boolean; + path: string; + /** The doc with `slug: /`, or first doc in first sidebar */ + mainDocId: string; + docs: GlobalDoc[]; + /** Unversioned IDs. In development, this list is empty. */ + draftIds: string[]; + sidebars?: {[sidebarId: string]: GlobalSidebar}; +}; + +export type GlobalSidebar = { + link?: { + label: string; + path: string; + }; + // ... we may add other things here later +}; +export type GlobalPluginData = { + path: string; + versions: GlobalVersion[]; + breadcrumbs: boolean; +}; +export type DocVersionSuggestions = { + /** Suggest the latest version */ + latestVersionSuggestion: GlobalVersion; + /** Suggest the same doc, in latest version (if one exists) */ + latestDocSuggestion?: GlobalDoc; +}; + // Important to use a constant object to avoid React useEffect executions etc. // see https://github.com/facebook/docusaurus/issues/5089 const StableEmptyObject = {}; @@ -71,6 +116,7 @@ export function useActivePluginAndVersion( }; } +/** Versions are returned ordered (most recent first). */ export function useVersions(pluginId: string | undefined): GlobalVersion[] { const data = useDocsData(pluginId); return data.versions; @@ -81,6 +127,10 @@ export function useLatestVersion(pluginId: string | undefined): GlobalVersion { return getLatestVersion(data); } +/** + * Returns `undefined` on doc-unrelated pages, because there's no version + * currently considered as active. + */ export function useActiveVersion( pluginId: string | undefined, ): GlobalVersion | undefined { @@ -96,7 +146,9 @@ export function useActiveDocContext( const {pathname} = useLocation(); return getActiveDocContext(data, pathname); } - +/** + * Useful to say "hey, you are not on the latest docs version, please switch" + */ export function useDocVersionSuggestions( pluginId: string | undefined, ): DocVersionSuggestions { diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index a96efd1b804d..8622b55543ac 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -616,89 +616,3 @@ declare module '@theme/DocPage' { export default function DocPage(props: Props): JSX.Element; } - -// TODO TS only supports reading `exports` in 4.7. We will need to merge the -// type defs (and JSDoc) here with the implementation after that -declare module '@docusaurus/plugin-content-docs/client' { - import type {UseDataOptions} from '@docusaurus/types'; - - export type ActivePlugin = { - pluginId: string; - pluginData: GlobalPluginData; - }; - export type ActiveDocContext = { - activeVersion?: GlobalVersion; - activeDoc?: GlobalDoc; - alternateDocVersions: {[versionName: string]: GlobalDoc}; - }; - export type GlobalDoc = { - /** - * For generated index pages, this is the `slug`, **not** `permalink` - * (without base URL). Because slugs have leading slashes but IDs don't, - * there won't be clashes. - */ - id: string; - path: string; - sidebar: string | undefined; - }; - - export type GlobalVersion = { - name: string; - label: string; - isLast: boolean; - path: string; - /** The doc with `slug: /`, or first doc in first sidebar */ - mainDocId: string; - docs: GlobalDoc[]; - /** Unversioned IDs. In development, this list is empty. */ - draftIds: string[]; - sidebars?: {[sidebarId: string]: GlobalSidebar}; - }; - - export type GlobalSidebar = { - link?: { - label: string; - path: string; - }; - // ... we may add other things here later - }; - export type GlobalPluginData = { - path: string; - versions: GlobalVersion[]; - breadcrumbs: boolean; - }; - export type DocVersionSuggestions = { - /** Suggest the latest version */ - latestVersionSuggestion: GlobalVersion; - /** Suggest the same doc, in latest version (if one exists) */ - latestDocSuggestion?: GlobalDoc; - }; - - export const useAllDocsData: () => {[pluginId: string]: GlobalPluginData}; - export const useDocsData: (pluginId?: string) => GlobalPluginData; - export const useActivePlugin: ( - options?: UseDataOptions, - ) => ActivePlugin | undefined; - export const useActivePluginAndVersion: ( - options?: UseDataOptions, - ) => - | {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined} - | undefined; - /** Versions are returned ordered (most recent first). */ - export const useVersions: (pluginId?: string) => GlobalVersion[]; - export const useLatestVersion: (pluginId?: string) => GlobalVersion; - /** - * Returns `undefined` on doc-unrelated pages, because there's no version - * currently considered as active. - */ - export const useActiveVersion: ( - pluginId?: string, - ) => GlobalVersion | undefined; - export const useActiveDocContext: (pluginId?: string) => ActiveDocContext; - /** - * Useful to say "hey, you are not on the latest docs version, please switch" - */ - export const useDocVersionSuggestions: ( - pluginId?: string, - ) => DocVersionSuggestions; -} diff --git a/packages/docusaurus-plugin-content-docs/tsconfig.json b/packages/docusaurus-plugin-content-docs/tsconfig.json index 48e200409e4e..3936df64b7e4 100644 --- a/packages/docusaurus-plugin-content-docs/tsconfig.json +++ b/packages/docusaurus-plugin-content-docs/tsconfig.json @@ -5,7 +5,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-content-pages/tsconfig.json b/packages/docusaurus-plugin-content-pages/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/docusaurus-plugin-content-pages/tsconfig.json +++ b/packages/docusaurus-plugin-content-pages/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-debug/tsconfig.json b/packages/docusaurus-plugin-debug/tsconfig.json index 7c1bb85ac462..18b9536626ad 100644 --- a/packages/docusaurus-plugin-debug/tsconfig.json +++ b/packages/docusaurus-plugin-debug/tsconfig.json @@ -5,7 +5,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-google-analytics/tsconfig.json b/packages/docusaurus-plugin-google-analytics/tsconfig.json index 77a4365aedfe..c7fda37effc4 100644 --- a/packages/docusaurus-plugin-google-analytics/tsconfig.json +++ b/packages/docusaurus-plugin-google-analytics/tsconfig.json @@ -5,7 +5,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-google-gtag/tsconfig.json b/packages/docusaurus-plugin-google-gtag/tsconfig.json index 1b3271a718e6..fe656511f355 100644 --- a/packages/docusaurus-plugin-google-gtag/tsconfig.json +++ b/packages/docusaurus-plugin-google-gtag/tsconfig.json @@ -5,7 +5,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-ideal-image/tsconfig.json b/packages/docusaurus-plugin-ideal-image/tsconfig.json index 7c1bb85ac462..18b9536626ad 100644 --- a/packages/docusaurus-plugin-ideal-image/tsconfig.json +++ b/packages/docusaurus-plugin-ideal-image/tsconfig.json @@ -5,7 +5,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-pwa/src/registerSw.ts b/packages/docusaurus-plugin-pwa/src/registerSw.ts index e84a33e5c961..1c834d3be843 100644 --- a/packages/docusaurus-plugin-pwa/src/registerSw.ts +++ b/packages/docusaurus-plugin-pwa/src/registerSw.ts @@ -139,7 +139,7 @@ async function registerSW() { if (!offlineMode) { return sendSkipWaiting(); } - return import('./renderReloadPopup').then(({default: renderReloadPopup}) => + return import('./renderReloadPopup.js').then(({renderReloadPopup}) => renderReloadPopup({ onReload() { wb.addEventListener('controlling', () => { diff --git a/packages/docusaurus-plugin-pwa/src/renderReloadPopup.tsx b/packages/docusaurus-plugin-pwa/src/renderReloadPopup.tsx index b814ae35ba63..89a8cd984890 100644 --- a/packages/docusaurus-plugin-pwa/src/renderReloadPopup.tsx +++ b/packages/docusaurus-plugin-pwa/src/renderReloadPopup.tsx @@ -20,7 +20,7 @@ const createContainer = () => { return container; }; -export default function renderReloadPopup(props: Props): Promise { +export function renderReloadPopup(props: Props): Promise { const container = getContainer() ?? createContainer(); return import('@theme/PwaReloadPopup').then(({default: ReloadPopup}) => { ReactDOM.render(, container); diff --git a/packages/docusaurus-plugin-pwa/tsconfig.json b/packages/docusaurus-plugin-pwa/tsconfig.json index 9da13f25341a..0bef8e601656 100644 --- a/packages/docusaurus-plugin-pwa/tsconfig.json +++ b/packages/docusaurus-plugin-pwa/tsconfig.json @@ -8,7 +8,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-plugin-sitemap/tsconfig.json b/packages/docusaurus-plugin-sitemap/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/docusaurus-plugin-sitemap/tsconfig.json +++ b/packages/docusaurus-plugin-sitemap/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-preset-classic/tsconfig.json b/packages/docusaurus-preset-classic/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/docusaurus-preset-classic/tsconfig.json +++ b/packages/docusaurus-preset-classic/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json b/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json index 7807612b51a5..69bd9341b47e 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json +++ b/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json @@ -6,7 +6,6 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "sourceMap": true, "declarationMap": true, - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-theme-classic/src/theme/CodeBlock/Container/index.tsx b/packages/docusaurus-theme-classic/src/theme/CodeBlock/Container/index.tsx index 293d700dad35..3e3961027373 100644 --- a/packages/docusaurus-theme-classic/src/theme/CodeBlock/Container/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/CodeBlock/Container/index.tsx @@ -23,7 +23,7 @@ export default function CodeBlockContainer({ return ( ; + return ; } diff --git a/packages/docusaurus-theme-classic/tsconfig.json b/packages/docusaurus-theme-classic/tsconfig.json index 9e41ec3ef68c..8aa1157eefbc 100644 --- a/packages/docusaurus-theme-classic/tsconfig.json +++ b/packages/docusaurus-theme-classic/tsconfig.json @@ -5,7 +5,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-theme-common/Details.d.ts b/packages/docusaurus-theme-common/Details.d.ts deleted file mode 100644 index 61dab97e7363..000000000000 --- a/packages/docusaurus-theme-common/Details.d.ts +++ /dev/null @@ -1,14 +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. - */ - -// `Details` is a separate export entry because of side-effects messing with CSS -// insertion order. See https://github.com/facebook/docusaurus/pull/7085. -// However, because TS doesn't recognize `exports` (also a problem in -// `content-docs`), we have to manually create a stub. - -// eslint-disable-next-line import/named -export {Details, type DetailsProps} from './lib/components/Details'; diff --git a/packages/docusaurus-theme-live-codeblock/tsconfig.json b/packages/docusaurus-theme-live-codeblock/tsconfig.json index c0b15f81fcc8..a6dc07deeeee 100644 --- a/packages/docusaurus-theme-live-codeblock/tsconfig.json +++ b/packages/docusaurus-theme-live-codeblock/tsconfig.json @@ -5,7 +5,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-theme-search-algolia/package.json b/packages/docusaurus-theme-search-algolia/package.json index 0a397b2ffc26..d1ae4db8ca9e 100644 --- a/packages/docusaurus-theme-search-algolia/package.json +++ b/packages/docusaurus-theme-search-algolia/package.json @@ -7,8 +7,14 @@ "*.css" ], "exports": { - "./client": "./lib/client/index.js", - ".": "./lib/index.js" + "./client": { + "types": "./lib/client/index.d.ts", + "default": "./lib/client/index.js" + }, + ".": { + "types": "./src/theme-search-algolia.d.ts", + "default": "./lib/index.js" + } }, "types": "src/theme-search-algolia.d.ts", "publishConfig": { diff --git a/packages/docusaurus-theme-search-algolia/src/deps.d.ts b/packages/docusaurus-theme-search-algolia/src/deps.d.ts index 1b69f16b7850..fd64b8228382 100644 --- a/packages/docusaurus-theme-search-algolia/src/deps.d.ts +++ b/packages/docusaurus-theme-search-algolia/src/deps.d.ts @@ -8,3 +8,13 @@ declare module '@docsearch/react/modal'; declare module '@docsearch/react/style'; + +// TODO incompatible declaration file +declare module 'eta' { + export const defaultConfig: object; + + export function compile( + template: string, + options?: object, + ): (data: object, config: object) => string; +} diff --git a/packages/docusaurus-theme-search-algolia/tsconfig.json b/packages/docusaurus-theme-search-algolia/tsconfig.json index 3c8737f51c0d..4eb745d80ce7 100644 --- a/packages/docusaurus-theme-search-algolia/tsconfig.json +++ b/packages/docusaurus-theme-search-algolia/tsconfig.json @@ -5,7 +5,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-theme-translations/tsconfig.build.json b/packages/docusaurus-theme-translations/tsconfig.build.json index 5370e86c56a5..912de0a933a8 100644 --- a/packages/docusaurus-theme-translations/tsconfig.build.json +++ b/packages/docusaurus-theme-translations/tsconfig.build.json @@ -7,7 +7,6 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "sourceMap": true, "declarationMap": true, - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-theme-translations/tsconfig.json b/packages/docusaurus-theme-translations/tsconfig.json index 2a44c196c440..6ba7dee96bcd 100644 --- a/packages/docusaurus-theme-translations/tsconfig.json +++ b/packages/docusaurus-theme-translations/tsconfig.json @@ -2,7 +2,6 @@ "extends": "../../tsconfig.json", "references": [{"path": "./tsconfig.build.json"}], "compilerOptions": { - "module": "esnext", "noEmit": true, "checkJs": true }, diff --git a/packages/docusaurus-utils-common/tsconfig.json b/packages/docusaurus-utils-common/tsconfig.json index f787010fcd78..0e5365a3c1e8 100644 --- a/packages/docusaurus-utils-common/tsconfig.json +++ b/packages/docusaurus-utils-common/tsconfig.json @@ -6,7 +6,6 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "sourceMap": true, "declarationMap": true, - "module": "commonjs", "rootDir": "src", "outDir": "lib", "noEmitHelpers": false diff --git a/packages/docusaurus-utils-validation/tsconfig.json b/packages/docusaurus-utils-validation/tsconfig.json index 7807612b51a5..69bd9341b47e 100644 --- a/packages/docusaurus-utils-validation/tsconfig.json +++ b/packages/docusaurus-utils-validation/tsconfig.json @@ -6,7 +6,6 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "sourceMap": true, "declarationMap": true, - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus-utils/tsconfig.json b/packages/docusaurus-utils/tsconfig.json index 7807612b51a5..69bd9341b47e 100644 --- a/packages/docusaurus-utils/tsconfig.json +++ b/packages/docusaurus-utils/tsconfig.json @@ -6,7 +6,6 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "sourceMap": true, "declarationMap": true, - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/docusaurus/src/client/exports/ComponentCreator.tsx b/packages/docusaurus/src/client/exports/ComponentCreator.tsx index 476d73febcf9..33c0f4c90a6d 100644 --- a/packages/docusaurus/src/client/exports/ComponentCreator.tsx +++ b/packages/docusaurus/src/client/exports/ComponentCreator.tsx @@ -132,7 +132,7 @@ export default function ComponentCreator( // Is there any way to put this RouteContextProvider upper in the tree? return ( - + ); }, diff --git a/packages/docusaurus/src/deps.d.ts b/packages/docusaurus/src/deps.d.ts index c781b107ab0f..49bca18d06f9 100644 --- a/packages/docusaurus/src/deps.d.ts +++ b/packages/docusaurus/src/deps.d.ts @@ -73,3 +73,22 @@ declare module 'webpack/lib/HotModuleReplacementPlugin' { export default HotModuleReplacementPlugin; } + +// TODO incompatible declaration file: https://github.com/unjs/webpackbar/pull/108 +declare module 'webpackbar' { + import webpack from 'webpack'; + + export default class WebpackBarPlugin extends webpack.ProgressPlugin { + constructor(options: {name: string; color?: string}); + } +} + +// TODO incompatible declaration file +declare module 'eta' { + export const defaultConfig: object; + + export function compile( + template: string, + options?: object, + ): (data: object, config: object) => string; +} diff --git a/packages/docusaurus/tsconfig.json b/packages/docusaurus/tsconfig.json index f547aea00fff..80dfc91440a4 100644 --- a/packages/docusaurus/tsconfig.json +++ b/packages/docusaurus/tsconfig.json @@ -7,8 +7,7 @@ "compilerOptions": { "noEmit": true, "checkJs": true, - "rootDir": ".", - "module": "esnext" + "rootDir": "." }, "include": ["bin"], "exclude": ["**/__tests__/**"] diff --git a/packages/docusaurus/tsconfig.server.json b/packages/docusaurus/tsconfig.server.json index f800ce6a5fe5..e26c376455c3 100644 --- a/packages/docusaurus/tsconfig.server.json +++ b/packages/docusaurus/tsconfig.server.json @@ -5,7 +5,6 @@ "composite": true, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/eslint-plugin/tsconfig.json b/packages/eslint-plugin/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/eslint-plugin/tsconfig.json +++ b/packages/eslint-plugin/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/lqip-loader/tsconfig.json b/packages/lqip-loader/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/lqip-loader/tsconfig.json +++ b/packages/lqip-loader/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/packages/stylelint-copyright/tsconfig.json b/packages/stylelint-copyright/tsconfig.json index 80e43e9813ef..e16d5c2c5d33 100644 --- a/packages/stylelint-copyright/tsconfig.json +++ b/packages/stylelint-copyright/tsconfig.json @@ -4,7 +4,6 @@ "noEmit": false, "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "module": "commonjs", "rootDir": "src", "outDir": "lib" }, diff --git a/tsconfig.json b/tsconfig.json index 5a379450a1b0..f0b893d74fb8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,8 +11,8 @@ "jsx": "react-native", "importHelpers": true, "noEmitHelpers": true, - // Will be overridden in every project - "module": "esnext", + // Will be overridden in client projects + "module": "NodeNext", // Avoid accidentally using this config to build "noEmit": true, @@ -42,7 +42,7 @@ "importsNotUsedAsValues": "remove", /* Module Resolution */ - "moduleResolution": "node", + "moduleResolution": "NodeNext", "resolveJsonModule": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true, diff --git a/yarn.lock b/yarn.lock index 58a8c4c8a40e..a4baf636d0d1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14884,16 +14884,11 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA== -typescript@^4.6.4: +typescript@^4.7.3, typescript@~4.7.3: version "4.7.3" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.3.tgz#8364b502d5257b540f9de4c40be84c98e23a129d" integrity sha512-WOkT3XYvrpXx4vMMqlD+8R8R37fZkjyLGlxavMc4iB8lrl8L0DeTcHbYgw/v0N/z9wAFsgBhcsF0ruoySS22mA== -typescript@~4.6.4: - version "4.6.4" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.4.tgz#caa78bbc3a59e6a5c510d35703f6a09877ce45e9" - integrity sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg== - ua-parser-js@^0.7.30: version "0.7.31" resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.31.tgz#649a656b191dffab4f21d5e053e27ca17cbff5c6" From 395e2b1f1a808072af4210346d1599f4af625f9e Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 9 Jun 2022 18:43:06 +0800 Subject: [PATCH 2/7] fix? --- packages/docusaurus-plugin-pwa/src/deps.d.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 packages/docusaurus-plugin-pwa/src/deps.d.ts diff --git a/packages/docusaurus-plugin-pwa/src/deps.d.ts b/packages/docusaurus-plugin-pwa/src/deps.d.ts new file mode 100644 index 000000000000..104a45712162 --- /dev/null +++ b/packages/docusaurus-plugin-pwa/src/deps.d.ts @@ -0,0 +1,15 @@ +/** + * 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. + */ + +// TODO incompatible declaration file: https://github.com/unjs/webpackbar/pull/108 +declare module 'webpackbar' { + import webpack from 'webpack'; + + export default class WebpackBarPlugin extends webpack.ProgressPlugin { + constructor(options: {name: string; color?: string}); + } +} From 43d0af37c12939eb3d2da446af8722cecaf7f59f Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 9 Jun 2022 19:02:07 +0800 Subject: [PATCH 3/7] fix typecheck --- .../src/client/index.ts | 16 ++++++---------- website/tsconfig.json | 2 ++ yarn.lock | 12 ++++++------ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/client/index.ts b/packages/docusaurus-plugin-content-docs/src/client/index.ts index bf475ee00e9d..06b86860dc1c 100644 --- a/packages/docusaurus-plugin-content-docs/src/client/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/client/index.ts @@ -85,7 +85,7 @@ export const useAllDocsData = (): {[pluginId: string]: GlobalPluginData} => } | undefined) ?? StableEmptyObject; -export const useDocsData = (pluginId: string | undefined): GlobalPluginData => +export const useDocsData = (pluginId?: string): GlobalPluginData => usePluginData('docusaurus-plugin-content-docs', pluginId, { failfast: true, }) as GlobalPluginData; @@ -117,12 +117,12 @@ export function useActivePluginAndVersion( } /** Versions are returned ordered (most recent first). */ -export function useVersions(pluginId: string | undefined): GlobalVersion[] { +export function useVersions(pluginId?: string): GlobalVersion[] { const data = useDocsData(pluginId); return data.versions; } -export function useLatestVersion(pluginId: string | undefined): GlobalVersion { +export function useLatestVersion(pluginId?: string): GlobalVersion { const data = useDocsData(pluginId); return getLatestVersion(data); } @@ -131,17 +131,13 @@ export function useLatestVersion(pluginId: string | undefined): GlobalVersion { * Returns `undefined` on doc-unrelated pages, because there's no version * currently considered as active. */ -export function useActiveVersion( - pluginId: string | undefined, -): GlobalVersion | undefined { +export function useActiveVersion(pluginId?: string): GlobalVersion | undefined { const data = useDocsData(pluginId); const {pathname} = useLocation(); return getActiveVersion(data, pathname); } -export function useActiveDocContext( - pluginId: string | undefined, -): ActiveDocContext { +export function useActiveDocContext(pluginId?: string): ActiveDocContext { const data = useDocsData(pluginId); const {pathname} = useLocation(); return getActiveDocContext(data, pathname); @@ -150,7 +146,7 @@ export function useActiveDocContext( * Useful to say "hey, you are not on the latest docs version, please switch" */ export function useDocVersionSuggestions( - pluginId: string | undefined, + pluginId?: string, ): DocVersionSuggestions { const data = useDocsData(pluginId); const {pathname} = useLocation(); diff --git a/website/tsconfig.json b/website/tsconfig.json index 40130b894da3..0fcd0946a271 100644 --- a/website/tsconfig.json +++ b/website/tsconfig.json @@ -28,6 +28,8 @@ "noUnusedParameters": false, "importsNotUsedAsValues": "remove", + "moduleResolution": "NodeNext", + // This is important. We run `yarn tsc` in website so we can catch issues // with our declaration files (mostly names that are forgotten to be // imported, invalid semantics...). Because we don't have end-to-end type diff --git a/yarn.lock b/yarn.lock index a4baf636d0d1..9b4ca85e62b0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3259,9 +3259,9 @@ integrity sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA== "@tsconfig/docusaurus@^1.0.5": - version "1.0.5" - resolved "https://registry.yarnpkg.com/@tsconfig/docusaurus/-/docusaurus-1.0.5.tgz#5298c5b0333c6263f06c3149b38ebccc9f169a4e" - integrity sha512-KM/TuJa9fugo67dTGx+ktIqf3fVc077J6jwHu845Hex4EQf7LABlNonP/mohDKT0cmncdtlYVHHF74xR/YpThg== + version "1.0.6" + resolved "https://registry.yarnpkg.com/@tsconfig/docusaurus/-/docusaurus-1.0.6.tgz#7305a7fa590decc0d5968500234e95fd68788978" + integrity sha512-1QxDaP54hpzM6bq9E+yFEo4F9WbWHhsDe4vktZXF/iDlc9FqGr9qlg+3X/nuKQXx8QxHV7ue8NXFazzajsxFBA== "@types/babel__core@^7.1.14": version "7.1.19" @@ -15360,9 +15360,9 @@ vfile@^4.0.0: vfile-message "^2.0.0" vfile@^5.0.0: - version "5.3.2" - resolved "https://registry.yarnpkg.com/vfile/-/vfile-5.3.2.tgz#b499fbc50197ea50ad3749e9b60beb16ca5b7c54" - integrity sha512-w0PLIugRY3Crkgw89TeMvHCzqCs/zpreR31hl4D92y6SOE07+bfJe+dK5Q2akwS+i/c801kzjoOr9gMcTe6IAA== + version "5.3.3" + resolved "https://registry.yarnpkg.com/vfile/-/vfile-5.3.3.tgz#70b75779d99e0698a8cb6536a09361ac37f800a0" + integrity sha512-xwALvwUKmXzHOri5dGXqXNN8JDEvxPhf8avC+E+pJEl32e4/grLdRdsgx23HpK7QI0cwgR4+QfaM8D5KUnki3g== dependencies: "@types/unist" "^2.0.0" is-buffer "^2.0.0" From c4e4f5b01565689ffb5e3c5d87c4d704efcf6af4 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 11 Jun 2022 23:29:01 +0800 Subject: [PATCH 4/7] fix wrap --- .../__snapshots__/actions.test.ts.snap | 15 +++++++++++--- .../__snapshots__/index.test.ts.snap | 20 +++++++++++++++---- .../src/commands/swizzle/actions.ts | 5 ++++- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/actions.test.ts.snap b/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/actions.test.ts.snap index 2608fd767172..36fc80b4478f 100644 --- a/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/actions.test.ts.snap +++ b/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/actions.test.ts.snap @@ -47,7 +47,10 @@ exports[`wrap TypeScript wrap ComponentInFolder 2`] = ` import ComponentInFolder from '@theme-original/ComponentInFolder'; import type ComponentInFolderType from '@theme/ComponentInFolder'; -type Props = ComponentProps; +type RawProps = ComponentProps; +// ComponentProps is a little buggy, quick fix. +// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 +type Props = unknown extends RawProps ? {} : RawProps; export default function ComponentInFolderWrapper(props: Props): JSX.Element { return ( @@ -64,7 +67,10 @@ exports[`wrap TypeScript wrap ComponentInFolder/ComponentInSubFolder 2`] = ` import ComponentInSubFolder from '@theme-original/ComponentInFolder/ComponentInSubFolder'; import type ComponentInSubFolderType from '@theme/ComponentInFolder/ComponentInSubFolder'; -type Props = ComponentProps; +type RawProps = ComponentProps; +// ComponentProps is a little buggy, quick fix. +// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 +type Props = unknown extends RawProps ? {} : RawProps; export default function ComponentInSubFolderWrapper(props: Props): JSX.Element { return ( @@ -81,7 +87,10 @@ exports[`wrap TypeScript wrap FirstLevelComponent 2`] = ` import FirstLevelComponent from '@theme-original/FirstLevelComponent'; import type FirstLevelComponentType from '@theme/FirstLevelComponent'; -type Props = ComponentProps; +type RawProps = ComponentProps; +// ComponentProps is a little buggy, quick fix. +// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 +type Props = unknown extends RawProps ? {} : RawProps; export default function FirstLevelComponentWrapper(props: Props): JSX.Element { return ( diff --git a/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap index a686e30fae35..e1d28fc40529 100644 --- a/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap @@ -277,7 +277,10 @@ exports[`swizzle wrap ComponentInFolder TS: ComponentInFolder/index.tsx 1`] = ` import ComponentInFolder from '@theme-original/ComponentInFolder'; import type ComponentInFolderType from '@theme/ComponentInFolder'; -type Props = ComponentProps; +type RawProps = ComponentProps; +// ComponentProps is a little buggy, quick fix. +// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 +type Props = unknown extends RawProps ? {} : RawProps; export default function ComponentInFolderWrapper(props: Props): JSX.Element { return ( @@ -321,7 +324,10 @@ exports[`swizzle wrap ComponentInFolder/ComponentInSubFolder TS: ComponentInFold import ComponentInSubFolder from '@theme-original/ComponentInFolder/ComponentInSubFolder'; import type ComponentInSubFolderType from '@theme/ComponentInFolder/ComponentInSubFolder'; -type Props = ComponentProps; +type RawProps = ComponentProps; +// ComponentProps is a little buggy, quick fix. +// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 +type Props = unknown extends RawProps ? {} : RawProps; export default function ComponentInSubFolderWrapper(props: Props): JSX.Element { return ( @@ -365,7 +371,10 @@ exports[`swizzle wrap ComponentInFolder/Sibling TS: ComponentInFolder/Sibling.ts import Sibling from '@theme-original/ComponentInFolder/Sibling'; import type SiblingType from '@theme/ComponentInFolder/Sibling'; -type Props = ComponentProps; +type RawProps = ComponentProps; +// ComponentProps is a little buggy, quick fix. +// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 +type Props = unknown extends RawProps ? {} : RawProps; export default function SiblingWrapper(props: Props): JSX.Element { return ( @@ -407,7 +416,10 @@ exports[`swizzle wrap FirstLevelComponent TS: FirstLevelComponent.tsx 1`] = ` import FirstLevelComponent from '@theme-original/FirstLevelComponent'; import type FirstLevelComponentType from '@theme/FirstLevelComponent'; -type Props = ComponentProps; +type RawProps = ComponentProps; +// ComponentProps is a little buggy, quick fix. +// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 +type Props = unknown extends RawProps ? {} : RawProps; export default function FirstLevelComponentWrapper(props: Props): JSX.Element { return ( diff --git a/packages/docusaurus/src/commands/swizzle/actions.ts b/packages/docusaurus/src/commands/swizzle/actions.ts index 44aba75cca13..548de7aedf98 100644 --- a/packages/docusaurus/src/commands/swizzle/actions.ts +++ b/packages/docusaurus/src/commands/swizzle/actions.ts @@ -125,7 +125,10 @@ export async function wrap({ import ${componentName} from '@theme-${importType}/${themeComponentName}'; import type ${componentName}Type from '@theme/${themeComponentName}'; -type Props = ComponentProps; +type RawProps = ComponentProps; +// ComponentProps is a little buggy, quick fix. +// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 +type Props = unknown extends RawProps ? {} : RawProps; export default function ${wrapperComponentName}(props: Props): JSX.Element { return ( From 5c6313bced75c929c5942d9c44c7c4f283d6bdde Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 15 Jun 2022 22:30:06 +0800 Subject: [PATCH 5/7] properly fix --- packages/docusaurus-types/package.json | 1 + packages/docusaurus-types/src/index.d.ts | 22 +++++++++++++ .../__snapshots__/actions.test.ts.snap | 24 ++++++-------- .../__snapshots__/index.test.ts.snap | 32 +++++++------------ .../src/commands/swizzle/actions.ts | 8 ++--- 5 files changed, 47 insertions(+), 40 deletions(-) diff --git a/packages/docusaurus-types/package.json b/packages/docusaurus-types/package.json index f88cdcdb1958..a029043b5181 100644 --- a/packages/docusaurus-types/package.json +++ b/packages/docusaurus-types/package.json @@ -14,6 +14,7 @@ "license": "MIT", "dependencies": { "@types/history": "^4.7.11", + "@types/react": "*", "commander": "^5.1.0", "joi": "^17.6.0", "react-helmet-async": "^1.3.0", diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index 2cae26753d5b..ced9b8bf6fe1 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import type {JSXElementConstructor} from 'react'; import type {RuleSetRule, Configuration as WebpackConfiguration} from 'webpack'; import type {CustomizeRuleString} from 'webpack-merge/dist/types'; import type {CommanderStatic} from 'commander'; @@ -758,3 +759,24 @@ export type UseDataOptions = { */ failfast?: boolean; }; + +/** + * This type is almost the same as `React.ComponentProps`, but with one minor + * fix: when the component is a function with no parameters, it produces `{}` + * instead of `unknown`, allowing us to spread the props derived from another + * component. This is useful for wrap swizzling. + * + * @see https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 + */ +export type WrapperProps< + // eslint-disable-next-line @typescript-eslint/no-explicit-any + T extends keyof JSX.IntrinsicElements | JSXElementConstructor, +> = T extends JSXElementConstructor + ? unknown extends P + ? // eslint-disable-next-line @typescript-eslint/ban-types + {} + : P + : T extends keyof JSX.IntrinsicElements + ? JSX.IntrinsicElements[T] + : // eslint-disable-next-line @typescript-eslint/ban-types + {}; diff --git a/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/actions.test.ts.snap b/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/actions.test.ts.snap index 36fc80b4478f..a6aa55acc13b 100644 --- a/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/actions.test.ts.snap +++ b/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/actions.test.ts.snap @@ -43,14 +43,12 @@ export default function FirstLevelComponentWrapper(props) { `; exports[`wrap TypeScript wrap ComponentInFolder 2`] = ` -"import React, {ComponentProps} from 'react'; +"import React from 'react'; import ComponentInFolder from '@theme-original/ComponentInFolder'; import type ComponentInFolderType from '@theme/ComponentInFolder'; +import type {WrapperProps} from '@docusaurus/types'; -type RawProps = ComponentProps; -// ComponentProps is a little buggy, quick fix. -// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 -type Props = unknown extends RawProps ? {} : RawProps; +type Props = WrapperProps; export default function ComponentInFolderWrapper(props: Props): JSX.Element { return ( @@ -63,14 +61,12 @@ export default function ComponentInFolderWrapper(props: Props): JSX.Element { `; exports[`wrap TypeScript wrap ComponentInFolder/ComponentInSubFolder 2`] = ` -"import React, {ComponentProps} from 'react'; +"import React from 'react'; import ComponentInSubFolder from '@theme-original/ComponentInFolder/ComponentInSubFolder'; import type ComponentInSubFolderType from '@theme/ComponentInFolder/ComponentInSubFolder'; +import type {WrapperProps} from '@docusaurus/types'; -type RawProps = ComponentProps; -// ComponentProps is a little buggy, quick fix. -// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 -type Props = unknown extends RawProps ? {} : RawProps; +type Props = WrapperProps; export default function ComponentInSubFolderWrapper(props: Props): JSX.Element { return ( @@ -83,14 +79,12 @@ export default function ComponentInSubFolderWrapper(props: Props): JSX.Element { `; exports[`wrap TypeScript wrap FirstLevelComponent 2`] = ` -"import React, {ComponentProps} from 'react'; +"import React from 'react'; import FirstLevelComponent from '@theme-original/FirstLevelComponent'; import type FirstLevelComponentType from '@theme/FirstLevelComponent'; +import type {WrapperProps} from '@docusaurus/types'; -type RawProps = ComponentProps; -// ComponentProps is a little buggy, quick fix. -// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 -type Props = unknown extends RawProps ? {} : RawProps; +type Props = WrapperProps; export default function FirstLevelComponentWrapper(props: Props): JSX.Element { return ( diff --git a/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap index e1d28fc40529..31fa888fe07c 100644 --- a/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap @@ -273,14 +273,12 @@ exports[`swizzle wrap ComponentInFolder JS: theme dir tree 1`] = ` `; exports[`swizzle wrap ComponentInFolder TS: ComponentInFolder/index.tsx 1`] = ` -"import React, {ComponentProps} from 'react'; +"import React from 'react'; import ComponentInFolder from '@theme-original/ComponentInFolder'; import type ComponentInFolderType from '@theme/ComponentInFolder'; +import type {WrapperProps} from '@docusaurus/types'; -type RawProps = ComponentProps; -// ComponentProps is a little buggy, quick fix. -// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 -type Props = unknown extends RawProps ? {} : RawProps; +type Props = WrapperProps; export default function ComponentInFolderWrapper(props: Props): JSX.Element { return ( @@ -320,14 +318,12 @@ exports[`swizzle wrap ComponentInFolder/ComponentInSubFolder JS: theme dir tree `; exports[`swizzle wrap ComponentInFolder/ComponentInSubFolder TS: ComponentInFolder/ComponentInSubFolder/index.tsx 1`] = ` -"import React, {ComponentProps} from 'react'; +"import React from 'react'; import ComponentInSubFolder from '@theme-original/ComponentInFolder/ComponentInSubFolder'; import type ComponentInSubFolderType from '@theme/ComponentInFolder/ComponentInSubFolder'; +import type {WrapperProps} from '@docusaurus/types'; -type RawProps = ComponentProps; -// ComponentProps is a little buggy, quick fix. -// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 -type Props = unknown extends RawProps ? {} : RawProps; +type Props = WrapperProps; export default function ComponentInSubFolderWrapper(props: Props): JSX.Element { return ( @@ -367,14 +363,12 @@ exports[`swizzle wrap ComponentInFolder/Sibling JS: theme dir tree 1`] = ` `; exports[`swizzle wrap ComponentInFolder/Sibling TS: ComponentInFolder/Sibling.tsx 1`] = ` -"import React, {ComponentProps} from 'react'; +"import React from 'react'; import Sibling from '@theme-original/ComponentInFolder/Sibling'; import type SiblingType from '@theme/ComponentInFolder/Sibling'; +import type {WrapperProps} from '@docusaurus/types'; -type RawProps = ComponentProps; -// ComponentProps is a little buggy, quick fix. -// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 -type Props = unknown extends RawProps ? {} : RawProps; +type Props = WrapperProps; export default function SiblingWrapper(props: Props): JSX.Element { return ( @@ -412,14 +406,12 @@ exports[`swizzle wrap FirstLevelComponent JS: theme dir tree 1`] = ` `; exports[`swizzle wrap FirstLevelComponent TS: FirstLevelComponent.tsx 1`] = ` -"import React, {ComponentProps} from 'react'; +"import React from 'react'; import FirstLevelComponent from '@theme-original/FirstLevelComponent'; import type FirstLevelComponentType from '@theme/FirstLevelComponent'; +import type {WrapperProps} from '@docusaurus/types'; -type RawProps = ComponentProps; -// ComponentProps is a little buggy, quick fix. -// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 -type Props = unknown extends RawProps ? {} : RawProps; +type Props = WrapperProps; export default function FirstLevelComponentWrapper(props: Props): JSX.Element { return ( diff --git a/packages/docusaurus/src/commands/swizzle/actions.ts b/packages/docusaurus/src/commands/swizzle/actions.ts index 548de7aedf98..3bc38febcd75 100644 --- a/packages/docusaurus/src/commands/swizzle/actions.ts +++ b/packages/docusaurus/src/commands/swizzle/actions.ts @@ -121,14 +121,12 @@ export async function wrap({ const toPath = path.resolve(siteDir, THEME_PATH, wrapperFileName); const content = typescript - ? `import React, {ComponentProps} from 'react'; + ? `import React from 'react'; import ${componentName} from '@theme-${importType}/${themeComponentName}'; import type ${componentName}Type from '@theme/${themeComponentName}'; +import type {WrapperProps} from '@docusaurus/types'; -type RawProps = ComponentProps; -// ComponentProps is a little buggy, quick fix. -// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766 -type Props = unknown extends RawProps ? {} : RawProps; +type Props = WrapperProps; export default function ${wrapperComponentName}(props: Props): JSX.Element { return ( From 6c1110a8f19f021195fec85eea763af9c1a6290a Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 15 Jun 2022 23:20:54 +0800 Subject: [PATCH 6/7] require param --- .../src/client/index.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/client/index.ts b/packages/docusaurus-plugin-content-docs/src/client/index.ts index 06b86860dc1c..bf475ee00e9d 100644 --- a/packages/docusaurus-plugin-content-docs/src/client/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/client/index.ts @@ -85,7 +85,7 @@ export const useAllDocsData = (): {[pluginId: string]: GlobalPluginData} => } | undefined) ?? StableEmptyObject; -export const useDocsData = (pluginId?: string): GlobalPluginData => +export const useDocsData = (pluginId: string | undefined): GlobalPluginData => usePluginData('docusaurus-plugin-content-docs', pluginId, { failfast: true, }) as GlobalPluginData; @@ -117,12 +117,12 @@ export function useActivePluginAndVersion( } /** Versions are returned ordered (most recent first). */ -export function useVersions(pluginId?: string): GlobalVersion[] { +export function useVersions(pluginId: string | undefined): GlobalVersion[] { const data = useDocsData(pluginId); return data.versions; } -export function useLatestVersion(pluginId?: string): GlobalVersion { +export function useLatestVersion(pluginId: string | undefined): GlobalVersion { const data = useDocsData(pluginId); return getLatestVersion(data); } @@ -131,13 +131,17 @@ export function useLatestVersion(pluginId?: string): GlobalVersion { * Returns `undefined` on doc-unrelated pages, because there's no version * currently considered as active. */ -export function useActiveVersion(pluginId?: string): GlobalVersion | undefined { +export function useActiveVersion( + pluginId: string | undefined, +): GlobalVersion | undefined { const data = useDocsData(pluginId); const {pathname} = useLocation(); return getActiveVersion(data, pathname); } -export function useActiveDocContext(pluginId?: string): ActiveDocContext { +export function useActiveDocContext( + pluginId: string | undefined, +): ActiveDocContext { const data = useDocsData(pluginId); const {pathname} = useLocation(); return getActiveDocContext(data, pathname); @@ -146,7 +150,7 @@ export function useActiveDocContext(pluginId?: string): ActiveDocContext { * Useful to say "hey, you are not on the latest docs version, please switch" */ export function useDocVersionSuggestions( - pluginId?: string, + pluginId: string | undefined, ): DocVersionSuggestions { const data = useDocsData(pluginId); const {pathname} = useLocation(); From 4e817b0e7c1c5698d1e406630531ac8a51fa06b6 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Wed, 15 Jun 2022 18:56:46 +0200 Subject: [PATCH 7/7] pass explicit plugin ids to docs client hooks --- website/src/components/ConfigTabs.tsx | 4 +++- website/src/components/UpgradeGuide/index.tsx | 10 ++++++---- website/src/pages/versions.tsx | 6 ++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/website/src/components/ConfigTabs.tsx b/website/src/components/ConfigTabs.tsx index d854e5427a64..4a0622a95e60 100644 --- a/website/src/components/ConfigTabs.tsx +++ b/website/src/components/ConfigTabs.tsx @@ -19,12 +19,14 @@ type Props = { presetOptionName: string; }; +const docsPluginId = undefined; // Default docs plugin instance + export default function ConfigTabs({ code, pluginName, presetOptionName, }: Props): JSX.Element { - const versionPath = useActiveVersion()!.path; + const versionPath = useActiveVersion(docsPluginId)!.path; return ( diff --git a/website/src/components/UpgradeGuide/index.tsx b/website/src/components/UpgradeGuide/index.tsx index 2d96203d095e..5cb2f118f1f6 100644 --- a/website/src/components/UpgradeGuide/index.tsx +++ b/website/src/components/UpgradeGuide/index.tsx @@ -17,9 +17,11 @@ import Translate from '@docusaurus/Translate'; import Admonition from '@theme/Admonition'; import CodeBlock from '@theme/CodeBlock'; +const docsPluginId = undefined; // Default docs plugin instance + function PackageJson() { - const latestVersion = useLatestVersion(); - const allVersions = useVersions(); + const latestVersion = useLatestVersion(docsPluginId); + const allVersions = useVersions(docsPluginId); // Only happens in deploy preview / local dev, but still nice const versionName = latestVersion.name === 'current' && allVersions.length > 1 @@ -37,8 +39,8 @@ function PackageJson() { } function VersionNotice() { - const latestVersion = useLatestVersion(); - const activeVersion = useActiveDocContext().activeVersion!; + const latestVersion = useLatestVersion(docsPluginId); + const activeVersion = useActiveDocContext(docsPluginId).activeVersion!; const isBrowser = useIsBrowser(); // It's possible that the user is browsing a snapshot version // which is only detectable once we are in the browser diff --git a/website/src/pages/versions.tsx b/website/src/pages/versions.tsx index 12d042e92eda..5e748d6571c4 100644 --- a/website/src/pages/versions.tsx +++ b/website/src/pages/versions.tsx @@ -17,6 +17,8 @@ import Layout from '@theme/Layout'; import Heading from '@theme/Heading'; import VersionsArchived from '@site/versionsArchived.json'; +const docsPluginId = undefined; // Default docs plugin instance + const VersionsArchivedList = Object.entries(VersionsArchived); function DocumentationLabel() { @@ -37,8 +39,8 @@ export default function Version(): JSX.Element { const { siteConfig: {organizationName, projectName}, } = useDocusaurusContext(); - const versions = useVersions(); - const latestVersion = useLatestVersion(); + const versions = useVersions(docsPluginId); + const latestVersion = useLatestVersion(docsPluginId); const currentVersion = versions.find( (version) => version.name === 'current', )!;