Skip to content

Commit

Permalink
SSG: use createRequire() instead of "require-like" package
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Feb 6, 2024
1 parent 72af15b commit 7953746
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/docusaurus/src/ssg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import fs from 'fs-extra';
import {createRequire} from 'module';
import path from 'path';
import _ from 'lodash';
import evaluate from 'eval';
Expand Down Expand Up @@ -46,6 +47,7 @@ export async function loadAppRenderer({
}: {
serverBundlePath: string;
}): Promise<AppRenderer> {
console.log(`SSG - Load server bundle`);
PerfLogger.start(`SSG - Load server bundle`);
const source = await fs.readFile(serverBundlePath);
PerfLogger.end(`SSG - Load server bundle`);
Expand All @@ -55,11 +57,17 @@ export async function loadAppRenderer({

const filename = path.basename(serverBundlePath);

// When using "new URL('file.js', import.meta.url)", Webpack will emit
// __filename, and this plugin will throw. not sure the __filename value
// has any importance for this plugin, just using an empty string to
// avoid the error. See https://github.com/facebook/docusaurus/issues/4922
const globals = {__filename: ''};
const globals = {
// When using "new URL('file.js', import.meta.url)", Webpack will emit
// __filename, and this plugin will throw. not sure the __filename value
// has any importance for this plugin, just using an empty string to
// avoid the error. See https://github.com/facebook/docusaurus/issues/4922
__filename: '',

// This uses module.createRequire() instead of very old "require-like" lib
// See also: https://github.com/pierrec/node-eval/issues/33
require: createRequire(serverBundlePath),
};

PerfLogger.start(`SSG - Evaluate server bundle`);
const serverEntry = evaluate(
Expand Down

0 comments on commit 7953746

Please sign in to comment.