@plaudit/webpack-extensions 3.3.0 → 3.4.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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [3.4.0] - 2026-03-16
9
+ ### Added
10
+ - Support for a directory-based `extensions` layout
11
+ - This only applies to sites using extensions v2 and higher
12
+ - This comes from version `2.87.0`
13
+
8
14
  ## [3.3.0] - 2026-03-16
9
15
  ### Added
10
16
  - A few helper methods for tools that reference this package and care about the entrypoint resolution logic
@@ -60,6 +66,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
60
66
  - Legacy PostCSS features that have been integrated into modern CSS
61
67
  - `@extends` support
62
68
 
69
+ ## [2.87.0] - 2026-03-16
70
+ ### Added
71
+ - Support for a directory-based `extensions` layout
72
+ - This only applies to sites using extensions v2 and higher
73
+
74
+ ### Internals
75
+ - Backported the features from `3.2.x` and `3.3.0`
76
+
63
77
  ## [2.86.0] - 2026-03-16
64
78
  ### Added
65
79
  - Support for `Location-Encoding Filenames`
package/USER-GUIDE.md CHANGED
@@ -501,8 +501,10 @@ This is used to associate additional assets with blocks that are not in control
501
501
  [Specially named files](#file-naming) in a single subdirectory of `src`
502
502
  ```
503
503
  src/extensions/
504
- ├── core-paragraph-view-script.ts
505
- └── core-paragraph-editor-style.pcss
504
+ ├─ core/paragraph/
505
+ │ ├─ view.ts
506
+ │ └─ editor.pcss
507
+ └─ plaudit/block-library/accordion/style.pcss
506
508
  ```
507
509
 
508
510
  #### File Naming
@@ -513,6 +515,9 @@ Filenames **must** follow the pattern: `{kebab-case-block-name}-{type}.{ext}`
513
515
  - `script-module`, `view-script-module`
514
516
  - `setup` (this allows for PHP code to be loaded only when certain blocks are enabled and is intended for use in plugins, not themes)
515
517
  - `ext` **must** be for a [supported file type](#supported-file-types) and **should** correspond with the extension type (basically, don't enqueue a `.css` file as a script)
518
+ - For convenience, everything before the `{type}` can be split into directories on the hyphens
519
+ - When using this format, the filenames must be [Location-Encoding Filenames](#location-encoding-filenames) or `setup.php`
520
+ - This is only available in extensions v2 and up
516
521
 
517
522
  #### Notes
518
523
  - In order to have a directory be treated as `extensions`, the config **must** include `directoryLayout: 'extensions'`
@@ -1,15 +1,19 @@
1
1
  import { AbstractBiPhasicGroupAndEntryPlugin, EntryProvider } from "./AbstractBiPhasicGroupAndEntryPlugin";
2
- import { ParsedAssetJsonProvider, VerifiedAdvancedOutputConfig } from "../shared";
2
+ import { MinimumViableMetadata, ParsedAssetJsonProvider, VerifiedAdvancedOutputConfig } from "../shared";
3
3
  import type { VerifiedPlauditWordpressWebpackConfig } from "../utils/common-config-helpers";
4
4
  import { Compilation, type Compiler } from "webpack";
5
- export declare class ExtensionsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAndEntryPlugin {
5
+ export type ExtensionConfigFileGeneratorMetadata = MinimumViableMetadata & {
6
+ extensionId: string;
7
+ };
8
+ export declare class ExtensionsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAndEntryPlugin<ExtensionConfigFileGeneratorMetadata> {
6
9
  private readonly extensionsSrcPath;
7
10
  private readonly dest;
8
11
  private static readonly semaphore;
9
12
  private setupFiles;
10
- constructor(config: VerifiedPlauditWordpressWebpackConfig, extensionsSrcPath: string, dest: VerifiedAdvancedOutputConfig, context: string, entry: EntryProvider);
13
+ constructor(config: VerifiedPlauditWordpressWebpackConfig, extensionsSrcPath: string, dest: VerifiedAdvancedOutputConfig, context: string, entry: EntryProvider<ExtensionConfigFileGeneratorMetadata>);
11
14
  get extensionsDestPath(): string;
12
15
  apply(compiler: Compiler): void;
16
+ private walkSubtreeForMetaInfo;
13
17
  private generateVersionThreeConfigFile;
14
18
  protected attachUniquePhase(compilation: Compilation): void;
15
19
  protected processAssets(compilation: Compilation, parsedAssetJsonProvider: ParsedAssetJsonProvider): void;
@@ -1,11 +1,41 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
5
35
  Object.defineProperty(exports, "__esModule", { value: true });
6
36
  exports.ExtensionsConfigFileGeneratorPlugin = void 0;
7
- const promises_1 = __importDefault(require("node:fs/promises"));
8
- const node_path_1 = __importDefault(require("node:path"));
37
+ const promises_1 = require("node:fs/promises");
38
+ const node_path_1 = __importStar(require("node:path"));
9
39
  const php_writer_1 = require("@plaudit/php-writer");
10
40
  const expressions_1 = require("@plaudit/php-writer/expressions");
11
41
  const AbstractBiPhasicGroupAndEntryPlugin_1 = require("./AbstractBiPhasicGroupAndEntryPlugin");
@@ -34,24 +64,49 @@ class ExtensionsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAndEntryP
34
64
  compilation.contextDependencies.add(this.extensionsSrcPath);
35
65
  }
36
66
  const emissionPromises = [];
37
- for await (const { name: setupFilePath } of await promises_1.default.opendir(this.extensionsSrcPath, { encoding: 'utf-8' })) {
67
+ for await (const { name: setupFilePath } of await (0, promises_1.opendir)(this.extensionsSrcPath, { encoding: 'utf-8' })) {
38
68
  if (setupFilePath.endsWith("-setup.php")) {
39
69
  const setupFileSourcePath = node_path_1.default.join(this.extensionsSrcPath, setupFilePath);
40
70
  compilation.fileDependencies.add(setupFileSourcePath);
41
- emissionPromises.push(promises_1.default.readFile(setupFileSourcePath).then(contents => {
71
+ emissionPromises.push((0, promises_1.readFile)(setupFileSourcePath).then(contents => {
42
72
  compilation.emitAsset(node_path_1.default.join(this.dest.destination, setupFilePath), new webpack_1.sources.RawSource(contents), { size: Buffer.byteLength(contents) });
43
73
  const blockSlug = /^(.+?)-setup.php$/i.exec(setupFilePath)?.[1];
44
74
  return [blockSlug, setupFilePath];
45
75
  }));
46
76
  }
47
77
  }
78
+ await this.walkSubtreeForMetaInfo(compilation, this.extensionsSrcPath, this.dest, [], emissionPromises);
48
79
  this.setupFiles = (await Promise.all(emissionPromises))
49
80
  .filter((item) => item[0] !== undefined).sort((a, b) => a[0].localeCompare(b[0]));
50
81
  });
51
82
  }
83
+ async walkSubtreeForMetaInfo(compilation, srcRoot, dest, parents, emissionPromises) {
84
+ if (!compilation.contextDependencies.has(srcRoot)) {
85
+ compilation.contextDependencies.add(srcRoot);
86
+ }
87
+ for await (const dirent of await (0, promises_1.opendir)(srcRoot)) {
88
+ if (dirent.name.startsWith("~")) {
89
+ continue;
90
+ }
91
+ if (dirent.isFile()) {
92
+ if (dirent.name === "setup.php") {
93
+ const setupFilePath = (0, node_path_1.join)(...parents, dirent.name);
94
+ const setupFileSourcePath = node_path_1.default.join(this.extensionsSrcPath, setupFilePath);
95
+ compilation.fileDependencies.add(setupFileSourcePath);
96
+ emissionPromises.push((0, promises_1.readFile)(setupFileSourcePath).then(contents => {
97
+ compilation.emitAsset(node_path_1.default.join(this.dest.destination, setupFilePath), new webpack_1.sources.RawSource(contents), { size: Buffer.byteLength(contents) });
98
+ return [parents.join("-") /* blockSlug */, setupFilePath];
99
+ }));
100
+ }
101
+ }
102
+ else if (dirent.isDirectory()) {
103
+ await this.walkSubtreeForMetaInfo(compilation, (0, node_path_1.join)(srcRoot, dirent.name), dest, [...parents, dirent.name], emissionPromises);
104
+ }
105
+ }
106
+ }
52
107
  generateVersionThreeConfigFile(compilation, relevantAssetData, setupFilePaths) {
53
108
  const handlePrefix = `${this.config.targetHandlePrefix}_block-extensions`;
54
- const regex = /^(.+?)-((?:editor-|view-|)(?:style|script|script-module))\.(?:css|m?js)$/i;
109
+ const regex = /^(.+?)-((?:editor-|view-|)(?:style|script|script-module))$/i;
55
110
  const blockExtensionsConfig = {
56
111
  metadata: { version: this.config.extensionsVersion }, scriptHandles: {}, scriptModuleHandles: {}, styleHandles: {}, blocks: {}, setupFiles: {}
57
112
  };
@@ -62,7 +117,7 @@ class ExtensionsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAndEntryP
62
117
  return [assetPath, entry[1]];
63
118
  });
64
119
  for (const [assetPath, assetData] of normalizedAssetData) {
65
- const match = regex.exec(assetPath);
120
+ const match = regex.exec(assetData.extensionId);
66
121
  if (!match) {
67
122
  continue;
68
123
  }
@@ -127,7 +182,16 @@ class ExtensionsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAndEntryP
127
182
  });
128
183
  }
129
184
  processAssets(compilation, parsedAssetJsonProvider) {
130
- const relevantAssetData = this.extractRelevantAssetData(compilation, parsedAssetJsonProvider);
185
+ const relevantAssetData = Object.fromEntries(this.getRelevantEntrypoints(compilation)
186
+ .map(({ entrypoint, metadata, srcPath }) => {
187
+ const providedData = parsedAssetJsonProvider(entrypoint, metadata);
188
+ if (providedData === undefined) {
189
+ compilation.errors.push((0, shared_1.newWebpackErrorForFile)(`assets.json did not contain information for ${srcPath}`, srcPath));
190
+ return undefined;
191
+ }
192
+ return [providedData.assetName, { ...providedData.assetData, extensionId: metadata.extensionId }];
193
+ })
194
+ .filter(item => item !== undefined));
131
195
  const staticallyLoadedEntrypoints = Object.keys(relevantAssetData);
132
196
  const generateLoader = (writer) => {
133
197
  writer.require(this.extensionsDestPath + "extensions-loader.php", { dirRelative: true, once: true });
@@ -6,6 +6,7 @@ export type ParsedLocationEncodingFilename = {
6
6
  register?: boolean | number;
7
7
  };
8
8
  flags: NormalizedEnqueuingControlFlags;
9
+ niceName?: string;
9
10
  };
10
11
  export declare function parseLocationEncodingFilename(filename: string): ParsedLocationEncodingFilename | undefined;
11
12
  export declare function parseLocationEncodingFilenameForBlock(filename: string, errorBuilder?: (message: string, filename: string) => unknown): Omit<ParsedLocationEncodingFilename, 'locations'> & {
@@ -19,13 +19,14 @@ function parseLocationEncodingFilename(filename) {
19
19
  const nameSegments = name.split("."); //Split always returns at least one string
20
20
  const typeOverrideMatch = /^(?<enqueuePosition>.+)-(?<typeOverride>script|style|script-module)$|^(?<typeOverride>script|style|script-module)$/.exec(nameSegments[0]);
21
21
  if (typeOverrideMatch) {
22
- nameSegments[0] = typeOverrideMatch.groups?.["enqueuePosition"] ?? "both"; // We override the first segment with the version without
22
+ nameSegments[0] = typeOverrideMatch.groups?.["enqueuePosition"] ?? "both"; // We override the first segment with the version without the type override
23
23
  type = typeOverrideMatch.groups?.["typeOverride"];
24
24
  }
25
25
  const inlinePattern = /^(?<inline>inline)(?:[-=](?<position>before|after))?$|^strategy[-=](?<inline>inline)(?:-(?<position>before|after))?$/;
26
26
  const strategyPattern = /^strategy[-=](?<strategy>eager|lazy|defer|async)$|^(?<strategy>lazy|eager)$/;
27
27
  const fetchpriorityPattern = /^fetchpriority[-=](auto|low|high)$/;
28
28
  const bothViewAndEditorLocationPattern = /^(?:client-)?(?<both>both)(?:[-=](?<priority>-?\d+))?$/;
29
+ let niceName = undefined;
29
30
  for (const segment of nameSegments) {
30
31
  const { locationName, priority } = extractLocationFromPropertyEncodingFilenameSegment(segment);
31
32
  if (locationName) {
@@ -67,6 +68,9 @@ function parseLocationEncodingFilename(filename) {
67
68
  flags.in_footer = false;
68
69
  break;
69
70
  default:
71
+ if (niceName === undefined) {
72
+ niceName = segment;
73
+ }
70
74
  break;
71
75
  }
72
76
  }
@@ -74,7 +78,7 @@ function parseLocationEncodingFilename(filename) {
74
78
  if (Object.values(locations).length === 0) {
75
79
  return undefined;
76
80
  }
77
- return { type, locations, flags };
81
+ return { type, locations, flags, niceName };
78
82
  }
79
83
  function parseLocationEncodingFilenameForBlock(filename, errorBuilder = shared_1.newWebpackErrorForFile) {
80
84
  const parsedFilename = parseLocationEncodingFilename(filename);
@@ -27,6 +27,7 @@ const UnifiedLoaderGenerator_1 = require("./plugins/UnifiedLoaderGenerator");
27
27
  const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
28
28
  const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
29
29
  const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
30
+ const location_encoding_filename_parser_1 = require("./utils/location-encoding-filename-parser");
30
31
  function testForDuplicatedEntryPaths(sources) {
31
32
  const seenPaths = (0, common_config_helpers_1.groupEntrypointsByAssetFile)(Array.isArray(sources)
32
33
  ? sources.map(s => typeof s === 'string' ? s : s[1].destination)
@@ -506,12 +507,22 @@ function processIndividualWebpackConfig(config, webpackConfig, sources, canClean
506
507
  const entry = async () => {
507
508
  const rawEntrypoints = [];
508
509
  for await (const dirent of await (0, promises_1.opendir)(srcRoot)) {
509
- if (dirent.isFile() && !dirent.name.startsWith("~")) {
510
- if (commonConfig.scriptExtension.test(dirent.name) || shared_1.styleExtension.test(dirent.name)) {
510
+ if (dirent.name.startsWith("~")) {
511
+ continue;
512
+ }
513
+ if (dirent.isFile()) {
514
+ if ((commonConfig.scriptExtension.test(dirent.name) || shared_1.styleExtension.test(dirent.name))) {
511
515
  const absoluteSrc = (0, common_config_helpers_1.joinPossiblyAbsolutePaths)(srcRoot, dirent.name);
512
- rawEntrypoints.push([(0, node_path_1.join)(dest.destination, (0, node_path_1.basename)(absoluteSrc, (0, node_path_1.extname)(absoluteSrc))), { import: [absoluteSrc], plauditMetadata: { dest, absoluteSrc } }]);
516
+ const extensionId = (0, node_path_1.parse)(dirent.name).name;
517
+ rawEntrypoints.push([(0, node_path_1.join)(dest.destination, (0, node_path_1.basename)(absoluteSrc, (0, node_path_1.extname)(absoluteSrc))), {
518
+ import: [absoluteSrc],
519
+ plauditMetadata: { dest, absoluteSrc, extensionId }
520
+ }]);
513
521
  }
514
522
  }
523
+ else if (dirent.isDirectory()) {
524
+ rawEntrypoints.push(...await walkExtensionsTree((0, node_path_1.join)(srcRoot, dirent.name), dest, [dirent.name]));
525
+ }
515
526
  }
516
527
  return Object.fromEntries(rawEntrypoints);
517
528
  };
@@ -552,7 +563,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources, canClean
552
563
  if (canCopyFiles) {
553
564
  plugins.push(new copy_webpack_plugin_1.default({
554
565
  patterns: [{
555
- from: config.standaloneBlocks ? '**/*.(php|twig|svg)' : '**/*.(asset\.php|svg)',
566
+ from: config.standaloneBlocks ? '**/*.(php|twig|svg)' : '**/*.(asset\.php|svg)', //TODO: I'm pretty sure that the dots need proper escaping
556
567
  to: dest.destination,
557
568
  context: srcRoot /* canCopyFiles can only be true if srcRoot is a string, so this is safe */, noErrorOnMissing: true
558
569
  }]
@@ -563,6 +574,45 @@ function processIndividualWebpackConfig(config, webpackConfig, sources, canClean
563
574
  return (0, common_config_helpers_1.commonMakeWebpackConfig)(config, commonConfig, webpackConfig, externalize, plugins, canClean && batches.length < 2);
564
575
  }).filter(cfg => cfg !== undefined);
565
576
  }
577
+ async function walkExtensionsTree(srcRoot, dest, parents) {
578
+ const res = [];
579
+ for await (const dirent of await (0, promises_1.opendir)(srcRoot)) {
580
+ if (dirent.name.startsWith("~")) {
581
+ continue;
582
+ }
583
+ if (dirent.isFile()) {
584
+ const parsedFilename = (0, location_encoding_filename_parser_1.parseLocationEncodingFilename)(dirent.name);
585
+ if (parsedFilename === undefined) {
586
+ continue;
587
+ }
588
+ const absoluteSrc = (0, common_config_helpers_1.joinPossiblyAbsolutePaths)(srcRoot, dirent.name);
589
+ let destFile;
590
+ if (Object.keys(parsedFilename.locations).length === 1) {
591
+ if (parsedFilename.locations.clientView) {
592
+ destFile = `view-${parsedFilename.type}`;
593
+ }
594
+ else if (parsedFilename.locations.clientEditor) {
595
+ destFile = `editor-${parsedFilename.type}`;
596
+ }
597
+ else {
598
+ destFile = (0, node_path_1.basename)(absoluteSrc, (0, node_path_1.extname)(absoluteSrc));
599
+ }
600
+ }
601
+ else {
602
+ destFile = (0, node_path_1.basename)(absoluteSrc, (0, node_path_1.extname)(absoluteSrc));
603
+ }
604
+ const extensionId = parents.join("-") + "-" + destFile;
605
+ res.push([(0, node_path_1.join)(dest.destination, ...parents, destFile), {
606
+ import: [absoluteSrc],
607
+ plauditMetadata: { dest, absoluteSrc, extensionId }
608
+ }]);
609
+ }
610
+ else if (dirent.isDirectory()) {
611
+ res.push(...await walkExtensionsTree((0, node_path_1.join)(srcRoot, dirent.name), dest, [...parents, dirent.name]));
612
+ }
613
+ }
614
+ return res;
615
+ }
566
616
  function stripExtension(filepath) {
567
617
  return (0, node_path_1.join)((0, node_path_1.dirname)(filepath), (0, node_path_1.basename)(filepath, (0, node_path_1.extname)(filepath)));
568
618
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "files": [
6
6
  "/build",