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

perf(v2): more efficient hot reload & consistent filegen #1950

Merged
merged 3 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Docusaurus 2 Changelog

## Unreleased
- More efficient hot reload & consistent generated file.
- Set babel `compact` options to `true` which removes "superfluous whitespace characters and line terminators.

## 2.0.0-alpha.33

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import {posixPath} from '@docusaurus/utils';
const createFakeActions = (routeConfigs: RouteConfig[], contentDir) => {
return {
addRoute: (config: RouteConfig) => {
config.routes.sort((a, b) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously had to sort it in test because snapshot will always fail previously due to inconsistent routes (async nature). Now no longer need to do that in test

a.path > b.path ? 1 : b.path > a.path ? -1 : 0,
);
routeConfigs.push(config);
},
createData: async (name, _content) => {
Expand Down
13 changes: 10 additions & 3 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import globby from 'globby';
import fs from 'fs-extra';
import path from 'path';
import {idx, normalizeUrl, docuHash} from '@docusaurus/utils';
import {
idx,
normalizeUrl,
docuHash,
objectWithKeySorted,
} from '@docusaurus/utils';
import {LoadContext, Plugin} from '@docusaurus/types';

import createOrder from './order';
Expand Down Expand Up @@ -202,7 +207,7 @@ export default function pluginContentDocs(
docsDir,
docsSidebars,
sourceToPermalink,
permalinkToSidebar,
permalinkToSidebar: objectWithKeySorted(permalinkToSidebar),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to do this because otherwise docsBaseMetadata (the root file generated) will always change without utilizing cache

};
},

Expand Down Expand Up @@ -252,7 +257,9 @@ export default function pluginContentDocs(
addRoute({
path: docsBaseRoute,
component: docLayoutComponent,
routes,
routes: routes.sort((a, b) =>
a.path > b.path ? 1 : b.path > a.path ? -1 : 0,
),
modules: {
docsMetadata: aliasedSource(docsBaseMetadataPath),
},
Expand Down
36 changes: 36 additions & 0 deletions packages/docusaurus-utils/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getSubFolder,
normalizeUrl,
posixPath,
objectWithKeySorted,
} from '../index';

describe('load utils', () => {
Expand Down Expand Up @@ -82,6 +83,41 @@ describe('load utils', () => {
});
});

test('objectWithKeySorted', () => {
const obj = {
'/docs/adding-blog': '4',
'/docs/versioning': '5',
'/': '1',
'/blog/2018': '3',
'/youtube': '7',
'/users/en/': '6',
'/blog': '2',
};
expect(objectWithKeySorted(obj)).toMatchInlineSnapshot(`
Object {
"/": "1",
"/blog": "2",
"/blog/2018": "3",
"/docs/adding-blog": "4",
"/docs/versioning": "5",
"/users/en/": "6",
"/youtube": "7",
}
`);
const obj2 = {
b: 'foo',
c: 'bar',
a: 'baz',
};
expect(objectWithKeySorted(obj2)).toMatchInlineSnapshot(`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even snapshot is consistent now in ordering😃

Object {
"a": "baz",
"b": "foo",
"c": "bar",
}
`);
});

test('genChunkName', () => {
const firstAssert = {
'/docs/adding-blog': 'docs-adding-blog-062',
Expand Down
9 changes: 9 additions & 0 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export async function generate(
}
}

export function objectWithKeySorted(obj: Object) {
// https://github.com/lodash/lodash/issues/1459#issuecomment-253969771
return _(obj)
.toPairs()
.sortBy(0)
.fromPairs()
.value();
}

const indexRE = /(^|.*\/)index\.(md|js)$/i;
const extRE = /\.(md|js)$/;

Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export async function load(siteDir: string): Promise<Props> {
'registry.js',
`export default {
${Object.keys(registry)
.sort()
.map(
key =>
` '${key}': [${registry[key].loader}, ${JSON.stringify(
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus/src/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export function getBabelLoader(isServer: boolean, babelOptions?: {}): Loader {
{
babelrc: false,
configFile: false,
// All optional newlines and whitespace will be omitted when generating code in compact mode
compact: true,
presets: [
isServer
? [
Expand Down