Skip to content

Commit a24a9f3

Browse files
committed
Add workaround to run docusaurus in ESM project
1 parent d482585 commit a24a9f3

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
"prettier:check": "prettier --cache --cache-strategy metadata --check .",
4646
"check:spelling": "cspell --cache --no-progress '**/*'",
4747
"check:integrations": "npm run build:npm && npm run build:deno && mocha --full-trace integrationTests/*-test.ts",
48-
"start": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus start ./website",
49-
"build:website": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus build --out-dir=\"$(pwd)/websiteDist\" ./website",
48+
"serve": "docusaurus serve --dir websiteDist/ --config website/docusaurus.config.cjs",
49+
"start": "npm run build:website && npm run serve",
50+
"build:website": "ts-node resources/build-docusaurus.ts",
5051
"build:npm": "ts-node resources/build-npm.ts",
5152
"build:deno": "ts-node resources/build-deno.ts",
5253
"diff:npm": "ts-node resources/diff-npm-package.ts",

resources/build-docusaurus.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// The whole script is a hack to allow building Docusaurus in ESM project
2+
// https://github.com/facebook/docusaurus/issues/6520
3+
// Should be just: `docusaurus build --out-dir ./websiteDist ./website`
4+
5+
import fs from 'node:fs';
6+
import os from 'node:os';
7+
import path from 'node:path';
8+
9+
import { localRepoPath, npm, readPackageJSON } from './utils.js';
10+
11+
const tmpDir = path.join(os.tmpdir(), 'graphql-run-docusaurus');
12+
fs.rmSync(tmpDir, { recursive: true, force: true });
13+
fs.mkdirSync(tmpDir);
14+
15+
const packageJSON = readPackageJSON();
16+
delete packageJSON.type;
17+
fs.writeFileSync(tmpDirPath('package.json'), JSON.stringify(packageJSON));
18+
19+
copyToTmpDir('package-lock.json');
20+
copyToTmpDir('tsconfig.json');
21+
copyToTmpDir('src');
22+
copyToTmpDir('website');
23+
24+
npm(['install', 'ci'], { cwd: tmpDir });
25+
26+
const env = {
27+
...process.env,
28+
DOCUSAURUS_GENERATED_FILES_DIR_NAME: tmpDirPath('.docusaurus'),
29+
};
30+
const docusaurusArgs = [
31+
'build',
32+
'--out-dir',
33+
localRepoPath('websiteDist'),
34+
tmpDirPath('website'),
35+
];
36+
npm(['exec', 'docusaurus', '--', ...docusaurusArgs], { env, cwd: tmpDir });
37+
38+
function copyToTmpDir(relativePath: string) {
39+
fs.cpSync(localRepoPath(relativePath), tmpDirPath(relativePath), {
40+
recursive: true,
41+
});
42+
}
43+
44+
function tmpDirPath(...paths: ReadonlyArray<string>): string {
45+
return path.join(tmpDir, ...paths);
46+
}

0 commit comments

Comments
 (0)