@plaudit/webpack-extensions 2.44.3 → 2.44.5

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.
@@ -171,7 +171,7 @@ class BlockJSONManagingPlugin {
171
171
  const inputPath = node_path_1.default.normalize(node_path_1.default.join(sourceDir, value.substring(5)));
172
172
  const outputAndAdditionalFiles = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name)?.get(inputPath);
173
173
  if (outputAndAdditionalFiles !== undefined) {
174
- const [output, additionalFiles] = outputAndAdditionalFiles;
174
+ const { output, additionalFiles } = outputAndAdditionalFiles;
175
175
  const prefix = value.startsWith("./", 5) ? "./" : "";
176
176
  const relativePath = node_path_1.default.relative(node_path_1.default.dirname(name), output.path);
177
177
  const res = [`file:${prefix}${relativePath}`, output.hash];
@@ -294,19 +294,30 @@ class BlockJSONManagingPlugin {
294
294
  continue;
295
295
  }
296
296
  const entrypointChunk = entrypoint.getEntrypointChunk();
297
- const additionalFiles = [];
298
- for (const entrypointChunkFile of entrypointChunk.files) {
299
- if (!entrypointChunkFile.endsWith(".asset.php") && entrypointChunkFile !== destPath) {
300
- additionalFiles.push(entrypointChunkFile);
297
+ const runtimeName = entrypointChunk.name;
298
+ let additionalFiles;
299
+ if (runtimeName) {
300
+ additionalFiles = new Set([...compilation.chunks]
301
+ .filter(chunk => typeof chunk.runtime === 'string' ? chunk.runtime === entrypointChunk.name : chunk.runtime?.has(runtimeName))
302
+ .flatMap(chunk => [...chunk.files])
303
+ .filter(file => !file.endsWith(".asset.php")));
304
+ additionalFiles.delete(destPath);
305
+ }
306
+ else {
307
+ additionalFiles = new Set();
308
+ for (const entrypointChunkFile of entrypointChunk.files) {
309
+ if (!entrypointChunkFile.endsWith(".asset.php") && entrypointChunkFile !== destPath) {
310
+ additionalFiles.add(entrypointChunkFile);
311
+ }
301
312
  }
302
313
  }
303
314
  const entryMeta = { hash: entrypointChunk.hash ?? destPath, path: destPath };
304
315
  const currentEntrypoints = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name);
305
316
  if (currentEntrypoints) {
306
- currentEntrypoints.set(srcPath, [entryMeta, additionalFiles]);
317
+ currentEntrypoints.set(srcPath, { output: entryMeta, additionalFiles: [...additionalFiles] });
307
318
  }
308
319
  else {
309
- BlockJSONManagingPlugin.blockJsonToEntrypointsMap.set(name, new Map([[srcPath, [entryMeta, additionalFiles]]]));
320
+ BlockJSONManagingPlugin.blockJsonToEntrypointsMap.set(name, new Map([[srcPath, { output: entryMeta, additionalFiles: [...additionalFiles] }]]));
310
321
  }
311
322
  }
312
323
  }
@@ -381,7 +392,21 @@ class BlockJSONManagingPlugin {
381
392
  if (typeof rawAssetDataSource === 'string') {
382
393
  const scriptHandles = {};
383
394
  const styleHandles = {};
395
+ const additionalFilesMap = new Map([...BlockJSONManagingPlugin.blockJsonToEntrypointsMap.values()] // Extract the recorded block.json files into an array
396
+ .flatMap(epMap => [...epMap.values()]) // Extract the recorded entrypoints for all block.json files into a single array
397
+ .flatMap(epMapValue => {
398
+ return epMapValue.additionalFiles.map(af => [af, epMapValue.output.path]); // Pair each additionalFile with its progenitor
399
+ }) // Flatten the per-entrypoint [additionalFile, progenitor] pairs into a single array
400
+ );
384
401
  const rawAssetData = JSON.parse(rawAssetDataSource);
402
+ for (const chunk of compilation.chunks) {
403
+ if (compilation.chunkGraph.getChunkModulesIterableBySourceType(chunk, 'css/mini-extract')) {
404
+ const output = [...chunk.files].find(f => f.endsWith(".css"));
405
+ if (output) {
406
+ rawAssetData[output] = { dependencies: [], version: chunk.contentHash['css/mini-extract'] ?? chunk.hash ?? "" };
407
+ }
408
+ }
409
+ }
385
410
  const usedHandles = {};
386
411
  for (const [blockFolder, config] of blockDirConfigs) {
387
412
  for (const mappableKey of BlockJSONManagingPlugin.mappableKeys) {
@@ -389,7 +414,7 @@ class BlockJSONManagingPlugin {
389
414
  if (cfg) {
390
415
  if (Array.isArray(cfg)) {
391
416
  for (let i = 0; i < cfg.length; i++) {
392
- const assetDetails = getAssetDetails(blockFolder, cfg[i], rawAssetData, mappableKey, usedHandles);
417
+ const assetDetails = getAssetDetails(blockFolder, cfg[i], rawAssetData, additionalFilesMap, mappableKey, usedHandles);
393
418
  if (assetDetails) {
394
419
  (assetDetails[0] ? styleHandles : scriptHandles)[assetDetails[1]] = assetDetails[2];
395
420
  cfg[i] = assetDetails[1];
@@ -397,7 +422,7 @@ class BlockJSONManagingPlugin {
397
422
  }
398
423
  }
399
424
  else {
400
- const assetDetails = getAssetDetails(blockFolder, cfg, rawAssetData, mappableKey, usedHandles);
425
+ const assetDetails = getAssetDetails(blockFolder, cfg, rawAssetData, additionalFilesMap, mappableKey, usedHandles);
401
426
  if (assetDetails) {
402
427
  (assetDetails[0] ? styleHandles : scriptHandles)[assetDetails[1]] = assetDetails[2];
403
428
  config[mappableKey] = assetDetails[1];
@@ -495,15 +520,26 @@ function makeSync() {
495
520
  });
496
521
  return res;
497
522
  }
498
- function getAssetDetails(blockFolder, asset, rawAssetData, mappableKey, usedHandles) {
523
+ function getAssetDetails(blockFolder, asset, rawAssetData, additionalFilesMap, mappableKey, usedHandles) {
499
524
  if (!asset.startsWith("file:./")) {
500
525
  return undefined;
501
526
  }
502
527
  const src = `${blockFolder}/${asset.substring(7)}`;
503
528
  const isCss = src.endsWith(".css");
504
- const assetData = isCss
505
- ? rawAssetData[src.substring(0, src.length - 3) + "js"] ?? rawAssetData[src.substring(0, src.length - 3).replace("style-style", "style") + "js"]
506
- : rawAssetData[src];
529
+ let assetData;
530
+ if (rawAssetData[src]) {
531
+ assetData = rawAssetData[src];
532
+ }
533
+ else {
534
+ const lookupSrc = additionalFilesMap.get(src);
535
+ if (lookupSrc) {
536
+ const tempAssetData = getAssetDataAccountingForCSS(lookupSrc, rawAssetData);
537
+ assetData = tempAssetData ? { ...tempAssetData, dependencies: [] } : undefined;
538
+ }
539
+ else {
540
+ assetData = getAssetDataAccountingForCSS(src, rawAssetData);
541
+ }
542
+ }
507
543
  if (!assetData) {
508
544
  return undefined;
509
545
  }
@@ -519,3 +555,13 @@ function getAssetDetails(blockFolder, asset, rawAssetData, mappableKey, usedHand
519
555
  { src, rest: isCss || mappableKey.startsWith("editor") ? [assetData.dependencies, assetData.version] : [assetData.dependencies, assetData.version, { strategy: 'defer' }] }
520
556
  ];
521
557
  }
558
+ function getAssetDataAccountingForCSS(src, rawAssetData) {
559
+ if (rawAssetData[src]) {
560
+ return rawAssetData[src];
561
+ }
562
+ if (src.endsWith(".css")) {
563
+ return rawAssetData[src.substring(0, src.length - 3) + "js"] // A simple style dependency that isn't named "style"
564
+ ?? rawAssetData[src.substring(0, src.length - 3).replace("style-style", "style") + "js"]; // A simple style dependency that IS named style
565
+ }
566
+ return undefined;
567
+ }
@@ -513,6 +513,24 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
513
513
  publicPath: publicPath,
514
514
  library: outputLibrary
515
515
  },
516
+ optimization: {
517
+ ...webpackConfig.optimization,
518
+ splitChunks: {
519
+ cacheGroups: {
520
+ style: {
521
+ // This is a flagrant abuse of cache groups, but it fixes a persistent problem wherein the dependencies and versions of scripts and styles were bleeding into each-other
522
+ type: 'css/mini-extract',
523
+ chunks: 'all',
524
+ enforce: true,
525
+ name(_, chunks, cacheGroupKey) {
526
+ const chunkName = chunks.find(chunk => chunk.name)?.name;
527
+ return chunkName.includes("script") ? `${node_path_1.default.dirname(chunkName)}/${cacheGroupKey}-${node_path_1.default.basename(chunkName)}` : chunkName;
528
+ },
529
+ },
530
+ default: false,
531
+ },
532
+ }
533
+ },
516
534
  module: {
517
535
  ...webpackConfig.module,
518
536
  rules: fixedRules
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.44.3",
3
+ "version": "2.44.5",
4
4
  "license": "UNLICENSED",
5
5
  "scripts": {
6
6
  "prepublishOnly": "rm -rf build && mkdir build && tsc",