@plaudit/webpack-extensions 2.30.1 → 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,12 +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
7
  private static readonly blockJsonRawDependenciesMap;
8
- private static readonly blockJSONAssetSources;
8
+ private static readonly blockJSONAssetSourceDirs;
9
9
  constructor(standaloneBlocks: boolean, processingModules: boolean);
10
10
  static recordRawDependency(entrypoint: string, dependency: string): void;
11
+ static recordBlockJSONAssetSourceDir(entrypoint: string, source: string): void;
11
12
  apply(compiler: Compiler): void;
12
13
  static resolveDestinationBySourceExtension(srcPath: string, entrypoint: Compilation['asyncEntrypoints'][number]): string | undefined;
13
14
  static findCommonAncestor(...paths: string[]): string[];
@@ -11,10 +11,10 @@ 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
16
  static blockJsonRawDependenciesMap = new Map();
17
- static blockJSONAssetSources = new Map();
17
+ static blockJSONAssetSourceDirs = new Map();
18
18
  constructor(standaloneBlocks, processingModules) {
19
19
  this.standaloneBlocks = standaloneBlocks;
20
20
  this.processingModules = processingModules;
@@ -26,56 +26,45 @@ class BlockJSONManagingPlugin {
26
26
  }
27
27
  deps.add(dependency);
28
28
  }
29
+ static recordBlockJSONAssetSourceDir(entrypoint, source) {
30
+ BlockJSONManagingPlugin.blockJSONAssetSourceDirs.set(entrypoint, source);
31
+ }
29
32
  apply(compiler) {
30
33
  compiler.hooks.compilation.tap(this.constructor.name, compilation => {
31
- compilation.hooks.processAssets.tap({ name: this.constructor.name, stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE }, () => {
32
- for (const blockEntrypoint of compilation.entrypoints.values()) {
33
- if (blockEntrypoint.name?.endsWith("/block.json")) {
34
- const source = blockEntrypoint.origins.map(origin => origin.request)[0];
35
- const name = blockEntrypoint.getEntrypointChunk().name;
36
- if (!source || !name) {
37
- continue;
38
- }
39
- BlockJSONManagingPlugin.blockJSONAssetSources.set(name, source);
40
- const dependencies = BlockJSONManagingPlugin.blockJsonRawDependenciesMap.get(blockEntrypoint.name);
41
- if (dependencies) {
42
- for (const dependency of dependencies) {
43
- const entrypoint = compilation.entrypoints.get(dependency);
44
- if (!entrypoint) {
45
- continue;
46
- }
47
- const srcPath = entrypoint.origins.map(origin => origin.request)[0];
48
- if (!srcPath) {
49
- continue;
50
- }
51
- const destPath = BlockJSONManagingPlugin.resolveDestinationBySourceExtension(srcPath, entrypoint);
52
- if (!destPath) {
53
- continue;
54
- }
55
- const entryMeta = { hash: entrypoint.getEntrypointChunk().hash ?? destPath, path: destPath };
56
- const currentEntrypoints = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name);
57
- if (currentEntrypoints) {
58
- currentEntrypoints.set(srcPath, entryMeta);
59
- }
60
- else {
61
- BlockJSONManagingPlugin.blockJsonToEntrypointsMap.set(name, new Map([[srcPath, entryMeta]]));
62
- }
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]]));
63
61
  }
64
62
  }
65
63
  }
66
64
  }
67
- });
68
- compilation.hooks.afterProcessAssets.tap(`${this.constructor.name}_ProcessBlockJSONFiles`, compilationAssets => {
69
- // We start by trimming away the unused metadata files that WebPack creates.
70
- for (const name of Object.keys(compilationAssets)) {
71
- if (name.endsWith("block.json.asset.php")) {
72
- compilation.deleteAsset(name);
73
- compilation.deleteAsset(name.substring(0, name.length - 10) + ".js"); // This removes the block.json.js and block.json.js.map files
74
- }
75
- }
76
65
  if (this.processingModules) {
77
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.
78
- for (const name of BlockJSONManagingPlugin.blockJSONAssetSources.keys()) {
67
+ for (const [name] of currentBlockJSONAssets) {
79
68
  compilation.deleteAsset(name); // While the method is called, "deleteAsset", it doesn't actually delete it - just halts emission.
80
69
  }
81
70
  return;
@@ -84,31 +73,30 @@ class BlockJSONManagingPlugin {
84
73
  const mappableKeys = ["editorStyle", "style", "viewStyle", "viewScript", "script", "editorScript", "viewScriptModule", "scriptModule"];
85
74
  const remapValue = (value, name) => {
86
75
  if (value.startsWith("file:")) {
87
- let res = BlockJSONManagingPlugin.sourceToOutputMapping.get(`${name};${value}`);
76
+ let res = BlockJSONManagingPlugin.sourceToOutputMappingCache.get(`${name};${value}`);
88
77
  if (res !== undefined) {
89
78
  return res;
90
79
  }
91
- 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);
92
81
  const inputPath = node_path_1.default.normalize(node_path_1.default.join(sourceDir, value.substring(5)));
93
82
  const output = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name)?.get(inputPath);
94
83
  if (output !== undefined) {
95
84
  const prefix = value.startsWith("./", 5) ? "./" : "";
96
85
  const relativePath = node_path_1.default.relative(node_path_1.default.dirname(name), output.path);
97
86
  const res = [`file:${prefix}${relativePath}`, output.hash];
98
- BlockJSONManagingPlugin.sourceToOutputMapping.set(`${name};${value}`, res);
87
+ BlockJSONManagingPlugin.sourceToOutputMappingCache.set(`${name};${value}`, res);
99
88
  return res;
100
89
  }
101
90
  }
102
91
  return [value, ""];
103
92
  };
104
- for (const [name, source] of BlockJSONManagingPlugin.blockJSONAssetSources.entries()) {
105
- const sourceDir = node_path_1.default.dirname(source);
106
- const asset = compilation.assets[name]?.buffer().toString();
107
- if (!asset) {
93
+ for (const [name, sourceDir] of currentBlockJSONAssets) {
94
+ const assetContents = compilation.assets[name]?.buffer().toString();
95
+ if (!assetContents) {
108
96
  continue;
109
97
  }
110
98
  blockDirConfigData[name] = true;
111
- const json = JSON.parse(asset);
99
+ const json = JSON.parse(assetContents);
112
100
  let compositeHash = "";
113
101
  for (const mappableKey of mappableKeys) {
114
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) {
@@ -460,16 +460,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
460
460
  },
461
461
  module: {
462
462
  ...webpackConfig.module,
463
- rules: [...fixedRules, {
464
- test: /block\.json/i,
465
- type: 'asset/resource',
466
- generator: {
467
- filename(pathData) {
468
- // This mess of a name-generator accounts for WebPack's calculated name changing when block.json has dependents.
469
- return node_path_1.default.join(node_path_1.default.basename(node_path_1.default.dirname(pathData.filename ?? pathData.runtime?.toString() ?? "[name]")), "[name][ext]");
470
- }
471
- }
472
- }]
463
+ rules: fixedRules
473
464
  },
474
465
  resolve: {
475
466
  ...webpackConfig.resolve,
@@ -479,7 +470,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
479
470
  stats: config.stats,
480
471
  plugins: copyFiles
481
472
  ? plugins.map(plugin => plugin.constructor.name === 'CopyPlugin'
482
- ? 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))',
483
474
  context: srcRoot, noErrorOnMissing: true }] })
484
475
  : plugin)
485
476
  : (srcIsDirectory
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.30.1",
3
+ "version": "2.30.2",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",