Skip to content

Commit

Permalink
Move logic for static dir CopyWebpackPlugin to appropriate file
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Feb 1, 2024
1 parent de70d67 commit fabf483
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
42 changes: 1 addition & 41 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import path from 'path';
import _ from 'lodash';
import logger from '@docusaurus/logger';
import {DOCUSAURUS_VERSION, mapAsyncSequential} from '@docusaurus/utils';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import ReactLoadableSSRAddon from 'react-loadable-ssr-addon-v5-slorber';
import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
import merge from 'webpack-merge';
Expand Down Expand Up @@ -151,12 +150,7 @@ async function buildLocale({
});

// Apply user webpack config.
const {
outDir,
generatedFilesDir,
plugins,
siteConfig: {staticDirectories: staticDirectoriesOption},
} = props;
const {outDir, generatedFilesDir, plugins} = props;

const clientManifestPath = path.join(
generatedFilesDir,
Expand All @@ -183,40 +177,6 @@ async function buildLocale({
props,
});

// The staticDirectories option can contain empty directories, or non-existent
// directories (e.g. user deleted `static`). Instead of issuing an error, we
// just silently filter them out, because user could have never configured it
// in the first place (the default option should always "work").
const staticDirectories = (
await Promise.all(
staticDirectoriesOption.map(async (dir) => {
const staticDir = path.resolve(siteDir, dir);
if (
(await fs.pathExists(staticDir)) &&
(await fs.readdir(staticDir)).length > 0
) {
return staticDir;
}
return '';
}),
)
).filter(Boolean);

// TODO move to server.ts?
if (staticDirectories.length > 0) {
serverConfig = merge(serverConfig, {
plugins: [
new CopyWebpackPlugin({
patterns: staticDirectories.map((dir) => ({
from: dir,
to: outDir,
toType: 'dir',
})),
}),
],
});
}

// Plugin Lifecycle - configureWebpack and configurePostCss.
plugins.forEach((plugin) => {
const {configureWebpack, configurePostCss} = plugin;
Expand Down
44 changes: 43 additions & 1 deletion packages/docusaurus/src/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/

import path from 'path';
import fs from 'fs-extra';
import merge from 'webpack-merge';
import {NODE_MAJOR_VERSION, NODE_MINOR_VERSION} from '@docusaurus/utils';
import WebpackBar from 'webpackbar';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import {createBaseConfig} from './base';
import type {Props} from '@docusaurus/types';
import type {Configuration} from 'webpack';
Expand Down Expand Up @@ -36,6 +38,46 @@ export default async function createServerConfig(params: {
name: 'Server',
color: 'yellow',
}),
],
await createStaticDirectoriesCopyPlugin(params),
].filter(Boolean),
});
}

async function createStaticDirectoriesCopyPlugin({props}: {props: Props}) {
const {
outDir,
siteDir,
siteConfig: {staticDirectories: staticDirectoriesOption},
} = props;

// The staticDirectories option can contain empty directories, or non-existent
// directories (e.g. user deleted `static`). Instead of issuing an error, we
// just silently filter them out, because user could have never configured it
// in the first place (the default option should always "work").
const staticDirectories: string[] = (
await Promise.all(
staticDirectoriesOption.map(async (dir) => {
const staticDir = path.resolve(siteDir, dir);
if (
(await fs.pathExists(staticDir)) &&
(await fs.readdir(staticDir)).length > 0
) {
return staticDir;
}
return '';
}),
)
).filter(Boolean);

if (staticDirectories.length === 0) {
return undefined;
}

return new CopyWebpackPlugin({
patterns: staticDirectories.map((dir) => ({
from: dir,
to: outDir,
toType: 'dir',
})),
});
}

0 comments on commit fabf483

Please sign in to comment.