@plaudit/webpack-extensions 2.31.2 → 2.33.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.
@@ -13,6 +13,7 @@ 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;
18
19
  }
@@ -121,32 +121,8 @@ class BlockJSONManagingPlugin {
121
121
  }
122
122
  const outputDir = node_path_1.default.join(compiler.outputPath, node_path_1.default.dirname(name));
123
123
  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
- }
124
+ BlockJSONManagingPlugin.remapReferencedPHPFilesOnKey(json, "setup", pathsNeedRemapping, sourceDir, outputDir, compilation, true);
125
+ BlockJSONManagingPlugin.remapReferencedPHPFilesOnKey(json, "variations", pathsNeedRemapping, sourceDir, outputDir, compilation, false);
150
126
  if (json["acf"]) {
151
127
  if (json["acf"]["renderTemplate"]) {
152
128
  json["render_template"] = json["acf"]["renderTemplate"];
@@ -232,6 +208,61 @@ class BlockJSONManagingPlugin {
232
208
  route.push(node_path_1.default.relative(commonAncestor.join(node_path_1.default.sep), path2));
233
209
  return route.join(node_path_1.default.sep);
234
210
  }
211
+ static remapReferencedPHPFilesOnKey(json, key, pathsNeedRemapping, sourceDir, outputDir, compilation, inPlaudit) {
212
+ const rawValue = (inPlaudit ? json["plaudit"] : json)?.[key];
213
+ let rawFiles;
214
+ let deleteOnEmpty = true;
215
+ if (typeof rawValue === 'string') {
216
+ rawFiles = [rawValue];
217
+ }
218
+ else if (Array.isArray(rawValue) && rawValue.length > 0) {
219
+ const fileReferences = rawValue.filter((value) => typeof value === 'string' && value.startsWith("file:./"));
220
+ if (fileReferences.length === rawValue.length) {
221
+ rawFiles = fileReferences;
222
+ }
223
+ else {
224
+ if (fileReferences.length !== 0) {
225
+ 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`);
226
+ error.file = node_path_1.default.join(sourceDir, 'block.json');
227
+ compilation.warnings.push(error);
228
+ rawFiles = fileReferences;
229
+ }
230
+ else {
231
+ 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
232
+ }
233
+ deleteOnEmpty = false;
234
+ }
235
+ }
236
+ else {
237
+ if (rawValue && (!Array.isArray(rawValue) || rawValue.length === 0)) {
238
+ deleteOnEmpty = false;
239
+ }
240
+ rawFiles = [`${key}.php`];
241
+ }
242
+ const mappedFiles = pathsNeedRemapping
243
+ ? rawFiles
244
+ .map(p => node_path_1.default.normalize(node_path_1.default.join(sourceDir, BlockJSONManagingPlugin.stripFilePrefix(p))))
245
+ .filter(p => node_fs_1.default.existsSync(p))
246
+ .map(p => `file:./${BlockJSONManagingPlugin.findRelativeRouteBetween(outputDir, p)}`)
247
+ : rawFiles
248
+ .filter(p => node_fs_1.default.existsSync(node_path_1.default.normalize(node_path_1.default.join(sourceDir, BlockJSONManagingPlugin.stripFilePrefix(p)))));
249
+ if (mappedFiles.length === 0) {
250
+ if (deleteOnEmpty && rawValue !== undefined) {
251
+ delete (inPlaudit ? json["plaudit"] : json)[key];
252
+ }
253
+ }
254
+ else {
255
+ if (inPlaudit && typeof json["plaudit"] !== 'object') {
256
+ if (json["plaudit"] === "native") {
257
+ json["plaudit"] = { type: "native" };
258
+ }
259
+ else {
260
+ json["plaudit"] = {};
261
+ }
262
+ }
263
+ (inPlaudit ? json["plaudit"] : json)[key] = mappedFiles.length === 1 ? mappedFiles[0] : mappedFiles;
264
+ }
265
+ }
235
266
  static hashThingForAsset(thing) {
236
267
  return node_crypto_1.default.createHash('md5').update(thing).digest("hex").substring(0, 20).toLowerCase();
237
268
  }
@@ -7,7 +7,7 @@ interface AdvancedOutputConfig {
7
7
  directoryLayout?: 'blocks' | 'extensions';
8
8
  }
9
9
  type IndividualExternalDepConfig = string | {
10
- import: string | [string, ...string[]];
10
+ import?: string | [string, ...string[]];
11
11
  handle: string;
12
12
  };
13
13
  type Externals = {
@@ -236,11 +236,12 @@ function makeDependencyExtractionPlugin(externals) {
236
236
  const exactExternals = {};
237
237
  const suffixExternals = [];
238
238
  for (const [key, value] of Object.entries(externals)) {
239
+ const normalizedValue = typeof value !== 'string' && value.import === undefined ? { ...value, import: key } : value;
239
240
  if (key.startsWith("*/")) {
240
- suffixExternals.push([key.substring(1), typeof value === 'string' ? { import: value, handle: key.substring(2) } : value]);
241
+ suffixExternals.push([key.substring(1), typeof normalizedValue === 'string' ? { import: normalizedValue, handle: key.substring(2) } : normalizedValue]);
241
242
  }
242
243
  else {
243
- exactExternals[key] = value;
244
+ exactExternals[key] = normalizedValue;
244
245
  }
245
246
  }
246
247
  const resolvePossibleExternal = (request) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.31.2",
3
+ "version": "2.33.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",