avoid leaking processed assets

This commit is contained in:
Tobias Koppers 2021-04-07 20:48:07 +02:00
parent b3e26cb5a8
commit 3caf3e95a8
1 changed files with 11 additions and 7 deletions

View File

@ -358,12 +358,13 @@ class Compilation {
const { fn, additionalAssets, ...remainingTap } = tap; const { fn, additionalAssets, ...remainingTap } = tap;
const additionalAssetsFn = const additionalAssetsFn =
additionalAssets === true ? fn : additionalAssets; additionalAssets === true ? fn : additionalAssets;
let processedAssets = undefined; const processedAssets = additionalAssetsFn ? new WeakSet() : undefined;
switch (type) { switch (type) {
case "sync": case "sync":
if (additionalAssetsFn) { if (additionalAssetsFn) {
this.hooks.processAdditionalAssets.tap(name, assets => { this.hooks.processAdditionalAssets.tap(name, assets => {
if (processedAssets === this.assets) additionalAssetsFn(assets); if (processedAssets.has(this.assets))
additionalAssetsFn(assets);
}); });
} }
return { return {
@ -375,7 +376,8 @@ class Compilation {
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }
processedAssets = this.assets; if (processedAssets !== undefined)
processedAssets.add(this.assets);
const newAssets = popNewAssets(assets); const newAssets = popNewAssets(assets);
if (newAssets !== undefined) { if (newAssets !== undefined) {
this.hooks.processAdditionalAssets.callAsync( this.hooks.processAdditionalAssets.callAsync(
@ -392,7 +394,7 @@ class Compilation {
this.hooks.processAdditionalAssets.tapAsync( this.hooks.processAdditionalAssets.tapAsync(
name, name,
(assets, callback) => { (assets, callback) => {
if (processedAssets === this.assets) if (processedAssets.has(this.assets))
return additionalAssetsFn(assets, callback); return additionalAssetsFn(assets, callback);
callback(); callback();
} }
@ -403,7 +405,8 @@ class Compilation {
fn: (assets, callback) => { fn: (assets, callback) => {
fn(assets, err => { fn(assets, err => {
if (err) return callback(err); if (err) return callback(err);
processedAssets = this.assets; if (processedAssets !== undefined)
processedAssets.add(this.assets);
const newAssets = popNewAssets(assets); const newAssets = popNewAssets(assets);
if (newAssets !== undefined) { if (newAssets !== undefined) {
this.hooks.processAdditionalAssets.callAsync( this.hooks.processAdditionalAssets.callAsync(
@ -419,7 +422,7 @@ class Compilation {
case "promise": case "promise":
if (additionalAssetsFn) { if (additionalAssetsFn) {
this.hooks.processAdditionalAssets.tapPromise(name, assets => { this.hooks.processAdditionalAssets.tapPromise(name, assets => {
if (processedAssets === this.assets) if (processedAssets.has(this.assets))
return additionalAssetsFn(assets); return additionalAssetsFn(assets);
return Promise.resolve(); return Promise.resolve();
}); });
@ -430,7 +433,8 @@ class Compilation {
const p = fn(assets); const p = fn(assets);
if (!p || !p.then) return p; if (!p || !p.then) return p;
return p.then(() => { return p.then(() => {
processedAssets = this.assets; if (processedAssets !== undefined)
processedAssets.add(this.assets);
const newAssets = popNewAssets(assets); const newAssets = popNewAssets(assets);
if (newAssets !== undefined) { if (newAssets !== undefined) {
return this.hooks.processAdditionalAssets.promise( return this.hooks.processAdditionalAssets.promise(