Skip to content

Commit

Permalink
move server bundle output to subfolder + add secret env var to not de…
Browse files Browse the repository at this point in the history
…lete it
  • Loading branch information
slorber committed Aug 20, 2024
1 parent 1c56fa5 commit a0804f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 13 additions & 7 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () =>
Expand Down Expand Up @@ -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});
});
}
}
4 changes: 3 additions & 1 deletion packages/docusaurus/src/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ 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}`,
entry: {
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)
Expand Down

0 comments on commit a0804f8

Please sign in to comment.