diff --git a/declarations/WebpackOptions.d.ts b/declarations/WebpackOptions.d.ts index 9cfd14b8d..c67818417 100644 --- a/declarations/WebpackOptions.d.ts +++ b/declarations/WebpackOptions.d.ts @@ -431,6 +431,10 @@ export interface FileCacheOptions { * Algorithm used for generation the hash (see node.js crypto package) */ hashAlgorithm?: string; + /** + * Time in ms after which idle period cache storing should happen (only for store: 'pack' or 'idle') + */ + idleTimeout?: number; /** * Display log info. (debug: all access and errors with stack trace, verbose: all access, info: all write access, warning: only failed serialization) */ diff --git a/lib/cache/FileCachePlugin.js b/lib/cache/FileCachePlugin.js index d6e0b6035..3f7ce24ca 100644 --- a/lib/cache/FileCachePlugin.js +++ b/lib/cache/FileCachePlugin.js @@ -126,6 +126,7 @@ class FileCachePlugin { ? { debug: 4, verbose: 3, info: 2, warning: 1 }[this.options.loglevel] : 0; const store = this.options.store || "pack"; + const idleTimeout = this.options.idleTimeout || 0; const resolvedPromise = Promise.resolve(); @@ -364,11 +365,19 @@ class FileCachePlugin { } } }; + let idleTimer = undefined; compiler.cache.hooks.beginIdle.tap("FileCachePlugin", () => { - isIdle = true; - resolvedPromise.then(processIdleTasks); + idleTimer = setTimeout(() => { + idleTimer = undefined; + isIdle = true; + resolvedPromise.then(processIdleTasks); + }, idleTimeout); }); compiler.cache.hooks.endIdle.tap("FileCachePlugin", () => { + if (idleTimer) { + clearTimeout(idleTimer); + idleTimer = undefined; + } isIdle = false; }); } diff --git a/schemas/WebpackOptions.json b/schemas/WebpackOptions.json index e555ae57d..6b8ce9b5e 100644 --- a/schemas/WebpackOptions.json +++ b/schemas/WebpackOptions.json @@ -168,6 +168,11 @@ "description": "Algorithm used for generation the hash (see node.js crypto package)", "type": "string" }, + "idleTimeout": { + "description": "Time in ms after which idle period cache storing should happen (only for store: 'pack' or 'idle')", + "type": "number", + "minimum": 0 + }, "loglevel": { "description": "Display log info. (debug: all access and errors with stack trace, verbose: all access, info: all write access, warning: only failed serialization)", "enum": ["debug", "verbose", "info", "warning"]