@plaudit/webpack-extensions 2.30.0 → 2.30.2

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.
@@ -2,10 +2,13 @@ import { type Compilation, type Compiler, type WebpackPluginInstance } from "web
2
2
  export default class BlockJSONManagingPlugin implements WebpackPluginInstance {
3
3
  private readonly standaloneBlocks;
4
4
  private readonly processingModules;
5
- private static readonly sourceToOutputMapping;
5
+ private static readonly sourceToOutputMappingCache;
6
6
  private static readonly blockJsonToEntrypointsMap;
7
- private static readonly blockJSONAssetSources;
7
+ private static readonly blockJsonRawDependenciesMap;
8
+ private static readonly blockJSONAssetSourceDirs;
8
9
  constructor(standaloneBlocks: boolean, processingModules: boolean);
10
+ static recordRawDependency(entrypoint: string, dependency: string): void;
11
+ static recordBlockJSONAssetSourceDir(entrypoint: string, source: string): void;
9
12
  apply(compiler: Compiler): void;
10
13
  static resolveDestinationBySourceExtension(srcPath: string, entrypoint: Compilation['asyncEntrypoints'][number]): string | undefined;
11
14
  static findCommonAncestor(...paths: string[]): string[];
@@ -11,63 +11,60 @@ const php_serializer_1 = __importDefault(require("./php-serializer"));
11
11
  class BlockJSONManagingPlugin {
12
12
  standaloneBlocks;
13
13
  processingModules;
14
- static sourceToOutputMapping = new Map();
14
+ static sourceToOutputMappingCache = new Map();
15
15
  static blockJsonToEntrypointsMap = new Map();
16
- static blockJSONAssetSources = new Map();
16
+ static blockJsonRawDependenciesMap = new Map();
17
+ static blockJSONAssetSourceDirs = new Map();
17
18
  constructor(standaloneBlocks, processingModules) {
18
19
  this.standaloneBlocks = standaloneBlocks;
19
20
  this.processingModules = processingModules;
20
21
  }
22
+ static recordRawDependency(entrypoint, dependency) {
23
+ let deps = BlockJSONManagingPlugin.blockJsonRawDependenciesMap.get(entrypoint);
24
+ if (deps === undefined) {
25
+ BlockJSONManagingPlugin.blockJsonRawDependenciesMap.set(entrypoint, deps = new Set());
26
+ }
27
+ deps.add(dependency);
28
+ }
29
+ static recordBlockJSONAssetSourceDir(entrypoint, source) {
30
+ BlockJSONManagingPlugin.blockJSONAssetSourceDirs.set(entrypoint, source);
31
+ }
21
32
  apply(compiler) {
22
33
  compiler.hooks.compilation.tap(this.constructor.name, compilation => {
23
- compilation.hooks.processAssets.tap({ name: this.constructor.name, stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE }, () => {
24
- for (const blockEntrypoint of compilation.entrypoints.values()) {
25
- if (blockEntrypoint.name?.endsWith("/block.json")) {
26
- const source = blockEntrypoint.origins.map(origin => origin.request)[0];
27
- const name = blockEntrypoint.getEntrypointChunk().name;
28
- if (!source || !name) {
29
- continue;
30
- }
31
- BlockJSONManagingPlugin.blockJSONAssetSources.set(name, source);
32
- const dependencies = blockEntrypoint.getEntrypointChunk().getEntryOptions()?.dependOn;
33
- if (dependencies) {
34
- for (const dependency of dependencies) {
35
- const entrypoint = compilation.entrypoints.get(dependency);
36
- if (!entrypoint) {
37
- continue;
38
- }
39
- const srcPath = entrypoint.origins.map(origin => origin.request)[0];
40
- if (!srcPath) {
41
- continue;
42
- }
43
- const destPath = BlockJSONManagingPlugin.resolveDestinationBySourceExtension(srcPath, entrypoint);
44
- if (!destPath) {
45
- continue;
46
- }
47
- const entryMeta = { hash: entrypoint.getEntrypointChunk().hash ?? destPath, path: destPath };
48
- const currentEntrypoints = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name);
49
- if (currentEntrypoints) {
50
- currentEntrypoints.set(srcPath, entryMeta);
51
- }
52
- else {
53
- BlockJSONManagingPlugin.blockJsonToEntrypointsMap.set(name, new Map([[srcPath, entryMeta]]));
54
- }
34
+ compilation.hooks.afterProcessAssets.tap(`${this.constructor.name}_ProcessBlockJSONFiles`, compilationAssets => {
35
+ const currentBlockJSONAssets = BlockJSONManagingPlugin.blockJSONAssetSourceDirs.entries()
36
+ .filter(([name]) => name in compilationAssets)
37
+ .toArray();
38
+ for (const [name] of currentBlockJSONAssets) {
39
+ const dependencies = BlockJSONManagingPlugin.blockJsonRawDependenciesMap.get(name);
40
+ if (dependencies) {
41
+ for (const dependency of dependencies) {
42
+ const entrypoint = compilation.entrypoints.get(dependency);
43
+ if (!entrypoint) {
44
+ continue;
45
+ }
46
+ const srcPath = entrypoint.origins.map(origin => origin.request)[0];
47
+ if (!srcPath) {
48
+ continue;
49
+ }
50
+ const destPath = BlockJSONManagingPlugin.resolveDestinationBySourceExtension(srcPath, entrypoint);
51
+ if (!destPath) {
52
+ continue;
53
+ }
54
+ const entryMeta = { hash: entrypoint.getEntrypointChunk().hash ?? destPath, path: destPath };
55
+ const currentEntrypoints = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name);
56
+ if (currentEntrypoints) {
57
+ currentEntrypoints.set(srcPath, entryMeta);
58
+ }
59
+ else {
60
+ BlockJSONManagingPlugin.blockJsonToEntrypointsMap.set(name, new Map([[srcPath, entryMeta]]));
55
61
  }
56
62
  }
57
63
  }
58
64
  }
59
- });
60
- compilation.hooks.afterProcessAssets.tap(`${this.constructor.name}_ProcessBlockJSONFiles`, compilationAssets => {
61
- // We start by trimming away the unused metadata files that WebPack creates.
62
- for (const name of Object.keys(compilationAssets)) {
63
- if (name.endsWith("block.json.asset.php")) {
64
- compilation.deleteAsset(name);
65
- compilation.deleteAsset(name.substring(0, name.length - 10) + ".js"); // This removes the block.json.js and block.json.js.map files
66
- }
67
- }
68
65
  if (this.processingModules) {
69
66
  // We perform the processing in the non-modules half ONLY because we don't necessarily have all the information we need during the modules half.
70
- for (const name of BlockJSONManagingPlugin.blockJSONAssetSources.keys()) {
67
+ for (const [name] of currentBlockJSONAssets) {
71
68
  compilation.deleteAsset(name); // While the method is called, "deleteAsset", it doesn't actually delete it - just halts emission.
72
69
  }
73
70
  return;
@@ -76,31 +73,30 @@ class BlockJSONManagingPlugin {
76
73
  const mappableKeys = ["editorStyle", "style", "viewStyle", "viewScript", "script", "editorScript", "viewScriptModule", "scriptModule"];
77
74
  const remapValue = (value, name) => {
78
75
  if (value.startsWith("file:")) {
79
- let res = BlockJSONManagingPlugin.sourceToOutputMapping.get(`${name};${value}`);
76
+ let res = BlockJSONManagingPlugin.sourceToOutputMappingCache.get(`${name};${value}`);
80
77
  if (res !== undefined) {
81
78
  return res;
82
79
  }
83
- const sourceDir = node_path_1.default.dirname(BlockJSONManagingPlugin.blockJSONAssetSources.get(name) ?? name);
80
+ const sourceDir = BlockJSONManagingPlugin.blockJSONAssetSourceDirs.get(name) ?? node_path_1.default.dirname(name);
84
81
  const inputPath = node_path_1.default.normalize(node_path_1.default.join(sourceDir, value.substring(5)));
85
82
  const output = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name)?.get(inputPath);
86
83
  if (output !== undefined) {
87
84
  const prefix = value.startsWith("./", 5) ? "./" : "";
88
85
  const relativePath = node_path_1.default.relative(node_path_1.default.dirname(name), output.path);
89
86
  const res = [`file:${prefix}${relativePath}`, output.hash];
90
- BlockJSONManagingPlugin.sourceToOutputMapping.set(`${name};${value}`, res);
87
+ BlockJSONManagingPlugin.sourceToOutputMappingCache.set(`${name};${value}`, res);
91
88
  return res;
92
89
  }
93
90
  }
94
91
  return [value, ""];
95
92
  };
96
- for (const [name, source] of BlockJSONManagingPlugin.blockJSONAssetSources.entries()) {
97
- const sourceDir = node_path_1.default.dirname(source);
98
- const asset = compilation.assets[name]?.buffer().toString();
99
- if (!asset) {
93
+ for (const [name, sourceDir] of currentBlockJSONAssets) {
94
+ const assetContents = compilation.assets[name]?.buffer().toString();
95
+ if (!assetContents) {
100
96
  continue;
101
97
  }
102
98
  blockDirConfigData[name] = true;
103
- const json = JSON.parse(asset);
99
+ const json = JSON.parse(assetContents);
104
100
  let compositeHash = "";
105
101
  for (const mappableKey of mappableKeys) {
106
102
  if (mappableKey in json) {
@@ -367,7 +367,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
367
367
  res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep, blockJSONChunkName));
368
368
  }
369
369
  }
370
- res.push([blockJSONChunkName, { import: node_path_1.default.join(dir, 'block.json') }]);
370
+ BlockJSONManagingPlugin_1.default.recordBlockJSONAssetSourceDir(blockJSONChunkName, dir);
371
371
  return res;
372
372
  }
373
373
  catch (e) {
@@ -403,22 +403,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
403
403
  // This is used to allow for block.json dependencies to correctly account for name-deduplication
404
404
  for (const [key, entry] of Object.entries(currentEntry)) {
405
405
  if (typeof entry === 'object' && !Array.isArray(entry) && 'lazyDependent' in entry && typeof entry.lazyDependent === 'string') {
406
- const target = currentEntry[entry.lazyDependent];
407
- if (typeof target === 'object' && !Array.isArray(target)) {
408
- let dependOn;
409
- if (target.dependOn === undefined) {
410
- dependOn = target.dependOn = [];
411
- }
412
- else if (typeof target.dependOn === 'string') {
413
- dependOn = target.dependOn = [target.dependOn];
414
- }
415
- else {
416
- dependOn = target.dependOn;
417
- }
418
- if (!dependOn.includes(key)) {
419
- dependOn.push(key);
420
- }
421
- }
406
+ BlockJSONManagingPlugin_1.default.recordRawDependency(entry.lazyDependent, key);
422
407
  }
423
408
  }
424
409
  return currentEntry;
@@ -475,16 +460,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
475
460
  },
476
461
  module: {
477
462
  ...webpackConfig.module,
478
- rules: [...fixedRules, {
479
- test: /block\.json/i,
480
- type: 'asset/resource',
481
- generator: {
482
- filename(pathData) {
483
- // This mess of a name-generator accounts for WebPack's calculated name changing when block.json has dependents.
484
- return node_path_1.default.join(node_path_1.default.basename(node_path_1.default.dirname(pathData.filename ?? pathData.runtime?.toString() ?? "[name]")), "[name][ext]");
485
- }
486
- }
487
- }]
463
+ rules: fixedRules
488
464
  },
489
465
  resolve: {
490
466
  ...webpackConfig.resolve,
@@ -494,7 +470,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
494
470
  stats: config.stats,
495
471
  plugins: copyFiles
496
472
  ? plugins.map(plugin => plugin.constructor.name === 'CopyPlugin'
497
- ? new copy_webpack_plugin_1.default({ patterns: [{ from: standaloneBlocks ? '**/(*.(php|twig|svg))' : '**/(*.(asset\.php|svg))',
473
+ ? new copy_webpack_plugin_1.default({ patterns: [{ from: standaloneBlocks ? '**/(block.json|*.(php|twig|svg))' : '**/(block.json|*.(asset\.php|svg))',
498
474
  context: srcRoot, noErrorOnMissing: true }] })
499
475
  : plugin)
500
476
  : (srcIsDirectory
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.30.0",
3
+ "version": "2.30.2",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",