@plaudit/webpack-extensions 2.64.0 → 2.65.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.
Files changed (34) hide show
  1. package/build/plugins/AbstractBiPhasicGroupAndEntryPlugin.d.ts +33 -0
  2. package/build/plugins/AbstractBiPhasicGroupAndEntryPlugin.js +86 -0
  3. package/build/plugins/AbstractBiPhasicGroupPlugin.d.ts +33 -0
  4. package/build/plugins/AbstractBiPhasicGroupPlugin.js +93 -0
  5. package/build/plugins/AdditionalDependencyInjectorPlugin.d.ts +8 -6
  6. package/build/plugins/AdditionalDependencyInjectorPlugin.js +37 -39
  7. package/build/plugins/EnhancedDynamicEntryPlugin.d.ts +4 -5
  8. package/build/plugins/EnhancedDynamicEntryPlugin.js +3 -3
  9. package/build/plugins/ExtensionsConfigFileGeneratorPlugin.d.ts +9 -8
  10. package/build/plugins/ExtensionsConfigFileGeneratorPlugin.js +103 -93
  11. package/build/plugins/ExtensionsConfigFileGeneratorPluginV1.d.ts +7 -8
  12. package/build/plugins/ExtensionsConfigFileGeneratorPluginV1.js +10 -17
  13. package/build/plugins/PlainEntrypointsConfigFileGeneratorPlugin.d.ts +6 -7
  14. package/build/plugins/PlainEntrypointsConfigFileGeneratorPlugin.js +15 -16
  15. package/build/plugins/PlainEntrypointsStyleBlockJSONPlugin.d.ts +6 -6
  16. package/build/plugins/PlainEntrypointsStyleBlockJSONPlugin.js +10 -9
  17. package/build/plugins/SpecialAssetHandlingPlugin.d.ts +5 -5
  18. package/build/plugins/SpecialAssetHandlingPlugin.js +44 -47
  19. package/build/plugins/UnifiedLoaderGenerator.d.ts +8 -8
  20. package/build/plugins/UnifiedLoaderGenerator.js +54 -69
  21. package/build/plugins/WPMLConfigBuilder.d.ts +2 -2
  22. package/build/shared.d.ts +10 -14
  23. package/build/shared.js +19 -23
  24. package/build/utils/common-config-helpers.d.ts +6 -6
  25. package/build/utils/common-config-helpers.js +11 -8
  26. package/build/utils/json-to-php-but-with-__-injection.js +1 -1
  27. package/build/utils/pseduo-semaphore.d.ts +2 -3
  28. package/build/utils/pseduo-semaphore.js +9 -5
  29. package/build/wordpress-scripts-wrapper.js +21 -15
  30. package/package.json +3 -3
  31. package/build/plugins/AbstractMultiPhaseLibraryAndEntryPlugin.d.ts +0 -21
  32. package/build/plugins/AbstractMultiPhaseLibraryAndEntryPlugin.js +0 -113
  33. package/build/plugins/AbstractMultiPhaseLibraryPlugin.d.ts +0 -17
  34. package/build/plugins/AbstractMultiPhaseLibraryPlugin.js +0 -44
@@ -360,7 +360,7 @@ function handleDisablingTSCheckerIfNecessary(srcRoot, scriptExtension, plugins)
360
360
  }
361
361
  }
362
362
  }
363
- function buildCommonPluginConfig(webpackConfig, config, bundleAnalyzer, assumeGlobalizedPlauditLibraries, externalize, additionalDependencies) {
363
+ function buildCommonPluginConfig(webpackConfig, config, bundleAnalyzer, assumeGlobalizedPlauditLibraries, externalize) {
364
364
  const { externals } = config;
365
365
  const processingModules = webpackConfig.output?.module ?? false;
366
366
  const plugins = webpackConfig.plugins?.filter(v => !!v)
@@ -388,7 +388,9 @@ function buildCommonPluginConfig(webpackConfig, config, bundleAnalyzer, assumeGl
388
388
  ignore: /block\.json/
389
389
  });
390
390
  plugins.push(removeEmptyScriptsPlugin, new MiniCSSExtractPluginErrorCleaner_1.MiniCSSExtractPluginErrorCleaner());
391
- plugins.push(new SpecialAssetHandlingPlugin_1.SpecialAssetHandlingPlugin(config));
391
+ if (config.useWebpackResourceFiltering) {
392
+ plugins.push(new SpecialAssetHandlingPlugin_1.SpecialAssetHandlingPlugin(config));
393
+ }
392
394
  plugins.push(new VariablesJSMonitorPlugin_1.VariablesJSMonitorPlugin(config));
393
395
  const dependencyExtractionPluginIndex = plugins.findIndex(plugin => plugin instanceof dependency_extraction_webpack_plugin_1.default);
394
396
  if (dependencyExtractionPluginIndex === -1) {
@@ -401,7 +403,7 @@ function buildCommonPluginConfig(webpackConfig, config, bundleAnalyzer, assumeGl
401
403
  return {
402
404
  plugins, removeEmptyScriptsPlugin,
403
405
  makeAdditionalDependencyInjectorPlugin(referencePlugin) {
404
- return new AdditionalDependencyInjectorPlugin_1.AdditionalDependencyInjectorPlugin(additionalDependencies ?? [], processingModules, builtDependencyExtractionWebpackPlugin.addExternalizedDep, referencePlugin);
406
+ return new AdditionalDependencyInjectorPlugin_1.AdditionalDependencyInjectorPlugin(config, processingModules, builtDependencyExtractionWebpackPlugin.addExternalizedDep, referencePlugin);
405
407
  }
406
408
  };
407
409
  }
@@ -444,9 +446,9 @@ function destToKey(dest) {
444
446
  const externalize = typeof dest.externalize === 'object' && dest.externalize !== undefined
445
447
  ? (Array.isArray(dest.externalize) ? dest.externalize.join("_") : (dest.externalize ? Object.entries(dest.externalize).flat().sort().join() : "undefined"))
446
448
  : dest.externalize?.toString() ?? "undefined";
447
- return [dest.additionalDependencies.join("_"), dest.assumeGlobalizedPlauditLibraries, dest.bundleAnalyzer, externalize, dest.withLegacyBlocksIn].join("~");
449
+ return [dest.assumeGlobalizedPlauditLibraries, dest.bundleAnalyzer, externalize].join("~");
448
450
  }
449
- function processIndividualWebpackConfig(config, webpackConfig, sources) {
451
+ function processIndividualWebpackConfig(config, webpackConfig, sources, canClean) {
450
452
  const commonConfig = commonConfigProcessingPrep(config, webpackConfig);
451
453
  const { processingModules, scriptExtension } = commonConfig;
452
454
  const allSrcRoots = sources.map(([src, dest]) => {
@@ -465,9 +467,10 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
465
467
  const key = destToKey(entry.dest);
466
468
  (groupedSrcRoots[key] ?? (groupedSrcRoots[key] = [])).push(entry);
467
469
  }
468
- return Object.values(groupedSrcRoots).filter(shared_1.hasAtLeastOneItem).map(currentBatch => {
469
- const { bundleAnalyzer, assumeGlobalizedPlauditLibraries, externalize, additionalDependencies } = currentBatch[0].dest;
470
- const { plugins, removeEmptyScriptsPlugin, makeAdditionalDependencyInjectorPlugin } = buildCommonPluginConfig(webpackConfig, config, bundleAnalyzer, assumeGlobalizedPlauditLibraries, externalize, additionalDependencies);
470
+ const batches = Object.values(groupedSrcRoots).filter(shared_1.hasAtLeastOneItem);
471
+ return batches.map(currentBatch => {
472
+ const { bundleAnalyzer, assumeGlobalizedPlauditLibraries, externalize } = currentBatch[0].dest;
473
+ const { plugins, removeEmptyScriptsPlugin, makeAdditionalDependencyInjectorPlugin } = buildCommonPluginConfig(webpackConfig, config, bundleAnalyzer, assumeGlobalizedPlauditLibraries, externalize);
471
474
  handleDisablingTSCheckerIfNecessary(currentBatch.flatMap(a => a.srcRoots), scriptExtension, plugins);
472
475
  for (const { dest, src, srcIsDirectory, srcRoot } of currentBatch) {
473
476
  let plugin;
@@ -492,7 +495,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
492
495
  if (dirent.isFile() && !dirent.name.startsWith("~")) {
493
496
  if (commonConfig.scriptExtension.test(dirent.name) || shared_1.styleExtension.test(dirent.name)) {
494
497
  const file = (0, common_config_helpers_1.joinPossiblyAbsolutePaths)(srcRoot, dirent.name);
495
- rawEntrypoints.push([node_path_1.default.join(dest.destination, node_path_1.default.basename(file, node_path_1.default.extname(file))), { import: [file], plauditMetadata: "entrypoint-v2" }]);
498
+ rawEntrypoints.push([node_path_1.default.join(dest.destination, node_path_1.default.basename(file, node_path_1.default.extname(file))), { import: [file], plauditMetadata: { dest } }]);
496
499
  }
497
500
  }
498
501
  }
@@ -514,15 +517,15 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
514
517
  if (config.plainEntrypointsVersion > 1) {
515
518
  const entry = srcIsDirectory // This being true implies that srcRoot is not an array
516
519
  ? (0, common_config_helpers_1.resolveEntryFromDirectory)(commonConfig, srcRoot, dest)
517
- : () => ({ [baseDest]: { import: Array.isArray(srcRoot) ? srcRoot : [srcRoot], plauditMetadata: "plain-entrypoint" } });
518
- plugin = new PlainEntrypointsConfigFileGeneratorPlugin_1.PlainEntrypointsConfigFileGeneratorPlugin(config, process.cwd(), config.outputDir, dest.locations, config.plainEntrypointsHandlePrefix, config.useUnifiedLoader, webpackConfig.context ?? process.cwd(), entry);
520
+ : () => ({ [baseDest]: { import: Array.isArray(srcRoot) ? srcRoot : [srcRoot], plauditMetadata: { purpose: "plain-entrypoint", dest } } });
521
+ plugin = new PlainEntrypointsConfigFileGeneratorPlugin_1.PlainEntrypointsConfigFileGeneratorPlugin(config, process.cwd(), config.outputDir, dest.locations, config.useUnifiedLoader, webpackConfig.context ?? process.cwd(), entry);
519
522
  }
520
523
  else {
521
524
  if (srcIsDirectory) {
522
525
  console.error("Using directory mode with legacy plain entrypoint handling is not supported");
523
526
  continue;
524
527
  }
525
- plugin = new EnhancedDynamicEntryPlugin_1.EnhancedDynamicEntryPlugin(config, webpackConfig.context ?? process.cwd(), () => ({ [baseDest]: { import: Array.isArray(srcRoot) ? srcRoot : [srcRoot], plauditMetadata: "plain-entrypoint" } }));
528
+ plugin = new EnhancedDynamicEntryPlugin_1.EnhancedDynamicEntryPlugin(config, webpackConfig.context ?? process.cwd(), () => ({ [baseDest]: { import: Array.isArray(srcRoot) ? srcRoot : [srcRoot], plauditMetadata: { purpose: "plain-entrypoint", dest } } }));
526
529
  }
527
530
  plugins.push(plugin);
528
531
  }
@@ -543,7 +546,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
543
546
  }
544
547
  }
545
548
  plugins.push(new UnifiedLoaderGenerator_1.UnifiedLoaderGenerator(config));
546
- return (0, common_config_helpers_1.commonMakeWebpackConfig)(config, commonConfig, webpackConfig, externalize, plugins);
549
+ return (0, common_config_helpers_1.commonMakeWebpackConfig)(config, commonConfig, webpackConfig, externalize, plugins, canClean && batches.length < 2);
547
550
  }).filter(cfg => cfg !== undefined);
548
551
  }
549
552
  function stripExtension(filepath) {
@@ -551,13 +554,16 @@ function stripExtension(filepath) {
551
554
  }
552
555
  module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
553
556
  const { cfg, sources } = buildVerifiedConfig(config);
557
+ if (Array.isArray(webpackConfig) && (0, shared_1.hasAtLeastOneItem)(webpackConfig)) {
558
+ webpackConfig = webpackConfig[0];
559
+ }
554
560
  if (Array.isArray(webpackConfig)) {
555
561
  return webpackConfig.toSorted((a, b) => {
556
562
  if (a.output?.module) {
557
563
  return b.output?.module ? 0 : -1;
558
564
  }
559
565
  return b.output?.module ? 1 : 0;
560
- }).flatMap(wpCfg => processIndividualWebpackConfig(cfg, wpCfg, sources));
566
+ }).flatMap(wpCfg => processIndividualWebpackConfig(cfg, wpCfg, sources, false));
561
567
  }
562
- return processIndividualWebpackConfig(cfg, webpackConfig, sources);
568
+ return processIndividualWebpackConfig(cfg, webpackConfig, sources, true);
563
569
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.64.0",
3
+ "version": "2.65.1",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "files": [
6
6
  "/build"
@@ -16,9 +16,9 @@
16
16
  }
17
17
  },
18
18
  "devDependencies": {
19
- "@plaudit/gutenberg-api-extensions": "^2.75.0",
19
+ "@plaudit/gutenberg-api-extensions": "^2.76.1",
20
20
  "@types/browser-sync-webpack-plugin": "^2.2.5",
21
- "@types/node": "^24.3.0",
21
+ "@types/node": "^24.3.1",
22
22
  "@types/postcss-functions": "^4.0.4",
23
23
  "@types/tapable": "^2.2.7",
24
24
  "@types/webpack": "^5.28.5",
@@ -1,21 +0,0 @@
1
- import { RawAssetData } from "../shared";
2
- import type { ExtensibleEntryObject, VerifiedPlauditWordpressWebpackConfig } from "../utils/common-config-helpers";
3
- import { PseudoSemaphore } from "../utils/pseduo-semaphore";
4
- import { AbstractMultiPhaseLibraryPlugin } from "./AbstractMultiPhaseLibraryPlugin";
5
- import { Compilation, Compiler, Entrypoint } from "webpack";
6
- export type EntryProvider<M> = () => ExtensibleEntryObject<M> | Promise<ExtensibleEntryObject<M>>;
7
- export declare abstract class AbstractMultiPhaseLibraryAndEntryPlugin<M> extends AbstractMultiPhaseLibraryPlugin {
8
- private readonly context;
9
- private readonly entry;
10
- private static readonly configuredCompilations;
11
- private readonly entryMetadataRecord;
12
- protected constructor(config: VerifiedPlauditWordpressWebpackConfig, libraryType: string, semaphores: PseudoSemaphore<any>[], context: string, entry: EntryProvider<M>);
13
- apply(compiler: Compiler): void;
14
- getRelevantEntrypoints(compilation: Compilation): {
15
- entrypoint: Entrypoint;
16
- srcPath: string;
17
- metadata: M;
18
- }[];
19
- protected abstract processAssets(compilation: Compilation, rawAssetData: RawAssetData): Promise<void> | void;
20
- protected extractRelevantAssetData(compilation: Compilation, rawAssetData: RawAssetData): RawAssetData;
21
- }
@@ -1,113 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractMultiPhaseLibraryAndEntryPlugin = void 0;
4
- const shared_1 = require("../shared");
5
- const AbstractMultiPhaseLibraryPlugin_1 = require("./AbstractMultiPhaseLibraryPlugin");
6
- const webpack_1 = require("webpack");
7
- class AbstractMultiPhaseLibraryAndEntryPlugin extends AbstractMultiPhaseLibraryPlugin_1.AbstractMultiPhaseLibraryPlugin {
8
- context;
9
- entry;
10
- static configuredCompilations = new WeakSet();
11
- entryMetadataRecord = {};
12
- constructor(config, libraryType, semaphores, context, entry) {
13
- super(config, libraryType, semaphores);
14
- this.context = context;
15
- this.entry = entry;
16
- }
17
- apply(compiler) {
18
- super.apply(compiler);
19
- compiler.hooks.compilation.tap(this.constructor.name, (compilation, { normalModuleFactory }) => {
20
- if (AbstractMultiPhaseLibraryAndEntryPlugin.configuredCompilations.has(compilation)) {
21
- return;
22
- }
23
- AbstractMultiPhaseLibraryAndEntryPlugin.configuredCompilations.add(compilation);
24
- const dummyEntry = webpack_1.EntryPlugin.createDependency("", "");
25
- compilation.dependencyFactories.set(dummyEntry.constructor, normalModuleFactory);
26
- });
27
- compiler.hooks.make.tapPromise(this.constructor.name, async (compilation) => {
28
- const entry = await this.entry();
29
- const promises = [];
30
- for (const [name, { plauditMetadata, ...desc }] of Object.entries(entry)) {
31
- const options = webpack_1.EntryOptionPlugin.entryDescriptionToOptions(compiler, name, desc);
32
- this.entryMetadataRecord[name] = plauditMetadata;
33
- for (const entry of desc.import) {
34
- promises.push(new Promise((resolve, reject) => {
35
- compilation.addEntry(this.context, webpack_1.EntryPlugin.createDependency(entry, options), options, err => {
36
- if (err) {
37
- return reject(err);
38
- }
39
- resolve();
40
- });
41
- }));
42
- }
43
- }
44
- await Promise.all(promises);
45
- });
46
- compiler.hooks.compilation.tap(this.constructor.name, compilation => {
47
- compilation.hooks.processAssets.tapPromise({ name: this.constructor.name, stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ANALYSE, additionalAssets: true }, async (assets) => {
48
- if (!("assets.json" in assets)) {
49
- return;
50
- }
51
- try {
52
- //TODO: It should be possible to use EntryPoints to determine the "original" file
53
- //TODO: There is no reason to not use basically the exact same logic to implement support for this in plain file contexts
54
- const rawAssetDataSource = assets["assets.json"]?.source();
55
- if (typeof rawAssetDataSource !== 'string') {
56
- this.rejectSemaphores();
57
- compilation.errors.push(new Error("assets.json is unexpectedly missing or not a string"));
58
- return;
59
- }
60
- const rawAssetData = JSON.parse(rawAssetDataSource);
61
- if (!(0, shared_1.isRawAssetData)(rawAssetData)) {
62
- this.rejectSemaphores();
63
- compilation.errors.push(new Error("assets.json is does not match the RawAssetData format"));
64
- return;
65
- }
66
- await this.processAssets(compilation, rawAssetData);
67
- }
68
- catch (e) {
69
- this.rejectSemaphores();
70
- if (e instanceof Error) {
71
- compilation.errors.push(e);
72
- return;
73
- }
74
- throw e;
75
- }
76
- });
77
- });
78
- }
79
- getRelevantEntrypoints(compilation) {
80
- return compilation.entrypoints.values()
81
- .map(entrypoint => {
82
- const name = entrypoint.name;
83
- if (!name) {
84
- return undefined;
85
- }
86
- const metadata = this.entryMetadataRecord[name];
87
- if (metadata === undefined) {
88
- return undefined;
89
- }
90
- const srcPath = entrypoint.origins[0]?.request;
91
- if (!srcPath) {
92
- return undefined;
93
- }
94
- return { entrypoint, srcPath, metadata };
95
- })
96
- .filter(item => item !== undefined)
97
- .toArray();
98
- }
99
- extractRelevantAssetData(compilation, rawAssetData) {
100
- const relevantAssetData = {};
101
- const relevantEntrypoints = this.getRelevantEntrypoints(compilation);
102
- for (const { entrypoint, srcPath } of relevantEntrypoints) {
103
- const fileWithAssetData = entrypoint.getEntrypointChunk().files.values().find(file => rawAssetData[file]);
104
- if (!fileWithAssetData) {
105
- compilation.errors.push((0, shared_1.newWebpackErrorForFile)(`assets.json did not contain information for ${srcPath}`, srcPath));
106
- continue;
107
- }
108
- relevantAssetData[fileWithAssetData] = rawAssetData[fileWithAssetData];
109
- }
110
- return relevantAssetData;
111
- }
112
- }
113
- exports.AbstractMultiPhaseLibraryAndEntryPlugin = AbstractMultiPhaseLibraryAndEntryPlugin;
@@ -1,17 +0,0 @@
1
- import { Compilation, Compiler, type WebpackPluginInstance } from "webpack";
2
- import { PseudoSemaphore } from "../utils/pseduo-semaphore";
3
- import type { VerifiedPlauditWordpressWebpackConfig } from "../utils/common-config-helpers";
4
- export declare abstract class AbstractMultiPhaseLibraryPlugin implements WebpackPluginInstance {
5
- protected readonly config: VerifiedPlauditWordpressWebpackConfig;
6
- readonly libraryType: string;
7
- private readonly semaphores;
8
- protected static readonly phaseTwoAttached: {
9
- [key in string]: any;
10
- };
11
- protected readonly id: string;
12
- protected constructor(config: VerifiedPlauditWordpressWebpackConfig, libraryType: string, semaphores: PseudoSemaphore<any>[]);
13
- apply(compiler: Compiler): void;
14
- protected resetSemaphores(): void;
15
- protected rejectSemaphores(): void;
16
- protected abstract attachSecondPhase(compilation: Compilation): void;
17
- }
@@ -1,44 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractMultiPhaseLibraryPlugin = void 0;
4
- const webpack_1 = require("webpack");
5
- class AbstractMultiPhaseLibraryPlugin {
6
- config;
7
- libraryType;
8
- semaphores;
9
- static phaseTwoAttached = {};
10
- id;
11
- constructor(config, libraryType, semaphores) {
12
- this.config = config;
13
- this.libraryType = libraryType;
14
- this.semaphores = semaphores;
15
- this.id = Math.random().toString();
16
- }
17
- apply(compiler) {
18
- webpack_1.library.EnableLibraryPlugin.setEnabled(compiler, this.libraryType);
19
- for (const semaphore of this.semaphores) {
20
- semaphore.register(this.id);
21
- }
22
- compiler.hooks.compilation.tap(this.constructor.name, compilation => {
23
- AbstractMultiPhaseLibraryPlugin.phaseTwoAttached[this.libraryType] = false;
24
- this.resetSemaphores();
25
- compilation.hooks.beforeChunkIds.tap(this.constructor.name, () => {
26
- if (!AbstractMultiPhaseLibraryPlugin.phaseTwoAttached[this.libraryType]) {
27
- AbstractMultiPhaseLibraryPlugin.phaseTwoAttached[this.libraryType] = this;
28
- this.attachSecondPhase(compilation);
29
- }
30
- });
31
- });
32
- }
33
- resetSemaphores() {
34
- for (const semaphore of this.semaphores) {
35
- semaphore.reset(this.id);
36
- }
37
- }
38
- rejectSemaphores() {
39
- for (const semaphore of this.semaphores) {
40
- semaphore.reject(this.id);
41
- }
42
- }
43
- }
44
- exports.AbstractMultiPhaseLibraryPlugin = AbstractMultiPhaseLibraryPlugin;