From e07ebadf6c459a7d0ee98901ab1d4dec25846162 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 8 Dec 2021 21:26:24 +0800 Subject: [PATCH] fix: make Docusaurus PnP strict mode compatible (#6047) Co-authored-by: Kristoffer K. --- .github/workflows/tests-e2e.yml | 8 +- .../templates/classic-typescript/package.json | 5 +- .../templates/classic/package.json | 5 +- .../templates/facebook/package.json | 5 +- packages/docusaurus-mdx-loader/package.json | 2 +- packages/docusaurus-mdx-loader/src/index.ts | 2 +- .../__snapshots__/index.test.ts.snap | 12 +- .../transformImage/__tests__/index.test.ts | 5 +- .../src/remark/transformImage/index.ts | 2 +- .../__snapshots__/index.test.ts.snap | 24 +-- .../transformLinks/__tests__/index.test.ts | 5 +- .../src/remark/transformLinks/index.ts | 2 +- .../package.json | 3 +- .../src/normalizePluginOptions.ts | 2 +- .../package.json | 3 +- .../src/index.ts | 2 +- .../package.json | 3 +- .../src/__tests__/cli.test.ts | 2 +- .../src/__tests__/docs.test.ts | 3 +- .../src/__tests__/index.test.ts | 3 +- .../src/__tests__/versions.test.ts | 2 +- .../docusaurus-plugin-content-docs/src/cli.ts | 2 +- .../src/index.ts | 2 +- .../src/versions.ts | 8 +- .../package.json | 3 +- .../src/index.ts | 2 +- packages/docusaurus-plugin-debug/package.json | 3 +- .../package.json | 3 +- .../package.json | 3 +- .../package.json | 3 +- packages/docusaurus-plugin-pwa/package.json | 2 +- .../docusaurus-plugin-sitemap/package.json | 3 +- .../docusaurus-preset-classic/package.json | 2 +- .../docusaurus-theme-classic/package.json | 2 +- packages/docusaurus-theme-common/package.json | 2 +- .../package.json | 3 +- .../src/theme/Playground/index.js | 2 +- .../package.json | 2 +- .../package.json | 4 +- .../src/validationSchemas.ts | 6 +- packages/docusaurus-utils/package.json | 8 +- .../src/__tests__/webpackUtils.test.ts | 33 ++++ .../src/constants.ts | 0 packages/docusaurus-utils/src/index.ts | 10 +- packages/docusaurus-utils/src/webpackUtils.ts | 144 ++++++++++++++++++ packages/docusaurus/package.json | 1 + packages/docusaurus/src/client/.eslintrc.js | 1 - packages/docusaurus/src/commands/clear.ts | 8 +- .../docusaurus/src/commands/commandUtils.ts | 3 +- packages/docusaurus/src/commands/swizzle.ts | 4 +- .../docusaurus/src/server/configValidation.ts | 2 +- packages/docusaurus/src/server/i18n.ts | 3 +- packages/docusaurus/src/server/index.ts | 10 +- .../docusaurus/src/server/plugins/index.ts | 3 +- .../docusaurus/src/server/plugins/init.ts | 2 +- .../src/server/plugins/pluginIds.ts | 2 +- .../docusaurus/src/server/themes/index.ts | 2 +- .../__tests__/translationsExtractor.test.ts | 2 +- .../translations/translationsExtractor.ts | 2 +- .../src/webpack/__tests__/base.test.ts | 5 +- .../src/webpack/__tests__/utils.test.ts | 26 ---- packages/docusaurus/src/webpack/base.ts | 3 +- packages/docusaurus/src/webpack/server.ts | 2 +- packages/docusaurus/src/webpack/utils.ts | 134 +--------------- yarn.lock | 6 +- 65 files changed, 304 insertions(+), 269 deletions(-) create mode 100644 packages/docusaurus-utils/src/__tests__/webpackUtils.test.ts rename packages/{docusaurus => docusaurus-utils}/src/constants.ts (100%) create mode 100644 packages/docusaurus-utils/src/webpackUtils.ts diff --git a/.github/workflows/tests-e2e.yml b/.github/workflows/tests-e2e.yml index a45bdc8f8ac4..ddd86aa0f822 100644 --- a/.github/workflows/tests-e2e.yml +++ b/.github/workflows/tests-e2e.yml @@ -72,17 +72,17 @@ jobs: KEEP_CONTAINER: true - name: Install test-website project with Yarn Berry and nodeLinker = ${{ matrix.nodeLinker }} run: | - # we have to switch to berry first before setting the version we want yarn set version berry - # temporary using canary for #5342 - yarn set version canary yarn config set nodeLinker ${{ matrix.nodeLinker }} - yarn config set pnpMode loose yarn config set npmRegistryServer http://localhost:4873 yarn config set unsafeHttpWhitelist --json '["localhost"]' yarn config set enableGlobalCache true + # Patch package so that peer deps are provided. This has been fixed in terser by making acorn a direct dependency + # TODO watch out for the next terser release. Commit: https://github.com/terser/terser/commit/05b23eeb682d732484ad51b19bf528258fd5dc2a + yarn config set packageExtensions --json '{"terser-webpack-plugin@*": {"dependencies": {"acorn": "^8.6.0"}}, "html-minifier-terser@*": {"dependencies": {"acorn": "^8.6.0"}}}' + yarn install working-directory: ../test-website env: diff --git a/packages/create-docusaurus/templates/classic-typescript/package.json b/packages/create-docusaurus/templates/classic-typescript/package.json index 37ea8c95309b..e7b63fa83000 100644 --- a/packages/create-docusaurus/templates/classic-typescript/package.json +++ b/packages/create-docusaurus/templates/classic-typescript/package.json @@ -18,13 +18,10 @@ "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/preset-classic": "2.0.0-beta.9", "@mdx-js/react": "^1.6.21", - "@svgr/webpack": "^6.0.0", "clsx": "^1.1.1", - "file-loader": "^6.2.0", "prism-react-renderer": "^1.2.1", "react": "^17.0.1", - "react-dom": "^17.0.1", - "url-loader": "^4.1.1" + "react-dom": "^17.0.1" }, "devDependencies": { "@docusaurus/module-type-aliases": "2.0.0-beta.9", diff --git a/packages/create-docusaurus/templates/classic/package.json b/packages/create-docusaurus/templates/classic/package.json index c0fcc74fe1f8..9970b01e1b5a 100644 --- a/packages/create-docusaurus/templates/classic/package.json +++ b/packages/create-docusaurus/templates/classic/package.json @@ -17,13 +17,10 @@ "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/preset-classic": "2.0.0-beta.9", "@mdx-js/react": "^1.6.21", - "@svgr/webpack": "^6.0.0", "clsx": "^1.1.1", - "file-loader": "^6.2.0", "prism-react-renderer": "^1.2.1", "react": "^17.0.1", - "react-dom": "^17.0.1", - "url-loader": "^4.1.1" + "react-dom": "^17.0.1" }, "browserslist": { "production": [ diff --git a/packages/create-docusaurus/templates/facebook/package.json b/packages/create-docusaurus/templates/facebook/package.json index f4f8859f8734..bd23ea78594e 100644 --- a/packages/create-docusaurus/templates/facebook/package.json +++ b/packages/create-docusaurus/templates/facebook/package.json @@ -21,12 +21,9 @@ "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/preset-classic": "2.0.0-beta.9", "@mdx-js/react": "^1.6.21", - "@svgr/webpack": "^6.0.0", "clsx": "^1.1.1", - "file-loader": "^6.2.0", "react": "^17.0.1", - "react-dom": "^17.0.1", - "url-loader": "^4.1.1" + "react-dom": "^17.0.1" }, "devDependencies": { "@babel/eslint-parser": "^7.16.3", diff --git a/packages/docusaurus-mdx-loader/package.json b/packages/docusaurus-mdx-loader/package.json index 6a8ac072604c..4b1e8b8d7702 100644 --- a/packages/docusaurus-mdx-loader/package.json +++ b/packages/docusaurus-mdx-loader/package.json @@ -20,7 +20,6 @@ "dependencies": { "@babel/parser": "^7.16.4", "@babel/traverse": "^7.16.3", - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", "@mdx-js/mdx": "^1.6.21", "@mdx-js/react": "^1.6.21", @@ -32,6 +31,7 @@ "mdast-util-to-string": "^2.0.0", "remark-emoji": "^2.1.0", "stringify-object": "^3.3.0", + "tslib": "^2.3.1", "unist-util-visit": "^2.0.2", "url-loader": "^4.1.1", "webpack": "^5.61.0" diff --git a/packages/docusaurus-mdx-loader/src/index.ts b/packages/docusaurus-mdx-loader/src/index.ts index 2ffb0e415d3c..a37256db8aa2 100644 --- a/packages/docusaurus-mdx-loader/src/index.ts +++ b/packages/docusaurus-mdx-loader/src/index.ts @@ -13,6 +13,7 @@ import { parseFrontMatter, parseMarkdownContentTitle, escapePath, + getFileLoaderUtils, } from '@docusaurus/utils'; import stringifyObject from 'stringify-object'; import headings from './remark/headings'; @@ -20,7 +21,6 @@ import toc from './remark/toc'; import unwrapMdxCodeBlocks from './remark/unwrapMdxCodeBlocks'; import transformImage from './remark/transformImage'; import transformLinks from './remark/transformLinks'; -import {getFileLoaderUtils} from '@docusaurus/core/lib/webpack/utils'; import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader'; import type {LoaderContext} from 'webpack'; diff --git a/packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__snapshots__/index.test.ts.snap index dd2e80abfc26..257aa421a2b0 100644 --- a/packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__snapshots__/index.test.ts.snap @@ -12,17 +12,17 @@ exports[`transformImage plugin pathname protocol 1`] = ` exports[`transformImage plugin transform md images to 1`] = ` "![img](https://example.com/img.png) - + -{\\"img\\"} +{\\"img\\"} -{\\"img +{\\"img -{\\"img +{\\"img -{\\"img\\"} {\\"img\\"} +{\\"img\\"} {\\"img\\"} -{\\"img +{\\"img ## Heading diff --git a/packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/index.test.ts b/packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/index.test.ts index 2e2c06aba3da..fec6f2980bea 100644 --- a/packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/index.test.ts +++ b/packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/index.test.ts @@ -11,6 +11,7 @@ import mdx from 'remark-mdx'; import vfile from 'to-vfile'; import plugin from '../index'; import headings from '../../headings/index'; +import {posixPath} from '@docusaurus/utils'; const processFixture = async (name, options) => { const filePath = path.join(__dirname, `__fixtures__/${name}.md`); @@ -21,7 +22,9 @@ const processFixture = async (name, options) => { .use(plugin, {...options, filePath}) .process(file); - return result.toString(); + return result + .toString() + .replace(new RegExp(posixPath(process.cwd()), 'g'), '[CWD]'); }; const staticDirs = [ diff --git a/packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts b/packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts index 5eb126003978..f72a6442124c 100644 --- a/packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts +++ b/packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts @@ -10,11 +10,11 @@ import path from 'path'; import url from 'url'; import fs from 'fs-extra'; import escapeHtml from 'escape-html'; -import {getFileLoaderUtils} from '@docusaurus/core/lib/webpack/utils'; import { posixPath, escapePath, toMessageRelativeFilePath, + getFileLoaderUtils, } from '@docusaurus/utils'; import type {Plugin, Transformer} from 'unified'; import type {Image, Literal} from 'mdast'; diff --git a/packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__snapshots__/index.test.ts.snap index 4b6b4d89a5db..3d3d22787a86 100644 --- a/packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__snapshots__/index.test.ts.snap @@ -10,13 +10,13 @@ exports[`transformAsset plugin pathname protocol 1`] = ` exports[`transformAsset plugin transform md links to 1`] = ` "[asset](https://example.com/asset.pdf) - + -asset +asset -asset with hash +asset with hash -asset +asset ## Heading @@ -28,20 +28,20 @@ exports[`transformAsset plugin transform md links to 1`] = ` [assets](/github/!file-loader!/assets.pdf) -asset +asset -asset2 +asset2 -staticAsset.pdf +staticAsset.pdf -@site/static/staticAsset.pdf +@site/static/staticAsset.pdf -@site/static/staticAsset.pdf +@site/static/staticAsset.pdf -Just staticAsset.pdf, and awesome staticAsset 2.pdf 'It is really "AWESOME"', but also coded staticAsset 3.pdf +Just staticAsset.pdf, and awesome staticAsset 2.pdf 'It is really "AWESOME"', but also coded staticAsset 3.pdf -{\\"Clickable +{\\"Clickable -Stylized link to asset file +Stylized link to asset file " `; diff --git a/packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/index.test.ts b/packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/index.test.ts index b9eaa062c521..6129510b413b 100644 --- a/packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/index.test.ts +++ b/packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/index.test.ts @@ -11,6 +11,7 @@ import mdx from 'remark-mdx'; import vfile from 'to-vfile'; import plugin from '..'; import transformImage from '../../transformImage'; +import {posixPath} from '@docusaurus/utils'; const processFixture = async (name: string, options?) => { const filePath = path.join(__dirname, `__fixtures__/${name}.md`); @@ -30,7 +31,9 @@ const processFixture = async (name: string, options?) => { }) .process(file); - return result.toString(); + return result + .toString() + .replace(new RegExp(posixPath(process.cwd()), 'g'), '[CWD]'); }; describe('transformAsset plugin', () => { diff --git a/packages/docusaurus-mdx-loader/src/remark/transformLinks/index.ts b/packages/docusaurus-mdx-loader/src/remark/transformLinks/index.ts index 4b2939a1593a..2f06dd6771a6 100644 --- a/packages/docusaurus-mdx-loader/src/remark/transformLinks/index.ts +++ b/packages/docusaurus-mdx-loader/src/remark/transformLinks/index.ts @@ -9,6 +9,7 @@ import { toMessageRelativeFilePath, posixPath, escapePath, + getFileLoaderUtils, } from '@docusaurus/utils'; import visit from 'unist-util-visit'; import path from 'path'; @@ -16,7 +17,6 @@ import url from 'url'; import fs from 'fs-extra'; import escapeHtml from 'escape-html'; import {stringifyContent} from '../utils'; -import {getFileLoaderUtils} from '@docusaurus/core/lib/webpack/utils'; import type {Plugin, Transformer} from 'unified'; import type {Link, Literal} from 'mdast'; diff --git a/packages/docusaurus-plugin-client-redirects/package.json b/packages/docusaurus-plugin-client-redirects/package.json index 5c61b0feb5e2..0939ebd7699e 100644 --- a/packages/docusaurus-plugin-client-redirects/package.json +++ b/packages/docusaurus-plugin-client-redirects/package.json @@ -18,7 +18,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", "@docusaurus/utils-common": "2.0.0-beta.9", "@docusaurus/utils-validation": "2.0.0-beta.9", @@ -29,9 +28,11 @@ "tslib": "^2.3.1" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts b/packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts index c312019137b9..8d3f79a4f259 100644 --- a/packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts +++ b/packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts @@ -7,7 +7,7 @@ import {PluginOptions, RedirectOption, UserPluginOptions} from './types'; import {Joi, PathnameSchema} from '@docusaurus/utils-validation'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; +import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; export const DefaultPluginOptions: PluginOptions = { id: DEFAULT_PLUGIN_ID, // TODO temporary diff --git a/packages/docusaurus-plugin-content-blog/package.json b/packages/docusaurus-plugin-content-blog/package.json index 81ddfc2c4c57..92e4e3bab211 100644 --- a/packages/docusaurus-plugin-content-blog/package.json +++ b/packages/docusaurus-plugin-content-blog/package.json @@ -18,7 +18,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/mdx-loader": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", "@docusaurus/utils-validation": "2.0.0-beta.9", @@ -37,9 +36,11 @@ "webpack": "^5.61.0" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-plugin-content-blog/src/index.ts b/packages/docusaurus-plugin-content-blog/src/index.ts index c06d4bfadcda..723b048bda3a 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.ts +++ b/packages/docusaurus-plugin-content-blog/src/index.ts @@ -16,8 +16,8 @@ import { posixPath, addTrailingPathSeparator, createAbsoluteFilePathMatcher, + DEFAULT_PLUGIN_ID, } from '@docusaurus/utils'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; import {translateContent, getTranslationFiles} from './translations'; import { diff --git a/packages/docusaurus-plugin-content-docs/package.json b/packages/docusaurus-plugin-content-docs/package.json index 723fe36ab0f7..8a2017ea3e0e 100644 --- a/packages/docusaurus-plugin-content-docs/package.json +++ b/packages/docusaurus-plugin-content-docs/package.json @@ -18,7 +18,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/mdx-loader": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", "@docusaurus/utils-validation": "2.0.0-beta.9", @@ -38,6 +37,7 @@ "webpack": "^5.61.0" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/module-type-aliases": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9", "@types/js-yaml": "^4.0.0", @@ -47,6 +47,7 @@ "utility-types": "^3.10.0" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts index ea004a7302b1..a96cdf4c1875 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts @@ -14,7 +14,7 @@ import { getVersionsFilePath, getVersionedSidebarsDirPath, } from '../versions'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; +import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; const fixtureDir = path.join(__dirname, '__fixtures__'); diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts index 9cfbad81bf31..17d343cbd529 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts @@ -26,10 +26,9 @@ import type { DocNavLink, } from '../types'; import type {LoadContext} from '@docusaurus/types'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; import {DEFAULT_OPTIONS} from '../options'; import {Optional} from 'utility-types'; -import {createSlugger, posixPath} from '@docusaurus/utils'; +import {createSlugger, posixPath, DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import {createSidebarsUtils} from '../sidebars/utils'; const fixtureDir = path.join(__dirname, '__fixtures__'); diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts index 858798eb06de..e897b6afaa7b 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts @@ -15,9 +15,8 @@ import pluginContentDocs from '../index'; import {loadContext} from '@docusaurus/core/src/server/index'; import {applyConfigureWebpack} from '@docusaurus/core/src/webpack/utils'; import type {RouteConfig} from '@docusaurus/types'; -import {posixPath} from '@docusaurus/utils'; +import {posixPath, DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import {sortConfig} from '@docusaurus/core/src/server/plugins'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; import * as cliDocs from '../cli'; import {OptionsSchema} from '../options'; diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts index f5a0b808c4c2..68653ddec8af 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts @@ -13,7 +13,7 @@ import { readVersionsMetadata, } from '../versions'; import {DEFAULT_OPTIONS} from '../options'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; +import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import {PluginOptions, VersionMetadata} from '../types'; import type {I18n} from '@docusaurus/types'; diff --git a/packages/docusaurus-plugin-content-docs/src/cli.ts b/packages/docusaurus-plugin-content-docs/src/cli.ts index 50fc5d20d90a..633ee74a8d19 100644 --- a/packages/docusaurus-plugin-content-docs/src/cli.ts +++ b/packages/docusaurus-plugin-content-docs/src/cli.ts @@ -14,7 +14,7 @@ import fs from 'fs-extra'; import path from 'path'; import type {PathOptions, SidebarOptions} from './types'; import {loadSidebarsFile, resolveSidebarPathOption} from './sidebars'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; +import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; function createVersionedSidebarFile({ siteDir, diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index ec5f4c3f9714..d5f6734cdfb5 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -7,7 +7,6 @@ import path from 'path'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; import { normalizeUrl, docuHash, @@ -17,6 +16,7 @@ import { addTrailingPathSeparator, createAbsoluteFilePathMatcher, createSlugger, + DEFAULT_PLUGIN_ID, } from '@docusaurus/utils'; import type {LoadContext, Plugin} from '@docusaurus/types'; import {loadSidebars} from './sidebars'; diff --git a/packages/docusaurus-plugin-content-docs/src/versions.ts b/packages/docusaurus-plugin-content-docs/src/versions.ts index c1151a34f438..6389a9dad44e 100644 --- a/packages/docusaurus-plugin-content-docs/src/versions.ts +++ b/packages/docusaurus-plugin-content-docs/src/versions.ts @@ -21,9 +21,13 @@ import { CURRENT_VERSION_NAME, } from './constants'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; import type {LoadContext} from '@docusaurus/types'; -import {getPluginI18nPath, normalizeUrl, posixPath} from '@docusaurus/utils'; +import { + getPluginI18nPath, + normalizeUrl, + posixPath, + DEFAULT_PLUGIN_ID, +} from '@docusaurus/utils'; import {difference} from 'lodash'; import {resolveSidebarPathOption} from './sidebars'; diff --git a/packages/docusaurus-plugin-content-pages/package.json b/packages/docusaurus-plugin-content-pages/package.json index 93b602635850..6b31c1874039 100644 --- a/packages/docusaurus-plugin-content-pages/package.json +++ b/packages/docusaurus-plugin-content-pages/package.json @@ -18,7 +18,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/mdx-loader": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", "@docusaurus/utils-validation": "2.0.0-beta.9", @@ -28,9 +27,11 @@ "webpack": "^5.61.0" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-plugin-content-pages/src/index.ts b/packages/docusaurus-plugin-content-pages/src/index.ts index 4cc5626d240e..13b9293bcd92 100644 --- a/packages/docusaurus-plugin-content-pages/src/index.ts +++ b/packages/docusaurus-plugin-content-pages/src/index.ts @@ -18,6 +18,7 @@ import { Globby, createAbsoluteFilePathMatcher, normalizeUrl, + DEFAULT_PLUGIN_ID, } from '@docusaurus/utils'; import { LoadContext, @@ -29,7 +30,6 @@ import { import {Configuration} from 'webpack'; import admonitions from 'remark-admonitions'; import {PluginOptionSchema} from './pluginOptionSchema'; -import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; import { PluginOptions, diff --git a/packages/docusaurus-plugin-debug/package.json b/packages/docusaurus-plugin-debug/package.json index d441ed77fb00..576eefd90692 100644 --- a/packages/docusaurus-plugin-debug/package.json +++ b/packages/docusaurus-plugin-debug/package.json @@ -18,16 +18,17 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", "fs-extra": "^10.0.0", "react-json-view": "^1.21.3", "tslib": "^2.3.1" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-plugin-google-analytics/package.json b/packages/docusaurus-plugin-google-analytics/package.json index 5be6bb96f74a..daf384d3eb2e 100644 --- a/packages/docusaurus-plugin-google-analytics/package.json +++ b/packages/docusaurus-plugin-google-analytics/package.json @@ -18,13 +18,14 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/utils-validation": "2.0.0-beta.9" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-plugin-google-gtag/package.json b/packages/docusaurus-plugin-google-gtag/package.json index 599cf606f0ff..7684fb1679a3 100644 --- a/packages/docusaurus-plugin-google-gtag/package.json +++ b/packages/docusaurus-plugin-google-gtag/package.json @@ -18,13 +18,14 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/utils-validation": "2.0.0-beta.9" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-plugin-ideal-image/package.json b/packages/docusaurus-plugin-ideal-image/package.json index 3eaf221cd55d..dcd802cc0a37 100644 --- a/packages/docusaurus-plugin-ideal-image/package.json +++ b/packages/docusaurus-plugin-ideal-image/package.json @@ -21,7 +21,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/lqip-loader": "2.0.0-beta.9", "@docusaurus/responsive-loader": "1.5.0", "@endiliey/react-ideal-image": "^0.0.11", @@ -31,10 +30,12 @@ "webpack": "^5.61.0" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9", "fs-extra": "^10.0.0" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-plugin-pwa/package.json b/packages/docusaurus-plugin-pwa/package.json index 1d74bd71c1b7..246a32d06ee6 100644 --- a/packages/docusaurus-plugin-pwa/package.json +++ b/packages/docusaurus-plugin-pwa/package.json @@ -23,7 +23,6 @@ "@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.0", "@babel/plugin-proposal-optional-chaining": "^7.16.0", "@babel/preset-env": "^7.16.4", - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/theme-common": "2.0.0-beta.9", "@docusaurus/theme-translations": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", @@ -44,6 +43,7 @@ }, "peerDependencies": { "@babel/core": "^7.0.0", + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" } diff --git a/packages/docusaurus-plugin-sitemap/package.json b/packages/docusaurus-plugin-sitemap/package.json index d3e24406596f..c3f4b75f1523 100644 --- a/packages/docusaurus-plugin-sitemap/package.json +++ b/packages/docusaurus-plugin-sitemap/package.json @@ -18,7 +18,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", "@docusaurus/utils-common": "2.0.0-beta.9", "@docusaurus/utils-validation": "2.0.0-beta.9", @@ -27,9 +26,11 @@ "tslib": "^2.3.1" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-preset-classic/package.json b/packages/docusaurus-preset-classic/package.json index 4318abc74b7a..b7ad44007d0b 100644 --- a/packages/docusaurus-preset-classic/package.json +++ b/packages/docusaurus-preset-classic/package.json @@ -18,7 +18,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/plugin-content-blog": "2.0.0-beta.9", "@docusaurus/plugin-content-docs": "2.0.0-beta.9", "@docusaurus/plugin-content-pages": "2.0.0-beta.9", @@ -30,6 +29,7 @@ "@docusaurus/theme-search-algolia": "2.0.0-beta.9" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-theme-classic/package.json b/packages/docusaurus-theme-classic/package.json index 3c371d2ed66c..a130cb66de86 100644 --- a/packages/docusaurus-theme-classic/package.json +++ b/packages/docusaurus-theme-classic/package.json @@ -23,7 +23,6 @@ "update-code-translations": "node -e 'require(\"./update-code-translations.js\").run()'" }, "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/plugin-content-blog": "2.0.0-beta.9", "@docusaurus/plugin-content-docs": "2.0.0-beta.9", "@docusaurus/plugin-content-pages": "2.0.0-beta.9", @@ -56,6 +55,7 @@ "utility-types": "^3.10.0" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-theme-common/package.json b/packages/docusaurus-theme-common/package.json index 115c70bcb33a..0e2ea9caad71 100644 --- a/packages/docusaurus-theme-common/package.json +++ b/packages/docusaurus-theme-common/package.json @@ -18,7 +18,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/plugin-content-blog": "2.0.0-beta.9", "@docusaurus/plugin-content-docs": "2.0.0-beta.9", "@docusaurus/plugin-content-pages": "2.0.0-beta.9", @@ -35,6 +34,7 @@ "lodash": "^4.17.20" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "prism-react-renderer": "^1.2.1", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" diff --git a/packages/docusaurus-theme-live-codeblock/package.json b/packages/docusaurus-theme-live-codeblock/package.json index e54873a6114a..839f6f18cdfc 100644 --- a/packages/docusaurus-theme-live-codeblock/package.json +++ b/packages/docusaurus-theme-live-codeblock/package.json @@ -17,7 +17,6 @@ }, "license": "MIT", "dependencies": { - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/theme-translations": "2.0.0-beta.9", "@docusaurus/utils-validation": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", @@ -29,10 +28,12 @@ "react-live": "2.2.3" }, "devDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/types": "2.0.0-beta.9", "@types/buble": "^0.20.1" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.js b/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.js index 92ee17f67d9a..4826a8868d64 100644 --- a/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.js +++ b/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.js @@ -13,7 +13,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import BrowserOnly from '@docusaurus/BrowserOnly'; import usePrismTheme from '@theme/hooks/usePrismTheme'; import styles from './styles.module.css'; -import useIsBrowser from '@docusaurus/core/lib/client/exports/useIsBrowser'; +import useIsBrowser from '@docusaurus/useIsBrowser'; function Header({children}) { return
{children}
; diff --git a/packages/docusaurus-theme-search-algolia/package.json b/packages/docusaurus-theme-search-algolia/package.json index f3d3ddf936b3..12d62f75cd2f 100644 --- a/packages/docusaurus-theme-search-algolia/package.json +++ b/packages/docusaurus-theme-search-algolia/package.json @@ -21,7 +21,6 @@ }, "dependencies": { "@docsearch/react": "^3.0.0-alpha.39", - "@docusaurus/core": "2.0.0-beta.9", "@docusaurus/theme-common": "2.0.0-beta.9", "@docusaurus/theme-translations": "2.0.0-beta.9", "@docusaurus/utils": "2.0.0-beta.9", @@ -37,6 +36,7 @@ "fs-extra": "^10.0.0" }, "peerDependencies": { + "@docusaurus/core": "2.0.0-beta.9", "react": "^16.8.4 || ^17.0.0", "react-dom": "^16.8.4 || ^17.0.0" }, diff --git a/packages/docusaurus-theme-translations/package.json b/packages/docusaurus-theme-translations/package.json index 483f08153f09..9c29e1b66518 100644 --- a/packages/docusaurus-theme-translations/package.json +++ b/packages/docusaurus-theme-translations/package.json @@ -22,10 +22,10 @@ "update": "node -e 'require(\"./update.js\").run()'" }, "dependencies": { - "fs-extra": "^10.0.0" + "fs-extra": "^10.0.0", + "tslib": "^2.3.1" }, "devDependencies": { - "@docusaurus/core": "2.0.0-beta.9", "chalk": "^4.1.2", "lodash": "^4.17.20" }, diff --git a/packages/docusaurus-utils-validation/src/validationSchemas.ts b/packages/docusaurus-utils-validation/src/validationSchemas.ts index ffb34f9198e3..f137c18fa5f0 100644 --- a/packages/docusaurus-utils-validation/src/validationSchemas.ts +++ b/packages/docusaurus-utils-validation/src/validationSchemas.ts @@ -4,15 +4,15 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + import Joi from './Joi'; -import {isValidPathname} from '@docusaurus/utils'; +import {isValidPathname, DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import type {Tag} from '@docusaurus/utils'; import {JoiFrontMatter} from './JoiFrontMatter'; export const PluginIdSchema = Joi.string() .regex(/^[a-zA-Z_-]+$/) - // duplicate core constant, otherwise cyclic dependency is created :( - .default('default'); + .default(DEFAULT_PLUGIN_ID); const MarkdownPluginsSchema = Joi.array() .items( diff --git a/packages/docusaurus-utils/package.json b/packages/docusaurus-utils/package.json index 39a136fbe3eb..6280979d3a68 100644 --- a/packages/docusaurus-utils/package.json +++ b/packages/docusaurus-utils/package.json @@ -19,8 +19,10 @@ "license": "MIT", "dependencies": { "@mdx-js/runtime": "^1.6.22", + "@svgr/webpack": "^6.0.0", "chalk": "^4.1.2", "escape-string-regexp": "^4.0.0", + "file-loader": "^6.2.0", "fs-extra": "^10.0.0", "github-slugger": "^1.4.0", "globby": "^11.0.4", @@ -30,7 +32,8 @@ "remark-mdx-remove-exports": "^1.6.22", "remark-mdx-remove-imports": "^1.6.22", "resolve-pathname": "^3.0.0", - "tslib": "^2.3.1" + "tslib": "^2.3.1", + "url-loader": "^4.1.1" }, "engines": { "node": ">=14" @@ -45,6 +48,7 @@ }, "peerDependencies": { "react": "*", - "react-dom": "*" + "react-dom": "*", + "webpack": "5.x" } } diff --git a/packages/docusaurus-utils/src/__tests__/webpackUtils.test.ts b/packages/docusaurus-utils/src/__tests__/webpackUtils.test.ts new file mode 100644 index 000000000000..f423fa605873 --- /dev/null +++ b/packages/docusaurus-utils/src/__tests__/webpackUtils.test.ts @@ -0,0 +1,33 @@ +/** + * 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. + */ + +import {getFileLoaderUtils} from '../webpackUtils'; + +describe('getFileLoaderUtils()', () => { + test('plugin svgo/removeViewBox should be disabled', () => { + const {oneOf} = getFileLoaderUtils().rules.svg(); + expect(oneOf[0].use).toContainEqual( + expect.objectContaining({ + loader: require.resolve('@svgr/webpack'), + options: expect.objectContaining({ + svgoConfig: { + plugins: [ + { + name: 'preset-default', + params: { + overrides: { + removeViewBox: false, + }, + }, + }, + ], + }, + }), + }), + ); + }); +}); diff --git a/packages/docusaurus/src/constants.ts b/packages/docusaurus-utils/src/constants.ts similarity index 100% rename from packages/docusaurus/src/constants.ts rename to packages/docusaurus-utils/src/constants.ts diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index ce888de25716..45f216f648fe 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -23,7 +23,9 @@ import resolvePathnameUnsafe from 'resolve-pathname'; import {posixPath as posixPathImport} from './posixPath'; import {simpleHash, docuHash} from './hashUtils'; import {normalizeUrl} from './normalizeUrl'; +import {DEFAULT_PLUGIN_ID} from './constants'; +export * from './constants'; export * from './mdxUtils'; export * from './normalizeUrl'; export * from './tags'; @@ -41,6 +43,7 @@ export { createMatcher, createAbsoluteFilePathMatcher, } from './globUtils'; +export * from './webpackUtils'; const fileHash = new Map(); export async function generate( @@ -282,7 +285,7 @@ export function getPluginI18nPath({ siteDir, locale, pluginName, - pluginId = 'default', // TODO duplicated constant + pluginId = DEFAULT_PLUGIN_ID, subPaths = [], }: { siteDir: string; @@ -298,10 +301,7 @@ export function getPluginI18nPath({ locale, // Make it convenient to use for single-instance // ie: return "docs", not "docs-default" nor "docs/default" - `${pluginName}${ - // TODO duplicate constant :( - pluginId === 'default' ? '' : `-${pluginId}` - }`, + `${pluginName}${pluginId === DEFAULT_PLUGIN_ID ? '' : `-${pluginId}`}`, ...subPaths, ); } diff --git a/packages/docusaurus-utils/src/webpackUtils.ts b/packages/docusaurus-utils/src/webpackUtils.ts new file mode 100644 index 000000000000..858b8175881e --- /dev/null +++ b/packages/docusaurus-utils/src/webpackUtils.ts @@ -0,0 +1,144 @@ +/** + * 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. + */ + +import type {RuleSetRule} from 'webpack'; +import path from 'path'; +import {posixPath} from './posixPath'; +import { + WEBPACK_URL_LOADER_LIMIT, + OUTPUT_STATIC_ASSETS_DIR_NAME, +} from './constants'; + +type AssetFolder = 'images' | 'files' | 'fonts' | 'medias'; + +type FileLoaderUtils = { + loaders: { + file: (options: {folder: AssetFolder}) => RuleSetRule; + url: (options: {folder: AssetFolder}) => RuleSetRule; + inlineMarkdownImageFileLoader: string; + inlineMarkdownLinkFileLoader: string; + }; + rules: { + images: () => RuleSetRule; + fonts: () => RuleSetRule; + media: () => RuleSetRule; + svg: () => RuleSetRule; + otherAssets: () => RuleSetRule; + }; +}; + +// Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447 +export function getFileLoaderUtils(): FileLoaderUtils { + // files/images < urlLoaderLimit will be inlined as base64 strings directly in the html + const urlLoaderLimit = WEBPACK_URL_LOADER_LIMIT; + + // defines the path/pattern of the assets handled by webpack + const fileLoaderFileName = (folder: AssetFolder) => + `${OUTPUT_STATIC_ASSETS_DIR_NAME}/${folder}/[name]-[hash].[ext]`; + + const loaders: FileLoaderUtils['loaders'] = { + file: (options: {folder: AssetFolder}) => ({ + loader: require.resolve(`file-loader`), + options: { + name: fileLoaderFileName(options.folder), + }, + }), + url: (options: {folder: AssetFolder}) => ({ + loader: require.resolve('url-loader'), + options: { + limit: urlLoaderLimit, + name: fileLoaderFileName(options.folder), + fallback: require.resolve('file-loader'), + }, + }), + + // TODO find a better solution to avoid conflicts with the ideal-image plugin + // TODO this may require a little breaking change for ideal-image users? + // Maybe with the ideal image plugin, all md images should be "ideal"? + // This is used to force url-loader+file-loader on markdown images + // https://webpack.js.org/concepts/loaders/#inline + inlineMarkdownImageFileLoader: `!${posixPath( + require.resolve('url-loader'), + )}?limit=${urlLoaderLimit}&name=${fileLoaderFileName( + 'images', + )}&fallback=${posixPath(require.resolve('file-loader'))}!`, + inlineMarkdownLinkFileLoader: `!${posixPath( + require.resolve('file-loader'), + )}?name=${fileLoaderFileName('files')}!`, + }; + + const rules: FileLoaderUtils['rules'] = { + /** + * Loads image assets, inlines images via a data URI if they are below + * the size threshold + */ + images: () => ({ + use: [loaders.url({folder: 'images'})], + test: /\.(ico|jpg|jpeg|png|gif|webp)(\?.*)?$/, + }), + + fonts: () => ({ + use: [loaders.url({folder: 'fonts'})], + test: /\.(woff|woff2|eot|ttf|otf)$/, + }), + + /** + * Loads audio and video and inlines them via a data URI if they are below + * the size threshold + */ + media: () => ({ + use: [loaders.url({folder: 'medias'})], + test: /\.(mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/, + }), + + svg: () => ({ + test: /\.svg?$/, + oneOf: [ + { + use: [ + { + loader: require.resolve('@svgr/webpack'), + options: { + prettier: false, + svgo: true, + svgoConfig: { + plugins: [ + { + name: 'preset-default', + params: { + overrides: { + removeViewBox: false, + }, + }, + }, + ], + }, + titleProp: true, + ref: ![path], + }, + }, + ], + // We don't want to use SVGR loader for non-React source code + // ie we don't want to use SVGR for CSS files... + issuer: { + and: [/\.(ts|tsx|js|jsx|md|mdx)$/], + }, + }, + { + use: [loaders.url({folder: 'images'})], + }, + ], + }), + + otherAssets: () => ({ + use: [loaders.file({folder: 'files'})], + test: /\.(pdf|doc|docx|xls|xlsx|zip|rar)$/, + }), + }; + + return {loaders, rules}; +} diff --git a/packages/docusaurus/package.json b/packages/docusaurus/package.json index d212e5eb44e9..a742b87950ec 100644 --- a/packages/docusaurus/package.json +++ b/packages/docusaurus/package.json @@ -42,6 +42,7 @@ "@babel/runtime-corejs3": "^7.16.3", "@babel/traverse": "^7.16.3", "@docusaurus/cssnano-preset": "2.0.0-beta.9", + "@docusaurus/mdx-loader": "2.0.0-beta.9", "@docusaurus/react-loadable": "5.5.2", "@docusaurus/utils": "2.0.0-beta.9", "@docusaurus/utils-common": "2.0.0-beta.9", diff --git a/packages/docusaurus/src/client/.eslintrc.js b/packages/docusaurus/src/client/.eslintrc.js index 0a60804545c0..5dce52424e15 100644 --- a/packages/docusaurus/src/client/.eslintrc.js +++ b/packages/docusaurus/src/client/.eslintrc.js @@ -21,7 +21,6 @@ module.exports = { '**/../server/**', '**/../commands/**', '**/../webpack/**', - '**/../constants', ], }, ], diff --git a/packages/docusaurus/src/commands/clear.ts b/packages/docusaurus/src/commands/clear.ts index 4486ac9b414a..bd9326e19a00 100644 --- a/packages/docusaurus/src/commands/clear.ts +++ b/packages/docusaurus/src/commands/clear.ts @@ -4,10 +4,14 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + import fs from 'fs-extra'; import path from 'path'; -import chalk = require('chalk'); -import {DEFAULT_BUILD_DIR_NAME, GENERATED_FILES_DIR_NAME} from '../constants'; +import chalk from 'chalk'; +import { + DEFAULT_BUILD_DIR_NAME, + GENERATED_FILES_DIR_NAME, +} from '@docusaurus/utils'; function removePath(fsPath: string) { return fs diff --git a/packages/docusaurus/src/commands/commandUtils.ts b/packages/docusaurus/src/commands/commandUtils.ts index 1a7edbc5b1b0..a496417647f8 100644 --- a/packages/docusaurus/src/commands/commandUtils.ts +++ b/packages/docusaurus/src/commands/commandUtils.ts @@ -4,9 +4,10 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + import choosePort from '../choosePort'; import {HostPortCLIOptions} from '@docusaurus/types'; -import {DEFAULT_PORT} from '../constants'; +import {DEFAULT_PORT} from '@docusaurus/utils'; export function getCLIOptionHost( hostOption: HostPortCLIOptions['host'], diff --git a/packages/docusaurus/src/commands/swizzle.ts b/packages/docusaurus/src/commands/swizzle.ts index 49b1bd719564..ef101d37c373 100644 --- a/packages/docusaurus/src/commands/swizzle.ts +++ b/packages/docusaurus/src/commands/swizzle.ts @@ -5,14 +5,14 @@ * 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'; import {ImportedPluginModule, PluginConfig} from '@docusaurus/types'; import leven from 'leven'; import {partition} from 'lodash'; -import {THEME_PATH} from '../constants'; +import {THEME_PATH} from '@docusaurus/utils'; import {loadContext, loadPluginConfigs} from '../server'; import initPlugins from '../server/plugins/init'; import {normalizePluginOptions} from '@docusaurus/utils-validation'; diff --git a/packages/docusaurus/src/server/configValidation.ts b/packages/docusaurus/src/server/configValidation.ts index d873c497777c..8c123acbb929 100644 --- a/packages/docusaurus/src/server/configValidation.ts +++ b/packages/docusaurus/src/server/configValidation.ts @@ -6,7 +6,7 @@ */ import {DocusaurusConfig, I18nConfig} from '@docusaurus/types'; -import {DEFAULT_CONFIG_FILE_NAME, STATIC_DIR_NAME} from '../constants'; +import {DEFAULT_CONFIG_FILE_NAME, STATIC_DIR_NAME} from '@docusaurus/utils'; import { Joi, logValidationBugReportHint, diff --git a/packages/docusaurus/src/server/i18n.ts b/packages/docusaurus/src/server/i18n.ts index 20bcd06c8c64..a3f935657936 100644 --- a/packages/docusaurus/src/server/i18n.ts +++ b/packages/docusaurus/src/server/i18n.ts @@ -6,9 +6,8 @@ */ import {I18n, DocusaurusConfig, I18nLocaleConfig} from '@docusaurus/types'; import path from 'path'; -import {normalizeUrl} from '@docusaurus/utils'; +import {normalizeUrl, NODE_MAJOR_VERSION} from '@docusaurus/utils'; import {getLangDir} from 'rtl-detect'; -import {NODE_MAJOR_VERSION} from '../constants'; import chalk from 'chalk'; function getDefaultLocaleLabel(locale: string) { diff --git a/packages/docusaurus/src/server/index.ts b/packages/docusaurus/src/server/index.ts index b6a4abf13ab5..beee3c31341c 100644 --- a/packages/docusaurus/src/server/index.ts +++ b/packages/docusaurus/src/server/index.ts @@ -5,15 +5,15 @@ * LICENSE file in the root directory of this source tree. */ -import {generate} from '@docusaurus/utils'; -import path, {join} from 'path'; -import chalk from 'chalk'; -import ssrDefaultTemplate from '../client/templates/ssr.html.template'; import { + generate, DEFAULT_BUILD_DIR_NAME, DEFAULT_CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME, -} from '../constants'; +} from '@docusaurus/utils'; +import path, {join} from 'path'; +import chalk from 'chalk'; +import ssrDefaultTemplate from '../client/templates/ssr.html.template'; import loadClientModules from './client-modules'; import loadConfig from './config'; import {loadPlugins} from './plugins'; diff --git a/packages/docusaurus/src/server/plugins/index.ts b/packages/docusaurus/src/server/plugins/index.ts index 751900e8f1dd..4be498014fd9 100644 --- a/packages/docusaurus/src/server/plugins/index.ts +++ b/packages/docusaurus/src/server/plugins/index.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {generate} from '@docusaurus/utils'; +import {generate, DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import fs from 'fs-extra'; import path from 'path'; import { @@ -21,7 +21,6 @@ import { } from '@docusaurus/types'; import initPlugins from './init'; import chalk from 'chalk'; -import {DEFAULT_PLUGIN_ID} from '../../constants'; import {chain} from 'lodash'; import {localizePluginTranslationFile} from '../translations/translations'; import applyRouteTrailingSlash from './applyRouteTrailingSlash'; diff --git a/packages/docusaurus/src/server/plugins/init.ts b/packages/docusaurus/src/server/plugins/init.ts index 12aedf10b635..4cb26188ee10 100644 --- a/packages/docusaurus/src/server/plugins/init.ts +++ b/packages/docusaurus/src/server/plugins/init.ts @@ -16,7 +16,7 @@ import { PluginOptions, InitializedPlugin, } from '@docusaurus/types'; -import {DEFAULT_PLUGIN_ID} from '../../constants'; +import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import {getPluginVersion} from '../versions'; import {ensureUniquePluginInstanceIds} from './pluginIds'; import { diff --git a/packages/docusaurus/src/server/plugins/pluginIds.ts b/packages/docusaurus/src/server/plugins/pluginIds.ts index ac0238ff3ccb..0b65c7c26fc6 100644 --- a/packages/docusaurus/src/server/plugins/pluginIds.ts +++ b/packages/docusaurus/src/server/plugins/pluginIds.ts @@ -6,7 +6,7 @@ */ import {groupBy} from 'lodash'; -import {DEFAULT_PLUGIN_ID} from '../../constants'; +import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import {InitializedPlugin} from '@docusaurus/types'; // It is forbidden to have 2 plugins of the same name sharing the same id diff --git a/packages/docusaurus/src/server/themes/index.ts b/packages/docusaurus/src/server/themes/index.ts index cc924636783a..e1740405fb32 100644 --- a/packages/docusaurus/src/server/themes/index.ts +++ b/packages/docusaurus/src/server/themes/index.ts @@ -7,7 +7,7 @@ import {ThemeAliases, LoadedPlugin} from '@docusaurus/types'; import path from 'path'; -import {THEME_PATH} from '../../constants'; +import {THEME_PATH} from '@docusaurus/utils'; import themeAlias, {sortAliases} from './alias'; const ThemeFallbackDir = path.resolve(__dirname, '../../client/theme-fallback'); diff --git a/packages/docusaurus/src/server/translations/__tests__/translationsExtractor.test.ts b/packages/docusaurus/src/server/translations/__tests__/translationsExtractor.test.ts index 46d91cc90b60..fcdb4579a434 100644 --- a/packages/docusaurus/src/server/translations/__tests__/translationsExtractor.test.ts +++ b/packages/docusaurus/src/server/translations/__tests__/translationsExtractor.test.ts @@ -14,7 +14,7 @@ import { import {getBabelOptions} from '../../../webpack/utils'; import path from 'path'; import {InitializedPlugin} from '@docusaurus/types'; -import {SRC_DIR_NAME} from '../../../constants'; +import {SRC_DIR_NAME} from '@docusaurus/utils'; const TestBabelOptions = getBabelOptions({ isServer: true, diff --git a/packages/docusaurus/src/server/translations/translationsExtractor.ts b/packages/docusaurus/src/server/translations/translationsExtractor.ts index a0eb5f730350..6a110974d4aa 100644 --- a/packages/docusaurus/src/server/translations/translationsExtractor.ts +++ b/packages/docusaurus/src/server/translations/translationsExtractor.ts @@ -15,7 +15,7 @@ import { TranslationMessage, } from '@docusaurus/types'; import nodePath from 'path'; -import {SRC_DIR_NAME} from '../../constants'; +import {SRC_DIR_NAME} from '@docusaurus/utils'; import {safeGlobby} from '../utils'; // We only support extracting source code translations from these kind of files diff --git a/packages/docusaurus/src/webpack/__tests__/base.test.ts b/packages/docusaurus/src/webpack/__tests__/base.test.ts index 564395e81250..d6ab5ca36e09 100644 --- a/packages/docusaurus/src/webpack/__tests__/base.test.ts +++ b/packages/docusaurus/src/webpack/__tests__/base.test.ts @@ -13,9 +13,10 @@ import { getDocusaurusAliases, createBaseConfig, } from '../base'; -import * as utils from '../utils'; -import {mapValues} from 'lodash'; +// TODO seems to be a bug with how TS does star exports +import * as utils from '@docusaurus/utils/lib/webpackUtils'; import {posixPath} from '@docusaurus/utils'; +import {mapValues} from 'lodash'; import {Props, ThemeAliases} from '@docusaurus/types'; describe('babel transpilation exclude logic', () => { diff --git a/packages/docusaurus/src/webpack/__tests__/utils.test.ts b/packages/docusaurus/src/webpack/__tests__/utils.test.ts index fd54d1bf8d5f..97f527b11337 100644 --- a/packages/docusaurus/src/webpack/__tests__/utils.test.ts +++ b/packages/docusaurus/src/webpack/__tests__/utils.test.ts @@ -12,7 +12,6 @@ import { getCustomizableJSLoader, applyConfigureWebpack, applyConfigurePostCss, - getFileLoaderUtils, } from '../utils'; import { ConfigureWebpackFn, @@ -178,31 +177,6 @@ describe('extending generated webpack config', () => { }); }); -describe('getFileLoaderUtils()', () => { - test('plugin svgo/removeViewBox should be disabled', () => { - const {oneOf} = getFileLoaderUtils().rules.svg(); - expect(oneOf[0].use).toContainEqual( - expect.objectContaining({ - loader: '@svgr/webpack', - options: expect.objectContaining({ - svgoConfig: { - plugins: [ - { - name: 'preset-default', - params: { - overrides: { - removeViewBox: false, - }, - }, - }, - ], - }, - }), - }), - ); - }); -}); - describe('extending PostCSS', () => { test('user plugin should be appended in PostCSS loader', () => { let webpackConfig: Configuration = { diff --git a/packages/docusaurus/src/webpack/base.ts b/packages/docusaurus/src/webpack/base.ts index b59d8c199a6d..d3f829ed1df1 100644 --- a/packages/docusaurus/src/webpack/base.ts +++ b/packages/docusaurus/src/webpack/base.ts @@ -13,12 +13,11 @@ import {Props} from '@docusaurus/types'; import { getCustomizableJSLoader, getStyleLoaders, - getFileLoaderUtils, getCustomBabelConfigFilePath, getMinimizer, } from './utils'; import {loadPluginsThemeAliases} from '../server/themes'; -import {md5Hash} from '@docusaurus/utils'; +import {md5Hash, getFileLoaderUtils} from '@docusaurus/utils'; const CSS_REGEX = /\.css$/; const CSS_MODULE_REGEX = /\.module\.css$/; diff --git a/packages/docusaurus/src/webpack/server.ts b/packages/docusaurus/src/webpack/server.ts index 8c00954cbdc4..4f2e598e3db6 100644 --- a/packages/docusaurus/src/webpack/server.ts +++ b/packages/docusaurus/src/webpack/server.ts @@ -13,7 +13,7 @@ import {Props} from '@docusaurus/types'; import {createBaseConfig} from './base'; import WaitPlugin from './plugins/WaitPlugin'; import LogPlugin from './plugins/LogPlugin'; -import {NODE_MAJOR_VERSION, NODE_MINOR_VERSION} from '../constants'; +import {NODE_MAJOR_VERSION, NODE_MINOR_VERSION} from '@docusaurus/utils'; // Forked for Docusaurus: https://github.com/slorber/static-site-generator-webpack-plugin import StaticSiteGeneratorPlugin from '@slorber/static-site-generator-webpack-plugin'; diff --git a/packages/docusaurus/src/webpack/utils.ts b/packages/docusaurus/src/webpack/utils.ts index 4c61023c9749..5cf5fdb3e85f 100644 --- a/packages/docusaurus/src/webpack/utils.ts +++ b/packages/docusaurus/src/webpack/utils.ts @@ -29,11 +29,7 @@ import { PostCssOptions, ConfigureWebpackUtils, } from '@docusaurus/types'; -import { - BABEL_CONFIG_FILE_NAME, - OUTPUT_STATIC_ASSETS_DIR_NAME, - WEBPACK_URL_LOADER_LIMIT, -} from '../constants'; +import {BABEL_CONFIG_FILE_NAME} from '@docusaurus/utils'; import {memoize} from 'lodash'; // Utility method to get style loaders @@ -307,134 +303,6 @@ export function compile(config: Configuration[]): Promise { }); } -type AssetFolder = 'images' | 'files' | 'fonts' | 'medias'; - -type FileLoaderUtils = { - loaders: { - file: (options: {folder: AssetFolder}) => RuleSetRule; - url: (options: {folder: AssetFolder}) => RuleSetRule; - inlineMarkdownImageFileLoader: string; - inlineMarkdownLinkFileLoader: string; - }; - rules: { - images: () => RuleSetRule; - fonts: () => RuleSetRule; - media: () => RuleSetRule; - svg: () => RuleSetRule; - otherAssets: () => RuleSetRule; - }; -}; - -// Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447 -export function getFileLoaderUtils(): FileLoaderUtils { - // files/images < urlLoaderLimit will be inlined as base64 strings directly in the html - const urlLoaderLimit = WEBPACK_URL_LOADER_LIMIT; - - // defines the path/pattern of the assets handled by webpack - const fileLoaderFileName = (folder: AssetFolder) => - `${OUTPUT_STATIC_ASSETS_DIR_NAME}/${folder}/[name]-[hash].[ext]`; - - const loaders: FileLoaderUtils['loaders'] = { - file: (options: {folder: AssetFolder}) => ({ - loader: require.resolve(`file-loader`), - options: { - name: fileLoaderFileName(options.folder), - }, - }), - url: (options: {folder: AssetFolder}) => ({ - loader: require.resolve(`url-loader`), - options: { - limit: urlLoaderLimit, - name: fileLoaderFileName(options.folder), - fallback: require.resolve(`file-loader`), - }, - }), - - // TODO find a better solution to avoid conflicts with the ideal-image plugin - // TODO this may require a little breaking change for ideal-image users? - // Maybe with the ideal image plugin, all md images should be "ideal"? - // This is used to force url-loader+file-loader on markdown images - // https://webpack.js.org/concepts/loaders/#inline - inlineMarkdownImageFileLoader: `!url-loader?limit=${urlLoaderLimit}&name=${fileLoaderFileName( - 'images', - )}&fallback=file-loader!`, - inlineMarkdownLinkFileLoader: `!file-loader?name=${fileLoaderFileName( - 'files', - )}!`, - }; - - const rules: FileLoaderUtils['rules'] = { - /** - * Loads image assets, inlines images via a data URI if they are below - * the size threshold - */ - images: () => ({ - use: [loaders.url({folder: 'images'})], - test: /\.(ico|jpg|jpeg|png|gif|webp)(\?.*)?$/, - }), - - fonts: () => ({ - use: [loaders.url({folder: 'fonts'})], - test: /\.(woff|woff2|eot|ttf|otf)$/, - }), - - /** - * Loads audio and video and inlines them via a data URI if they are below - * the size threshold - */ - media: () => ({ - use: [loaders.url({folder: 'medias'})], - test: /\.(mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/, - }), - - svg: () => ({ - test: /\.svg?$/, - oneOf: [ - { - use: [ - { - loader: '@svgr/webpack', - options: { - prettier: false, - svgo: true, - svgoConfig: { - plugins: [ - { - name: 'preset-default', - params: { - overrides: { - removeViewBox: false, - }, - }, - }, - ], - }, - titleProp: true, - ref: ![path], - }, - }, - ], - // We don't want to use SVGR loader for non-React source code - // ie we don't want to use SVGR for CSS files... - issuer: { - and: [/\.(ts|tsx|js|jsx|md|mdx)$/], - }, - }, - { - use: [loaders.url({folder: 'images'})], - }, - ], - }), - - otherAssets: () => ({ - use: [loaders.file({folder: 'files'})], - test: /\.(pdf|doc|docx|xls|xlsx|zip|rar)$/, - }), - }; - - return {loaders, rules}; -} - // Ensure the certificate and key provided are valid and if not // throw an easy to debug error function validateKeyAndCerts({ diff --git a/yarn.lock b/yarn.lock index 6bcfcbe56352..52d8b5c26918 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4737,9 +4737,9 @@ acorn@^7.1.1: integrity sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A== acorn@^8.0.4, acorn@^8.2.4, acorn@^8.3.0, acorn@^8.4.1, acorn@^8.5.0: - version "8.5.0" - resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.5.0.tgz#4512ccb99b3698c752591e9bb4472e38ad43cee2" - integrity sha512-yXbYeFy+jUuYd3/CDcg2NkIYE991XYX/bje7LmjJigUciaeO1JR4XxXgCIV1/Zc/dRuFEyw1L0pbA+qynJkW5Q== + version "8.6.0" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.6.0.tgz#e3692ba0eb1a0c83eaa4f37f5fa7368dd7142895" + integrity sha512-U1riIR+lBSNi3IbxtaHOIKdH8sLFv3NYfNv8sg7ZsNhcfl4HF2++BfqqrNAxoCLQW1iiylOj76ecnaUxz+z9yw== address@^1.0.1, address@^1.1.2: version "1.1.2"