Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): allow sourcing from multiple static directories #4095

Merged
merged 16 commits into from
Nov 18, 2021
1 change: 1 addition & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface DocusaurusConfig {
)[];
clientModules?: string[];
ssrTemplate?: string;
staticDirectories: string[];
stylesheets?: (
| string
| {
Expand Down
28 changes: 11 additions & 17 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import ReactLoadableSSRAddon from 'react-loadable-ssr-addon-v5-slorber';
import {Configuration} from 'webpack';
import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
import merge from 'webpack-merge';
import {STATIC_DIR_NAME} from '../constants';
import {load, loadContext} from '../server';
import {handleBrokenLinks} from '../server/brokenLinks';

Expand Down Expand Up @@ -129,7 +128,7 @@ async function buildLocale({
outDir,
generatedFilesDir,
plugins,
siteConfig: {baseUrl, onBrokenLinks},
siteConfig: {baseUrl, onBrokenLinks, staticDirectories},
routes,
} = props;

Expand Down Expand Up @@ -162,21 +161,16 @@ async function buildLocale({
},
});

const staticDir = path.resolve(siteDir, STATIC_DIR_NAME);
if (await fs.pathExists(staticDir)) {
serverConfig = merge(serverConfig, {
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: staticDir,
to: outDir,
},
],
}),
],
});
}
serverConfig = merge(serverConfig, {
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved
plugins: [
new CopyWebpackPlugin({
patterns: staticDirectories
.map((dir) => path.resolve(siteDir, dir))
.filter(fs.existsSync)
.map((dir) => ({from: dir, to: outDir})),
}),
],
});

// Plugin Lifecycle - configureWebpack and configurePostCss.
plugins.forEach((plugin) => {
Expand Down
7 changes: 3 additions & 4 deletions packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import WebpackDevServer from 'webpack-dev-server';
import merge from 'webpack-merge';
import {load} from '../server';
import {StartCLIOptions} from '@docusaurus/types';
import {STATIC_DIR_NAME} from '../constants';
import createClientConfig from '../webpack/client';
import {
applyConfigureWebpack,
Expand Down Expand Up @@ -187,17 +186,17 @@ export default async function start(
// Reduce log verbosity, see https://github.com/facebook/docusaurus/pull/5420#issuecomment-906613105
stats: 'summary',
},
static: {
static: siteConfig.staticDirectories.map((dir) => ({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, hopefully they allow an array here 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, they do:
https://webpack.js.org/configuration/dev-server/#directory
https://webpack.js.org/plugins/copy-webpack-plugin/#root

The one thing I'm slightly worried about though is path collisions. I don't know if any warning should be given in that case

publicPath: baseUrl,
directory: path.resolve(siteDir, STATIC_DIR_NAME),
directory: path.resolve(siteDir, dir),
watch: {
// Useful options for our own monorepo using symlinks!
// See https://github.com/webpack/webpack/issues/11612#issuecomment-879259806
followSymlinks: true,
ignored: /node_modules\/(?!@docusaurus)/,
...{pollingOptions},
},
},
})),
historyApiFallback: {
rewrites: [{from: /\/*/, to: baseUrl}],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Object {
],
"presets": Array [],
"projectName": "hello",
"staticDirectories": Array [
"static",
],
"tagline": "Hello World",
"themeConfig": Object {},
"themes": Array [],
Expand Down
7 changes: 6 additions & 1 deletion packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import {DocusaurusConfig, I18nConfig} from '@docusaurus/types';
import {DEFAULT_CONFIG_FILE_NAME} from '../constants';
import {DEFAULT_CONFIG_FILE_NAME, STATIC_DIR_NAME} from '../constants';
import {
Joi,
logValidationBugReportHint,
Expand Down Expand Up @@ -37,6 +37,7 @@ export const DEFAULT_CONFIG: Pick<
| 'titleDelimiter'
| 'noIndex'
| 'baseUrlIssueBanner'
| 'staticDirectories'
> = {
i18n: DEFAULT_I18N_CONFIG,
onBrokenLinks: 'throw',
Expand All @@ -50,6 +51,7 @@ export const DEFAULT_CONFIG: Pick<
titleDelimiter: '|',
noIndex: false,
baseUrlIssueBanner: true,
staticDirectories: [STATIC_DIR_NAME],
};

const PluginSchema = Joi.alternatives()
Expand Down Expand Up @@ -142,6 +144,9 @@ export const ConfigSchema = Joi.object({
.equal('ignore', 'log', 'warn', 'error', 'throw')
.default(DEFAULT_CONFIG.onDuplicateRoutes),
organizationName: Joi.string().allow(''),
staticDirectories: Joi.array()
.items(Joi.string())
.default(DEFAULT_CONFIG.staticDirectories),
projectName: Joi.string().allow(''),
deploymentBranch: Joi.string().optional(),
customFields: Joi.object().unknown().default(DEFAULT_CONFIG.customFields),
Expand Down
4 changes: 4 additions & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const config = {
tagline: 'Build optimized websites quickly, focus on your content',
organizationName: 'facebook',
projectName: 'docusaurus',
staticDirectories: [
path.resolve(__dirname, './static'),
path.resolve(__dirname, './test'),
],
baseUrl,
baseUrlIssueBanner: true,
url: 'https://docusaurus.io',
Expand Down
1 change: 1 addition & 0 deletions website/test/hey.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hey hey hey!!!
Binary file added website/test/img/test-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.