Skip to content

Commit

Permalink
feat(v2): presets (#1401)
Browse files Browse the repository at this point in the history
* feat(v2): presets

* misc: add newline

* misc: add tests

* misc: fix path
  • Loading branch information
yangshun committed Apr 27, 2019
1 parent 7ebcf10 commit 3619865
Show file tree
Hide file tree
Showing 28 changed files with 394 additions and 89 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/src/rightToc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/src/rightToc/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')();
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-blog/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 21 additions & 21 deletions packages/docusaurus-plugin-content-docs/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-docs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-pages/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions packages/docusaurus-plugin-sitemap/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ 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(
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">`,
);
},
);

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"`,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-sitemap/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DEFAULT_OPTIONS = {
};

class DocusaurusPluginSitemap {
constructor(opts, context) {
constructor(context, opts) {
this.options = {...DEFAULT_OPTIONS, ...opts};
this.context = context;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/docusaurus-preset-classic/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
29 changes: 29 additions & 0 deletions packages/docusaurus-preset-classic/src/index.js
Original file line number Diff line number Diff line change
@@ -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,
},
],
};
};
10 changes: 8 additions & 2 deletions packages/docusaurus/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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).
22 changes: 11 additions & 11 deletions packages/docusaurus/lib/client/prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -22,29 +22,29 @@ 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);
});
};

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 = () => {
Expand All @@ -59,7 +59,7 @@ const xhrPrefetchStrategy = function(url) {
});
};

const supportedPrefetchStrategy = support(`prefetch`)
const supportedPrefetchStrategy = support('prefetch')
? linkPrefetchStrategy
: xhrPrefetchStrategy;

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/lib/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 3619865

Please sign in to comment.