From a0804f81aacb6611566d2d998fa9f39b7fa90618 Mon Sep 17 00:00:00 2001 From: sebastien Date: Tue, 20 Aug 2024 13:48:32 +0200 Subject: [PATCH] move server bundle output to subfolder + add secret env var to not delete it --- packages/docusaurus/src/commands/build.ts | 20 +++++++++++++------- packages/docusaurus/src/webpack/server.ts | 4 +++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/docusaurus/src/commands/build.ts b/packages/docusaurus/src/commands/build.ts index cc587d24f033..531f621849c2 100644 --- a/packages/docusaurus/src/commands/build.ts +++ b/packages/docusaurus/src/commands/build.ts @@ -196,10 +196,7 @@ async function buildLocale({ }), ); - // Remove server.bundle.js because it is not needed. - await PerfLogger.async('Deleting server bundle', () => - ensureUnlink(serverBundlePath), - ); + await cleanupServerBundle(serverBundlePath); // Plugin Lifecycle - postBuild. await PerfLogger.async('postBuild()', () => @@ -361,8 +358,17 @@ async function getBuildServerConfig({props}: {props: Props}) { return {serverConfig: config, serverBundlePath: result.serverBundlePath}; } -async function ensureUnlink(filepath: string) { - if (await fs.pathExists(filepath)) { - await fs.unlink(filepath); +// Remove /build/server server.bundle.js because it is not needed. +async function cleanupServerBundle(serverBundlePath: string) { + if (process.env.DOCUSAURUS_KEEP_SERVER_BUNDLE === 'true') { + logger.warn( + "Will NOT delete server bundle because DOCUSAURUS_KEEP_SERVER_BUNDLE is set to 'true'", + ); + } else { + await PerfLogger.async('Deleting server bundle', async () => { + // For now we assume server entry is at the root of the server out dir + const serverDir = path.dirname(serverBundlePath); + await fs.rm(serverDir, {recursive: true, force: true}); + }); } } diff --git a/packages/docusaurus/src/webpack/server.ts b/packages/docusaurus/src/webpack/server.ts index 4e7e4dabeb7f..6857c51fe2e3 100644 --- a/packages/docusaurus/src/webpack/server.ts +++ b/packages/docusaurus/src/webpack/server.ts @@ -27,7 +27,8 @@ export default async function createServerConfig(params: { }); const outputFilename = 'server.bundle.js'; - const serverBundlePath = path.join(props.outDir, outputFilename); + const outputDir = path.join(props.outDir, '__server'); + const serverBundlePath = path.join(outputDir, outputFilename); const config = merge(baseConfig, { target: `node${NODE_MAJOR_VERSION}.${NODE_MINOR_VERSION}`, @@ -35,6 +36,7 @@ export default async function createServerConfig(params: { main: path.resolve(__dirname, '../client/serverEntry.js'), }, output: { + path: outputDir, filename: outputFilename, libraryTarget: 'commonjs2', // Workaround for Webpack 4 Bug (https://github.com/webpack/webpack/issues/6522)