mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-20 19:47:52 +02:00
refactor(v2): avoid synchronous/ blocking operation when possible (#1957)
* perf(v2): avoid synchronous/ blocking operation when possible * save variable
This commit is contained in:
parent
5e445a0011
commit
1235fc9f7e
6 changed files with 85 additions and 76 deletions
|
@ -52,4 +52,10 @@ describe('loadSidebars', () => {
|
|||
const result = loadSidebars(null);
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
test('fake sidebars path', () => {
|
||||
expect(() => {
|
||||
loadSidebars('/fake/path');
|
||||
}).toThrowError();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -14,17 +14,17 @@ import {getBundles} from 'react-loadable-ssr-addon';
|
|||
import Loadable from 'react-loadable';
|
||||
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import fs from 'fs-extra';
|
||||
import routes from '@generated/routes';
|
||||
import preload from './preload';
|
||||
import App from './App';
|
||||
import ssrTemplate from './templates/ssr.html.template';
|
||||
|
||||
// Renderer for static-site-generator-webpack-plugin (async rendering via promises)
|
||||
export default function render(locals) {
|
||||
export default async function render(locals) {
|
||||
const {routesLocation} = locals;
|
||||
const location = routesLocation[locals.path];
|
||||
return preload(routes, location).then(() => {
|
||||
await preload(routes, location);
|
||||
const modules = new Set();
|
||||
const context = {};
|
||||
const appHtml = ReactDOMServer.renderToString(
|
||||
|
@ -47,12 +47,12 @@ export default function render(locals) {
|
|||
|
||||
const {outDir} = locals;
|
||||
const manifestPath = path.join(outDir, 'client-manifest.json');
|
||||
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
||||
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
|
||||
|
||||
// chunkName -> chunkAssets mapping.
|
||||
const chunkManifestPath = path.join(outDir, 'chunk-map.json');
|
||||
const chunkManifest = JSON.parse(
|
||||
fs.readFileSync(chunkManifestPath, 'utf-8'),
|
||||
await fs.readFile(chunkManifestPath, 'utf8'),
|
||||
);
|
||||
const chunkManifestScript =
|
||||
`<script type="text/javascript">` +
|
||||
|
@ -84,5 +84,4 @@ export default function render(locals) {
|
|||
rmWhitespace: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -110,7 +110,9 @@ export async function build(
|
|||
// Remove server.bundle.js because it is useless
|
||||
if (serverConfig.output && serverConfig.output.filename) {
|
||||
const serverBundle = path.join(outDir, serverConfig.output.filename);
|
||||
fs.existsSync(serverBundle) && fs.unlinkSync(serverBundle);
|
||||
fs.pathExists(serverBundle).then(exist => {
|
||||
exist && fs.unlink(serverBundle);
|
||||
});
|
||||
}
|
||||
|
||||
/* Plugin lifecycle - postBuild */
|
||||
|
|
|
@ -26,7 +26,7 @@ class ChunkManifestPlugin {
|
|||
const {path: outputPath, publicPath} = compiler.options.output;
|
||||
|
||||
// Build the chunk mapping
|
||||
compiler.hooks.afterCompile.tap(pluginName, compilation => {
|
||||
compiler.hooks.afterCompile.tapAsync(pluginName, (compilation, done) => {
|
||||
const assets = {};
|
||||
const assetsMap = {};
|
||||
// eslint-disable-next-line
|
||||
|
@ -50,8 +50,11 @@ class ChunkManifestPlugin {
|
|||
chunkManifest = assetsMap;
|
||||
if (!this.options.inlineManifest) {
|
||||
const finalPath = path.resolve(outputPath, this.options.filename);
|
||||
fs.ensureDirSync(path.dirname(finalPath));
|
||||
fs.writeFileSync(finalPath, JSON.stringify(chunkManifest, null, 2));
|
||||
fs.ensureDir(path.dirname(finalPath), () => {
|
||||
fs.writeFile(finalPath, JSON.stringify(chunkManifest, null, 2), done);
|
||||
});
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -18,8 +18,7 @@ class WaitPlugin {
|
|||
// Before finishing the compilation step
|
||||
compiler.hooks.make.tapAsync('WaitPlugin', (compilation, callback) => {
|
||||
// To prevent 'waitFile' error on waiting non-existing directory
|
||||
fs.ensureDirSync(path.dirname(this.filepath));
|
||||
|
||||
fs.ensureDir(path.dirname(this.filepath), () => {
|
||||
// Wait until file exist
|
||||
waitFile({
|
||||
resources: [this.filepath],
|
||||
|
@ -32,6 +31,7 @@ class WaitPlugin {
|
|||
console.warn(`WaitPlugin error: ${error}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue