Skip to content

Commit

Permalink
properly fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 5, 2022
1 parent d5a079d commit 1645290
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 17 deletions.
4 changes: 4 additions & 0 deletions packages/docusaurus-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 11 additions & 5 deletions packages/docusaurus/bin/beforeCli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>} */
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;

Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,9 +29,7 @@ await beforeCli();

const resolveDir = (dir = '.') => fs.realpath(dir);

cli
.version(createRequire(import.meta.url)('../package.json').version)
.usage('<command> [options]');
cli.version(DOCUSAURUS_VERSION).usage('<command> [options]');

cli
.command('build [siteDir]')
Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus/src/client/serverEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -131,7 +129,7 @@ async function doRender(locals: Locals & {path: string}) {
scripts,
stylesheets,
noIndex,
version: packageJson.version,
version: DOCUSAURUS_VERSION,
});

try {
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus/src/deps.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions packages/docusaurus/src/server/siteMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -98,9 +99,7 @@ export async function loadSiteMetadata({
siteDir: string;
}): Promise<SiteMetadata> {
const siteMetadata: SiteMetadata = {
docusaurusVersion: (await getPackageJsonVersion(
path.join(__dirname, '../../package.json'),
))!,
docusaurusVersion: DOCUSAURUS_VERSION,
siteVersion: await getPackageJsonVersion(
path.join(siteDir, 'package.json'),
),
Expand Down
7 changes: 6 additions & 1 deletion packages/docusaurus/src/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,6 +82,7 @@ export default async function createServerConfig({
onHeadTagsCollected,
ssrTemplate: ssrTemplate ?? ssrDefaultTemplate,
noIndex,
DOCUSAURUS_VERSION,
},
paths: ssgPaths,
preferFoldersOutput: trailingSlash,
Expand Down

0 comments on commit 1645290

Please sign in to comment.