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 @@ -49,6 +49,7 @@ export interface DocusaurusConfig {
)[];
clientModules?: string[];
ssrTemplate?: string;
staticDirectories?: string[];
stylesheets?: (
| string
| {
Expand Down
32 changes: 16 additions & 16 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async function buildLocale({
outDir,
generatedFilesDir,
plugins,
siteConfig: {baseUrl, onBrokenLinks},
siteConfig: {baseUrl, onBrokenLinks, staticDirectories = [STATIC_DIR_NAME]},
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved
routes,
} = props;

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

const staticDir = path.resolve(siteDir, STATIC_DIR_NAME);
if (fs.existsSync(staticDir)) {
serverConfig = merge(serverConfig, {
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: staticDir,
to: outDir,
},
],
}),
],
});
}
const staticDirectoryConfigurations = staticDirectories
.map((dir) => path.resolve(siteDir, dir))
.filter((dir) => fs.existsSync(dir))
.map((dir) => ({
from: dir,
to: outDir,
}));

serverConfig = merge(serverConfig, {
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved
plugins: [
new CopyWebpackPlugin({
patterns: staticDirectoryConfigurations,
}),
],
});

// Plugin Lifecycle - configureWebpack.
plugins.forEach((plugin) => {
Expand Down
9 changes: 6 additions & 3 deletions packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,12 @@ export default async function start(
overlay: false,
host,
before: (app, server) => {
app.use(
baseUrl,
express.static(path.resolve(siteDir, STATIC_DIR_NAME)),
const staticDirectories = siteConfig.staticDirectories || [
STATIC_DIR_NAME,
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved
];

staticDirectories.forEach((dir) =>
app.use(baseUrl, express.static(path.resolve(siteDir, dir))),
);

// This lets us fetch source contents from webpack for the error overlay.
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const ConfigSchema = Joi.object({
.equal('ignore', 'log', 'warn', 'error', 'throw')
.default(DEFAULT_CONFIG.onDuplicateRoutes),
organizationName: Joi.string().allow(''),
staticDirectories: Joi.array().items(Joi.string()),
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved
projectName: Joi.string().allow(''),
customFields: Joi.object().unknown().default(DEFAULT_CONFIG.customFields),
githubHost: Joi.string(),
Expand Down
4 changes: 4 additions & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ module.exports = {
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://v2.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.