@plaudit/webpack-extensions 2.33.0 → 2.34.1

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;
@@ -16,4 +16,6 @@ export default class BlockJSONManagingPlugin implements WebpackPluginInstance {
16
16
  private static remapReferencedPHPFilesOnKey;
17
17
  private static hashThingForAsset;
18
18
  private static stripFilePrefix;
19
+ private static incorporateRemappedValue;
20
+ private static deriveAdditionalFilesMappableKey;
19
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
  }
@@ -269,5 +279,47 @@ class BlockJSONManagingPlugin {
269
279
  static stripFilePrefix(file) {
270
280
  return file.startsWith("file:./") ? file.substring(7) : file;
271
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
+ }
272
324
  }
273
325
  exports.default = BlockJSONManagingPlugin;
@@ -20,18 +20,25 @@ function phpSerialize(value, objectsAsArrays = true) {
20
20
  case "boolean":
21
21
  return `b:${value ? 1 : 0};`;
22
22
  case "number":
23
- return `${Number.isInteger(value) ? 'i' : 'd'}:${value};`;
23
+ return Number.isInteger(value) ? serializeInteger(value) : `d:${value}`;
24
24
  case "string":
25
- return `s:${value.length}:"${value}";`;
25
+ return `s:${Buffer.byteLength(value, 'utf8')}:"${value}";`;
26
26
  case "function":
27
27
  throw new Error(`We are unable to serialize functions into a form readable by PHP.`);
28
28
  case "symbol":
29
29
  value = value.toString();
30
- return `s:${value.length}:"${value}";`;
30
+ return `s:${Buffer.byteLength(value, 'utf8')}:"${value}";`;
31
31
  case "bigint":
32
- return `i:${value};`;
32
+ return serializeInteger(value);
33
33
  }
34
34
  }
35
+ function serializeInteger(value) {
36
+ const str = value.toString();
37
+ if (str.includes("e") || str.includes("E")) {
38
+ return `d:${str.replace(/e/i, ".0e")}`;
39
+ }
40
+ return `i:${str}`;
41
+ }
35
42
  function entriesInArrayAndObjectFormat(prefix, entries, objectsAsArrays) {
36
43
  let res = `${prefix}:${entries.length}:{`;
37
44
  for (const [k, v] of entries) {
@@ -17,6 +17,9 @@ const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-e
17
17
  function joinPossiblyAbsolutePaths(...paths) {
18
18
  return paths.reduce((res, p) => !res || node_path_1.default.isAbsolute(p) ? p : node_path_1.default.join(res, p), '') || '.';
19
19
  }
20
+ function test(content, map, meta) {
21
+ this.resourcePath;
22
+ }
20
23
  function mapToRealEntrypoints(entrypoint, dir, mapper = (entrypoint) => entrypoint, lazyDependent) {
21
24
  return (Array.isArray(entrypoint) ? entrypoint : [entrypoint])
22
25
  .map(ep => joinPossiblyAbsolutePaths(dir, mapper(ep)))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.33.0",
3
+ "version": "2.34.1",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",