docusaurus/packages/docusaurus/src/webpack/plugins/WaitPlugin.ts
Sébastien Lorber a2e05d2118
chore: release Docusaurus 3.0.1 (#9596)
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
Co-authored-by: Joey Clover <joey@popos.local>
Co-authored-by: reece-white <93522192+reece-white@users.noreply.github.com>
Co-authored-by: Shreesh Nautiyal <114166000+Shreesh09@users.noreply.github.com>
Co-authored-by: Nick Gerleman <nick@nickgerleman.com>
Co-authored-by: Chongyi Zheng <git@zcy.dev>
Co-authored-by: MCR Studio <99176216+mcrstudio@users.noreply.github.com>
fix(create-docusaurus): fix readme docusaurus 2 ref (#9487)
fix(theme): fix firefox CSS :has() support bug (#9530)
fix(theme): docs html sidebar items should always be visible (#9531)
fix: v3 admonitions should support v2 title syntax for nested admonitions (#9535)
fix(theme-classic): fixed wrong cursor on dropdown menu in navbar, when window is small (#9398)
fix(theme): upgrade prism-react-renderer, fix html script and style tag highlighting (#9567)
fix: add v2 retrocompatible support for quoted admonitions (#9570)
2023-11-30 19:47:23 +01:00

57 lines
1.4 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import type {Compiler} from 'webpack';
type WaitPluginOptions = {
filepath: string;
};
export default class WaitPlugin {
filepath: string;
constructor(options: WaitPluginOptions) {
this.filepath = options.filepath;
}
apply(compiler: Compiler): void {
// Before finishing the compilation step
compiler.hooks.make.tapPromise('WaitPlugin', () => waitOn(this.filepath));
}
}
// This is a re-implementation of the algorithm used by the "wait-on" package
// https://github.com/jeffbski/wait-on/blob/master/lib/wait-on.js#L200
async function waitOn(filepath: string): Promise<void> {
const pollingIntervalMs = 300;
const stabilityWindowMs = 750;
let lastFileSize = -1;
let lastFileTime = -1;
for (;;) {
let size = -1;
try {
size = (await fs.stat(filepath)).size;
} catch (err) {}
if (size !== -1) {
if (lastFileTime === -1 || size !== lastFileSize) {
lastFileSize = size;
lastFileTime = performance.now();
} else if (performance.now() - lastFileTime >= stabilityWindowMs) {
return;
}
}
await new Promise((resolve) => {
setTimeout(resolve, pollingIntervalMs);
});
}
}