Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade to TS 4.7, compile with NodeNext #7586

Merged
merged 7 commits into from Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions .eslintrc.js
Expand Up @@ -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',
Expand Down
2 changes: 0 additions & 2 deletions __tests__/validate-tsconfig.test.ts
Expand Up @@ -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(),
},
),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
}
}
Expand Up @@ -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": [
Expand Down
1 change: 0 additions & 1 deletion packages/create-docusaurus/tsconfig.build.json
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"noEmit": false,
"composite": true,
"module": "es2020",
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
Expand Down
1 change: 0 additions & 1 deletion packages/create-docusaurus/tsconfig.json
Expand Up @@ -3,7 +3,6 @@
"references": [{"path": "./tsconfig.build.json"}],
"compilerOptions": {
"noEmit": true,
"module": "esnext",
"rootDir": "."
},
"include": ["bin"],
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-cssnano-preset/tsconfig.json
Expand Up @@ -4,7 +4,6 @@
"noEmit": false,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"module": "commonjs",
"rootDir": "src",
"outDir": "lib"
},
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-logger/tsconfig.json
Expand Up @@ -6,7 +6,6 @@
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"sourceMap": true,
"declarationMap": true,
"module": "commonjs",
"rootDir": "src",
"outDir": "lib"
},
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-mdx-loader/tsconfig.json
Expand Up @@ -6,7 +6,6 @@
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"sourceMap": true,
"declarationMap": true,
"module": "commonjs",
"rootDir": "src",
"outDir": "lib",
"types": []
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-migrate/tsconfig.build.json
Expand Up @@ -5,7 +5,6 @@
"composite": true,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"module": "commonjs",
"rootDir": "src",
"outDir": "lib"
},
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-migrate/tsconfig.json
Expand Up @@ -3,7 +3,6 @@
"references": [{"path": "./tsconfig.build.json"}],
"compilerOptions": {
"noEmit": true,
"module": "esnext",
"rootDir": "."
},
"include": ["bin"],
Expand Down
16 changes: 16 additions & 0 deletions 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;
}
Expand Up @@ -4,7 +4,6 @@
"noEmit": false,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"module": "commonjs",
"rootDir": "src",
"outDir": "lib"
},
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-content-blog/tsconfig.json
Expand Up @@ -4,7 +4,6 @@
"noEmit": false,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"module": "commonjs",
"rootDir": "src",
"outDir": "lib"
},
Expand Down
15 changes: 12 additions & 3 deletions packages/docusaurus-plugin-content-docs/package.json
Expand Up @@ -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": {
Expand Down
84 changes: 66 additions & 18 deletions packages/docusaurus-plugin-content-docs/src/client/index.ts
Expand Up @@ -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 = {};
Expand All @@ -40,7 +85,7 @@ export const useAllDocsData = (): {[pluginId: string]: GlobalPluginData} =>
}
| undefined) ?? StableEmptyObject;

export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
export const useDocsData = (pluginId?: string): GlobalPluginData =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using | undefined is done on purpose to prevent useDocsData() calls: if the pluginId does not matter in a given context, user should be explicit and use useDocsData(undefined)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types exposed in the declaration that I just removed had pluginId?: string though 😄 Should I correct this here? I also feel like it's largely unsafe, but if you are not using multi-instance, omitting the ID is very convenient.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep this | undefined for now, just for us, considering those client APIs are not documented

usePluginData('docusaurus-plugin-content-docs', pluginId, {
failfast: true,
}) as GlobalPluginData;
Expand Down Expand Up @@ -71,34 +116,37 @@ export function useActivePluginAndVersion(
};
}

export function useVersions(pluginId: string | undefined): GlobalVersion[] {
/** Versions are returned ordered (most recent first). */
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);
}

export function useActiveVersion(
pluginId: string | undefined,
): GlobalVersion | undefined {
/**
* Returns `undefined` on doc-unrelated pages, because there's no version
* currently considered as active.
*/
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);
}

/**
* 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();
Expand Down
Expand Up @@ -616,89 +616,3 @@ declare module '@theme/DocPage' {

export default function DocPage(props: Props): JSX.Element;
}

slorber marked this conversation as resolved.
Show resolved Hide resolved
// 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;
}
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-content-docs/tsconfig.json
Expand Up @@ -5,7 +5,6 @@
"noEmit": false,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"module": "commonjs",
"rootDir": "src",
"outDir": "lib"
},
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-content-pages/tsconfig.json
Expand Up @@ -4,7 +4,6 @@
"noEmit": false,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"module": "commonjs",
"rootDir": "src",
"outDir": "lib"
},
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-debug/tsconfig.json
Expand Up @@ -5,7 +5,6 @@
"noEmit": false,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"module": "commonjs",
"rootDir": "src",
"outDir": "lib"
},
Expand Down