mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 08:37:25 +02:00
perf/fix(v2): more efficient hot reload & consistent filegen (#1950)
* perf(v2): efficient hot reload, consistent generated file * changelog * more
This commit is contained in:
parent
87f864e5ba
commit
e04c8f140f
7 changed files with 60 additions and 6 deletions
|
@ -1,6 +1,8 @@
|
||||||
# Docusaurus 2 Changelog
|
# Docusaurus 2 Changelog
|
||||||
|
|
||||||
## Unreleased
|
## 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
|
## 2.0.0-alpha.33
|
||||||
|
|
||||||
|
|
|
@ -18,9 +18,6 @@ import {posixPath} from '@docusaurus/utils';
|
||||||
const createFakeActions = (routeConfigs: RouteConfig[], contentDir) => {
|
const createFakeActions = (routeConfigs: RouteConfig[], contentDir) => {
|
||||||
return {
|
return {
|
||||||
addRoute: (config: RouteConfig) => {
|
addRoute: (config: RouteConfig) => {
|
||||||
config.routes.sort((a, b) =>
|
|
||||||
a.path > b.path ? 1 : b.path > a.path ? -1 : 0,
|
|
||||||
);
|
|
||||||
routeConfigs.push(config);
|
routeConfigs.push(config);
|
||||||
},
|
},
|
||||||
createData: async (name, _content) => {
|
createData: async (name, _content) => {
|
||||||
|
|
|
@ -8,7 +8,12 @@
|
||||||
import globby from 'globby';
|
import globby from 'globby';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import path from 'path';
|
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 {LoadContext, Plugin} from '@docusaurus/types';
|
||||||
|
|
||||||
import createOrder from './order';
|
import createOrder from './order';
|
||||||
|
@ -202,7 +207,7 @@ export default function pluginContentDocs(
|
||||||
docsDir,
|
docsDir,
|
||||||
docsSidebars,
|
docsSidebars,
|
||||||
sourceToPermalink,
|
sourceToPermalink,
|
||||||
permalinkToSidebar,
|
permalinkToSidebar: objectWithKeySorted(permalinkToSidebar),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -252,7 +257,9 @@ export default function pluginContentDocs(
|
||||||
addRoute({
|
addRoute({
|
||||||
path: docsBaseRoute,
|
path: docsBaseRoute,
|
||||||
component: docLayoutComponent,
|
component: docLayoutComponent,
|
||||||
routes,
|
routes: routes.sort((a, b) =>
|
||||||
|
a.path > b.path ? 1 : b.path > a.path ? -1 : 0,
|
||||||
|
),
|
||||||
modules: {
|
modules: {
|
||||||
docsMetadata: aliasedSource(docsBaseMetadataPath),
|
docsMetadata: aliasedSource(docsBaseMetadataPath),
|
||||||
},
|
},
|
||||||
|
|
|
@ -15,6 +15,7 @@ import {
|
||||||
getSubFolder,
|
getSubFolder,
|
||||||
normalizeUrl,
|
normalizeUrl,
|
||||||
posixPath,
|
posixPath,
|
||||||
|
objectWithKeySorted,
|
||||||
} from '../index';
|
} from '../index';
|
||||||
|
|
||||||
describe('load utils', () => {
|
describe('load utils', () => {
|
||||||
|
@ -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(`
|
||||||
|
Object {
|
||||||
|
"a": "baz",
|
||||||
|
"b": "foo",
|
||||||
|
"c": "bar",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
test('genChunkName', () => {
|
test('genChunkName', () => {
|
||||||
const firstAssert = {
|
const firstAssert = {
|
||||||
'/docs/adding-blog': 'docs-adding-blog-062',
|
'/docs/adding-blog': 'docs-adding-blog-062',
|
||||||
|
|
|
@ -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 indexRE = /(^|.*\/)index\.(md|js)$/i;
|
||||||
const extRE = /\.(md|js)$/;
|
const extRE = /\.(md|js)$/;
|
||||||
|
|
||||||
|
|
|
@ -116,6 +116,7 @@ export async function load(siteDir: string): Promise<Props> {
|
||||||
'registry.js',
|
'registry.js',
|
||||||
`export default {
|
`export default {
|
||||||
${Object.keys(registry)
|
${Object.keys(registry)
|
||||||
|
.sort()
|
||||||
.map(
|
.map(
|
||||||
key =>
|
key =>
|
||||||
` '${key}': [${registry[key].loader}, ${JSON.stringify(
|
` '${key}': [${registry[key].loader}, ${JSON.stringify(
|
||||||
|
|
|
@ -89,6 +89,8 @@ export function getBabelLoader(isServer: boolean, babelOptions?: {}): Loader {
|
||||||
{
|
{
|
||||||
babelrc: false,
|
babelrc: false,
|
||||||
configFile: false,
|
configFile: false,
|
||||||
|
// All optional newlines and whitespace will be omitted when generating code in compact mode
|
||||||
|
compact: true,
|
||||||
presets: [
|
presets: [
|
||||||
isServer
|
isServer
|
||||||
? [
|
? [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue