mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-21 12:08:03 +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);
|
const result = loadSidebars(null);
|
||||||
expect(result).toEqual({});
|
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.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import fs from 'fs';
|
|
||||||
import importFresh from 'import-fresh';
|
import importFresh from 'import-fresh';
|
||||||
import {
|
import {
|
||||||
SidebarItemCategory,
|
SidebarItemCategory,
|
||||||
|
@ -105,7 +104,7 @@ function normalizeSidebar(sidebars: SidebarRaw): Sidebar {
|
||||||
export default function loadSidebars(sidebarPath: string): Sidebar {
|
export default function loadSidebars(sidebarPath: string): Sidebar {
|
||||||
// We don't want sidebars to be cached because of hotreloading.
|
// We don't want sidebars to be cached because of hotreloading.
|
||||||
let allSidebars: SidebarRaw = {};
|
let allSidebars: SidebarRaw = {};
|
||||||
if (sidebarPath && fs.existsSync(sidebarPath)) {
|
if (sidebarPath) {
|
||||||
allSidebars = importFresh(sidebarPath) as SidebarRaw;
|
allSidebars = importFresh(sidebarPath) as SidebarRaw;
|
||||||
}
|
}
|
||||||
return normalizeSidebar(allSidebars);
|
return normalizeSidebar(allSidebars);
|
||||||
|
|
|
@ -14,17 +14,17 @@ import {getBundles} from 'react-loadable-ssr-addon';
|
||||||
import Loadable from 'react-loadable';
|
import Loadable from 'react-loadable';
|
||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs-extra';
|
||||||
import routes from '@generated/routes';
|
import routes from '@generated/routes';
|
||||||
import preload from './preload';
|
import preload from './preload';
|
||||||
import App from './App';
|
import App from './App';
|
||||||
import ssrTemplate from './templates/ssr.html.template';
|
import ssrTemplate from './templates/ssr.html.template';
|
||||||
|
|
||||||
// Renderer for static-site-generator-webpack-plugin (async rendering via promises)
|
// 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 {routesLocation} = locals;
|
||||||
const location = routesLocation[locals.path];
|
const location = routesLocation[locals.path];
|
||||||
return preload(routes, location).then(() => {
|
await preload(routes, location);
|
||||||
const modules = new Set();
|
const modules = new Set();
|
||||||
const context = {};
|
const context = {};
|
||||||
const appHtml = ReactDOMServer.renderToString(
|
const appHtml = ReactDOMServer.renderToString(
|
||||||
|
@ -47,12 +47,12 @@ export default function render(locals) {
|
||||||
|
|
||||||
const {outDir} = locals;
|
const {outDir} = locals;
|
||||||
const manifestPath = path.join(outDir, 'client-manifest.json');
|
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.
|
// chunkName -> chunkAssets mapping.
|
||||||
const chunkManifestPath = path.join(outDir, 'chunk-map.json');
|
const chunkManifestPath = path.join(outDir, 'chunk-map.json');
|
||||||
const chunkManifest = JSON.parse(
|
const chunkManifest = JSON.parse(
|
||||||
fs.readFileSync(chunkManifestPath, 'utf-8'),
|
await fs.readFile(chunkManifestPath, 'utf8'),
|
||||||
);
|
);
|
||||||
const chunkManifestScript =
|
const chunkManifestScript =
|
||||||
`<script type="text/javascript">` +
|
`<script type="text/javascript">` +
|
||||||
|
@ -84,5 +84,4 @@ export default function render(locals) {
|
||||||
rmWhitespace: true,
|
rmWhitespace: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,9 @@ export async function build(
|
||||||
// Remove server.bundle.js because it is useless
|
// Remove server.bundle.js because it is useless
|
||||||
if (serverConfig.output && serverConfig.output.filename) {
|
if (serverConfig.output && serverConfig.output.filename) {
|
||||||
const serverBundle = path.join(outDir, 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 */
|
/* Plugin lifecycle - postBuild */
|
||||||
|
|
|
@ -26,7 +26,7 @@ class ChunkManifestPlugin {
|
||||||
const {path: outputPath, publicPath} = compiler.options.output;
|
const {path: outputPath, publicPath} = compiler.options.output;
|
||||||
|
|
||||||
// Build the chunk mapping
|
// Build the chunk mapping
|
||||||
compiler.hooks.afterCompile.tap(pluginName, compilation => {
|
compiler.hooks.afterCompile.tapAsync(pluginName, (compilation, done) => {
|
||||||
const assets = {};
|
const assets = {};
|
||||||
const assetsMap = {};
|
const assetsMap = {};
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
|
@ -50,8 +50,11 @@ class ChunkManifestPlugin {
|
||||||
chunkManifest = assetsMap;
|
chunkManifest = assetsMap;
|
||||||
if (!this.options.inlineManifest) {
|
if (!this.options.inlineManifest) {
|
||||||
const finalPath = path.resolve(outputPath, this.options.filename);
|
const finalPath = path.resolve(outputPath, this.options.filename);
|
||||||
fs.ensureDirSync(path.dirname(finalPath));
|
fs.ensureDir(path.dirname(finalPath), () => {
|
||||||
fs.writeFileSync(finalPath, JSON.stringify(chunkManifest, null, 2));
|
fs.writeFile(finalPath, JSON.stringify(chunkManifest, null, 2), done);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,7 @@ class WaitPlugin {
|
||||||
// Before finishing the compilation step
|
// Before finishing the compilation step
|
||||||
compiler.hooks.make.tapAsync('WaitPlugin', (compilation, callback) => {
|
compiler.hooks.make.tapAsync('WaitPlugin', (compilation, callback) => {
|
||||||
// To prevent 'waitFile' error on waiting non-existing directory
|
// 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
|
// Wait until file exist
|
||||||
waitFile({
|
waitFile({
|
||||||
resources: [this.filepath],
|
resources: [this.filepath],
|
||||||
|
@ -32,6 +31,7 @@ class WaitPlugin {
|
||||||
console.warn(`WaitPlugin error: ${error}`);
|
console.warn(`WaitPlugin error: ${error}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue