chore(core): replace wait-on dependency with custom lighter code (#9547)

This commit is contained in:
Nick Gerleman 2023-11-20 08:45:01 -08:00 committed by GitHub
parent 7dcad0c632
commit 424ffd2e29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 48 deletions

View file

@ -97,7 +97,6 @@
"tslib": "^2.6.0",
"update-notifier": "^6.0.2",
"url-loader": "^4.1.1",
"wait-on": "^7.0.1",
"webpack": "^5.88.1",
"webpack-bundle-analyzer": "^4.9.0",
"webpack-dev-server": "^4.15.1",
@ -113,7 +112,6 @@
"@types/rtl-detect": "^1.0.0",
"@types/serve-handler": "^6.1.1",
"@types/update-notifier": "^6.0.4",
"@types/wait-on": "^5.3.1",
"@types/webpack-bundle-analyzer": "^4.6.0",
"react-test-renderer": "^18.0.0",
"tmp-promise": "^3.0.3",

View file

@ -5,9 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import path from 'path';
import fs from 'fs-extra';
import waitOn from 'wait-on';
import type {Compiler} from 'webpack';
type WaitPluginOptions = {
@ -23,21 +21,36 @@ export default class WaitPlugin {
apply(compiler: Compiler): void {
// Before finishing the compilation step
compiler.hooks.make.tapAsync('WaitPlugin', (compilation, callback) => {
// To prevent 'waitFile' error on waiting non-existing directory
fs.ensureDir(path.dirname(this.filepath), {}, () => {
// Wait until file exist
waitOn({
resources: [this.filepath],
interval: 300,
})
.then(() => {
callback();
})
.catch((error: Error) => {
console.warn(`WaitPlugin error: ${error}`);
});
});
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);
});
}
}