Skip to content

Commit

Permalink
Ability to use classic theme/preset while not using docs/pages withou…
Browse files Browse the repository at this point in the history
…t trouble.

#3360
  • Loading branch information
slorber committed Sep 1, 2020
1 parent 66d7bbf commit e1f6f75
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 16 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
"start:v2:watch": "nodemon --watch \"./packages/*/lib/**/*.*\" --exec \"yarn start:v2\"",
"start:v2:baseUrl": "yarn workspace docusaurus-2-website start:baseUrl",
"start:v2:bootstrap": "yarn workspace docusaurus-2-website start:bootstrap",
"start:v2:blogOnly": "yarn workspace docusaurus-2-website start:blogOnly",
"build": "yarn build:packages && yarn build:v2",
"build:packages": "lerna run build --no-private",
"build:v1": "yarn workspace docusaurus-1-website build",
"build:v2": "yarn workspace docusaurus-2-website build",
"build:v2:baseUrl": "yarn workspace docusaurus-2-website build:baseUrl",
"build:v2:blogOnly": "yarn workspace docusaurus-2-website build:blogOnly",
"serve:v1": "serve website-1.x/build/docusaurus",
"serve:v2": "serve website/build",
"serve:v2:baseUrl": "serve website",
Expand Down
18 changes: 15 additions & 3 deletions packages/docusaurus-preset-bootstrap/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@ module.exports = function preset(context, opts = {}) {
return {
themes: [[require.resolve('@docusaurus/theme-bootstrap'), opts.theme]],
plugins: [
[require.resolve('@docusaurus/plugin-content-pages'), opts.pages],
[require.resolve('@docusaurus/plugin-content-blog'), opts.blog],
[require.resolve('@docusaurus/plugin-content-docs'), opts.docs],
[
opts.pages !== false &&
require.resolve('@docusaurus/plugin-content-pages'),
opts.pages,
],
[
opts.blog !== false &&
require.resolve('@docusaurus/plugin-content-blog'),
opts.blog,
],
[
opts.docs !== false &&
require.resolve('@docusaurus/plugin-content-docs'),
opts.docs,
],
],
};
};
21 changes: 17 additions & 4 deletions packages/docusaurus-preset-classic/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,28 @@ module.exports = function preset(context, opts = {}) {
algolia && require.resolve('@docusaurus/theme-search-algolia'),
],
plugins: [
[require.resolve('@docusaurus/plugin-content-docs'), opts.docs],
[require.resolve('@docusaurus/plugin-content-blog'), opts.blog],
[require.resolve('@docusaurus/plugin-content-pages'), opts.pages],
opts.docs !== false && [
require.resolve('@docusaurus/plugin-content-docs'),
opts.docs,
],
opts.blog !== false && [
require.resolve('@docusaurus/plugin-content-blog'),
opts.blog,
],
opts.pages !== false && [
require.resolve('@docusaurus/plugin-content-pages'),
opts.pages,
],
isProd &&
googleAnalytics &&
require.resolve('@docusaurus/plugin-google-analytics'),
debug && require.resolve('@docusaurus/plugin-debug'),
isProd && gtag && require.resolve('@docusaurus/plugin-google-gtag'),
isProd && [require.resolve('@docusaurus/plugin-sitemap'), opts.sitemap],
isProd &&
opts.sitemap !== false && [
require.resolve('@docusaurus/plugin-sitemap'),
opts.sitemap,
],
],
};
};
21 changes: 13 additions & 8 deletions packages/docusaurus-theme-classic/src/theme/NavbarItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@
*/

import React from 'react';
import DocsVersionNavbarItem from '@theme/NavbarItem/DocsVersionNavbarItem';
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
import DocsVersionDropdownNavbarItem from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
import type {Props} from '@theme/NavbarItem';

const NavbarItemComponents = {
default: DefaultNavbarItem,
docsVersion: DocsVersionNavbarItem,
docsVersionDropdown: DocsVersionDropdownNavbarItem,
default: () => DefaultNavbarItem,

// Need to lazy load these items as we don't know for sure the docs plugin is loaded
// See https://github.com/facebook/docusaurus/issues/3360
docsVersion: () =>
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('@theme/NavbarItem/DocsVersionNavbarItem').default,
docsVersionDropdown: () =>
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('@theme/NavbarItem/DocsVersionDropdownNavbarItem').default,
} as const;

const getNavbarItemComponent = (
type: keyof typeof NavbarItemComponents = 'default',
) => {
const NavbarItemComponent = NavbarItemComponents[type];
if (!NavbarItemComponent) {
const navbarItemComponent = NavbarItemComponents[type];
if (!navbarItemComponent) {
throw new Error(`No NavbarItem component found for type=${type}.`);
}
return NavbarItemComponent;
return navbarItemComponent();
};

export default function NavbarItem({type, ...props}: Props): JSX.Element {
Expand Down
12 changes: 12 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/hooks/useDocs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// See https://github.com/facebook/docusaurus/issues/3360
// TODO find a better solution, this shouldn't be needed
throw new Error(
"The docs plugin is not used, so you can't require the useDocs hooks. ",
);
6 changes: 5 additions & 1 deletion packages/docusaurus/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {CONFIG_FILE_NAME} from '../constants';
import {validateConfig} from './configValidation';

export default function loadConfig(siteDir: string): DocusaurusConfig {
const configPath = path.resolve(siteDir, CONFIG_FILE_NAME);
// TODO temporary undocumented env variable: we should be able to use a cli option instead!
const loadedConfigFileName =
process.env.DOCUSAURUS_CONFIG || CONFIG_FILE_NAME;

const configPath = path.resolve(siteDir, loadedConfigFileName);

if (!fs.existsSync(configPath)) {
throw new Error(`${CONFIG_FILE_NAME} not found`);
Expand Down
63 changes: 63 additions & 0 deletions website/docusaurus.config-blog-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
title: 'Docusaurus blog only!',
tagline: 'Build optimized websites quickly, focus on your content',
organizationName: 'facebook',
projectName: 'docusaurus',
baseUrl: '/blog-only/',
url: 'https://v2.docusaurus.io',
onBrokenLinks: 'throw',
favicon: 'img/docusaurus.ico',
themes: ['@docusaurus/theme-live-codeblock'],
plugins: [],
presets: [
[
'@docusaurus/preset-classic',
{
docs: false,
pages: false,
blog: {
routeBasePath: '/',
path: '../website-1.x/blog',
editUrl:
'https://github.com/facebook/docusaurus/edit/master/website-1.x/',
postsPerPage: 3,
feedOptions: {
type: 'all',
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`,
},
},
theme: {
customCss: require.resolve('./src/css/custom.css'),
},
},
],
],
themeConfig: {
image: 'img/docusaurus-soc.png',
navbar: {
hideOnScroll: true,
title: 'Docusaurus',
logo: {
alt: 'Docusaurus Logo',
src: 'img/docusaurus.svg',
srcDark: 'img/docusaurus_keytar.svg',
},
},
footer: {
style: 'dark',
logo: {
alt: 'Facebook Open Source Logo',
src: 'img/oss_logo.png',
href: 'https://opensource.facebook.com',
},
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc. Built with Docusaurus.`,
},
},
};
3 changes: 3 additions & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
"build:baseUrl": "BASE_URL='/build/' yarn build",
"start:bootstrap": "DOCUSAURUS_PRESET=bootstrap yarn start",
"build:bootstrap": "DOCUSAURUS_PRESET=bootstrap yarn build",
"start:blogOnly": "DOCUSAURUS_CONFIG='docusaurus.config-blog-only.js' yarn start",
"build:blogOnly": "DOCUSAURUS_CONFIG='docusaurus.config-blog-only.js' yarn build",
"netlify:build:production": "yarn build",
"netlify:build:deployPreview": "yarn rimraf netlifyDeploy && yarn netlify:build:deployPreview:classic && yarn netlify:build:deployPreview:bootstrap && yarn netlify:build:deployPreview:redirects",
"netlify:build:deployPreview:classic": "BASE_URL='/classic/' yarn build --out-dir netlifyDeploy/classic",
"netlify:build:deployPreview:bootstrap": "BASE_URL='/bootstrap/' DOCUSAURUS_PRESET=bootstrap DISABLE_VERSIONING=true yarn build --out-dir netlifyDeploy/bootstrap",
"netlify:build:deployPreview:blogOnly": "yarn build:blogOnly --out-dir netlifyDeploy/blog-only",
"netlify:build:deployPreview:redirects": "echo 'Writing Netlify baseUrl deployPreview _redirects file' && echo '/classic/* /classic/404.html 200' >> netlifyDeploy/_redirects && echo '/bootstrap/* /bootstrap/404.html 200' >> netlifyDeploy/_redirects && echo '/* /classic/' >> netlifyDeploy/_redirects",
"netlify:test": "yarn netlify:build:deployPreview && yarn netlify dev --debug"
},
Expand Down

0 comments on commit e1f6f75

Please sign in to comment.