-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
chore(core): replace wait-on dependency with custom lighter code
#9547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the previous logic, but it seems a little bit fragile, or prone to add delay.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As long as no regression, we'll take it! It should also be much more trivial to make it more robust now :) |
||
|
|
||
| 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); | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.