From a08dd9c54726d7ccef3e47f31e7920e4bd6d5dff Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Fri, 29 Oct 2021 08:41:56 +0800 Subject: [PATCH 01/14] Build packages with ESM --- admin/new.docusaurus.io/package.json | 1 + packages/create-docusaurus/package.json | 1 + .../templates/classic-typescript/package.json | 1 + .../templates/classic/package.json | 1 + .../templates/facebook/package.json | 1 + .../docusaurus-cssnano-preset/package.json | 1 + packages/docusaurus-mdx-loader/package.json | 1 + packages/docusaurus-migrate/package.json | 1 + .../package.json | 1 + .../package.json | 1 + .../package.json | 1 + .../package.json | 1 + .../package.json | 1 + packages/docusaurus-plugin-debug/package.json | 1 + .../package.json | 1 + .../package.json | 1 + .../package.json | 1 + packages/docusaurus-plugin-pwa/package.json | 1 + .../docusaurus-plugin-sitemap/package.json | 1 + .../docusaurus-preset-classic/package.json | 1 + .../package.json | 1 + .../docusaurus-theme-classic/package.json | 1 + packages/docusaurus-theme-common/package.json | 1 + .../package.json | 1 + .../package.json | 1 + packages/docusaurus-types/package.json | 1 + packages/docusaurus-utils-common/package.json | 1 + .../docusaurus-utils-validation/package.json | 1 + packages/docusaurus-utils/package.json | 1 + packages/docusaurus/bin/beforeCli.js | 123 +++++++++--------- packages/docusaurus/bin/docusaurus.js | 12 +- packages/docusaurus/package.json | 1 + packages/docusaurus/src/commands/clear.ts | 2 +- packages/docusaurus/src/commands/start.ts | 2 +- packages/docusaurus/src/commands/swizzle.ts | 2 +- packages/lqip-loader/package.json | 1 + packages/stylelint-copyright/package.json | 1 + tsconfig.json | 2 +- website/package.json | 1 + 39 files changed, 106 insertions(+), 70 deletions(-) diff --git a/admin/new.docusaurus.io/package.json b/admin/new.docusaurus.io/package.json index 96805ea0c1ca..03cfb47efbb5 100644 --- a/admin/new.docusaurus.io/package.json +++ b/admin/new.docusaurus.io/package.json @@ -2,6 +2,7 @@ "name": "new.docusaurus.io", "version": "2.0.0-beta.9", "private": true, + "type": "module", "scripts": { "start": "netlify dev" }, diff --git a/packages/create-docusaurus/package.json b/packages/create-docusaurus/package.json index 26f2373694f5..2060a767554c 100755 --- a/packages/create-docusaurus/package.json +++ b/packages/create-docusaurus/package.json @@ -2,6 +2,7 @@ "name": "create-docusaurus", "version": "2.0.0-beta.9", "description": "Create Docusaurus apps easily.", + "type": "module", "repository": { "type": "git", "url": "https://github.com/facebook/docusaurus.git", diff --git a/packages/create-docusaurus/templates/classic-typescript/package.json b/packages/create-docusaurus/templates/classic-typescript/package.json index 28d1e67b8dad..de812a7e0f9f 100644 --- a/packages/create-docusaurus/templates/classic-typescript/package.json +++ b/packages/create-docusaurus/templates/classic-typescript/package.json @@ -2,6 +2,7 @@ "name": "docusaurus-2-classic-typescript-template", "version": "2.0.0-beta.9", "private": true, + "type": "module", "scripts": { "docusaurus": "docusaurus", "start": "docusaurus start", diff --git a/packages/create-docusaurus/templates/classic/package.json b/packages/create-docusaurus/templates/classic/package.json index 3a99aeee0bb9..fbf54b5205f3 100644 --- a/packages/create-docusaurus/templates/classic/package.json +++ b/packages/create-docusaurus/templates/classic/package.json @@ -2,6 +2,7 @@ "name": "docusaurus-2-classic-template", "version": "2.0.0-beta.9", "private": true, + "type": "module", "scripts": { "docusaurus": "docusaurus", "start": "docusaurus start", diff --git a/packages/create-docusaurus/templates/facebook/package.json b/packages/create-docusaurus/templates/facebook/package.json index acaa90466861..21b4801a05be 100644 --- a/packages/create-docusaurus/templates/facebook/package.json +++ b/packages/create-docusaurus/templates/facebook/package.json @@ -2,6 +2,7 @@ "name": "docusaurus-2-facebook-template", "version": "2.0.0-beta.9", "private": true, + "type": "module", "scripts": { "docusaurus": "docusaurus", "start": "docusaurus start", diff --git a/packages/docusaurus-cssnano-preset/package.json b/packages/docusaurus-cssnano-preset/package.json index 15db23aad0da..14bc38218ec8 100644 --- a/packages/docusaurus-cssnano-preset/package.json +++ b/packages/docusaurus-cssnano-preset/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/cssnano-preset", "version": "2.0.0-beta.9", "description": "Advanced cssnano preset for maximum optimization.", + "type": "module", "main": "index.js", "license": "MIT", "publishConfig": { diff --git a/packages/docusaurus-mdx-loader/package.json b/packages/docusaurus-mdx-loader/package.json index 25ee47fa79b6..dfe1cc20fae3 100644 --- a/packages/docusaurus-mdx-loader/package.json +++ b/packages/docusaurus-mdx-loader/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/mdx-loader", "version": "2.0.0-beta.9", "description": "Docusaurus Loader for MDX", + "type": "module", "main": "lib/index.js", "types": "src/types.d.ts", "publishConfig": { diff --git a/packages/docusaurus-migrate/package.json b/packages/docusaurus-migrate/package.json index f71e609aef51..6603eb16aaa0 100644 --- a/packages/docusaurus-migrate/package.json +++ b/packages/docusaurus-migrate/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/migrate", "version": "2.0.0-beta.9", "description": "A CLI tool to migrate from older versions of Docusaurus.", + "type": "module", "main": "lib/index.js", "license": "MIT", "engines": { diff --git a/packages/docusaurus-module-type-aliases/package.json b/packages/docusaurus-module-type-aliases/package.json index 8f4b618c4a0e..8d1537ec48db 100644 --- a/packages/docusaurus-module-type-aliases/package.json +++ b/packages/docusaurus-module-type-aliases/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/module-type-aliases", "version": "2.0.0-beta.9", "description": "Docusaurus module type aliases.", + "type": "module", "types": "./src/index.d.ts", "publishConfig": { "access": "public" diff --git a/packages/docusaurus-plugin-client-redirects/package.json b/packages/docusaurus-plugin-client-redirects/package.json index 396b3dba90c5..001b835bc491 100644 --- a/packages/docusaurus-plugin-client-redirects/package.json +++ b/packages/docusaurus-plugin-client-redirects/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-client-redirects", "version": "2.0.0-beta.9", "description": "Client redirects plugin for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/plugin-client-redirects.d.ts", "scripts": { diff --git a/packages/docusaurus-plugin-content-blog/package.json b/packages/docusaurus-plugin-content-blog/package.json index 53f9202093da..e04df8c23e1b 100644 --- a/packages/docusaurus-plugin-content-blog/package.json +++ b/packages/docusaurus-plugin-content-blog/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-content-blog", "version": "2.0.0-beta.9", "description": "Blog plugin for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/plugin-content-blog.d.ts", "scripts": { diff --git a/packages/docusaurus-plugin-content-docs/package.json b/packages/docusaurus-plugin-content-docs/package.json index 61849c6a1ca0..e32344eca370 100644 --- a/packages/docusaurus-plugin-content-docs/package.json +++ b/packages/docusaurus-plugin-content-docs/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-content-docs", "version": "2.0.0-beta.9", "description": "Docs plugin for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/plugin-content-docs.d.ts", "scripts": { diff --git a/packages/docusaurus-plugin-content-pages/package.json b/packages/docusaurus-plugin-content-pages/package.json index d300663868f1..7c00d66ccf9f 100644 --- a/packages/docusaurus-plugin-content-pages/package.json +++ b/packages/docusaurus-plugin-content-pages/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-content-pages", "version": "2.0.0-beta.9", "description": "Pages plugin for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/plugin-content-pages.d.ts", "scripts": { diff --git a/packages/docusaurus-plugin-debug/package.json b/packages/docusaurus-plugin-debug/package.json index 4a4d357de0f1..a7f854eef8b6 100644 --- a/packages/docusaurus-plugin-debug/package.json +++ b/packages/docusaurus-plugin-debug/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-debug", "version": "2.0.0-beta.9", "description": "Debug plugin for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/types.d.ts", "scripts": { diff --git a/packages/docusaurus-plugin-google-analytics/package.json b/packages/docusaurus-plugin-google-analytics/package.json index 2d4df876ee7e..2866e62dcabd 100644 --- a/packages/docusaurus-plugin-google-analytics/package.json +++ b/packages/docusaurus-plugin-google-analytics/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-google-analytics", "version": "2.0.0-beta.9", "description": "Global analytics (analytics.js) plugin for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/plugin-google-analytics.d.ts", "publishConfig": { diff --git a/packages/docusaurus-plugin-google-gtag/package.json b/packages/docusaurus-plugin-google-gtag/package.json index d95aa9a302e5..5a4ed7225f06 100644 --- a/packages/docusaurus-plugin-google-gtag/package.json +++ b/packages/docusaurus-plugin-google-gtag/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-google-gtag", "version": "2.0.0-beta.9", "description": "Global Site Tag (gtag.js) plugin for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/plugin-google-gtag.d.ts", "scripts": { diff --git a/packages/docusaurus-plugin-ideal-image/package.json b/packages/docusaurus-plugin-ideal-image/package.json index 6e2606ff3331..e2b78c6b8c21 100644 --- a/packages/docusaurus-plugin-ideal-image/package.json +++ b/packages/docusaurus-plugin-ideal-image/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-ideal-image", "version": "2.0.0-beta.9", "description": "Docusaurus Plugin to generate an almost ideal image (responsive, lazy-loading, and low quality placeholder).", + "type": "module", "main": "lib/index.js", "types": "src/plugin-ideal-image.d.ts", "scripts": { diff --git a/packages/docusaurus-plugin-pwa/package.json b/packages/docusaurus-plugin-pwa/package.json index fa7c48b0f82b..34b238ddc385 100644 --- a/packages/docusaurus-plugin-pwa/package.json +++ b/packages/docusaurus-plugin-pwa/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-pwa", "version": "2.0.0-beta.9", "description": "Docusaurus Plugin to add PWA support.", + "type": "module", "main": "src/index.js", "publishConfig": { "access": "public" diff --git a/packages/docusaurus-plugin-sitemap/package.json b/packages/docusaurus-plugin-sitemap/package.json index 742e69227699..620fb2ec26fc 100644 --- a/packages/docusaurus-plugin-sitemap/package.json +++ b/packages/docusaurus-plugin-sitemap/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/plugin-sitemap", "version": "2.0.0-beta.9", "description": "Simple sitemap generation plugin for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/plugin-sitemap.d.ts", "scripts": { diff --git a/packages/docusaurus-preset-classic/package.json b/packages/docusaurus-preset-classic/package.json index 4318abc74b7a..c5ee5d872383 100644 --- a/packages/docusaurus-preset-classic/package.json +++ b/packages/docusaurus-preset-classic/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/preset-classic", "version": "2.0.0-beta.9", "description": "Classic preset for Docusaurus.", + "type": "module", "main": "lib/index.js", "types": "src/preset-classic.d.ts", "scripts": { diff --git a/packages/docusaurus-remark-plugin-npm2yarn/package.json b/packages/docusaurus-remark-plugin-npm2yarn/package.json index f0b9c5c623c0..67c20befef98 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/package.json +++ b/packages/docusaurus-remark-plugin-npm2yarn/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/remark-plugin-npm2yarn", "version": "2.0.0-beta.9", "description": "Remark plugin for converting npm commands to Yarn commands as tabs.", + "type": "module", "main": "src/index.js", "publishConfig": { "access": "public" diff --git a/packages/docusaurus-theme-classic/package.json b/packages/docusaurus-theme-classic/package.json index 581c181970eb..22f6c54b5b5e 100644 --- a/packages/docusaurus-theme-classic/package.json +++ b/packages/docusaurus-theme-classic/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/theme-classic", "version": "2.0.0-beta.9", "description": "Classic theme for Docusaurus", + "type": "module", "main": "lib/index.js", "types": "src/types.d.ts", "publishConfig": { diff --git a/packages/docusaurus-theme-common/package.json b/packages/docusaurus-theme-common/package.json index 56f8b378db13..6025feacbdf3 100644 --- a/packages/docusaurus-theme-common/package.json +++ b/packages/docusaurus-theme-common/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/theme-common", "version": "2.0.0-beta.9", "description": "Common code for Docusaurus themes.", + "type": "module", "main": "./lib/index.js", "types": "./lib/index.d.ts", "scripts": { diff --git a/packages/docusaurus-theme-live-codeblock/package.json b/packages/docusaurus-theme-live-codeblock/package.json index 790f09febf63..6ece439d7d05 100644 --- a/packages/docusaurus-theme-live-codeblock/package.json +++ b/packages/docusaurus-theme-live-codeblock/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/theme-live-codeblock", "version": "2.0.0-beta.9", "description": "Docusaurus live code block component.", + "type": "module", "main": "lib/index.js", "publishConfig": { "access": "public" diff --git a/packages/docusaurus-theme-search-algolia/package.json b/packages/docusaurus-theme-search-algolia/package.json index 3e64f07c68a4..74b8bb750858 100644 --- a/packages/docusaurus-theme-search-algolia/package.json +++ b/packages/docusaurus-theme-search-algolia/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/theme-search-algolia", "version": "2.0.0-beta.9", "description": "Algolia search component for Docusaurus.", + "type": "module", "main": "src/index.js", "publishConfig": { "access": "public" diff --git a/packages/docusaurus-types/package.json b/packages/docusaurus-types/package.json index 8e0b907426bd..4b3fc8e2d939 100644 --- a/packages/docusaurus-types/package.json +++ b/packages/docusaurus-types/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/types", "version": "2.0.0-beta.9", "description": "Common types for Docusaurus packages.", + "type": "module", "types": "./src/index.d.ts", "publishConfig": { "access": "public" diff --git a/packages/docusaurus-utils-common/package.json b/packages/docusaurus-utils-common/package.json index 6d8d82e6d58f..a65386ae849b 100644 --- a/packages/docusaurus-utils-common/package.json +++ b/packages/docusaurus-utils-common/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/utils-common", "version": "2.0.0-beta.9", "description": "Common (Node/Browser) utility functions for Docusaurus packages.", + "type": "module", "main": "./lib/index.js", "types": "./lib/index.d.ts", "scripts": { diff --git a/packages/docusaurus-utils-validation/package.json b/packages/docusaurus-utils-validation/package.json index 92b40fa560f7..22345d3a37d8 100644 --- a/packages/docusaurus-utils-validation/package.json +++ b/packages/docusaurus-utils-validation/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/utils-validation", "version": "2.0.0-beta.9", "description": "Node validation utility functions for Docusaurus packages.", + "type": "module", "main": "./lib/index.js", "types": "./lib/index.d.ts", "scripts": { diff --git a/packages/docusaurus-utils/package.json b/packages/docusaurus-utils/package.json index 8f3febb9b88e..22b5b5fe5ebb 100644 --- a/packages/docusaurus-utils/package.json +++ b/packages/docusaurus-utils/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/utils", "version": "2.0.0-beta.9", "description": "Node utility functions for Docusaurus packages.", + "type": "module", "main": "./lib/index.js", "types": "./lib/index.d.ts", "scripts": { diff --git a/packages/docusaurus/bin/beforeCli.js b/packages/docusaurus/bin/beforeCli.js index 7daa30b8176b..f12537c9596a 100644 --- a/packages/docusaurus/bin/beforeCli.js +++ b/packages/docusaurus/bin/beforeCli.js @@ -5,18 +5,20 @@ * LICENSE file in the root directory of this source tree. */ -const chalk = require('chalk'); -const fs = require('fs-extra'); -const semver = require('semver'); -const path = require('path'); -const updateNotifier = require('update-notifier'); -const boxen = require('boxen'); +import chalk from 'chalk'; +import fs from 'fs-extra'; +import semver from 'semver'; +import path from 'path'; +import updateNotifier from 'update-notifier'; +import boxen from 'boxen'; + +import packageJSON from '../package.json'; const { name, version, engines: {node: requiredVersion}, -} = require('../package.json'); +} = packageJSON; // Notify user if @docusaurus packages is outdated // @@ -64,62 +66,63 @@ function ignoreUpdate(update) { return isCanaryRelease; } -if ( - notifier.config && - notifier.update && - notifier.update.current !== notifier.update.latest -) { - // Because notifier clears cached data after reading it, leading to notifier not consistently displaying the update - // See https://github.com/yeoman/update-notifier/issues/209 - notifier.config.set('update', notifier.update); +(async function () { + if ( + notifier.config && + notifier.update && + notifier.update.current !== notifier.update.latest + ) { + // Because notifier clears cached data after reading it, leading to notifier not consistently displaying the update + // See https://github.com/yeoman/update-notifier/issues/209 + notifier.config.set('update', notifier.update); - if (ignoreUpdate(notifier.update)) { - return; - } + if (ignoreUpdate(notifier.update)) { + return; + } - // eslint-disable-next-line import/no-dynamic-require, global-require - const sitePkg = require(path.resolve(process.cwd(), 'package.json')); - const siteDocusaurusPackagesForUpdate = Object.keys({ - ...sitePkg.dependencies, - ...sitePkg.devDependencies, - }) - .filter((p) => p.startsWith('@docusaurus')) - .map((p) => p.concat('@latest')) - .join(' '); - const isYarnUsed = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock')); - const upgradeCommand = isYarnUsed - ? `yarn upgrade ${siteDocusaurusPackagesForUpdate}` - : `npm i ${siteDocusaurusPackagesForUpdate}`; + const sitePkg = await import(path.resolve(process.cwd(), 'package.json')); + const siteDocusaurusPackagesForUpdate = Object.keys({ + ...sitePkg.dependencies, + ...sitePkg.devDependencies, + }) + .filter((p) => p.startsWith('@docusaurus')) + .map((p) => p.concat('@latest')) + .join(' '); + const isYarnUsed = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock')); + const upgradeCommand = isYarnUsed + ? `yarn upgrade ${siteDocusaurusPackagesForUpdate}` + : `npm i ${siteDocusaurusPackagesForUpdate}`; - const boxenOptions = { - padding: 1, - margin: 1, - align: 'center', - borderColor: 'yellow', - borderStyle: 'round', - }; + const boxenOptions = { + padding: 1, + margin: 1, + align: 'center', + borderColor: 'yellow', + borderStyle: 'round', + }; - const docusaurusUpdateMessage = boxen( - `Update available ${chalk.dim(`${notifier.update.current}`)}${chalk.reset( - ' → ', - )}${chalk.green( - `${notifier.update.latest}`, - )}\n\nTo upgrade Docusaurus packages with the latest version, run the following command:\n${chalk.cyan( - `${upgradeCommand}`, - )}`, - boxenOptions, - ); + const docusaurusUpdateMessage = boxen( + `Update available ${chalk.dim(`${notifier.update.current}`)}${chalk.reset( + ' → ', + )}${chalk.green( + `${notifier.update.latest}`, + )}\n\nTo upgrade Docusaurus packages with the latest version, run the following command:\n${chalk.cyan( + `${upgradeCommand}`, + )}`, + boxenOptions, + ); - console.log(docusaurusUpdateMessage); -} + console.log(docusaurusUpdateMessage); + } -// notify user if node version needs to be updated -if (!semver.satisfies(process.version, requiredVersion)) { - console.log( - chalk.red(`\nMinimum Node version not met :(`) + - chalk.yellow( - `\n\nYou are using Node ${process.version}. We require Node ${requiredVersion} or up!\n`, - ), - ); - process.exit(1); -} + // notify user if node version needs to be updated + if (!semver.satisfies(process.version, requiredVersion)) { + console.log( + chalk.red(`\nMinimum Node version not met :(`) + + chalk.yellow( + `\n\nYou are using Node ${process.version}. We require Node ${requiredVersion} or up!\n`, + ), + ); + process.exit(1); + } +})(); diff --git a/packages/docusaurus/bin/docusaurus.js b/packages/docusaurus/bin/docusaurus.js index 279623514af1..328cd45ec68a 100755 --- a/packages/docusaurus/bin/docusaurus.js +++ b/packages/docusaurus/bin/docusaurus.js @@ -6,10 +6,10 @@ * LICENSE file in the root directory of this source tree. */ -const chalk = require('chalk'); -const fs = require('fs'); -const cli = require('commander'); -const { +import chalk from 'chalk'; +import fs from 'fs'; +import cli from 'commander'; +import { build, swizzle, deploy, @@ -19,9 +19,9 @@ const { clear, writeTranslations, writeHeadingIds, -} = require('../lib'); +} from '../lib'; -require('./beforeCli'); +import './beforeCli'; const resolveDir = (dir = '.') => fs.realpathSync(dir); diff --git a/packages/docusaurus/package.json b/packages/docusaurus/package.json index 4000e8996bd0..1eda2d014930 100644 --- a/packages/docusaurus/package.json +++ b/packages/docusaurus/package.json @@ -3,6 +3,7 @@ "description": "Easy to Maintain Open Source Documentation Websites", "version": "2.0.0-beta.9", "license": "MIT", + "type": "module", "publishConfig": { "access": "public" }, diff --git a/packages/docusaurus/src/commands/clear.ts b/packages/docusaurus/src/commands/clear.ts index 4486ac9b414a..5aa3351f658d 100644 --- a/packages/docusaurus/src/commands/clear.ts +++ b/packages/docusaurus/src/commands/clear.ts @@ -6,7 +6,7 @@ */ import fs from 'fs-extra'; import path from 'path'; -import chalk = require('chalk'); +import chalk from 'chalk'; import {DEFAULT_BUILD_DIR_NAME, GENERATED_FILES_DIR_NAME} from '../constants'; function removePath(fsPath: string) { diff --git a/packages/docusaurus/src/commands/start.ts b/packages/docusaurus/src/commands/start.ts index ed0848feaacc..515267796e4e 100644 --- a/packages/docusaurus/src/commands/start.ts +++ b/packages/docusaurus/src/commands/start.ts @@ -6,7 +6,7 @@ */ import {normalizeUrl, posixPath} from '@docusaurus/utils'; -import chalk = require('chalk'); +import chalk from 'chalk'; import chokidar from 'chokidar'; import HtmlWebpackPlugin from 'html-webpack-plugin'; diff --git a/packages/docusaurus/src/commands/swizzle.ts b/packages/docusaurus/src/commands/swizzle.ts index 49b1bd719564..c78cdaedad03 100644 --- a/packages/docusaurus/src/commands/swizzle.ts +++ b/packages/docusaurus/src/commands/swizzle.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import chalk = require('chalk'); +import chalk from 'chalk'; import fs from 'fs-extra'; import importFresh from 'import-fresh'; import path from 'path'; diff --git a/packages/lqip-loader/package.json b/packages/lqip-loader/package.json index eeac05b615ab..613a78491535 100644 --- a/packages/lqip-loader/package.json +++ b/packages/lqip-loader/package.json @@ -2,6 +2,7 @@ "name": "@docusaurus/lqip-loader", "version": "2.0.0-beta.9", "description": "Low Quality Image Placeholders (LQIP) loader for webpack.", + "type": "module", "main": "lib/index.js", "publishConfig": { "access": "public" diff --git a/packages/stylelint-copyright/package.json b/packages/stylelint-copyright/package.json index 0a39fe10719e..447254765152 100644 --- a/packages/stylelint-copyright/package.json +++ b/packages/stylelint-copyright/package.json @@ -2,6 +2,7 @@ "name": "stylelint-copyright", "version": "2.0.0-beta.9", "description": "Stylelint plugin to check CSS files for a copyright header.", + "type": "module", "main": "index.js", "license": "MIT", "private": true, diff --git a/tsconfig.json b/tsconfig.json index 61e2b2df3107..4008b60bbd02 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "target": "ES2019", - "module": "commonjs", + "module": "ES6", "lib": ["ESNext", "DOM"], "declaration": true, "declarationMap": false, diff --git a/website/package.json b/website/package.json index daf8e430ee27..0f354713e9e6 100644 --- a/website/package.json +++ b/website/package.json @@ -2,6 +2,7 @@ "name": "website", "version": "2.0.0-beta.9", "private": true, + "type": "module", "scripts": { "docusaurus": "docusaurus", "start": "docusaurus start", From 6bb5eea2babf0a47ee75a94a4614f4aef72a67e1 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Fri, 29 Oct 2021 09:09:10 +0800 Subject: [PATCH 02/14] More migration --- generateExamples.js | 7 ++--- jest/polyfills.js | 20 ++++++------ package.json | 4 ++- packages/create-docusaurus/bin/index.js | 14 +++++---- .../templates/classic/docusaurus.config.js | 6 ++-- .../update-code-translations.js | 14 +++++---- .../src/index.js | 31 +++++++++---------- .../src/validateThemeConfig.js | 15 +++------ packages/lqip-loader/src/lqip.ts | 4 +-- packages/lqip-loader/tsconfig.json | 2 +- website/_dogfooding/dogfooding.config.js | 4 +-- website/docusaurus.config-blog-only.js | 2 +- website/docusaurus.config.js | 20 ++++++------ website/waitForCrowdin.js | 2 +- 14 files changed, 72 insertions(+), 73 deletions(-) diff --git a/generateExamples.js b/generateExamples.js index c027b603b2b9..6b831ff6dc16 100644 --- a/generateExamples.js +++ b/generateExamples.js @@ -5,10 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -// eslint-disable-next-line import/no-extraneous-dependencies -const rimraf = require('rimraf'); -const {readFileSync, writeFileSync, readdirSync} = require('fs'); -const {execSync} = require('child_process'); +import rimraf from 'rimraf'; +import {readFileSync, writeFileSync, readdirSync} from 'fs'; +import {execSync} from 'child_process'; // Generate one example per init template // We use those generated examples as CodeSandbox projects diff --git a/jest/polyfills.js b/jest/polyfills.js index 951678076e32..bc98cab09d9c 100644 --- a/jest/polyfills.js +++ b/jest/polyfills.js @@ -5,15 +5,15 @@ * LICENSE file in the root directory of this source tree. */ /* eslint-disable import/no-extraneous-dependencies */ -require('@formatjs/intl-pluralrules/polyfill'); -require('@formatjs/intl-pluralrules/locale-data/en'); -require('@formatjs/intl-pluralrules/locale-data/fr'); +import '@formatjs/intl-pluralrules/polyfill'; +import '@formatjs/intl-pluralrules/locale-data/en'; +import '@formatjs/intl-pluralrules/locale-data/fr'; -require('@formatjs/intl-numberformat/polyfill'); -require('@formatjs/intl-numberformat/locale-data/en'); -require('@formatjs/intl-numberformat/locale-data/fr'); +import '@formatjs/intl-numberformat/polyfill'; +import '@formatjs/intl-numberformat/locale-data/en'; +import '@formatjs/intl-numberformat/locale-data/fr'; -require('@formatjs/intl-datetimeformat/polyfill'); -require('@formatjs/intl-datetimeformat/add-all-tz'); -require('@formatjs/intl-datetimeformat/locale-data/en'); -require('@formatjs/intl-datetimeformat/locale-data/fr'); +import '@formatjs/intl-datetimeformat/polyfill'; +import '@formatjs/intl-datetimeformat/add-all-tz'; +import '@formatjs/intl-datetimeformat/locale-data/en'; +import '@formatjs/intl-datetimeformat/locale-data/fr'; diff --git a/package.json b/package.json index cd9417d802b5..704869422975 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,9 @@ "test:baseUrl": "yarn build:website:baseUrl && yarn serve:website:baseUrl", "lock:update": "npx yarn-deduplicate" }, + "dependencies": { + "rimraf": "^3.0.2" + }, "devDependencies": { "@babel/cli": "^7.15.7", "@babel/core": "^7.12.16", @@ -113,7 +116,6 @@ "react": "^17.0.1", "react-dom": "^17.0.1", "react-test-renderer": "^17.0.2", - "rimraf": "^3.0.2", "serve": "^12.0.1", "stylelint": "^13.10.0", "tslib": "^2.3.1", diff --git a/packages/create-docusaurus/bin/index.js b/packages/create-docusaurus/bin/index.js index 2585601056c2..2d908fa25a52 100755 --- a/packages/create-docusaurus/bin/index.js +++ b/packages/create-docusaurus/bin/index.js @@ -6,12 +6,14 @@ * LICENSE file in the root directory of this source tree. */ -const chalk = require('chalk'); -const semver = require('semver'); -const path = require('path'); -const program = require('commander'); -const {default: init} = require('../lib'); -const requiredVersion = require('../package.json').engines.node; +import chalk from 'chalk'; +import semver from 'semver'; +import path from 'path'; +import program from 'commander'; +import init from '../lib'; +import packageJSON from '../package.json'; + +const requiredVersion = packageJSON.engines.node; if (!semver.satisfies(process.version, requiredVersion)) { console.log( diff --git a/packages/create-docusaurus/templates/classic/docusaurus.config.js b/packages/create-docusaurus/templates/classic/docusaurus.config.js index ba5f8adcb7a6..7f36e0e51e60 100644 --- a/packages/create-docusaurus/templates/classic/docusaurus.config.js +++ b/packages/create-docusaurus/templates/classic/docusaurus.config.js @@ -1,8 +1,8 @@ // @ts-check // Note: type annotations allow type checking and IDEs autocompletion -const lightCodeTheme = require('prism-react-renderer/themes/github'); -const darkCodeTheme = require('prism-react-renderer/themes/dracula'); +import lightCodeTheme from 'prism-react-renderer/themes/github'; +import darkCodeTheme from 'prism-react-renderer/themes/dracula'; /** @type {import('@docusaurus/types').Config} */ const config = { @@ -115,4 +115,4 @@ const config = { }), }; -module.exports = config; +export default config; diff --git a/packages/docusaurus-theme-classic/update-code-translations.js b/packages/docusaurus-theme-classic/update-code-translations.js index b5857991dc63..2c47c31a03d0 100644 --- a/packages/docusaurus-theme-classic/update-code-translations.js +++ b/packages/docusaurus-theme-classic/update-code-translations.js @@ -5,11 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -const chalk = require('chalk'); -const path = require('path'); -const fs = require('fs-extra'); -const globby = require('globby'); -const {mapValues, pickBy, difference, orderBy} = require('lodash'); +import chalk from 'chalk'; +import path from 'path'; +import fs from 'fs-extra'; +import globby from 'globby'; +import {mapValues, pickBy, difference, orderBy} from 'lodash'; const CodeDirPaths = [ path.join(__dirname, 'lib-next'), @@ -55,7 +55,9 @@ async function extractThemeCodeMessages() { globSourceCodeFilePaths, extractAllSourceCodeFileTranslations, // eslint-disable-next-line global-require - } = require('@docusaurus/core/lib/server/translations/translationsExtractor'); + } = await import( + '@docusaurus/core/lib/server/translations/translationsExtractor' + ); const filePaths = (await globSourceCodeFilePaths(CodeDirPaths)).filter( (filePath) => ['.js', '.jsx'].includes(path.extname(filePath)), diff --git a/packages/docusaurus-theme-search-algolia/src/index.js b/packages/docusaurus-theme-search-algolia/src/index.js index e0988535f20a..725ca2b9e116 100644 --- a/packages/docusaurus-theme-search-algolia/src/index.js +++ b/packages/docusaurus-theme-search-algolia/src/index.js @@ -5,21 +5,21 @@ * LICENSE file in the root directory of this source tree. */ -const path = require('path'); -const fs = require('fs'); -const eta = require('eta'); -const {normalizeUrl, getSwizzledComponent} = require('@docusaurus/utils'); -const openSearchTemplate = require('./templates/opensearch'); -const {validateThemeConfig} = require('./validateThemeConfig'); -const {memoize} = require('lodash'); +import {resolve, join} from 'path'; +import {writeFileSync} from 'fs'; +import {compile, defaultConfig} from 'eta'; +import {normalizeUrl, getSwizzledComponent} from '@docusaurus/utils'; +import {trim} from './templates/opensearch'; +import {validateThemeConfig} from './validateThemeConfig'; +import {memoize} from 'lodash'; const getCompiledOpenSearchTemplate = memoize(() => { - return eta.compile(openSearchTemplate.trim()); + return compile(trim()); }); function renderOpenSearchTemplate(data) { const compiled = getCompiledOpenSearchTemplate(); - return compiled(data, eta.defaultConfig); + return compiled(data, defaultConfig); } const OPEN_SEARCH_FILENAME = 'opensearch.xml'; @@ -31,14 +31,13 @@ function theme(context) { } = context; const pageComponent = './theme/SearchPage/index.js'; const pagePath = - getSwizzledComponent(pageComponent) || - path.resolve(__dirname, pageComponent); + getSwizzledComponent(pageComponent) || resolve(__dirname, pageComponent); return { name: 'docusaurus-theme-search-algolia', getThemePath() { - return path.resolve(__dirname, './theme'); + return resolve(__dirname, './theme'); }, getPathsToWatch() { @@ -55,8 +54,8 @@ function theme(context) { async postBuild({outDir}) { try { - fs.writeFileSync( - path.join(outDir, OPEN_SEARCH_FILENAME), + writeFileSync( + join(outDir, OPEN_SEARCH_FILENAME), renderOpenSearchTemplate({ title, url: url + baseUrl, @@ -87,6 +86,6 @@ function theme(context) { }; } -module.exports = theme; +export default theme; -theme.validateThemeConfig = validateThemeConfig; +export {validateThemeConfig}; diff --git a/packages/docusaurus-theme-search-algolia/src/validateThemeConfig.js b/packages/docusaurus-theme-search-algolia/src/validateThemeConfig.js index 8cfe4bfed9e3..c90ba876f358 100644 --- a/packages/docusaurus-theme-search-algolia/src/validateThemeConfig.js +++ b/packages/docusaurus-theme-search-algolia/src/validateThemeConfig.js @@ -5,9 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -const {Joi} = require('@docusaurus/utils-validation'); +import {Joi} from '@docusaurus/utils-validation'; -const DEFAULT_CONFIG = { +export const DEFAULT_CONFIG = { contextualSearch: false, // future: maybe we want to enable this by default // By default, all Docusaurus sites are using the same AppId @@ -16,9 +16,8 @@ const DEFAULT_CONFIG = { searchParameters: {}, }; -exports.DEFAULT_CONFIG = DEFAULT_CONFIG; -const Schema = Joi.object({ +export const Schema = Joi.object({ algolia: Joi.object({ // Docusaurus attributes contextualSearch: Joi.boolean().default(DEFAULT_CONFIG.contextualSearch), @@ -35,11 +34,7 @@ const Schema = Joi.object({ .required() .unknown(), // DocSearch 3 is still alpha: don't validate the rest for now }); -exports.Schema = Schema; -exports.validateThemeConfig = function validateThemeConfig({ - validate, - themeConfig, -}) { +export function validateThemeConfig({validate, themeConfig}) { return validate(Schema, themeConfig); -}; +} diff --git a/packages/lqip-loader/src/lqip.ts b/packages/lqip-loader/src/lqip.ts index 7387cdf6cc4f..029e96ab451c 100644 --- a/packages/lqip-loader/src/lqip.ts +++ b/packages/lqip-loader/src/lqip.ts @@ -9,9 +9,9 @@ import Vibrant from 'node-vibrant'; import path from 'path'; import sharp from 'sharp'; import {toPalette, toBase64} from './utils'; +import packageJSON from '../package.json'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const {version} = require('../package.json'); +const {version} = packageJSON; const ERROR_EXT = `Error: Input file is missing or uses unsupported image format, lqip v${version}`; diff --git a/packages/lqip-loader/tsconfig.json b/packages/lqip-loader/tsconfig.json index f5902ba1089b..7afa256d91fd 100644 --- a/packages/lqip-loader/tsconfig.json +++ b/packages/lqip-loader/tsconfig.json @@ -3,7 +3,7 @@ "compilerOptions": { "incremental": true, "tsBuildInfoFile": "./lib/.tsbuildinfo", - "rootDir": "src", + "rootDir": ".", "outDir": "lib" } } diff --git a/website/_dogfooding/dogfooding.config.js b/website/_dogfooding/dogfooding.config.js index be9960027238..00ab8f1d9fa9 100644 --- a/website/_dogfooding/dogfooding.config.js +++ b/website/_dogfooding/dogfooding.config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +import fs from 'fs'; /** @type {import('@docusaurus/types').PluginConfig[]} */ const dogfoodingPluginInstances = [ @@ -49,4 +49,4 @@ const dogfoodingPluginInstances = [ ], ]; -exports.dogfoodingPluginInstances = dogfoodingPluginInstances; +export {dogfoodingPluginInstances}; diff --git a/website/docusaurus.config-blog-only.js b/website/docusaurus.config-blog-only.js index 33bca3ed33f6..2bbc413e949b 100644 --- a/website/docusaurus.config-blog-only.js +++ b/website/docusaurus.config-blog-only.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -module.exports = { +export default { title: 'Docusaurus blog only!', tagline: 'Build optimized websites quickly, focus on your content', organizationName: 'facebook', diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 0da2f9af6b11..5dc594e5a187 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -6,12 +6,12 @@ */ // @ts-check -const path = require('path'); -const versions = require('./versions.json'); -const math = require('remark-math'); -const katex = require('rehype-katex'); -const VersionsArchived = require('./versionsArchived.json'); -const {dogfoodingPluginInstances} = require('./_dogfooding/dogfooding.config'); +import {resolve} from 'path'; +import versions, {slice} from './versions.json'; +import math from 'remark-math'; +import katex from 'rehype-katex'; +import VersionsArchived from './versionsArchived.json'; +import {dogfoodingPluginInstances} from './_dogfooding/dogfooding.config'; // This probably only makes sense for the beta phase, temporary function getNextBetaVersionName() { @@ -30,7 +30,7 @@ function getNextBetaVersionName() { const allDocHomesPaths = [ '/docs/', '/docs/next/', - ...versions.slice(1).map((version) => `/docs/${version}/`), + ...slice(1).map((version) => `/docs/${version}/`), ]; const isDev = process.env.NODE_ENV === 'development'; @@ -172,7 +172,7 @@ const config = { 'queryString', ], // swRegister: false, - swCustom: path.resolve(__dirname, 'src/sw.js'), + swCustom: resolve(__dirname, 'src/sw.js'), pwaHead: [ { tagName: 'link', @@ -258,7 +258,7 @@ const config = { onlyIncludeVersions: isBuildFast ? ['current'] : !isVersioningDisabled && (isDev || isDeployPreview) - ? ['current', ...versions.slice(0, 2)] + ? ['current', ...slice(0, 2)] : undefined, versions: { current: { @@ -498,4 +498,4 @@ const config = { }), }; -module.exports = config; +export default config; diff --git a/website/waitForCrowdin.js b/website/waitForCrowdin.js index 95947804ea9f..2542ff12a4cf 100644 --- a/website/waitForCrowdin.js +++ b/website/waitForCrowdin.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -const {Translations} = require('@crowdin/crowdin-api-client'); +import {Translations} from '@crowdin/crowdin-api-client'; /* Crowdin does not support concurrent "project builds" (downloads of translations). From 76e6d00b52fdcd581402161ccd7d372729573581 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Sun, 31 Oct 2021 16:56:18 +0800 Subject: [PATCH 03/14] Fix build errors --- .eslintignore | 1 - .gitignore | 1 - .prettierignore | 1 - jest.config.js | 1 - package.json | 2 +- packages/create-docusaurus/src/index.ts | 2 +- .../copyUntypedFiles.js | 17 +++++++------- .../copyUntypedFiles.js | 17 +++++++------- .../docusaurus-theme-classic/babel.config.js | 18 +-------------- .../docusaurus-theme-classic/package.json | 7 +++--- .../docusaurus-theme-classic/src/index.ts | 23 +++++-------------- .../update-code-translations.js | 16 ++++++------- .../copyUntypedFiles.js | 17 +++++++------- packages/docusaurus/copyUntypedFiles.js | 17 +++++++------- packages/docusaurus/src/commands/start.ts | 6 ++--- tsconfig.json | 2 +- 16 files changed, 60 insertions(+), 88 deletions(-) diff --git a/.eslintignore b/.eslintignore index 9990df5fb57e..5e48fc471eb2 100644 --- a/.eslintignore +++ b/.eslintignore @@ -13,7 +13,6 @@ examples/ packages/lqip-loader/lib/ packages/docusaurus/lib/ packages/docusaurus-*/lib/* -packages/docusaurus-*/lib-next/ packages/docusaurus-plugin-ideal-image/copyUntypedFiles.js packages/create-docusaurus/lib/* diff --git a/.gitignore b/.gitignore index 8e116a2e9431..14c75a08fe54 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,6 @@ packages/create-docusaurus/lib/ packages/lqip-loader/lib/ packages/docusaurus/lib/ packages/docusaurus-*/lib/* -packages/docusaurus-*/lib-next/ website/netlifyDeployPreview/* !website/netlifyDeployPreview/index.html diff --git a/.prettierignore b/.prettierignore index d6488c4ee052..7323e35eda9e 100644 --- a/.prettierignore +++ b/.prettierignore @@ -7,7 +7,6 @@ coverage packages/lqip-loader/lib/ packages/docusaurus/lib/ packages/docusaurus-*/lib/* -packages/docusaurus-*/lib-next/ packages/create-docusaurus/lib/* packages/create-docusaurus/templates/*/docusaurus.config.js __fixtures__ diff --git a/jest.config.js b/jest.config.js index 2a7d9159c8a6..5e803b703b09 100644 --- a/jest.config.js +++ b/jest.config.js @@ -17,7 +17,6 @@ const ignorePatterns = [ '/packages/docusaurus-plugin-content-docs/lib', '/packages/docusaurus-plugin-content-pages/lib', '/packages/docusaurus-theme-classic/lib', - '/packages/docusaurus-theme-classic/lib-next', '/packages/docusaurus-migrate/lib', ]; diff --git a/package.json b/package.json index 704869422975..3bdbfe2c7c6f 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "test": "cross-env TZ=UTC jest", "test:build:website": "./admin/scripts/test-release.sh", "watch": "yarn lerna run --parallel watch", - "clear": "(yarn workspace website clear || echo 'Failure while running docusaurus clear') && yarn lerna exec --ignore docusaurus yarn rimraf lib lib-next", + "clear": "(yarn workspace website clear || echo 'Failure while running docusaurus clear') && yarn lerna exec --ignore docusaurus yarn rimraf lib", "test:baseUrl": "yarn build:website:baseUrl && yarn serve:website:baseUrl", "lock:update": "npx yarn-deduplicate" }, diff --git a/packages/create-docusaurus/src/index.ts b/packages/create-docusaurus/src/index.ts index 406acf43effe..0d89f263833d 100755 --- a/packages/create-docusaurus/src/index.ts +++ b/packages/create-docusaurus/src/index.ts @@ -109,7 +109,7 @@ export default async function init( }> = {}, ): Promise { const useYarn = cliOptions.useNpm ? false : hasYarn(); - const templatesDir = path.resolve(__dirname, '../templates'); + const templatesDir = new URL('../templates', import.meta.url).pathname; const templates = readTemplates(templatesDir); const hasTS = (templateName: string) => fs.pathExistsSync( diff --git a/packages/docusaurus-plugin-debug/copyUntypedFiles.js b/packages/docusaurus-plugin-debug/copyUntypedFiles.js index e5555c55368f..877b570c4137 100644 --- a/packages/docusaurus-plugin-debug/copyUntypedFiles.js +++ b/packages/docusaurus-plugin-debug/copyUntypedFiles.js @@ -5,16 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -const path = require('path'); -const fs = require('fs-extra'); +import fs from 'fs-extra'; /** * Copy all untyped and static assets files to lib. */ -const srcDir = path.resolve(__dirname, 'src'); -const libDir = path.resolve(__dirname, 'lib'); -fs.copySync(srcDir, libDir, { - filter(filepath) { - return !/__tests__/.test(filepath) && !/\.tsx?$/.test(filepath); +fs.copySync( + new URL('src', import.meta.url).pathname, + new URL('lib', import.meta.url).pathname, + { + filter(filepath) { + return !/__tests__/.test(filepath) && !/\.tsx?$/.test(filepath); + }, }, -}); +); diff --git a/packages/docusaurus-plugin-ideal-image/copyUntypedFiles.js b/packages/docusaurus-plugin-ideal-image/copyUntypedFiles.js index 3f814cf0990d..877b570c4137 100644 --- a/packages/docusaurus-plugin-ideal-image/copyUntypedFiles.js +++ b/packages/docusaurus-plugin-ideal-image/copyUntypedFiles.js @@ -5,16 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -const path = require('path'); -const fs = require('fs-extra'); +import fs from 'fs-extra'; /** * Copy all untyped and static assets files to lib. */ -const srcDir = path.resolve(__dirname, 'src'); -const libDir = path.resolve(__dirname, 'lib'); -fs.copySync(srcDir, libDir, { - filter(filepath) { - return !/__tests__/.test(filepath) && !/\.ts$/.test(filepath); +fs.copySync( + new URL('src', import.meta.url).pathname, + new URL('lib', import.meta.url).pathname, + { + filter(filepath) { + return !/__tests__/.test(filepath) && !/\.tsx?$/.test(filepath); + }, }, -}); +); diff --git a/packages/docusaurus-theme-classic/babel.config.js b/packages/docusaurus-theme-classic/babel.config.js index 9e75cdf74863..3e3c91806086 100644 --- a/packages/docusaurus-theme-classic/babel.config.js +++ b/packages/docusaurus-theme-classic/babel.config.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -module.exports = { +export default { env: { // USED FOR NODE/RUNTIME // maybe we should differenciate both cases because @@ -14,22 +14,6 @@ module.exports = { presets: [ ['@babel/preset-typescript', {isTSX: true, allExtensions: true}], ], - // Useful to transpile for older node versions - plugins: [ - '@babel/plugin-transform-modules-commonjs', - '@babel/plugin-proposal-nullish-coalescing-operator', - '@babel/plugin-proposal-optional-chaining', - ], - }, - - // USED FOR JS SWIZZLE - // /lib-next folder is used as source to swizzle JS source code - // This JS code is created from TS source code - // This source code should look clean/human readable to be usable - 'lib-next': { - presets: [ - ['@babel/preset-typescript', {isTSX: true, allExtensions: true}], - ], }, }, }; diff --git a/packages/docusaurus-theme-classic/package.json b/packages/docusaurus-theme-classic/package.json index 22f6c54b5b5e..06bc72383042 100644 --- a/packages/docusaurus-theme-classic/package.json +++ b/packages/docusaurus-theme-classic/package.json @@ -15,12 +15,11 @@ }, "license": "MIT", "scripts": { - "build": "tsc --noEmit && yarn babel:lib && yarn babel:lib-next && yarn prettier:lib-next", - "watch": "concurrently --names \"lib,lib-next,tsc\" --kill-others \"yarn babel:lib --watch\" \"yarn babel:lib-next --watch\" \"yarn tsc --watch\"", + "build": "tsc --noEmit && yarn babel:lib && yarn prettier:lib", + "watch": "concurrently --names \"lib,tsc\" --kill-others \"yarn babel:lib --watch\" \"yarn tsc --watch\"", "babel:lib": "cross-env BABEL_ENV=lib babel src -d lib --extensions \".tsx,.ts\" --ignore \"**/*.d.ts\" --copy-files", - "babel:lib-next": "cross-env BABEL_ENV=lib-next babel src -d lib-next --extensions \".tsx,.ts\" --ignore \"**/*.d.ts\" --copy-files", "prettier": "prettier --config ../../.prettierrc --ignore-path ../../.prettierignore --write \"**/*.{js,ts,jsx,tsc}\"", - "prettier:lib-next": "prettier --config ../../.prettierrc --write \"lib-next/**/*.{js,ts,jsx,tsc}\"", + "prettier:lib": "prettier --config ../../.prettierrc --write \"lib/**/*.{js,ts,jsx,tsx}\"", "update-code-translations": "node -e 'require(\"./update-code-translations.js\").run()'" }, "dependencies": { diff --git a/packages/docusaurus-theme-classic/src/index.ts b/packages/docusaurus-theme-classic/src/index.ts index 33fd4fd8d35c..8931e0ffbc54 100644 --- a/packages/docusaurus-theme-classic/src/index.ts +++ b/packages/docusaurus-theme-classic/src/index.ts @@ -8,14 +8,13 @@ import {DocusaurusContext, Plugin} from '@docusaurus/types'; import type {ThemeConfig} from '@docusaurus/theme-common'; import {getTranslationFiles, translateThemeConfig} from './translations'; -import path from 'path'; import {createRequire} from 'module'; import type {AcceptedPlugin, Plugin as PostCssPlugin} from 'postcss'; import rtlcss from 'rtlcss'; import {readDefaultCodeTranslationMessages} from '@docusaurus/utils'; const requireFromDocusaurusCore = createRequire( - require.resolve('@docusaurus/core/package.json'), + '@docusaurus/core/package.json', ); const ContextReplacementPlugin = requireFromDocusaurusCore( 'webpack/lib/ContextReplacementPlugin', @@ -115,22 +114,12 @@ export default function docusaurusThemeClassic( return { name: 'docusaurus-theme-classic', - /* - Does not seem needed: webpack can already hot reload theme files - getPathsToWatch() { - return [ - path.join(__dirname, '..', 'lib'), - path.join(__dirname, '..', 'lib-next'), - ]; - }, - */ - getThemePath() { - return path.join(__dirname, '..', 'lib-next', 'theme'); + return new URL('../lib/theme', import.meta.url).pathname; }, getTypeScriptThemePath() { - return path.resolve(__dirname, '..', 'src', 'theme'); + return new URL('../src/theme', import.meta.url).pathname; }, getTranslationFiles: async () => getTranslationFiles({themeConfig}), @@ -138,7 +127,7 @@ export default function docusaurusThemeClassic( getDefaultCodeTranslationMessages: () => { return readDefaultCodeTranslationMessages({ - dirPath: path.resolve(__dirname, '..', 'codeTranslations'), + dirPath: new URL('../codeTranslations', import.meta.url).pathname, locale: currentLocale, }); }, @@ -146,8 +135,8 @@ export default function docusaurusThemeClassic( getClientModules() { const modules = [ require.resolve(getInfimaCSSFile(direction)), - path.resolve(__dirname, './prism-include-languages'), - path.resolve(__dirname, './admonitions.css'), + new URL('./prism-include-languages', import.meta.url).pathname, + new URL('./admonitions.css', import.meta.url).pathname, ]; if (customCss) { diff --git a/packages/docusaurus-theme-classic/update-code-translations.js b/packages/docusaurus-theme-classic/update-code-translations.js index 2c47c31a03d0..9c75bd2435ff 100644 --- a/packages/docusaurus-theme-classic/update-code-translations.js +++ b/packages/docusaurus-theme-classic/update-code-translations.js @@ -12,12 +12,12 @@ import globby from 'globby'; import {mapValues, pickBy, difference, orderBy} from 'lodash'; const CodeDirPaths = [ - path.join(__dirname, 'lib-next'), + new URL('lib', import.meta.url).pathname, // TODO other themes should rather define their own translations in the future? - path.join(__dirname, '..', 'docusaurus-theme-common', 'lib'), - path.join(__dirname, '..', 'docusaurus-theme-search-algolia', 'src', 'theme'), - path.join(__dirname, '..', 'docusaurus-theme-live-codeblock', 'src', 'theme'), - path.join(__dirname, '..', 'docusaurus-plugin-pwa', 'src', 'theme'), + new URL('../docusaurus-theme-common/lib', import.meta.url).pathname, + new URL('../docusaurus-theme-search-algolia/lib', import.meta.url).pathname, + new URL('../docusaurus-theme-live-codeblock/lib', import.meta.url).pathname, + new URL('../docusaurus-plugin-pwa/lib', import.meta.url).pathname, ]; console.log('Will scan folders for code translations:', CodeDirPaths); @@ -108,7 +108,8 @@ async function writeMessagesFile(filePath, messages) { } async function getCodeTranslationFiles() { - const codeTranslationsDir = path.join(__dirname, 'codeTranslations'); + const codeTranslationsDir = new URL('codeTranslations', import.meta.url) + .pathname; const baseFile = path.join(codeTranslationsDir, 'base.json'); const localesFiles = (await globby(codeTranslationsDir)).filter( (filepath) => @@ -241,5 +242,4 @@ function run() { ); } -exports.run = run; -exports.extractThemeCodeMessages = extractThemeCodeMessages; +export {run, extractThemeCodeMessages}; diff --git a/packages/docusaurus-theme-common/copyUntypedFiles.js b/packages/docusaurus-theme-common/copyUntypedFiles.js index e5555c55368f..877b570c4137 100644 --- a/packages/docusaurus-theme-common/copyUntypedFiles.js +++ b/packages/docusaurus-theme-common/copyUntypedFiles.js @@ -5,16 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -const path = require('path'); -const fs = require('fs-extra'); +import fs from 'fs-extra'; /** * Copy all untyped and static assets files to lib. */ -const srcDir = path.resolve(__dirname, 'src'); -const libDir = path.resolve(__dirname, 'lib'); -fs.copySync(srcDir, libDir, { - filter(filepath) { - return !/__tests__/.test(filepath) && !/\.tsx?$/.test(filepath); +fs.copySync( + new URL('src', import.meta.url).pathname, + new URL('lib', import.meta.url).pathname, + { + filter(filepath) { + return !/__tests__/.test(filepath) && !/\.tsx?$/.test(filepath); + }, }, -}); +); diff --git a/packages/docusaurus/copyUntypedFiles.js b/packages/docusaurus/copyUntypedFiles.js index e5555c55368f..877b570c4137 100644 --- a/packages/docusaurus/copyUntypedFiles.js +++ b/packages/docusaurus/copyUntypedFiles.js @@ -5,16 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -const path = require('path'); -const fs = require('fs-extra'); +import fs from 'fs-extra'; /** * Copy all untyped and static assets files to lib. */ -const srcDir = path.resolve(__dirname, 'src'); -const libDir = path.resolve(__dirname, 'lib'); -fs.copySync(srcDir, libDir, { - filter(filepath) { - return !/__tests__/.test(filepath) && !/\.tsx?$/.test(filepath); +fs.copySync( + new URL('src', import.meta.url).pathname, + new URL('lib', import.meta.url).pathname, + { + filter(filepath) { + return !/__tests__/.test(filepath) && !/\.tsx?$/.test(filepath); + }, }, -}); +); diff --git a/packages/docusaurus/src/commands/start.ts b/packages/docusaurus/src/commands/start.ts index 515267796e4e..446b8fbd3a47 100644 --- a/packages/docusaurus/src/commands/start.ts +++ b/packages/docusaurus/src/commands/start.ts @@ -133,10 +133,10 @@ export default async function start( plugins: [ // Generates an `index.html` file with the