From d5a079deff4220d8bf6ca8dd74c6c34fad831bab Mon Sep 17 00:00:00 2001 From: Reece Dunham Date: Wed, 4 May 2022 15:28:44 -0400 Subject: [PATCH 1/2] Revert "fix(core): handle case where package.json is not available at CWD (#7187)" This reverts commit 3b32a41f229a1cbeb869ba4232035af3ad6b8a37. --- packages/docusaurus-utils/src/constants.ts | 4 ---- packages/docusaurus-utils/src/index.ts | 1 - packages/docusaurus/bin/beforeCli.mjs | 16 +++++----------- packages/docusaurus/bin/docusaurus.mjs | 6 ++++-- packages/docusaurus/src/client/serverEntry.tsx | 6 ++++-- packages/docusaurus/src/server/siteMetadata.ts | 5 +++-- 6 files changed, 16 insertions(+), 22 deletions(-) diff --git a/packages/docusaurus-utils/src/constants.ts b/packages/docusaurus-utils/src/constants.ts index a9a91394f48c..0b413e6f4182 100644 --- a/packages/docusaurus-utils/src/constants.ts +++ b/packages/docusaurus-utils/src/constants.ts @@ -16,10 +16,6 @@ export const NODE_MINOR_VERSION = parseInt( 10, ); -/** Docusaurus core version. */ -// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires -export const DOCUSAURUS_VERSION = require('../package.json').version; - /** * Can be overridden with cli option `--out-dir`. Code should generally use * `context.outDir` instead (which is always absolute and localized). diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index fb2c66435ea2..6431c9192658 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -8,7 +8,6 @@ export { NODE_MAJOR_VERSION, NODE_MINOR_VERSION, - DOCUSAURUS_VERSION, DEFAULT_BUILD_DIR_NAME, DEFAULT_CONFIG_FILE_NAME, BABEL_CONFIG_FILE_NAME, diff --git a/packages/docusaurus/bin/beforeCli.mjs b/packages/docusaurus/bin/beforeCli.mjs index 0c374dba1722..522867ec1b55 100644 --- a/packages/docusaurus/bin/beforeCli.mjs +++ b/packages/docusaurus/bin/beforeCli.mjs @@ -14,21 +14,15 @@ import path from 'path'; import updateNotifier from 'update-notifier'; import boxen from 'boxen'; import {createRequire} from 'module'; -import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; const packageJson = createRequire(import.meta.url)('../package.json'); -/** @type {Record} */ -let sitePkg; -try { - sitePkg = createRequire(path.resolve('package.json'))('./package.json'); -} catch { - logger.warn`path=${'package.json'} file not found at CWD: path=${process.cwd()}.`; - logger.info`This is non-critical, but could lead to undesired behavior downstream. Docusaurus assumes that path=${'package.json'} exists at CWD, because it's where the package manager looks up the script at. A common reason is because you have changed directory in the script. Instead of writing code=${'"start": "cd website && docusaurus start"'}, consider using the code=${'[siteDir]'} argument: code=${'"start": "docusaurus start website"'}.`; - sitePkg = {}; -} +const sitePkg = createRequire(path.join(process.cwd(), 'package.json'))( + './package.json', +); const { name, + version, engines: {node: requiredVersion}, } = packageJson; @@ -46,7 +40,7 @@ export default async function beforeCli() { const notifier = updateNotifier({ pkg: { name, - version: DOCUSAURUS_VERSION, + version, }, // Check is in background so it's fine to use a small value like 1h // Use 0 for debugging diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index 759aad41cc98..8d156d790185 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -11,7 +11,7 @@ import logger from '@docusaurus/logger'; import fs from 'fs-extra'; import cli from 'commander'; -import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; +import {createRequire} from 'module'; import { build, swizzle, @@ -29,7 +29,9 @@ await beforeCli(); const resolveDir = (dir = '.') => fs.realpath(dir); -cli.version(DOCUSAURUS_VERSION).usage(' [options]'); +cli + .version(createRequire(import.meta.url)('../package.json').version) + .usage(' [options]'); cli .command('build [siteDir]') diff --git a/packages/docusaurus/src/client/serverEntry.tsx b/packages/docusaurus/src/client/serverEntry.tsx index 2f14ecabd238..ec01d1b9b441 100644 --- a/packages/docusaurus/src/client/serverEntry.tsx +++ b/packages/docusaurus/src/client/serverEntry.tsx @@ -26,7 +26,9 @@ import logger from '@docusaurus/logger'; // eslint-disable-next-line no-restricted-imports import _ from 'lodash'; import type {Locals} from '@slorber/static-site-generator-webpack-plugin'; -import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; + +// eslint-disable-next-line @typescript-eslint/no-var-requires +const packageJson = require('../../package.json'); const getCompiledSSRTemplate = _.memoize((template: string) => eta.compile(template.trim(), { @@ -129,7 +131,7 @@ async function doRender(locals: Locals & {path: string}) { scripts, stylesheets, noIndex, - version: DOCUSAURUS_VERSION, + version: packageJson.version, }); try { diff --git a/packages/docusaurus/src/server/siteMetadata.ts b/packages/docusaurus/src/server/siteMetadata.ts index bde32ad967c3..ec855f94d57b 100644 --- a/packages/docusaurus/src/server/siteMetadata.ts +++ b/packages/docusaurus/src/server/siteMetadata.ts @@ -10,7 +10,6 @@ import type { PluginVersionInformation, SiteMetadata, } from '@docusaurus/types'; -import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; import fs from 'fs-extra'; import path from 'path'; import logger from '@docusaurus/logger'; @@ -99,7 +98,9 @@ export async function loadSiteMetadata({ siteDir: string; }): Promise { const siteMetadata: SiteMetadata = { - docusaurusVersion: DOCUSAURUS_VERSION, + docusaurusVersion: (await getPackageJsonVersion( + path.join(__dirname, '../../package.json'), + ))!, siteVersion: await getPackageJsonVersion( path.join(siteDir, 'package.json'), ), From 16452909c74ab2792f6ec44b5ca4342947d701ba Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 5 May 2022 09:02:01 +0800 Subject: [PATCH 2/2] properly fix --- packages/docusaurus-utils/src/constants.ts | 4 ++++ packages/docusaurus-utils/src/index.ts | 1 + packages/docusaurus/bin/beforeCli.mjs | 16 +++++++++++----- packages/docusaurus/bin/docusaurus.mjs | 6 ++---- packages/docusaurus/src/client/serverEntry.tsx | 6 ++---- packages/docusaurus/src/deps.d.ts | 1 + packages/docusaurus/src/server/siteMetadata.ts | 5 ++--- packages/docusaurus/src/webpack/server.ts | 7 ++++++- 8 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/docusaurus-utils/src/constants.ts b/packages/docusaurus-utils/src/constants.ts index 0b413e6f4182..a9a91394f48c 100644 --- a/packages/docusaurus-utils/src/constants.ts +++ b/packages/docusaurus-utils/src/constants.ts @@ -16,6 +16,10 @@ export const NODE_MINOR_VERSION = parseInt( 10, ); +/** Docusaurus core version. */ +// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires +export const DOCUSAURUS_VERSION = require('../package.json').version; + /** * Can be overridden with cli option `--out-dir`. Code should generally use * `context.outDir` instead (which is always absolute and localized). diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 6431c9192658..fb2c66435ea2 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -8,6 +8,7 @@ export { NODE_MAJOR_VERSION, NODE_MINOR_VERSION, + DOCUSAURUS_VERSION, DEFAULT_BUILD_DIR_NAME, DEFAULT_CONFIG_FILE_NAME, BABEL_CONFIG_FILE_NAME, diff --git a/packages/docusaurus/bin/beforeCli.mjs b/packages/docusaurus/bin/beforeCli.mjs index 522867ec1b55..0c374dba1722 100644 --- a/packages/docusaurus/bin/beforeCli.mjs +++ b/packages/docusaurus/bin/beforeCli.mjs @@ -14,15 +14,21 @@ import path from 'path'; import updateNotifier from 'update-notifier'; import boxen from 'boxen'; import {createRequire} from 'module'; +import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; const packageJson = createRequire(import.meta.url)('../package.json'); -const sitePkg = createRequire(path.join(process.cwd(), 'package.json'))( - './package.json', -); +/** @type {Record} */ +let sitePkg; +try { + sitePkg = createRequire(path.resolve('package.json'))('./package.json'); +} catch { + logger.warn`path=${'package.json'} file not found at CWD: path=${process.cwd()}.`; + logger.info`This is non-critical, but could lead to undesired behavior downstream. Docusaurus assumes that path=${'package.json'} exists at CWD, because it's where the package manager looks up the script at. A common reason is because you have changed directory in the script. Instead of writing code=${'"start": "cd website && docusaurus start"'}, consider using the code=${'[siteDir]'} argument: code=${'"start": "docusaurus start website"'}.`; + sitePkg = {}; +} const { name, - version, engines: {node: requiredVersion}, } = packageJson; @@ -40,7 +46,7 @@ export default async function beforeCli() { const notifier = updateNotifier({ pkg: { name, - version, + version: DOCUSAURUS_VERSION, }, // Check is in background so it's fine to use a small value like 1h // Use 0 for debugging diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index 8d156d790185..759aad41cc98 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -11,7 +11,7 @@ import logger from '@docusaurus/logger'; import fs from 'fs-extra'; import cli from 'commander'; -import {createRequire} from 'module'; +import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; import { build, swizzle, @@ -29,9 +29,7 @@ await beforeCli(); const resolveDir = (dir = '.') => fs.realpath(dir); -cli - .version(createRequire(import.meta.url)('../package.json').version) - .usage(' [options]'); +cli.version(DOCUSAURUS_VERSION).usage(' [options]'); cli .command('build [siteDir]') diff --git a/packages/docusaurus/src/client/serverEntry.tsx b/packages/docusaurus/src/client/serverEntry.tsx index ec01d1b9b441..408bfee0e3dc 100644 --- a/packages/docusaurus/src/client/serverEntry.tsx +++ b/packages/docusaurus/src/client/serverEntry.tsx @@ -27,9 +27,6 @@ import logger from '@docusaurus/logger'; import _ from 'lodash'; import type {Locals} from '@slorber/static-site-generator-webpack-plugin'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJson = require('../../package.json'); - const getCompiledSSRTemplate = _.memoize((template: string) => eta.compile(template.trim(), { rmWhitespace: true, @@ -74,6 +71,7 @@ async function doRender(locals: Locals & {path: string}) { baseUrl, ssrTemplate, noIndex, + DOCUSAURUS_VERSION, } = locals; const location = routesLocation[locals.path]!; await preload(location); @@ -131,7 +129,7 @@ async function doRender(locals: Locals & {path: string}) { scripts, stylesheets, noIndex, - version: packageJson.version, + version: DOCUSAURUS_VERSION, }); try { diff --git a/packages/docusaurus/src/deps.d.ts b/packages/docusaurus/src/deps.d.ts index c2dc3ccd1fd4..06f478ca6330 100644 --- a/packages/docusaurus/src/deps.d.ts +++ b/packages/docusaurus/src/deps.d.ts @@ -52,6 +52,7 @@ declare module '@slorber/static-site-generator-webpack-plugin' { baseUrl: string; ssrTemplate: string; noIndex: boolean; + DOCUSAURUS_VERSION: string; }; export default class StaticSiteGeneratorPlugin diff --git a/packages/docusaurus/src/server/siteMetadata.ts b/packages/docusaurus/src/server/siteMetadata.ts index ec855f94d57b..bde32ad967c3 100644 --- a/packages/docusaurus/src/server/siteMetadata.ts +++ b/packages/docusaurus/src/server/siteMetadata.ts @@ -10,6 +10,7 @@ import type { PluginVersionInformation, SiteMetadata, } from '@docusaurus/types'; +import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; import fs from 'fs-extra'; import path from 'path'; import logger from '@docusaurus/logger'; @@ -98,9 +99,7 @@ export async function loadSiteMetadata({ siteDir: string; }): Promise { const siteMetadata: SiteMetadata = { - docusaurusVersion: (await getPackageJsonVersion( - path.join(__dirname, '../../package.json'), - ))!, + docusaurusVersion: DOCUSAURUS_VERSION, siteVersion: await getPackageJsonVersion( path.join(siteDir, 'package.json'), ), diff --git a/packages/docusaurus/src/webpack/server.ts b/packages/docusaurus/src/webpack/server.ts index fa2472828ed4..c0aab9ed99d9 100644 --- a/packages/docusaurus/src/webpack/server.ts +++ b/packages/docusaurus/src/webpack/server.ts @@ -13,7 +13,11 @@ import type {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 '@docusaurus/utils'; +import { + NODE_MAJOR_VERSION, + NODE_MINOR_VERSION, + DOCUSAURUS_VERSION, +} from '@docusaurus/utils'; import ssrDefaultTemplate from './templates/ssr.html.template'; // Forked for Docusaurus: https://github.com/slorber/static-site-generator-webpack-plugin @@ -78,6 +82,7 @@ export default async function createServerConfig({ onHeadTagsCollected, ssrTemplate: ssrTemplate ?? ssrDefaultTemplate, noIndex, + DOCUSAURUS_VERSION, }, paths: ssgPaths, preferFoldersOutput: trailingSlash,