@plaudit/webpack-extensions 2.32.0 → 2.34.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.
@@ -2,7 +2,7 @@ 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 sourceToOutputMappingCache;
5
+ private static readonly mappableKeys;
6
6
  private static readonly blockJsonToEntrypointsMap;
7
7
  private static readonly blockJsonRawDependenciesMap;
8
8
  private static readonly blockJSONAssetSourceDirs;
@@ -13,6 +13,9 @@ export default class BlockJSONManagingPlugin implements WebpackPluginInstance {
13
13
  static resolveDestinationBySourceExtension(srcPath: string, entrypoint: Compilation['asyncEntrypoints'][number]): string | undefined;
14
14
  static findCommonAncestor(...paths: string[]): string[];
15
15
  static findRelativeRouteBetween(path1: string, path2: string): string;
16
+ private static remapReferencedPHPFilesOnKey;
16
17
  private static hashThingForAsset;
17
18
  private static stripFilePrefix;
19
+ private static incorporateRemappedValue;
20
+ private static deriveAdditionalFilesMappableKey;
18
21
  }
@@ -11,7 +11,9 @@ const php_serializer_1 = __importDefault(require("./php-serializer"));
11
11
  class BlockJSONManagingPlugin {
12
12
  standaloneBlocks;
13
13
  processingModules;
14
- static sourceToOutputMappingCache = new Map();
14
+ static mappableKeys = [
15
+ "editorStyle", "style", "viewStyle", "editorScript", "script", "viewScript", "viewScriptModule", "scriptModule"
16
+ ];
15
17
  static blockJsonToEntrypointsMap = new Map();
16
18
  static blockJsonRawDependenciesMap = new Map();
17
19
  static blockJSONAssetSourceDirs = new Map();
@@ -51,13 +53,20 @@ class BlockJSONManagingPlugin {
51
53
  if (!destPath) {
52
54
  continue;
53
55
  }
54
- const entryMeta = { hash: entrypoint.getEntrypointChunk().hash ?? destPath, path: destPath };
56
+ const entrypointChunk = entrypoint.getEntrypointChunk();
57
+ const additionalFiles = [];
58
+ for (const entrypointChunkFile of entrypointChunk.files) {
59
+ if (!entrypointChunkFile.endsWith(".asset.php") && entrypointChunkFile !== destPath) {
60
+ additionalFiles.push(entrypointChunkFile);
61
+ }
62
+ }
63
+ const entryMeta = { hash: entrypointChunk.hash ?? destPath, path: destPath };
55
64
  const currentEntrypoints = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name);
56
65
  if (currentEntrypoints) {
57
- currentEntrypoints.set(srcPath, entryMeta);
66
+ currentEntrypoints.set(srcPath, [entryMeta, additionalFiles]);
58
67
  }
59
68
  else {
60
- BlockJSONManagingPlugin.blockJsonToEntrypointsMap.set(name, new Map([[srcPath, entryMeta]]));
69
+ BlockJSONManagingPlugin.blockJsonToEntrypointsMap.set(name, new Map([[srcPath, [entryMeta, additionalFiles]]]));
61
70
  }
62
71
  }
63
72
  }
@@ -70,25 +79,20 @@ class BlockJSONManagingPlugin {
70
79
  return;
71
80
  }
72
81
  const blockDirConfigData = {};
73
- const mappableKeys = ["editorStyle", "style", "viewStyle", "viewScript", "script", "editorScript", "viewScriptModule", "scriptModule"];
74
82
  const remapValue = (value, name) => {
75
83
  if (value.startsWith("file:")) {
76
- let res = BlockJSONManagingPlugin.sourceToOutputMappingCache.get(`${name};${value}`);
77
- if (res !== undefined) {
78
- return res;
79
- }
80
84
  const sourceDir = BlockJSONManagingPlugin.blockJSONAssetSourceDirs.get(name) ?? node_path_1.default.dirname(name);
81
85
  const inputPath = node_path_1.default.normalize(node_path_1.default.join(sourceDir, value.substring(5)));
82
- const output = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name)?.get(inputPath);
83
- if (output !== undefined) {
86
+ const outputAndAdditionalFiles = BlockJSONManagingPlugin.blockJsonToEntrypointsMap.get(name)?.get(inputPath);
87
+ if (outputAndAdditionalFiles !== undefined) {
88
+ const [output, additionalFiles] = outputAndAdditionalFiles;
84
89
  const prefix = value.startsWith("./", 5) ? "./" : "";
85
90
  const relativePath = node_path_1.default.relative(node_path_1.default.dirname(name), output.path);
86
91
  const res = [`file:${prefix}${relativePath}`, output.hash];
87
- BlockJSONManagingPlugin.sourceToOutputMappingCache.set(`${name};${value}`, res);
88
- return res;
92
+ return [res, additionalFiles.map(additionalFile => `file:./${node_path_1.default.relative(node_path_1.default.dirname(name), additionalFile)}`)];
89
93
  }
90
94
  }
91
- return [value, ""];
95
+ return [[value, ""], []];
92
96
  };
93
97
  for (const [name, sourceDir] of currentBlockJSONAssets) {
94
98
  const assetContents = compilation.assets[name]?.buffer().toString();
@@ -98,21 +102,27 @@ class BlockJSONManagingPlugin {
98
102
  blockDirConfigData[name] = true;
99
103
  const json = JSON.parse(assetContents);
100
104
  let compositeHash = "";
101
- for (const mappableKey of mappableKeys) {
105
+ const tempMappedKeys = {};
106
+ for (const mappableKey of BlockJSONManagingPlugin.mappableKeys) {
102
107
  if (mappableKey in json) {
103
108
  const unmappedValue = json[mappableKey];
104
109
  if (Array.isArray(unmappedValue)) {
105
- const remappedValue = unmappedValue.map(value => remapValue(value, name));
106
- json[mappableKey] = remappedValue.map(([resource]) => resource);
107
- compositeHash += "~" + remappedValue.map(([_, hash]) => hash).join("~");
110
+ for (const uv of unmappedValue) {
111
+ const [remappedValue, additionalValues] = remapValue(uv, name);
112
+ BlockJSONManagingPlugin.incorporateRemappedValue(tempMappedKeys, mappableKey, remappedValue[0], additionalValues);
113
+ compositeHash += "~" + remappedValue[1];
114
+ }
108
115
  }
109
116
  else if (typeof unmappedValue === 'string') {
110
- const remappedValue = remapValue(unmappedValue, name);
111
- json[mappableKey] = remappedValue[0];
117
+ const [remappedValue, additionalValues] = remapValue(unmappedValue, name);
118
+ BlockJSONManagingPlugin.incorporateRemappedValue(tempMappedKeys, mappableKey, remappedValue[0], additionalValues);
112
119
  compositeHash += "~" + remappedValue[1];
113
120
  }
114
121
  }
115
122
  }
123
+ for (const [mappableKey, mappedValue] of Object.entries(tempMappedKeys)) {
124
+ json[mappableKey] = mappedValue;
125
+ }
116
126
  if (json["version"]) {
117
127
  json["version"] = `${json["version"]}-${BlockJSONManagingPlugin.hashThingForAsset(compositeHash)}`;
118
128
  }
@@ -121,32 +131,8 @@ class BlockJSONManagingPlugin {
121
131
  }
122
132
  const outputDir = node_path_1.default.join(compiler.outputPath, node_path_1.default.dirname(name));
123
133
  const pathsNeedRemapping = !this.standaloneBlocks && json["plaudit"] !== "simple";
124
- const rawSetupFiles = json["plaudit"]?.["setup"]
125
- ? (typeof json["plaudit"]["setup"] === 'string' ? [json["plaudit"]["setup"]] : json["plaudit"]["setup"])
126
- : ["setup.php"];
127
- const setupFiles = pathsNeedRemapping
128
- ? rawSetupFiles
129
- .map(p => node_path_1.default.normalize(node_path_1.default.join(sourceDir, BlockJSONManagingPlugin.stripFilePrefix(p))))
130
- .filter(p => node_fs_1.default.existsSync(p))
131
- .map(p => `file:./${BlockJSONManagingPlugin.findRelativeRouteBetween(outputDir, p)}`)
132
- : rawSetupFiles
133
- .filter(p => node_fs_1.default.existsSync(node_path_1.default.normalize(node_path_1.default.join(sourceDir, BlockJSONManagingPlugin.stripFilePrefix(p)))));
134
- if (setupFiles.length === 0) {
135
- if (json["plaudit"]?.["setup"] !== undefined) {
136
- delete json["plaudit"]["setup"];
137
- }
138
- }
139
- else {
140
- if (typeof json["plaudit"] !== 'object') {
141
- if (json["plaudit"] === "native") {
142
- json["plaudit"] = { type: "native" };
143
- }
144
- else {
145
- json["plaudit"] = {};
146
- }
147
- }
148
- json["plaudit"]["setup"] = setupFiles.length === 1 ? setupFiles[0] : setupFiles;
149
- }
134
+ BlockJSONManagingPlugin.remapReferencedPHPFilesOnKey(json, "setup", pathsNeedRemapping, sourceDir, outputDir, compilation, true);
135
+ BlockJSONManagingPlugin.remapReferencedPHPFilesOnKey(json, "variations", pathsNeedRemapping, sourceDir, outputDir, compilation, false);
150
136
  if (json["acf"]) {
151
137
  if (json["acf"]["renderTemplate"]) {
152
138
  json["render_template"] = json["acf"]["renderTemplate"];
@@ -232,11 +218,108 @@ class BlockJSONManagingPlugin {
232
218
  route.push(node_path_1.default.relative(commonAncestor.join(node_path_1.default.sep), path2));
233
219
  return route.join(node_path_1.default.sep);
234
220
  }
221
+ static remapReferencedPHPFilesOnKey(json, key, pathsNeedRemapping, sourceDir, outputDir, compilation, inPlaudit) {
222
+ const rawValue = (inPlaudit ? json["plaudit"] : json)?.[key];
223
+ let rawFiles;
224
+ let deleteOnEmpty = true;
225
+ if (typeof rawValue === 'string') {
226
+ rawFiles = [rawValue];
227
+ }
228
+ else if (Array.isArray(rawValue) && rawValue.length > 0) {
229
+ const fileReferences = rawValue.filter((value) => typeof value === 'string' && value.startsWith("file:./"));
230
+ if (fileReferences.length === rawValue.length) {
231
+ rawFiles = fileReferences;
232
+ }
233
+ else {
234
+ if (fileReferences.length !== 0) {
235
+ const error = new webpack_1.WebpackError(`Encountered a block with a mixture of file-reference and non-file-reference elements in the "${inPlaudit ? 'plaudit.' : ''}${key}" property`);
236
+ error.file = node_path_1.default.join(sourceDir, 'block.json');
237
+ compilation.warnings.push(error);
238
+ rawFiles = fileReferences;
239
+ }
240
+ else {
241
+ rawFiles = [`${key}.php`]; // We want to emit an error if both an appropriately-named file AND an incompatible value exist, so we still need to do the check
242
+ }
243
+ deleteOnEmpty = false;
244
+ }
245
+ }
246
+ else {
247
+ if (rawValue && (!Array.isArray(rawValue) || rawValue.length === 0)) {
248
+ deleteOnEmpty = false;
249
+ }
250
+ rawFiles = [`${key}.php`];
251
+ }
252
+ const mappedFiles = pathsNeedRemapping
253
+ ? rawFiles
254
+ .map(p => node_path_1.default.normalize(node_path_1.default.join(sourceDir, BlockJSONManagingPlugin.stripFilePrefix(p))))
255
+ .filter(p => node_fs_1.default.existsSync(p))
256
+ .map(p => `file:./${BlockJSONManagingPlugin.findRelativeRouteBetween(outputDir, p)}`)
257
+ : rawFiles
258
+ .filter(p => node_fs_1.default.existsSync(node_path_1.default.normalize(node_path_1.default.join(sourceDir, BlockJSONManagingPlugin.stripFilePrefix(p)))));
259
+ if (mappedFiles.length === 0) {
260
+ if (deleteOnEmpty && rawValue !== undefined) {
261
+ delete (inPlaudit ? json["plaudit"] : json)[key];
262
+ }
263
+ }
264
+ else {
265
+ if (inPlaudit && typeof json["plaudit"] !== 'object') {
266
+ if (json["plaudit"] === "native") {
267
+ json["plaudit"] = { type: "native" };
268
+ }
269
+ else {
270
+ json["plaudit"] = {};
271
+ }
272
+ }
273
+ (inPlaudit ? json["plaudit"] : json)[key] = mappedFiles.length === 1 ? mappedFiles[0] : mappedFiles;
274
+ }
275
+ }
235
276
  static hashThingForAsset(thing) {
236
277
  return node_crypto_1.default.createHash('md5').update(thing).digest("hex").substring(0, 20).toLowerCase();
237
278
  }
238
279
  static stripFilePrefix(file) {
239
280
  return file.startsWith("file:./") ? file.substring(7) : file;
240
281
  }
282
+ static incorporateRemappedValue(mappedKeys, mappableKey, remappedValue, additionalValues) {
283
+ if (typeof mappedKeys[mappableKey] === 'string') {
284
+ mappedKeys[mappableKey] = [mappedKeys[mappableKey], remappedValue];
285
+ }
286
+ else if (Array.isArray(mappedKeys[mappableKey])) {
287
+ mappedKeys[mappableKey].push(remappedValue);
288
+ }
289
+ else {
290
+ mappedKeys[mappableKey] = remappedValue;
291
+ }
292
+ if (additionalValues.length) {
293
+ const additionalValuesMappableKey = BlockJSONManagingPlugin.deriveAdditionalFilesMappableKey(mappableKey);
294
+ if (typeof mappedKeys[additionalValuesMappableKey] === 'string') {
295
+ mappedKeys[additionalValuesMappableKey] = [mappedKeys[additionalValuesMappableKey], ...additionalValues];
296
+ }
297
+ else if (Array.isArray(mappedKeys[additionalValuesMappableKey])) {
298
+ mappedKeys[additionalValuesMappableKey].push(...additionalValues);
299
+ }
300
+ else {
301
+ mappedKeys[additionalValuesMappableKey] = additionalValues;
302
+ }
303
+ }
304
+ }
305
+ static deriveAdditionalFilesMappableKey(mappableKey) {
306
+ let value;
307
+ if (mappableKey.endsWith("tyle")) { // If the original file was a stylesheet, then any additional files will be imported via the script tags
308
+ value = this.mappableKeys[this.mappableKeys.indexOf(mappableKey) + 3];
309
+ }
310
+ else if (mappableKey.endsWith("cript")) { // If the original file was a script, then any additional files will be imported via the style tags
311
+ value = this.mappableKeys[this.mappableKeys.indexOf(mappableKey) - 3];
312
+ }
313
+ else if (mappableKey === "viewScriptModule") {
314
+ return "viewStyle";
315
+ }
316
+ else if (mappableKey === "scriptModule") {
317
+ return "style";
318
+ }
319
+ if (value === undefined) {
320
+ throw new webpack_1.WebpackError(`Encountered a mappable key (${mappableKey}) that has additional files but no valid mapping for them`);
321
+ }
322
+ return value;
323
+ }
241
324
  }
242
325
  exports.default = BlockJSONManagingPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.32.0",
3
+ "version": "2.34.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",