Skip to content

Commit

Permalink
Add workaround to run docusaurus in ESM project
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Sep 1, 2022
1 parent d482585 commit a24a9f3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
"prettier:check": "prettier --cache --cache-strategy metadata --check .",
"check:spelling": "cspell --cache --no-progress '**/*'",
"check:integrations": "npm run build:npm && npm run build:deno && mocha --full-trace integrationTests/*-test.ts",
"start": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus start ./website",
"build:website": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus build --out-dir=\"$(pwd)/websiteDist\" ./website",
"serve": "docusaurus serve --dir websiteDist/ --config website/docusaurus.config.cjs",
"start": "npm run build:website && npm run serve",
"build:website": "ts-node resources/build-docusaurus.ts",
"build:npm": "ts-node resources/build-npm.ts",
"build:deno": "ts-node resources/build-deno.ts",
"diff:npm": "ts-node resources/diff-npm-package.ts",
Expand Down
46 changes: 46 additions & 0 deletions resources/build-docusaurus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// The whole script is a hack to allow building Docusaurus in ESM project
// https://github.com/facebook/docusaurus/issues/6520
// Should be just: `docusaurus build --out-dir ./websiteDist ./website`

import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

import { localRepoPath, npm, readPackageJSON } from './utils.js';

const tmpDir = path.join(os.tmpdir(), 'graphql-run-docusaurus');
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.mkdirSync(tmpDir);

const packageJSON = readPackageJSON();
delete packageJSON.type;
fs.writeFileSync(tmpDirPath('package.json'), JSON.stringify(packageJSON));

copyToTmpDir('package-lock.json');
copyToTmpDir('tsconfig.json');
copyToTmpDir('src');
copyToTmpDir('website');

npm(['install', 'ci'], { cwd: tmpDir });

const env = {
...process.env,
DOCUSAURUS_GENERATED_FILES_DIR_NAME: tmpDirPath('.docusaurus'),
};
const docusaurusArgs = [
'build',
'--out-dir',
localRepoPath('websiteDist'),
tmpDirPath('website'),
];
npm(['exec', 'docusaurus', '--', ...docusaurusArgs], { env, cwd: tmpDir });

function copyToTmpDir(relativePath: string) {
fs.cpSync(localRepoPath(relativePath), tmpDirPath(relativePath), {
recursive: true,
});
}

function tmpDirPath(...paths: ReadonlyArray<string>): string {
return path.join(tmpDir, ...paths);
}

0 comments on commit a24a9f3

Please sign in to comment.