From 361986515c671d8d687d212b955a4e2715394455 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Sat, 27 Apr 2019 10:47:56 -0700 Subject: [PATCH] feat(v2): presets (#1401) * feat(v2): presets * misc: add newline * misc: add tests * misc: fix path --- packages/docusaurus-mdx-loader/src/index.js | 1 + .../src/rightToc/__tests__/index.test.js | 1 + .../src/rightToc/index.js | 1 + .../src/rightToc/search.js | 1 + .../src/index.js | 2 +- .../src/__tests__/index.test.js | 42 ++-- .../src/index.js | 2 +- .../src/__tests__/index.test.js | 2 +- .../src/index.js | 2 +- .../src/__tests__/index.test.js | 12 +- .../docusaurus-plugin-sitemap/src/index.js | 2 +- .../docusaurus-preset-classic/package.json | 19 ++ .../docusaurus-preset-classic/src/index.js | 29 +++ packages/docusaurus/CHANGES.md | 10 +- packages/docusaurus/lib/client/prefetch.js | 22 +- packages/docusaurus/lib/commands/build.js | 2 +- packages/docusaurus/lib/commands/start.js | 2 +- .../docusaurus/lib/server/{load => }/index.js | 21 +- .../load/__tests__/__fixtures__/preset-bar.js | 14 ++ .../load/__tests__/__fixtures__/preset-foo.js | 14 ++ .../lib/server/load/__tests__/presets.test.js | 193 ++++++++++++++++++ packages/docusaurus/lib/server/load/config.js | 1 + .../docusaurus/lib/server/load/plugins.js | 14 +- .../docusaurus/lib/server/load/presets.js | 35 ++++ packages/docusaurus/package.json | 1 + packages/docusaurus/test/loadSetup.js | 2 +- website/docusaurus.config.js | 31 ++- website/package.json | 5 +- 28 files changed, 394 insertions(+), 89 deletions(-) create mode 100644 packages/docusaurus-preset-classic/package.json create mode 100644 packages/docusaurus-preset-classic/src/index.js rename packages/docusaurus/lib/server/{load => }/index.js (81%) create mode 100644 packages/docusaurus/lib/server/load/__tests__/__fixtures__/preset-bar.js create mode 100644 packages/docusaurus/lib/server/load/__tests__/__fixtures__/preset-foo.js create mode 100644 packages/docusaurus/lib/server/load/__tests__/presets.test.js create mode 100644 packages/docusaurus/lib/server/load/presets.js diff --git a/packages/docusaurus-mdx-loader/src/index.js b/packages/docusaurus-mdx-loader/src/index.js index 63f3c0ccda9a..ca4cbaad9044 100644 --- a/packages/docusaurus-mdx-loader/src/index.js +++ b/packages/docusaurus-mdx-loader/src/index.js @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + const {getOptions} = require('loader-utils'); const mdx = require('@mdx-js/mdx'); const rehypePrism = require('@mapbox/rehype-prism'); diff --git a/packages/docusaurus-mdx-loader/src/rightToc/__tests__/index.test.js b/packages/docusaurus-mdx-loader/src/rightToc/__tests__/index.test.js index d1d36e7b8fb1..4caf3bbe44d6 100644 --- a/packages/docusaurus-mdx-loader/src/rightToc/__tests__/index.test.js +++ b/packages/docusaurus-mdx-loader/src/rightToc/__tests__/index.test.js @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + import '@babel/polyfill'; import {join} from 'path'; import remark from 'remark'; diff --git a/packages/docusaurus-mdx-loader/src/rightToc/index.js b/packages/docusaurus-mdx-loader/src/rightToc/index.js index 9fb05b9d30f3..9f889ef1afb8 100644 --- a/packages/docusaurus-mdx-loader/src/rightToc/index.js +++ b/packages/docusaurus-mdx-loader/src/rightToc/index.js @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + const {parse} = require('@babel/parser'); const traverse = require('@babel/traverse').default; const stringifyObject = require('stringify-object'); diff --git a/packages/docusaurus-mdx-loader/src/rightToc/search.js b/packages/docusaurus-mdx-loader/src/rightToc/search.js index fe24d5df1450..fe2687b9fd66 100644 --- a/packages/docusaurus-mdx-loader/src/rightToc/search.js +++ b/packages/docusaurus-mdx-loader/src/rightToc/search.js @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + const toString = require('mdast-util-to-string'); const visit = require('unist-util-visit'); const slugs = require('github-slugger')(); diff --git a/packages/docusaurus-plugin-content-blog/src/index.js b/packages/docusaurus-plugin-content-blog/src/index.js index dfdca369f55b..74cc941274ef 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.js +++ b/packages/docusaurus-plugin-content-blog/src/index.js @@ -29,7 +29,7 @@ const DEFAULT_OPTIONS = { }; class DocusaurusPluginContentBlog { - constructor(opts, context) { + constructor(context, opts) { this.options = {...DEFAULT_OPTIONS, ...opts}; this.context = context; this.contentPath = path.resolve(this.context.siteDir, this.options.path); diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.js b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.js index c6e96b54a49e..0e3c71144cfd 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.js +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.js @@ -15,15 +15,15 @@ describe('loadDocs', () => { const {env, siteDir, siteConfig} = await loadSetup('simple'); const sidebarPath = path.join(siteDir, 'sidebars.json'); const plugin = new DocusaurusPluginContentDocs( - { - path: '../docs', - sidebarPath, - }, { env, siteDir, siteConfig, }, + { + path: '../docs', + sidebarPath, + }, ); const {docs: docsMetadata} = await plugin.loadContent(); const docsDir = plugin.contentPath; @@ -62,15 +62,15 @@ describe('loadDocs', () => { const {env, siteDir, siteConfig} = await loadSetup('versioned'); const sidebarPath = path.join(siteDir, 'sidebars.json'); const plugin = new DocusaurusPluginContentDocs( - { - path: '../docs', - sidebarPath, - }, { env, siteDir, siteConfig, }, + { + path: '../docs', + sidebarPath, + }, ); const {docs: docsMetadata, versionedDir} = await plugin.loadContent(); const docsDir = plugin.contentPath; @@ -109,15 +109,15 @@ describe('loadDocs', () => { const {env, siteDir, siteConfig} = await loadSetup('transversioned'); const sidebarPath = path.join(siteDir, 'sidebars.json'); const plugin = new DocusaurusPluginContentDocs( - { - path: '../docs', - sidebarPath, - }, { env, siteDir, siteConfig, }, + { + path: '../docs', + sidebarPath, + }, ); const { docs: docsMetadata, @@ -177,15 +177,15 @@ describe('loadDocs', () => { const {env, siteDir, siteConfig} = await loadSetup('translated'); const sidebarPath = path.join(siteDir, 'sidebars.json'); const plugin = new DocusaurusPluginContentDocs( - { - path: '../docs', - sidebarPath, - }, { env, siteDir, siteConfig, }, + { + path: '../docs', + sidebarPath, + }, ); const {docs: docsMetadata, translatedDir} = await plugin.loadContent(); const docsDir = plugin.contentPath; @@ -227,16 +227,16 @@ describe('loadDocs', () => { const {env, siteDir, siteConfig} = await loadSetup('versioned'); const sidebarPath = path.join(siteDir, 'sidebars.json'); const plugin = new DocusaurusPluginContentDocs( - { - path: '../docs', - sidebarPath, - skipNextRelease: true, - }, { env, siteDir, siteConfig, }, + { + path: '../docs', + sidebarPath, + skipNextRelease: true, + }, ); const {docs: docsMetadata, versionedDir} = await plugin.loadContent(); diff --git a/packages/docusaurus-plugin-content-docs/src/index.js b/packages/docusaurus-plugin-content-docs/src/index.js index 5ee779657edc..9f5adbc21bd1 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.js +++ b/packages/docusaurus-plugin-content-docs/src/index.js @@ -32,7 +32,7 @@ const DEFAULT_OPTIONS = { }; class DocusaurusPluginContentDocs { - constructor(opts, context) { + constructor(context, opts) { this.options = {...DEFAULT_OPTIONS, ...opts}; this.context = context; this.contentPath = path.resolve(this.context.siteDir, this.options.path); diff --git a/packages/docusaurus-plugin-content-pages/src/__tests__/index.test.js b/packages/docusaurus-plugin-content-pages/src/__tests__/index.test.js index 3771239e850a..4662f7b2d1d0 100644 --- a/packages/docusaurus-plugin-content-pages/src/__tests__/index.test.js +++ b/packages/docusaurus-plugin-content-pages/src/__tests__/index.test.js @@ -111,7 +111,7 @@ describe('docusaurus-plugin-content-pages', () => { ], ])('%s website', async (type, expected) => { const {env, siteDir, siteConfig} = await loadSetup(type); - const plugin = new DocusaurusPluginContentPages(null, { + const plugin = new DocusaurusPluginContentPages({ env, siteDir, siteConfig, diff --git a/packages/docusaurus-plugin-content-pages/src/index.js b/packages/docusaurus-plugin-content-pages/src/index.js index 4129babecf5a..de6c60c45c22 100644 --- a/packages/docusaurus-plugin-content-pages/src/index.js +++ b/packages/docusaurus-plugin-content-pages/src/index.js @@ -17,7 +17,7 @@ const DEFAULT_OPTIONS = { }; class DocusaurusPluginContentPages { - constructor(opts, context) { + constructor(context, opts) { this.options = {...DEFAULT_OPTIONS, ...opts}; this.context = context; this.contentPath = path.resolve(this.context.siteDir, this.options.path); diff --git a/packages/docusaurus-plugin-sitemap/src/__tests__/index.test.js b/packages/docusaurus-plugin-sitemap/src/__tests__/index.test.js index c9e2e8cf6418..5b40294b44c5 100644 --- a/packages/docusaurus-plugin-sitemap/src/__tests__/index.test.js +++ b/packages/docusaurus-plugin-sitemap/src/__tests__/index.test.js @@ -13,9 +13,9 @@ describe('docusaurus-plugin-sitemap', () => { test.each(['simple', 'versioned', 'translated', 'transversioned'])( '%s website', async type => { - const props = await loadSetup(type); - const plugin = new DocusaurusPluginSitemap(null, props); - const sitemap = await plugin.createSitemap(props); + const context = await loadSetup(type); + const plugin = new DocusaurusPluginSitemap(context, null); + const sitemap = await plugin.createSitemap(context); expect(sitemap).toContain( ``, ); @@ -23,10 +23,10 @@ describe('docusaurus-plugin-sitemap', () => { ); test('empty site', async () => { - const props = await loadSetup('empty'); - const plugin = new DocusaurusPluginSitemap(null, props); + const context = await loadSetup('empty'); + const plugin = new DocusaurusPluginSitemap(context, null); expect( - plugin.createSitemap(props), + plugin.createSitemap(context), ).rejects.toThrowErrorMatchingInlineSnapshot( `"Url in docusaurus.config.js cannot be empty/undefined"`, ); diff --git a/packages/docusaurus-plugin-sitemap/src/index.js b/packages/docusaurus-plugin-sitemap/src/index.js index fd0713291be2..dd724c88f74c 100644 --- a/packages/docusaurus-plugin-sitemap/src/index.js +++ b/packages/docusaurus-plugin-sitemap/src/index.js @@ -16,7 +16,7 @@ const DEFAULT_OPTIONS = { }; class DocusaurusPluginSitemap { - constructor(opts, context) { + constructor(context, opts) { this.options = {...DEFAULT_OPTIONS, ...opts}; this.context = context; } diff --git a/packages/docusaurus-preset-classic/package.json b/packages/docusaurus-preset-classic/package.json new file mode 100644 index 000000000000..d486a5e9afa9 --- /dev/null +++ b/packages/docusaurus-preset-classic/package.json @@ -0,0 +1,19 @@ +{ + "name": "@docusaurus/preset-classic", + "version": "2.0.0-alpha.11", + "description": "Preset for classic Docusaurus", + "main": "src/index.js", + "publishConfig": { + "access": "public" + }, + "license": "MIT", + "dependencies": { + "@docusaurus/plugin-content-blog": "^2.0.0-alpha.11", + "@docusaurus/plugin-content-docs": "^2.0.0-alpha.11", + "@docusaurus/plugin-content-pages": "^2.0.0-alpha.11", + "@docusaurus/plugin-sitemap": "^2.0.0-alpha.11" + }, + "peerDependencies": { + "@docusaurus/core": "^2.0.0" + } +} diff --git a/packages/docusaurus-preset-classic/src/index.js b/packages/docusaurus-preset-classic/src/index.js new file mode 100644 index 000000000000..04cbece645c2 --- /dev/null +++ b/packages/docusaurus-preset-classic/src/index.js @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2017-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +module.exports = function preset(context, opts = {}) { + return { + plugins: [ + { + name: '@docusaurus/plugin-content-docs', + options: opts.docs, + }, + { + name: '@docusaurus/plugin-content-blog', + options: opts.blog, + }, + { + name: '@docusaurus/plugin-content-pages', + options: opts.pages, + }, + { + name: '@docusaurus/plugin-sitemap', + options: opts.sitemap, + }, + ], + }; +}; diff --git a/packages/docusaurus/CHANGES.md b/packages/docusaurus/CHANGES.md index 0b580f0f663f..00c4d45bcc5a 100644 --- a/packages/docusaurus/CHANGES.md +++ b/packages/docusaurus/CHANGES.md @@ -9,7 +9,7 @@ - `sidebars.json` now has to be explicitly loaded by users and passed into the the plugin option on `docusaurus-plugin-content-docs`. - `headerLinks` doc, page, blog is deprecated. The syntax is now: - ```js +```js headerLinks: [ // Link to internal page (without baseUrl) { url: "help", label: "Help" }, @@ -20,4 +20,10 @@ headerLinks: [ // Determines language drop down position among links { languages: true } ], -```js +``` + +# Additions + +### Presets + +- Added presets for plugins that follow the [Babel preset convention](https://babeljs.io/docs/en/presets). diff --git a/packages/docusaurus/lib/client/prefetch.js b/packages/docusaurus/lib/client/prefetch.js index 1b6ede02f2a0..5d684e68890a 100644 --- a/packages/docusaurus/lib/client/prefetch.js +++ b/packages/docusaurus/lib/client/prefetch.js @@ -6,12 +6,12 @@ */ const support = function(feature) { - if (typeof document === `undefined`) { + if (typeof document === 'undefined') { return false; } - const fakeLink = document.createElement(`link`); + const fakeLink = document.createElement('link'); try { - if (fakeLink.relList && typeof fakeLink.relList.supports === `function`) { + if (fakeLink.relList && typeof fakeLink.relList.supports === 'function') { return fakeLink.relList.supports(feature); } } catch (err) { @@ -22,21 +22,21 @@ const support = function(feature) { const linkPrefetchStrategy = function(url) { return new Promise((resolve, reject) => { - if (typeof document === `undefined`) { + if (typeof document === 'undefined') { reject(); return; } - const link = document.createElement(`link`); - link.setAttribute(`rel`, `prefetch`); - link.setAttribute(`href`, url); + const link = document.createElement('link'); + link.setAttribute('rel', 'prefetch'); + link.setAttribute('href', url); link.onload = resolve; link.onerror = reject; const parentElement = - document.getElementsByTagName(`head`)[0] || - document.getElementsByName(`script`)[0].parentNode; + document.getElementsByTagName('head')[0] || + document.getElementsByName('script')[0].parentNode; parentElement.appendChild(link); }); }; @@ -44,7 +44,7 @@ const linkPrefetchStrategy = function(url) { const xhrPrefetchStrategy = function(url) { return new Promise((resolve, reject) => { const req = new XMLHttpRequest(); - req.open(`GET`, url, true); + req.open('GET', url, true); req.withCredentials = true; req.onload = () => { @@ -59,7 +59,7 @@ const xhrPrefetchStrategy = function(url) { }); }; -const supportedPrefetchStrategy = support(`prefetch`) +const supportedPrefetchStrategy = support('prefetch') ? linkPrefetchStrategy : xhrPrefetchStrategy; diff --git a/packages/docusaurus/lib/commands/build.js b/packages/docusaurus/lib/commands/build.js index 66de5b665191..71d16e822582 100644 --- a/packages/docusaurus/lib/commands/build.js +++ b/packages/docusaurus/lib/commands/build.js @@ -14,7 +14,7 @@ const path = require('path'); const chalk = require('chalk'); const fs = require('fs-extra'); const globby = require('globby'); -const load = require('../server/load'); +const load = require('../server'); const createServerConfig = require('../webpack/server'); const createClientConfig = require('../webpack/client'); const {applyConfigureWebpack} = require('../webpack/utils'); diff --git a/packages/docusaurus/lib/commands/start.js b/packages/docusaurus/lib/commands/start.js index fc7efcd74b16..ef913e60261c 100644 --- a/packages/docusaurus/lib/commands/start.js +++ b/packages/docusaurus/lib/commands/start.js @@ -19,7 +19,7 @@ const HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlug const WebpackDevServer = require('webpack-dev-server'); const merge = require('webpack-merge'); const {normalizeUrl} = require('@docusaurus/utils'); -const load = require('../server/load'); +const load = require('../server'); const {CONFIG_FILE_NAME} = require('../constants'); const createClientConfig = require('../webpack/client'); const {applyConfigureWebpack} = require('../webpack/utils'); diff --git a/packages/docusaurus/lib/server/load/index.js b/packages/docusaurus/lib/server/index.js similarity index 81% rename from packages/docusaurus/lib/server/load/index.js rename to packages/docusaurus/lib/server/index.js index 0e218647786b..c94e98f3825b 100644 --- a/packages/docusaurus/lib/server/load/index.js +++ b/packages/docusaurus/lib/server/index.js @@ -8,12 +8,13 @@ const path = require('path'); const {generate} = require('@docusaurus/utils'); -const loadConfig = require('./config'); -const loadEnv = require('./env'); -const loadTheme = require('./theme'); -const loadRoutes = require('./routes'); -const loadPlugins = require('./plugins'); -const constants = require('../../constants'); +const loadConfig = require('./load/config'); +const loadEnv = require('./load/env'); +const loadTheme = require('./load/theme'); +const loadRoutes = require('./load/routes'); +const loadPlugins = require('./load/plugins'); +const loadPresets = require('./load/presets'); +const constants = require('../constants'); module.exports = async function load(siteDir, cliOptions = {}) { const generatedFilesDir = path.resolve( @@ -35,9 +36,13 @@ module.exports = async function load(siteDir, cliOptions = {}) { `export default ${JSON.stringify(env, null, 2)};`, ); - // Process plugins. - const pluginConfigs = siteConfig.plugins || []; const context = {env, siteDir, generatedFilesDir, siteConfig, cliOptions}; + + // Process presets. + const presetPlugins = loadPresets(context); + + // Process plugins. + const pluginConfigs = [...presetPlugins, ...siteConfig.plugins]; const {plugins, pluginsRouteConfigs} = await loadPlugins({ pluginConfigs, context, diff --git a/packages/docusaurus/lib/server/load/__tests__/__fixtures__/preset-bar.js b/packages/docusaurus/lib/server/load/__tests__/__fixtures__/preset-bar.js new file mode 100644 index 000000000000..347bd339faf0 --- /dev/null +++ b/packages/docusaurus/lib/server/load/__tests__/__fixtures__/preset-bar.js @@ -0,0 +1,14 @@ +module.exports = function preset(context, opts = {}) { + return { + plugins: [ + { + name: '@docusaurus/plugin-content-docs', + options: opts.docs, + }, + { + name: '@docusaurus/plugin-content-blog', + options: opts.blog, + }, + ], + }; +}; diff --git a/packages/docusaurus/lib/server/load/__tests__/__fixtures__/preset-foo.js b/packages/docusaurus/lib/server/load/__tests__/__fixtures__/preset-foo.js new file mode 100644 index 000000000000..c3968042b35a --- /dev/null +++ b/packages/docusaurus/lib/server/load/__tests__/__fixtures__/preset-foo.js @@ -0,0 +1,14 @@ +module.exports = function preset(context, opts = {}) { + return { + plugins: [ + { + name: '@docusaurus/plugin-content-pages', + options: opts.pages, + }, + { + name: '@docusaurus/plugin-sitemap', + options: opts.sitemap, + }, + ], + }; +}; diff --git a/packages/docusaurus/lib/server/load/__tests__/presets.test.js b/packages/docusaurus/lib/server/load/__tests__/presets.test.js new file mode 100644 index 000000000000..93c3dedfd89a --- /dev/null +++ b/packages/docusaurus/lib/server/load/__tests__/presets.test.js @@ -0,0 +1,193 @@ +/** + * Copyright (c) 2017-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import '@babel/polyfill'; +import path from 'path'; + +import loadPresets from '../presets'; + +describe('loadPresets', () => { + test('no presets', () => { + const presets = loadPresets({siteConfig: {presets: []}}); + expect(presets).toEqual([]); + }); + + test('string form', () => { + const presets = loadPresets({ + siteConfig: { + presets: [path.join(__dirname, '__fixtures__/preset-bar.js')], + }, + }); + expect(presets).toMatchInlineSnapshot(` +Array [ + Object { + "name": "@docusaurus/plugin-content-docs", + "options": undefined, + }, + Object { + "name": "@docusaurus/plugin-content-blog", + "options": undefined, + }, +] +`); + }); + + test('string form composite', () => { + const presets = loadPresets({ + siteConfig: { + presets: [ + path.join(__dirname, '__fixtures__/preset-bar.js'), + path.join(__dirname, '__fixtures__/preset-foo.js'), + ], + }, + }); + expect(presets).toMatchInlineSnapshot(` +Array [ + Object { + "name": "@docusaurus/plugin-content-docs", + "options": undefined, + }, + Object { + "name": "@docusaurus/plugin-content-blog", + "options": undefined, + }, + Object { + "name": "@docusaurus/plugin-content-pages", + "options": undefined, + }, + Object { + "name": "@docusaurus/plugin-sitemap", + "options": undefined, + }, +] +`); + }); + + test('array form', () => { + const presets = loadPresets({ + siteConfig: { + presets: [[path.join(__dirname, '__fixtures__/preset-bar.js')]], + }, + }); + expect(presets).toMatchInlineSnapshot(` +Array [ + Object { + "name": "@docusaurus/plugin-content-docs", + "options": undefined, + }, + Object { + "name": "@docusaurus/plugin-content-blog", + "options": undefined, + }, +] +`); + }); + + test('array form with options', () => { + const presets = loadPresets({ + siteConfig: { + presets: [ + [ + path.join(__dirname, '__fixtures__/preset-bar.js'), + {docs: {path: '../'}}, + ], + ], + }, + }); + expect(presets).toMatchInlineSnapshot(` +Array [ + Object { + "name": "@docusaurus/plugin-content-docs", + "options": Object { + "path": "../", + }, + }, + Object { + "name": "@docusaurus/plugin-content-blog", + "options": undefined, + }, +] +`); + }); + + test('array form composite', () => { + const presets = loadPresets({ + siteConfig: { + presets: [ + [ + path.join(__dirname, '__fixtures__/preset-bar.js'), + {docs: {path: '../'}}, + ], + [ + path.join(__dirname, '__fixtures__/preset-foo.js'), + {pages: {path: '../'}}, + ], + ], + }, + }); + expect(presets).toMatchInlineSnapshot(` +Array [ + Object { + "name": "@docusaurus/plugin-content-docs", + "options": Object { + "path": "../", + }, + }, + Object { + "name": "@docusaurus/plugin-content-blog", + "options": undefined, + }, + Object { + "name": "@docusaurus/plugin-content-pages", + "options": Object { + "path": "../", + }, + }, + Object { + "name": "@docusaurus/plugin-sitemap", + "options": undefined, + }, +] +`); + }); + + test('mixed form', () => { + const presets = loadPresets({ + siteConfig: { + presets: [ + [ + path.join(__dirname, '__fixtures__/preset-bar.js'), + {docs: {path: '../'}}, + ], + path.join(__dirname, '__fixtures__/preset-foo.js'), + ], + }, + }); + expect(presets).toMatchInlineSnapshot(` +Array [ + Object { + "name": "@docusaurus/plugin-content-docs", + "options": Object { + "path": "../", + }, + }, + Object { + "name": "@docusaurus/plugin-content-blog", + "options": undefined, + }, + Object { + "name": "@docusaurus/plugin-content-pages", + "options": undefined, + }, + Object { + "name": "@docusaurus/plugin-sitemap", + "options": undefined, + }, +] +`); + }); +}); diff --git a/packages/docusaurus/lib/server/load/config.js b/packages/docusaurus/lib/server/load/config.js index dea8b23872db..8c9e6ff74281 100644 --- a/packages/docusaurus/lib/server/load/config.js +++ b/packages/docusaurus/lib/server/load/config.js @@ -31,6 +31,7 @@ const OPTIONAL_FIELDS = [ 'highlight', 'markdownPlugins', 'plugins', + 'presets', ]; const DEFAULT_CONFIG = { diff --git a/packages/docusaurus/lib/server/load/plugins.js b/packages/docusaurus/lib/server/load/plugins.js index e9b78bbc04f6..dae3e4aa8b25 100644 --- a/packages/docusaurus/lib/server/load/plugins.js +++ b/packages/docusaurus/lib/server/load/plugins.js @@ -6,6 +6,7 @@ */ const fs = require('fs-extra'); +const importFresh = require('import-fresh'); const path = require('path'); const {generate} = require('@docusaurus/utils'); @@ -14,17 +15,11 @@ module.exports = async function loadPlugins({pluginConfigs = [], context}) { const plugins = pluginConfigs.map(({name, path: pluginPath, options}) => { let Plugin; if (pluginPath && fs.existsSync(pluginPath)) { - // eslint-disable-next-line - Plugin = require(pluginPath); + Plugin = importFresh(pluginPath); } else { - try { - // eslint-disable-next-line - Plugin = require(name); - } catch (ex) { - throw new Error(`Error loading '${name}' plugin.`); - } + Plugin = importFresh(name); } - return new Plugin(options, context); + return new Plugin(context, options); }); // 2. Plugin lifecycle - loadContent @@ -72,6 +67,5 @@ module.exports = async function loadPlugins({pluginConfigs = [], context}) { return { plugins, pluginsRouteConfigs, - pluginsLoadedContent, }; }; diff --git a/packages/docusaurus/lib/server/load/presets.js b/packages/docusaurus/lib/server/load/presets.js new file mode 100644 index 000000000000..8e40f12f4756 --- /dev/null +++ b/packages/docusaurus/lib/server/load/presets.js @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2017-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const importFresh = require('import-fresh'); +const _ = require('lodash'); +const fs = require('fs-extra'); + +module.exports = function loadPresets(context) { + const presets = context.siteConfig.presets || []; + return _.flatten( + presets.map(presetItem => { + let presetModule; + let presetOptions = {}; + if (typeof presetItem === 'string') { + presetModule = presetItem; + } else if (Array.isArray(presetItem)) { + [presetModule, presetOptions] = presetItem; + } + + let preset; + if (presetModule && fs.existsSync(presetModule)) { + // Local preset. + preset = importFresh(presetModule); + } else { + // From npm. + preset = importFresh(presetModule); + } + return preset(context, presetOptions).plugins; + }), + ); +}; diff --git a/packages/docusaurus/package.json b/packages/docusaurus/package.json index e19a0d131d77..e76298d6da10 100644 --- a/packages/docusaurus/package.json +++ b/packages/docusaurus/package.json @@ -52,6 +52,7 @@ "fs-extra": "^7.0.0", "globby": "^9.1.0", "html-webpack-plugin": "^4.0.0-beta.5", + "import-fresh": "^3.0.0", "is-wsl": "^1.1.0", "lodash": "^4.17.11", "mini-css-extract-plugin": "^0.4.1", diff --git a/packages/docusaurus/test/loadSetup.js b/packages/docusaurus/test/loadSetup.js index 74c60f68adaf..033477028d5c 100644 --- a/packages/docusaurus/test/loadSetup.js +++ b/packages/docusaurus/test/loadSetup.js @@ -7,7 +7,7 @@ import '@babel/polyfill'; import path from 'path'; -import load from '@lib/server/load'; +import load from '@lib/server'; // Helper methods to setup dummy/fake projects const loadSetup = async name => { diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 9436f834bd48..5d71dfc69426 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -24,25 +24,18 @@ module.exports = { indexName: 'docusaurus-2', algoliaOptions: {}, }, - plugins: [ - { - name: '@docusaurus/plugin-content-docs', - options: { - path: '../docs', - sidebarPath: require.resolve('./sidebars.json'), + presets: [ + [ + '@docusaurus/preset-classic', + { + docs: { + path: '../docs', + sidebarPath: require.resolve('./sidebars.json'), + }, + blog: { + path: '../website-1.x/blog', + }, }, - }, - { - name: '@docusaurus/plugin-content-blog', - options: { - path: '../website-1.x/blog', - }, - }, - { - name: '@docusaurus/plugin-content-pages', - }, - { - name: '@docusaurus/plugin-sitemap', - }, + ], ], }; diff --git a/website/package.json b/website/package.json index 8c0b44008ba9..cea36671699e 100644 --- a/website/package.json +++ b/website/package.json @@ -10,10 +10,7 @@ }, "dependencies": { "@docusaurus/core": "^2.0.0-alpha.11", - "@docusaurus/plugin-content-blog": "^2.0.0-alpha.11", - "@docusaurus/plugin-content-docs": "^2.0.0-alpha.11", - "@docusaurus/plugin-content-pages": "^2.0.0-alpha.11", - "@docusaurus/plugin-sitemap": "^2.0.0-alpha.11", + "@docusaurus/preset-classic": "^2.0.0-alpha.11", "classnames": "^2.2.6", "react": "^16.8.4", "react-dom": "^16.8.4"