refactor(v2): avoid synchronous/ blocking operation when possible (#1957)

* perf(v2): avoid synchronous/ blocking operation when possible

* save variable
This commit is contained in:
Endi 2019-11-11 20:56:23 +07:00 committed by GitHub
parent 5e445a0011
commit 1235fc9f7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 76 deletions

View file

@ -52,4 +52,10 @@ describe('loadSidebars', () => {
const result = loadSidebars(null);
expect(result).toEqual({});
});
test('fake sidebars path', () => {
expect(() => {
loadSidebars('/fake/path');
}).toThrowError();
});
});

View file

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs';
import importFresh from 'import-fresh';
import {
SidebarItemCategory,
@ -105,7 +104,7 @@ function normalizeSidebar(sidebars: SidebarRaw): Sidebar {
export default function loadSidebars(sidebarPath: string): Sidebar {
// We don't want sidebars to be cached because of hotreloading.
let allSidebars: SidebarRaw = {};
if (sidebarPath && fs.existsSync(sidebarPath)) {
if (sidebarPath) {
allSidebars = importFresh(sidebarPath) as SidebarRaw;
}
return normalizeSidebar(allSidebars);