webpack 5.34.0 → 5.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

@@ -1249,7 +1249,7 @@ class FileSystemInfo {
1249
1249
  return callback();
1250
1250
  }
1251
1251
  resolveResults.set(key, undefined);
1252
- resolveContext(context, path, resolverContext, (err, result) => {
1252
+ resolveContext(context, path, resolverContext, (err, _, result) => {
1253
1253
  if (err) {
1254
1254
  if (expected === false) {
1255
1255
  resolveResults.set(key, false);
@@ -1259,11 +1259,12 @@ class FileSystemInfo {
1259
1259
  err.message += `\nwhile resolving '${path}' in ${context} to a directory`;
1260
1260
  return callback(err);
1261
1261
  }
1262
- resolveResults.set(key, result);
1262
+ const resultPath = result.path;
1263
+ resolveResults.set(key, resultPath);
1263
1264
  push({
1264
1265
  type: RBDT_DIRECTORY,
1265
1266
  context: undefined,
1266
- path: result,
1267
+ path: resultPath,
1267
1268
  expected: undefined,
1268
1269
  issuer: job
1269
1270
  });
@@ -1276,14 +1277,16 @@ class FileSystemInfo {
1276
1277
  return callback();
1277
1278
  }
1278
1279
  resolveResults.set(key, undefined);
1279
- resolve(context, path, resolverContext, (err, result) => {
1280
+ resolve(context, path, resolverContext, (err, _, result) => {
1280
1281
  if (typeof expected === "string") {
1281
- if (result === expected) {
1282
- resolveResults.set(key, result);
1282
+ if (!err && result && result.path === expected) {
1283
+ resolveResults.set(key, result.path);
1283
1284
  } else {
1284
1285
  invalidResolveResults.add(key);
1285
1286
  this.logger.warn(
1286
- `Resolving '${path}' in ${context} for build dependencies doesn't lead to expected result '${expected}', but to '${result}' instead. Resolving dependencies are ignored for this path.\n${pathToString(
1287
+ `Resolving '${path}' in ${context} for build dependencies doesn't lead to expected result '${expected}', but to '${
1288
+ err || (result && result.path)
1289
+ }' instead. Resolving dependencies are ignored for this path.\n${pathToString(
1287
1290
  job
1288
1291
  )}`
1289
1292
  );
@@ -1300,11 +1303,12 @@ class FileSystemInfo {
1300
1303
  )}`;
1301
1304
  return callback(err);
1302
1305
  }
1303
- resolveResults.set(key, result);
1306
+ const resultPath = result.path;
1307
+ resolveResults.set(key, resultPath);
1304
1308
  push({
1305
1309
  type: RBDT_FILE,
1306
1310
  context: undefined,
1307
- path: result,
1311
+ path: resultPath,
1308
1312
  expected: undefined,
1309
1313
  issuer: job
1310
1314
  });
@@ -1646,38 +1650,42 @@ class FileSystemInfo {
1646
1650
  const [type, context, path] = key.split("\n");
1647
1651
  switch (type) {
1648
1652
  case "d":
1649
- resolveContext(context, path, {}, (err, result) => {
1653
+ resolveContext(context, path, {}, (err, _, result) => {
1650
1654
  if (expectedResult === false)
1651
1655
  return callback(err ? undefined : INVALID);
1652
1656
  if (err) return callback(err);
1653
- if (result !== expectedResult) return callback(INVALID);
1657
+ const resultPath = result.path;
1658
+ if (resultPath !== expectedResult) return callback(INVALID);
1654
1659
  callback();
1655
1660
  });
1656
1661
  break;
1657
1662
  case "f":
1658
- resolveCjs(context, path, {}, (err, result) => {
1663
+ resolveCjs(context, path, {}, (err, _, result) => {
1659
1664
  if (expectedResult === false)
1660
1665
  return callback(err ? undefined : INVALID);
1661
1666
  if (err) return callback(err);
1662
- if (result !== expectedResult) return callback(INVALID);
1667
+ const resultPath = result.path;
1668
+ if (resultPath !== expectedResult) return callback(INVALID);
1663
1669
  callback();
1664
1670
  });
1665
1671
  break;
1666
1672
  case "c":
1667
- resolveCjsAsChild(context, path, {}, (err, result) => {
1673
+ resolveCjsAsChild(context, path, {}, (err, _, result) => {
1668
1674
  if (expectedResult === false)
1669
1675
  return callback(err ? undefined : INVALID);
1670
1676
  if (err) return callback(err);
1671
- if (result !== expectedResult) return callback(INVALID);
1677
+ const resultPath = result.path;
1678
+ if (resultPath !== expectedResult) return callback(INVALID);
1672
1679
  callback();
1673
1680
  });
1674
1681
  break;
1675
1682
  case "e":
1676
- resolveEsm(context, path, {}, (err, result) => {
1683
+ resolveEsm(context, path, {}, (err, _, result) => {
1677
1684
  if (expectedResult === false)
1678
1685
  return callback(err ? undefined : INVALID);
1679
1686
  if (err) return callback(err);
1680
- if (result !== expectedResult) return callback(INVALID);
1687
+ const resultPath = result.path;
1688
+ if (resultPath !== expectedResult) return callback(INVALID);
1681
1689
  callback();
1682
1690
  });
1683
1691
  break;
@@ -568,7 +568,8 @@ class WebpackOptionsApply extends OptionsApply {
568
568
  "webpack.cache.PackFileCacheStrategy"
569
569
  ),
570
570
  snapshot: options.snapshot,
571
- maxAge: cacheOptions.maxAge
571
+ maxAge: cacheOptions.maxAge,
572
+ allowCollectingMemory: cacheOptions.allowCollectingMemory
572
573
  }),
573
574
  cacheOptions.idleTimeout,
574
575
  cacheOptions.idleTimeoutForInitialStore
@@ -768,6 +768,14 @@ class PackContent {
768
768
  }
769
769
  }
770
770
 
771
+ const allowCollectingMemory = buf => {
772
+ const wasted = buf.buffer.byteLength - buf.byteLength;
773
+ if (wasted > 8192 && (wasted > 1048576 || wasted > buf.byteLength)) {
774
+ return Buffer.from(buf);
775
+ }
776
+ return buf;
777
+ };
778
+
771
779
  class PackFileCacheStrategy {
772
780
  /**
773
781
  * @param {Object} options options
@@ -779,6 +787,7 @@ class PackFileCacheStrategy {
779
787
  * @param {Logger} options.logger a logger
780
788
  * @param {SnapshotOptions} options.snapshot options regarding snapshotting
781
789
  * @param {number} options.maxAge max age of cache items
790
+ * @param {boolean} options.allowCollectingMemory allow to collect unused memory created during deserialization
782
791
  */
783
792
  constructor({
784
793
  compiler,
@@ -788,7 +797,8 @@ class PackFileCacheStrategy {
788
797
  version,
789
798
  logger,
790
799
  snapshot,
791
- maxAge
800
+ maxAge,
801
+ allowCollectingMemory
792
802
  }) {
793
803
  this.fileSerializer = createFileSerializer(fs);
794
804
  this.fileSystemInfo = new FileSystemInfo(fs, {
@@ -802,6 +812,7 @@ class PackFileCacheStrategy {
802
812
  this.version = version;
803
813
  this.logger = logger;
804
814
  this.maxAge = maxAge;
815
+ this.allowCollectingMemory = allowCollectingMemory;
805
816
  this.snapshot = snapshot;
806
817
  /** @type {Set<string>} */
807
818
  this.buildDependencies = new Set();
@@ -845,7 +856,10 @@ class PackFileCacheStrategy {
845
856
  .deserialize(null, {
846
857
  filename: `${cacheLocation}/index.pack`,
847
858
  extension: ".pack",
848
- logger
859
+ logger,
860
+ retainedBuffer: this.allowCollectingMemory
861
+ ? allowCollectingMemory
862
+ : undefined
849
863
  })
850
864
  .catch(err => {
851
865
  if (err.code !== "ENOENT") {
@@ -295,8 +295,9 @@ const applyCacheDefaults = (cache, { name, mode, development }) => {
295
295
  D(cache, "store", "pack");
296
296
  D(cache, "idleTimeout", 60000);
297
297
  D(cache, "idleTimeoutForInitialStore", 0);
298
- D(cache, "maxMemoryGenerations", development ? 10 : Infinity);
298
+ D(cache, "maxMemoryGenerations", development ? 5 : Infinity);
299
299
  D(cache, "maxAge", 1000 * 60 * 60 * 24 * 60); // 1 month
300
+ D(cache, "allowCollectingMemory", development);
300
301
  D(cache.buildDependencies, "defaultWebpack", [
301
302
  path.resolve(__dirname, "..") + path.sep
302
303
  ]);
@@ -123,6 +123,12 @@ class BinaryMiddleware extends SerializerMiddleware {
123
123
  return this._serialize(data, context);
124
124
  }
125
125
 
126
+ _serializeLazy(fn, context) {
127
+ return SerializerMiddleware.serializeLazy(fn, data =>
128
+ this._serialize(data, context)
129
+ );
130
+ }
131
+
126
132
  /**
127
133
  * @param {DeserializedType} data data
128
134
  * @param {Object} context context object
@@ -135,7 +141,7 @@ class BinaryMiddleware extends SerializerMiddleware {
135
141
  let leftOverBuffer = null;
136
142
  let currentPosition = 0;
137
143
  /** @type {BufferSerializableType[]} */
138
- const buffers = [];
144
+ let buffers = [];
139
145
  let buffersTotalLength = 0;
140
146
  const allocate = bytesNeeded => {
141
147
  if (currentBuffer !== null) {
@@ -204,10 +210,7 @@ class BinaryMiddleware extends SerializerMiddleware {
204
210
  SerializerMiddleware.setLazySerializedValue(thing, data);
205
211
  serializedData = data;
206
212
  } else {
207
- serializedData = SerializerMiddleware.serializeLazy(
208
- thing,
209
- data => this._serialize(data, context)
210
- );
213
+ serializedData = this._serializeLazy(thing, context);
211
214
  }
212
215
  }
213
216
  if (typeof serializedData === "function") {
@@ -474,7 +477,13 @@ class BinaryMiddleware extends SerializerMiddleware {
474
477
  };
475
478
  serializeData(data);
476
479
  flush();
477
- return buffers;
480
+
481
+ // avoid leaking memory
482
+ currentBuffer = null;
483
+ leftOverBuffer = null;
484
+ const _buffers = buffers;
485
+ buffers = undefined;
486
+ return _buffers;
478
487
  }
479
488
 
480
489
  /**
@@ -486,6 +495,21 @@ class BinaryMiddleware extends SerializerMiddleware {
486
495
  return this._deserialize(data, context);
487
496
  }
488
497
 
498
+ _createLazyDeserialized(content, context) {
499
+ return SerializerMiddleware.createLazy(
500
+ memoize(() => this._deserialize(content, context)),
501
+ this,
502
+ undefined,
503
+ content
504
+ );
505
+ }
506
+
507
+ _deserializeLazy(fn, context) {
508
+ return SerializerMiddleware.deserializeLazy(fn, data =>
509
+ this._deserialize(data, context)
510
+ );
511
+ }
512
+
489
513
  /**
490
514
  * @param {SerializedType} data data
491
515
  * @param {Object} context context object
@@ -497,6 +521,8 @@ class BinaryMiddleware extends SerializerMiddleware {
497
521
  let currentIsBuffer = Buffer.isBuffer(currentBuffer);
498
522
  let currentPosition = 0;
499
523
 
524
+ const retainedBuffer = context.retainedBuffer || (x => x);
525
+
500
526
  const checkOverflow = () => {
501
527
  if (currentPosition >= currentBuffer.length) {
502
528
  currentPosition = 0;
@@ -610,23 +636,16 @@ class BinaryMiddleware extends SerializerMiddleware {
610
636
  do {
611
637
  const buf = readUpTo(l);
612
638
  l -= buf.length;
613
- content.push(buf);
639
+ content.push(retainedBuffer(buf));
614
640
  } while (l > 0);
615
641
  }
616
642
  }
617
- result.push(
618
- SerializerMiddleware.createLazy(
619
- memoize(() => this._deserialize(content, context)),
620
- this,
621
- undefined,
622
- content
623
- )
624
- );
643
+ result.push(this._createLazyDeserialized(content, context));
625
644
  };
626
645
  case BUFFER_HEADER:
627
646
  return () => {
628
647
  const len = readU32();
629
- result.push(read(len));
648
+ result.push(retainedBuffer(read(len)));
630
649
  };
631
650
  case TRUE_HEADER:
632
651
  return () => result.push(true);
@@ -852,14 +871,10 @@ class BinaryMiddleware extends SerializerMiddleware {
852
871
  });
853
872
 
854
873
  /** @type {DeserializedType} */
855
- const result = [];
874
+ let result = [];
856
875
  while (currentBuffer !== null) {
857
876
  if (typeof currentBuffer === "function") {
858
- result.push(
859
- SerializerMiddleware.deserializeLazy(currentBuffer, data =>
860
- this._deserialize(data, context)
861
- )
862
- );
877
+ result.push(this._deserializeLazy(currentBuffer, context));
863
878
  currentDataItem++;
864
879
  currentBuffer =
865
880
  currentDataItem < data.length ? data[currentDataItem] : null;
@@ -869,7 +884,11 @@ class BinaryMiddleware extends SerializerMiddleware {
869
884
  dispatchTable[header]();
870
885
  }
871
886
  }
872
- return result;
887
+
888
+ // avoid leaking memory in context
889
+ let _result = result;
890
+ result = undefined;
891
+ return _result;
873
892
  }
874
893
  }
875
894
 
@@ -34,9 +34,7 @@ const createCachedParameterizedComparator = fn => {
34
34
  * @param {T} b second item
35
35
  * @returns {-1|0|1} compare result
36
36
  */
37
- const result = (a, b) => {
38
- return fn(arg, a, b);
39
- };
37
+ const result = fn.bind(null, arg);
40
38
  map.set(arg, result);
41
39
  return result;
42
40
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webpack",
3
- "version": "5.34.0",
3
+ "version": "5.35.0",
4
4
  "author": "Tobias Koppers @sokra",
5
5
  "description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
6
6
  "license": "MIT",