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

feat(core): support TypeScript + ESM configuration #9317

Merged
merged 49 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
3f54f4f
feat: support typescript configuration
harryzcy Sep 17, 2023
b9db574
Apply suggestions from code review
harryzcy Sep 19, 2023
b88e1c8
fix: handle function typed config
harryzcy Sep 19, 2023
bb5b90a
Merge branch 'main' into config-in-ts
harryzcy Sep 24, 2023
0f66191
chore: remove other usage of `import-fresh`
harryzcy Sep 24, 2023
8f48f9d
chore: remove `import-fresh` in package.json
harryzcy Sep 24, 2023
138e25e
fix: fix tests related to config loading
harryzcy Sep 24, 2023
901c95e
fix: test case when config is promise
harryzcy Sep 25, 2023
3913aa8
chore: add testcase for typescript
harryzcy Sep 25, 2023
b9a12d3
Merge branch 'main' into config-in-ts
slorber Oct 12, 2023
6932057
Attempt to use TS sidebar
slorber Oct 12, 2023
201c874
Hide jiti behind Docusaurus utils loadFreshModule abstraction
slorber Oct 13, 2023
a7752ab
Merge branch 'main' into config-in-ts
slorber Oct 13, 2023
7af9f72
Add more loadSiteConfig test fixtures
slorber Oct 13, 2023
1e4d95e
scaffold loadFreshModule tests
slorber Oct 13, 2023
2c4a2e7
loadFreshModule tests for CJS / ESM
slorber Oct 13, 2023
2f522b2
add loadFreshModule tests
slorber Oct 13, 2023
5468bf9
Migrate templates to ESM
slorber Oct 13, 2023
d187645
Refactor init templates to ESM + TS
slorber Oct 13, 2023
5b120e6
add @docusaurus/types to templates by default
slorber Oct 13, 2023
80fb76d
refactor TS docs
slorber Oct 13, 2023
bad83af
update docs to ESM
slorber Oct 13, 2023
4a36942
remove useless require.resolve from docs
slorber Oct 13, 2023
81e2ec6
do not show CJS usage of remark-math plugin because it doesn't work a…
slorber Oct 13, 2023
5c764a3
docs cleanups + use consistant module quotes in docs
slorber Oct 13, 2023
4f9047c
docs cleanups
slorber Oct 13, 2023
a646e41
refactor docs pages for ESM/TS support
slorber Oct 13, 2023
9ac8565
add ts config loading test
slorber Oct 13, 2023
c94aee2
Test support for ESM / TS presets
slorber Oct 13, 2023
2b72d98
Add plugins loading ESM + TS tests
slorber Oct 13, 2023
d6ea99a
esm config
slorber Oct 13, 2023
6105396
remove useless require.resolve
slorber Oct 13, 2023
0a20fbd
migrate website config to TS
slorber Oct 13, 2023
0fe678b
migrate website config to TS
slorber Oct 13, 2023
2355ade
fix docs Joi import
slorber Oct 13, 2023
88e7d63
fix getSwizzleComponentList docs
slorber Oct 13, 2023
2cf10ee
fix ideal image type consistency
slorber Oct 13, 2023
b165e1d
migrate more website config to TS
slorber Oct 13, 2023
b9248dd
migrate more website config to TS
slorber Oct 13, 2023
71666f2
migrate more website config to TS
slorber Oct 13, 2023
047e394
migrate more website config to TS
slorber Oct 13, 2023
7c1cc5e
fix pwa error
slorber Oct 13, 2023
708c32f
fix weird changelog plugin bug due to using CJS?
slorber Oct 13, 2023
3e53198
more website config refactors
slorber Oct 13, 2023
dd40aea
lint
slorber Oct 13, 2023
729b6f8
refactor mdx plugin docs
slorber Oct 13, 2023
22b7c8c
refactor mdx plugin docs
slorber Oct 13, 2023
4f3e37b
lint
slorber Oct 14, 2023
67c333e
templates should use ESM
slorber Oct 14, 2023
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
11 changes: 0 additions & 11 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,6 @@ async function copyTemplate(
): Promise<void> {
await fs.copy(path.join(templatesDir, 'shared'), dest);

// TypeScript variants will copy duplicate resources like CSS & config from
// base template
if (typescript) {
await fs.copy(template.path, dest, {
filter: async (filePath) =>
(await fs.stat(filePath)).isDirectory() ||
path.extname(filePath) === '.css' ||
path.basename(filePath) === 'docusaurus.config.js',
});
}

await fs.copy(typescript ? template.tsVariantPath! : template.path, dest, {
// Symlinks don't exist in published npm packages anymore, so this is only
// to prevent errors during local testing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {themes as prismThemes} from 'prism-react-renderer';
import type {Config} from '@docusaurus/types';
import type * as Preset from '@docusaurus/preset-classic';

const config: Config = {
title: 'My Site',
tagline: 'Dinosaurs are cool',
favicon: 'img/favicon.ico',

// Set the production url of your site here
url: 'https://your-docusaurus-site.example.com',
// Set the /<baseUrl>/ pathname under which your site is served
// For GitHub pages deployment, it is often '/<projectName>/'
baseUrl: '/',

// GitHub pages deployment config.
// If you aren't using GitHub pages, you don't need these.
organizationName: 'facebook', // Usually your GitHub org/user name.
projectName: 'docusaurus', // Usually your repo name.

onBrokenLinks: 'throw',
onBrokenMarkdownLinks: 'warn',

// Even if you don't use internationalization, you can use this field to set
// useful metadata like html lang. For example, if your site is Chinese, you
// may want to replace "en" with "zh-Hans".
i18n: {
defaultLocale: 'en',
locales: ['en'],
},

presets: [
[
'classic',
{
docs: {
sidebarPath: './sidebars.ts',
// Please change this to your repo.
// Remove this to remove the "edit this page" links.
editUrl:
'https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/',
},
blog: {
showReadingTime: true,
// Please change this to your repo.
// Remove this to remove the "edit this page" links.
editUrl:
'https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/',
},
theme: {
customCss: './src/css/custom.css',
},
} satisfies Preset.Options,
],
],

themeConfig: {
// Replace with your project's social card
image: 'img/docusaurus-social-card.jpg',
navbar: {
title: 'My Site',
logo: {
alt: 'My Site Logo',
src: 'img/logo.svg',
},
items: [
{
type: 'docSidebar',
sidebarId: 'tutorialSidebar',
position: 'left',
label: 'Tutorial',
},
{to: '/blog', label: 'Blog', position: 'left'},
{
href: 'https://github.com/facebook/docusaurus',
label: 'GitHub',
position: 'right',
},
],
},
footer: {
style: 'dark',
links: [
{
title: 'Docs',
items: [
{
label: 'Tutorial',
to: '/docs/intro',
},
],
},
{
title: 'Community',
items: [
{
label: 'Stack Overflow',
href: 'https://stackoverflow.com/questions/tagged/docusaurus',
},
{
label: 'Discord',
href: 'https://discordapp.com/invite/docusaurus',
},
{
label: 'Twitter',
href: 'https://twitter.com/docusaurus',
},
],
},
{
title: 'More',
items: [
{
label: 'Blog',
to: '/blog',
},
{
label: 'GitHub',
href: 'https://github.com/facebook/docusaurus',
},
],
},
],
copyright: `Copyright © ${new Date().getFullYear()} My Project, Inc. Built with Docusaurus.`,
},
prism: {
theme: prismThemes.github,
darkTheme: prismThemes.dracula,
},
} satisfies Preset.ThemeConfig,
};

export default config;
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"devDependencies": {
"@docusaurus/module-type-aliases": "3.0.0-beta.0",
"@docusaurus/tsconfig": "3.0.0-beta.0",
"@docusaurus/types": "3.0.0-beta.0",
"typescript": "~5.2.2"
},
"browserslist": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type {SidebarsConfig} from '@docusaurus/plugin-content-docs';

/**
* Creating a sidebar enables you to:
- create an ordered group of docs
- render a sidebar for each doc of that group
- provide next/previous navigation
The sidebars can be generated from the filesystem, or explicitly defined here.
Create as many sidebars as you want.
*/
const sidebars: SidebarsConfig = {
// By default, Docusaurus generates a sidebar from the docs folder structure
tutorialSidebar: [{type: 'autogenerated', dirName: '.'}],

// But you can create a sidebar manually
/*
tutorialSidebar: [
'intro',
'hello',
{
type: 'category',
label: 'Tutorial',
items: ['tutorial-basics/create-a-document'],
},
],
*/
};

export default sidebars;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
// There are various equivalent ways to declare your Docusaurus config.
// See: https://docusaurus.io/docs/api/docusaurus-config

const {themes} = require('prism-react-renderer');

const lightCodeTheme = themes.github;
const darkCodeTheme = themes.dracula;
import {themes as prismThemes} from 'prism-react-renderer';

/** @type {import('@docusaurus/types').Config} */
const config = {
Expand Down Expand Up @@ -43,7 +40,7 @@ const config = {
/** @type {import('@docusaurus/preset-classic').Options} */
({
docs: {
sidebarPath: require.resolve('./sidebars.js'),
sidebarPath: './sidebars.js',
// Please change this to your repo.
// Remove this to remove the "edit this page" links.
editUrl:
Expand All @@ -57,7 +54,7 @@ const config = {
'https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/',
},
theme: {
customCss: require.resolve('./src/css/custom.css'),
customCss: './src/css/custom.css',
},
}),
],
Expand Down Expand Up @@ -135,10 +132,10 @@ const config = {
copyright: `Copyright © ${new Date().getFullYear()} My Project, Inc. Built with Docusaurus.`,
},
prism: {
theme: lightCodeTheme,
darkTheme: darkCodeTheme,
theme: prismThemes.github,
darkTheme: prismThemes.dracula,
},
}),
};

module.exports = config;
export default config;
3 changes: 2 additions & 1 deletion packages/create-docusaurus/templates/classic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"react-dom": "^18.0.0"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "3.0.0-beta.0"
"@docusaurus/module-type-aliases": "3.0.0-beta.0",
"@docusaurus/types": "3.0.0-beta.0"
},
"browserslist": {
"production": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ const sidebars = {
*/
};

module.exports = sidebars;
export default sidebars;
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ This is my **first Docusaurus document**!
It is also possible to create your sidebar explicitly in `sidebars.js`:

```js title="sidebars.js"
module.exports = {
export default {
tutorialSidebar: [
'intro',
// highlight-next-line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ To navigate seamlessly across versions, add a version dropdown.
Modify the `docusaurus.config.js` file:

```js title="docusaurus.config.js"
module.exports = {
export default {
themeConfig: {
navbar: {
items: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Let's translate `docs/intro.md` to French.
Modify `docusaurus.config.js` to add support for the `fr` locale:

```js title="docusaurus.config.js"
module.exports = {
export default {
i18n: {
defaultLocale: 'en',
locales: ['en', 'fr'],
Expand Down Expand Up @@ -54,7 +54,7 @@ To navigate seamlessly across languages, add a locale dropdown.
Modify the `docusaurus.config.js` file:

```js title="docusaurus.config.js"
module.exports = {
export default {
themeConfig: {
navbar: {
items: [
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-content-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@types/react-router-config": "^5.0.7",
"combine-promises": "^1.1.0",
"fs-extra": "^11.1.1",
"import-fresh": "^3.3.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"tslib": "^2.6.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
readVersionsFile,
} from './versions/files';
import {validateVersionName} from './versions/validation';
import {loadSidebarsFileUnsafe} from './sidebars';
import {loadSidebarsFile} from './sidebars';
import {CURRENT_VERSION_NAME} from './constants';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
import type {LoadContext} from '@docusaurus/types';
Expand All @@ -37,7 +37,7 @@ async function createVersionedSidebarFile({
// Note: we don't need the sidebars file to be normalized: it's ok to let
// plugin option changes to impact older, versioned sidebars
// We don't validate here, assuming the user has already built the version
const sidebars = await loadSidebarsFileUnsafe(sidebarPath);
const sidebars = await loadSidebarsFile(sidebarPath);

// Do not create a useless versioned sidebars file if sidebars file is empty
// or sidebars are disabled/false)
Expand Down
19 changes: 15 additions & 4 deletions packages/docusaurus-plugin-content-docs/src/sidebars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import fs from 'fs-extra';
import path from 'path';
import _ from 'lodash';
import logger from '@docusaurus/logger';
import {Globby} from '@docusaurus/utils';
import {loadFreshModule, Globby} from '@docusaurus/utils';
import Yaml from 'js-yaml';
import combinePromises from 'combine-promises';
import importFresh from 'import-fresh';
import {validateSidebars, validateCategoryMetadataFile} from './validation';
import {normalizeSidebars} from './normalization';
import {processSidebars} from './processor';
Expand Down Expand Up @@ -68,7 +67,7 @@ async function readCategoriesMetadata(contentPath: string) {
);
}

export async function loadSidebarsFileUnsafe(
async function loadSidebarsFileUnsafe(
sidebarFilePath: string | false | undefined,
): Promise<SidebarsConfig> {
// false => no sidebars
Expand All @@ -89,7 +88,19 @@ export async function loadSidebarsFileUnsafe(
}

// We don't want sidebars to be cached because of hot reloading.
return importFresh(sidebarFilePath);
const module = await loadFreshModule(sidebarFilePath);

// TODO unsafe, need to refactor and improve validation
return module as SidebarsConfig;
}

export async function loadSidebarsFile(
sidebarFilePath: string | false | undefined,
): Promise<SidebarsConfig> {
const sidebars = await loadSidebarsFileUnsafe(sidebarFilePath);

// TODO unsafe, need to refactor and improve validation
return sidebars as SidebarsConfig;
}

export async function loadSidebars(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ declare module '@docusaurus/plugin-ideal-image' {
*/
disableInDev?: boolean;
};

export type Options = Partial<PluginOptions>;
}

declare module '@theme/IdealImage' {
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export {
PluginVersionInformation,
Preset,
PresetConfig,
PresetConfigDefined,
PresetModule,
OptionValidationContext,
ThemeConfigValidationContext,
Expand Down
8 changes: 3 additions & 5 deletions packages/docusaurus-types/src/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ export type PluginConfig =
| false
| null;

export type PresetConfig =
| string
| [string, {[key: string]: unknown}]
| false
| null;
export type PresetConfigDefined = string | [string, {[key: string]: unknown}];

export type PresetConfig = PresetConfigDefined | false | null;

/**
* - `type: 'package'`, plugin is in a different package.
Expand Down