Skip to content

Commit

Permalink
Merge branch 'master' into endi/fixsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshun committed Oct 27, 2019
2 parents 4fc6d02 + fabaf77 commit 5fd6054
Show file tree
Hide file tree
Showing 29 changed files with 491 additions and 291 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ packages/docusaurus-init/lib/
packages/docusaurus-plugin-content-blog/lib/
packages/docusaurus-plugin-content-docs/lib/
packages/docusaurus-plugin-content-pages/lib/
packages/docusaurus-plugin-sitemap/lib/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ packages/docusaurus-init/lib/
packages/docusaurus-plugin-content-blog/lib/
packages/docusaurus-plugin-content-docs/lib/
packages/docusaurus-plugin-content-pages/lib/
packages/docusaurus-plugin-sitemap/lib/
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ packages/docusaurus-init/lib/
packages/docusaurus-plugin-content-blog/lib/
packages/docusaurus-plugin-content-docs/lib/
packages/docusaurus-plugin-content-pages/lib/
packages/docusaurus-plugin-sitemap/lib/
3 changes: 2 additions & 1 deletion CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## Unreleased

- Slightly adjust search icon position to be more aligned on small width device.

- Convert sitemap plugin to TypeScript.
- Significantly reduce main bundle size and initial HTML payload on production build. Generated JS files from webpack is also shorter in name.

## 2.0.0-alpha.31

Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-plugin-sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
"name": "@docusaurus/plugin-sitemap",
"version": "2.0.0-alpha.31",
"description": "Simple sitemap generation plugin for Docusaurus",
"main": "src/index.js",
"main": "lib/index.js",
"scripts": {
"tsc": "tsc"
},
"publishConfig": {
"access": "public"
},
"license": "MIT",
"dependencies": {
"@docusaurus/types": "^2.0.0-alpha.30",
"sitemap": "^3.2.2"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,30 @@
*/

import createSitemap from '../createSitemap';
import {DocusaurusConfig} from '@docusaurus/types';
import DEFAULT_OPTIONS from '../index';

describe('createSitemap', () => {
test('simple site', () => {
const sitemap = createSitemap({
siteConfig: {
const sitemap = createSitemap(
{
url: 'https://example.com',
} as DocusaurusConfig,
['/', '/test'],
{
cacheTime: 600,
changefreq: 'daily',
priority: 0.7,
},
routesPaths: ['/', '/test'],
});
expect(sitemap).toContain(
);
expect(sitemap.toString()).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', () => {
expect(() => {
createSitemap({});
createSitemap({} as any, [], {} as any);
}).toThrowErrorMatchingInlineSnapshot(
`"Url in docusaurus.config.js cannot be empty/undefined"`,
);
Expand Down
33 changes: 0 additions & 33 deletions packages/docusaurus-plugin-sitemap/src/createSitemap.js

This file was deleted.

36 changes: 36 additions & 0 deletions packages/docusaurus-plugin-sitemap/src/createSitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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 sitemap, {SitemapItemOptions} from 'sitemap';
import {PluginOptions} from './types';
import {DocusaurusConfig} from '@docusaurus/types';

export default function createSitemap(
siteConfig: DocusaurusConfig,
routesPaths: string[],
options: PluginOptions,
) {
const {url: hostname} = siteConfig;
if (!hostname) {
throw new Error('Url in docusaurus.config.js cannot be empty/undefined');
}

const urls = routesPaths.map(
routesPath =>
({
url: routesPath,
changefreq: options.changefreq,
priority: options.priority,
} as SitemapItemOptions),
);

return sitemap.createSitemap({
hostname,
cacheTime: options.cacheTime,
urls,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@
* LICENSE file in the root directory of this source tree.
*/

const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';
import {PluginOptions} from './types';
import createSitemap from './createSitemap';
import {LoadContext, Props} from '@docusaurus/types';

const createSitemap = require('./createSitemap');

const DEFAULT_OPTIONS = {
const DEFAULT_OPTIONS: PluginOptions = {
cacheTime: 600 * 1000, // 600 sec - cache purge period
changefreq: 'weekly',
priority: 0.5,
};

module.exports = function(context, opts) {
export default function pluginSitemap(
_context: LoadContext,
opts: Partial<PluginOptions>,
) {
const options = {...DEFAULT_OPTIONS, ...opts};

return {
name: 'docusaurus-plugin-sitemap',

async postBuild({siteConfig = {}, routesPaths = [], outDir}) {
async postBuild({siteConfig, routesPaths, outDir}: Props) {
// Generate sitemap
const generatedSitemap = createSitemap({
const generatedSitemap = createSitemap(
siteConfig,
routesPaths,
options,
}).toString();
).toString();

// Write sitemap file
const sitemapPath = path.join(outDir, 'sitemap.xml');
Expand All @@ -39,4 +43,4 @@ module.exports = function(context, opts) {
});
},
};
};
}
12 changes: 12 additions & 0 deletions packages/docusaurus-plugin-sitemap/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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.
*/

export interface PluginOptions {
cacheTime: number;
changefreq: string;
priority: number;
}
9 changes: 9 additions & 0 deletions packages/docusaurus-plugin-sitemap/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib"
}
}
2 changes: 1 addition & 1 deletion packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export interface Plugin<T> {
getThemePath?(): string;
getPathsToWatch?(): string[];
getClientModules?(): string[];
extendCli?(cli: CommanderStatic): any;
extendCli?(cli: CommanderStatic): void;
}

export type PluginConfig = [string, Object] | [string] | string;
Expand Down
14 changes: 14 additions & 0 deletions packages/docusaurus-utils/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ describe('load utils', () => {
Object.keys(secondAssert).forEach(str => {
expect(genChunkName(str, undefined, 'blog')).toBe(secondAssert[str]);
});

// Only generate short unique id
const thirdAssert = {
a: '0cc175b9',
b: '92eb5ffe',
c: '4a8a08f0',
d: '8277e091',
};
Object.keys(thirdAssert).forEach(str => {
expect(genChunkName(str, undefined, undefined, true)).toBe(
thirdAssert[str],
);
});
expect(genChunkName('d', undefined, undefined, true)).toBe('8277e091');
});

test('idx', () => {
Expand Down
22 changes: 15 additions & 7 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,27 @@ export function genChunkName(
modulePath: string,
prefix?: string,
preferredName?: string,
shortId?: boolean,
): string {
let chunkName: string | undefined = chunkNameCache.get(modulePath);
if (!chunkName) {
let str = modulePath;
if (preferredName) {
const shortHash = createHash('md5')
if (shortId) {
chunkName = createHash('md5')
.update(modulePath)
.digest('hex')
.substr(0, 3);
str = `${preferredName}${shortHash}`;
.substr(0, 8);
} else {
let str = modulePath;
if (preferredName) {
const shortHash = createHash('md5')
.update(modulePath)
.digest('hex')
.substr(0, 3);
str = `${preferredName}${shortHash}`;
}
const name = str === '/' ? 'index' : docuHash(str);
chunkName = prefix ? `${prefix}---${name}` : name;
}
const name = str === '/' ? 'index' : docuHash(str);
chunkName = prefix ? `${prefix}---${name}` : name;
chunkNameCache.set(modulePath, chunkName);
}
return chunkName;
Expand Down
8 changes: 5 additions & 3 deletions packages/docusaurus/src/client/exports/ComponentCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ function ComponentCreator(path) {
}

const chunkRegistry = registry[target] || {};
optsLoader[keys.join('.')] = chunkRegistry.loader;
optsModules.push(chunkRegistry.module);
optsWebpack.push(chunkRegistry.webpack);
/* eslint-disable prefer-destructuring */
optsLoader[keys.join('.')] = chunkRegistry[0];
optsModules.push(chunkRegistry[1]);
optsWebpack.push(chunkRegistry[2]);
/* eslint-enable prefer-destructuring */
}

traverseChunk(chunkNames, []);
Expand Down
9 changes: 4 additions & 5 deletions packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,10 @@ export async function load(siteDir: string): Promise<Props> {
`export default {
${Object.keys(registry)
.map(
key => ` '${key}': {
'loader': ${registry[key].loader},
'module': ${JSON.stringify(registry[key].modulePath)},
'webpack': require.resolveWeak(${JSON.stringify(registry[key].modulePath)}),
},`,
key =>
` '${key}': [${registry[key].loader}, ${JSON.stringify(
registry[key].modulePath,
)}, require.resolveWeak(${JSON.stringify(registry[key].modulePath)})],`,
)
.join('\n')}};\n`,
);
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus/src/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function getModulePath(target: Module): string {
}

export async function loadRoutes(pluginsRouteConfigs: RouteConfig[]) {
const isProd = process.env.NODE_ENV === 'production';
const routesImports = [
`import React from 'react';`,
`import ComponentCreator from '@docusaurus/ComponentCreator';`,
Expand Down Expand Up @@ -82,7 +83,7 @@ export async function loadRoutes(pluginsRouteConfigs: RouteConfig[]) {
}

const modulePath = getModulePath(value as Module);
const chunkName = genChunkName(modulePath, prefix, name);
const chunkName = genChunkName(modulePath, prefix, name, isProd);
const loader = `() => import(/* webpackChunkName: '${chunkName}' */ ${JSON.stringify(
modulePath,
)})`;
Expand Down
8 changes: 4 additions & 4 deletions packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export function createBaseConfig(
output: {
pathinfo: false,
path: outDir,
filename: isProd ? '[name].[contenthash].js' : '[name].js',
chunkFilename: isProd ? '[name].[contenthash].js' : '[name].js',
filename: isProd ? '[name].[contenthash:8].js' : '[name].js',
chunkFilename: isProd ? '[name].[contenthash:8].js' : '[name].js',
publicPath: baseUrl,
},
// Don't throw warning when asset created is over 250kb
Expand Down Expand Up @@ -157,8 +157,8 @@ export function createBaseConfig(
},
plugins: [
new MiniCssExtractPlugin({
filename: isProd ? '[name].[contenthash].css' : '[name].css',
chunkFilename: isProd ? '[name].[contenthash].css' : '[name].css',
filename: isProd ? '[name].[contenthash:8].css' : '[name].css',
chunkFilename: isProd ? '[name].[contenthash:8].css' : '[name].css',
}),
],
};
Expand Down
Loading

0 comments on commit 5fd6054

Please sign in to comment.