@plaudit/webpack-extensions 2.47.0 → 2.49.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.
@@ -0,0 +1,11 @@
1
+ export type SharedCache = {
2
+ specialAssets?: {
3
+ files: {
4
+ [k: string]: {
5
+ preload?: true;
6
+ fetchpriority?: string | null;
7
+ crossorigin?: string | null;
8
+ };
9
+ };
10
+ };
11
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { Compiler, WebpackPluginInstance } from "webpack";
2
+ import { SharedCache } from "../shared";
3
+ export default class SpecialAssetHandlingPlugin implements WebpackPluginInstance {
4
+ private readonly cache;
5
+ constructor(cache: SharedCache);
6
+ apply(compiler: Compiler): void;
7
+ }
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const node_path_1 = __importDefault(require("node:path"));
7
+ const webpack_1 = require("webpack");
8
+ class SpecialAssetHandlingPlugin {
9
+ cache;
10
+ constructor(cache) {
11
+ this.cache = cache;
12
+ }
13
+ apply(compiler) {
14
+ compiler.hooks.compilation.tap(this.constructor.name, compilation => {
15
+ this.cache.specialAssets = undefined;
16
+ compilation.hooks.processAssets.tap(this.constructor.name, assets => {
17
+ for (const pathname of Object.keys(assets)) {
18
+ const assetInfo = compilation.assetsInfo.get(pathname);
19
+ if (!assetInfo?.sourceFilename) {
20
+ continue;
21
+ }
22
+ const queryStart = assetInfo.sourceFilename.indexOf('?');
23
+ if (queryStart < 0) {
24
+ continue;
25
+ }
26
+ const params = new URLSearchParams(assetInfo.sourceFilename.substring(queryStart + 1)
27
+ .replace(/(?:fetchPriority|crossOrigin)=/gi, str => str.toLowerCase()) //This ensures consistent capitalization of the fetchPriority parameter
28
+ );
29
+ if (this.cache.specialAssets === undefined) {
30
+ this.cache.specialAssets = { files: {} };
31
+ const outputFile = node_path_1.default.join(node_path_1.default.dirname(node_path_1.default.dirname(pathname)), "special-assets.php");
32
+ compilation.hooks.afterProcessAssets.tap(`${this.constructor.name}_GeneratePreloadingFile`, () => {
33
+ if (!this.cache.specialAssets?.files) {
34
+ return;
35
+ }
36
+ const hookTextLines = [
37
+ "<?php",
38
+ "add_action('wp_head', function() {",
39
+ "\tif (str_starts_with(__DIR__, ABSPATH)) {",
40
+ "\t\t$path = ltrim(substr(__DIR__, strlen(ABSPATH)), '/');",
41
+ "\t} else if (str_starts_with(__DIR__, '/workspace/website')) {",
42
+ "\t\t$path = ltrim(substr(__DIR__, 18), '/');",
43
+ "\t} else {",
44
+ "\t\terror_log('UNABLE TO FIGURE OUT WHAT THE RELATIVE PATH TO THE FONT DIRECTORY SHOULD BE');",
45
+ "\t\treturn;",
46
+ "\t}",
47
+ "\t$base_uri = trailingslashit(home_url($path));",
48
+ "\t?>"
49
+ ];
50
+ const preloadedFonts = Object.entries(this.cache.specialAssets.files)
51
+ .filter(([_, { preload }]) => preload)
52
+ .sort(([a], [b]) => a.localeCompare(b));
53
+ for (let [filename, { preload, fetchpriority, crossorigin }] of preloadedFonts) {
54
+ if (!preload) {
55
+ continue;
56
+ }
57
+ let fileExtension = node_path_1.default.extname(filename);
58
+ if (fileExtension.startsWith(".")) {
59
+ fileExtension = fileExtension.substring(1).toLowerCase();
60
+ }
61
+ else {
62
+ fileExtension = fileExtension.toLowerCase();
63
+ }
64
+ const attributes = [];
65
+ if (["woff", "woff2", "ttf", "otf", "eot"].includes(fileExtension)) {
66
+ if (!crossorigin) {
67
+ crossorigin = "anonymous";
68
+ }
69
+ attributes.push(["as", "font"], ["type", `font/${fileExtension}`]);
70
+ }
71
+ else if (fileExtension === "svg") {
72
+ attributes.push(["as", "image"], ["type", `image/svg+xml`]);
73
+ }
74
+ else if (fileExtension === "jpeg" || fileExtension === "jpg") {
75
+ attributes.push(["as", "image"], ["type", `image/jpeg`]);
76
+ }
77
+ else if (["png", "webp", "avif", "gif", "bmp"].includes(fileExtension)) {
78
+ attributes.push(["as", "image"], ["type", `image/${fileExtension}`]);
79
+ }
80
+ else {
81
+ compilation.errors.push(new Error(`Encountered preloading markup on an unsupported file extension (${fileExtension}). Full resource path: ${assetInfo.sourceFilename}`));
82
+ continue;
83
+ }
84
+ if (fetchpriority && fetchpriority !== 'auto') {
85
+ attributes.push(["fetchpriority", fetchpriority]);
86
+ }
87
+ if (crossorigin || crossorigin === "") {
88
+ attributes.push(["crossorigin", crossorigin]);
89
+ }
90
+ const dynamicAttrs = attributes.map(([k, v]) => v ? `${k}="${v}"` : k).join(" ");
91
+ hookTextLines.push(`\t<link rel="preload" href="<?= esc_url($base_uri.'${filename}') ?>" ${dynamicAttrs}>`);
92
+ }
93
+ hookTextLines.push("\t<?php", "});\n");
94
+ compilation.emitAsset(outputFile, new webpack_1.sources.RawSource(hookTextLines.join("\n")));
95
+ });
96
+ }
97
+ const filename = node_path_1.default.join(node_path_1.default.basename(node_path_1.default.dirname(pathname)), node_path_1.default.basename(pathname));
98
+ if (params.has("preload")) {
99
+ this.cache.specialAssets.files[filename] = { preload: true, fetchpriority: params.get("fetchpriority"), crossorigin: params.get("crossorigin") };
100
+ }
101
+ }
102
+ });
103
+ });
104
+ }
105
+ }
106
+ exports.default = SpecialAssetHandlingPlugin;
@@ -89,7 +89,7 @@ function firstLayersEqual(first, second) {
89
89
  return true;
90
90
  }
91
91
  const PLAUDIT_NAMESPACE = '@plaudit/';
92
- const EXTERNALIZABLE_PLAUDIT_LIBRARIES = ['@plaudit/frontend-apis', '@plaudit/library-extensions'];
92
+ const EXTERNALIZABLE_PLAUDIT_LIBRARIES = ['@plaudit/frontend-apis', '@plaudit/library-extensions', '@plaudit/block-supports'];
93
93
  function plauditRequestToExternal(ext) {
94
94
  return function (request) {
95
95
  if (EXTERNALIZABLE_PLAUDIT_LIBRARIES.includes(request)) {
@@ -22,6 +22,7 @@ type PlauditWordpressWebpackConfig = {
22
22
  assumeGlobalizedPlauditLibraries?: boolean;
23
23
  processTranslationConfigs?: boolean;
24
24
  combineAssetMetadata?: boolean;
25
+ useWebpackResourceFiltering?: boolean;
25
26
  };
26
27
  declare const _default: (config: PlauditWordpressWebpackConfig, webpackConfig?: Configuration[] | Configuration) => Configuration[];
27
28
  export = _default;
@@ -9,6 +9,7 @@ const AdditionalDependencyInjectorPlugin_1 = __importDefault(require("./wordpres
9
9
  const BlockJSONManagingPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/BlockJSONManagingPlugin"));
10
10
  const dependency_extraction_webpack_plugin_config_builder_1 = require("./wordpress-scripts-wrapper/dependency-extraction-webpack-plugin-config-builder");
11
11
  const ExtensionsConfigFileGeneratorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/ExtensionsConfigFileGeneratorPlugin"));
12
+ const SpecialAssetHandlingPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/SpecialAssetHandlingPlugin"));
12
13
  const MiniCSSExtractPluginErrorCleaner_1 = __importDefault(require("./wordpress-scripts-wrapper/MiniCSSExtractPluginErrorCleaner"));
13
14
  const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
14
15
  const BrowserSyncPlugin_1 = require("./wordpress-scripts-wrapper/BrowserSyncPlugin");
@@ -130,35 +131,38 @@ function testForDuplicatedEntryPaths(config) {
130
131
  process.exit(1);
131
132
  }
132
133
  }
133
- function replaceDefaultURLProcessing(rules) {
134
- const cssLoader = require.resolve('css-loader');
135
- if (cssLoader) {
136
- return rules.map(rule => {
137
- if (rule && typeof rule === 'object' && "use" in rule && Array.isArray(rule.use)) {
138
- return {
139
- ...rule,
140
- use: rule.use.map(useElement => {
141
- if (useElement && typeof useElement === 'object' && useElement.loader === cssLoader && typeof useElement.options === 'object') {
142
- return {
143
- ...useElement,
144
- options: {
145
- ...useElement.options,
146
- url: {
147
- filter(url) {
148
- return !url.startsWith("/") && !!url.match(/\.(woff|woff2|eot|ttf|otf|png|jpe?g|svg|webp|avif|gif)[^.]*?(\?.*)?/i);
134
+ function replaceDefaultURLProcessing(useWebpackResourceFiltering) {
135
+ return (rules) => {
136
+ const cssLoader = require.resolve('css-loader');
137
+ if (cssLoader) {
138
+ return rules.map(rule => {
139
+ if (rule && typeof rule === 'object' && "use" in rule && Array.isArray(rule.use)) {
140
+ return {
141
+ ...rule,
142
+ use: rule.use.map(useElement => {
143
+ if (useElement && typeof useElement === 'object' && useElement.loader === cssLoader && typeof useElement.options === 'object') {
144
+ return {
145
+ ...useElement,
146
+ options: {
147
+ ...useElement.options,
148
+ url: {
149
+ filter(url) {
150
+ return (useWebpackResourceFiltering || !url.startsWith("/"))
151
+ && !!url.match(/\.(woff2?|eot|ttf|otf|png|jpe?g|svg|webp|avif|gif|bmp)[^.]*?(\?.*)?/i);
152
+ }
149
153
  }
150
154
  }
151
- }
152
- };
153
- }
154
- return useElement;
155
- })
156
- };
157
- }
158
- return rule;
159
- });
160
- }
161
- return rules;
155
+ };
156
+ }
157
+ return useElement;
158
+ })
159
+ };
160
+ }
161
+ return rule;
162
+ });
163
+ }
164
+ return rules;
165
+ };
162
166
  }
163
167
  function injectPostcssConfigOverrides(rules, variables, postcssFunctionsConfig, verbose) {
164
168
  const postcssConfig = (0, static_configs_1.postcssConfigBuilder)(verbose, name => variables(name) ?? (name === 'ENV' ? '' : undefined), postcssFunctionsConfig);
@@ -236,7 +240,7 @@ function parseEntrypointsJSON(dir) {
236
240
  });
237
241
  }
238
242
  }
239
- function processIndividualWebpackConfig(config, webpackConfig, sources) {
243
+ function processIndividualWebpackConfig(config, webpackConfig, sources, sharedCache) {
240
244
  let scriptExtension; // This is only used in non-block contexts. It might not actually be necessary, but it is good to have
241
245
  let entrypointFields;
242
246
  const processingModules = webpackConfig.output?.module ?? false;
@@ -251,7 +255,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
251
255
  const { standaloneBlocks, variablesFilePath, verbose, externals, assumeGlobalizedPlauditLibraries, combineAssetMetadata } = config;
252
256
  let currentVariables = config.currentVariables;
253
257
  const fixedRules = [
254
- replaceDefaultURLProcessing,
258
+ replaceDefaultURLProcessing(config.useWebpackResourceFiltering),
255
259
  (rules) => {
256
260
  return injectPostcssConfigOverrides(rules, name => currentVariables[name], config.postcss.functions ?? (() => ({})), verbose);
257
261
  },
@@ -323,6 +327,9 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
323
327
  stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS,
324
328
  extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss']
325
329
  }), new MiniCSSExtractPluginErrorCleaner_1.default());
330
+ if (config.useWebpackResourceFiltering) {
331
+ plugins.push(new SpecialAssetHandlingPlugin_1.default(sharedCache));
332
+ }
326
333
  if (variablesFilePath) {
327
334
  plugins.push(new VariablesJSMonitorPlugin_1.default(variablesFilePath));
328
335
  }
@@ -577,21 +584,25 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
577
584
  }
578
585
  module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
579
586
  testForDuplicatedEntryPaths(config);
580
- const { standaloneBlocks = false, stats = 'errors-warnings', variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true', postcss = {}, externals, assumeGlobalizedPlauditLibraries = true, processTranslationConfigs = true, combineAssetMetadata = true } = config;
587
+ const { standaloneBlocks = false, stats = 'errors-warnings', variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true', postcss = {}, externals, assumeGlobalizedPlauditLibraries = true, processTranslationConfigs = true, combineAssetMetadata = true, useWebpackResourceFiltering = true } = config;
581
588
  let variablesFilePath = undefined;
582
589
  const currentVariables = rawVariables ?? {};
583
590
  if (!rawVariables) {
584
591
  variablesFilePath = ["variables.js", "src/site/variables.js"].map(p => node_path_1.default.join(process.cwd(), p)).filter(p => node_fs_1.default.existsSync(p))[0];
585
592
  }
586
- const cfg = { currentVariables, postcss, standaloneBlocks, stats, variablesFilePath, verbose, externals, assumeGlobalizedPlauditLibraries, processTranslationConfigs, combineAssetMetadata };
593
+ const cfg = {
594
+ currentVariables, postcss, standaloneBlocks, stats, variablesFilePath, verbose, externals, assumeGlobalizedPlauditLibraries, processTranslationConfigs, combineAssetMetadata,
595
+ useWebpackResourceFiltering
596
+ };
587
597
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
598
+ const sharedCache = {};
588
599
  if (Array.isArray(webpackConfig)) {
589
600
  return webpackConfig.toSorted((a, b) => {
590
601
  if (a.output?.module) {
591
602
  return b.output?.module ? 0 : -1;
592
603
  }
593
604
  return b.output?.module ? 1 : 0;
594
- }).flatMap(wpCfg => processIndividualWebpackConfig(cfg, wpCfg, sources));
605
+ }).flatMap(wpCfg => processIndividualWebpackConfig(cfg, wpCfg, sources, sharedCache));
595
606
  }
596
- return processIndividualWebpackConfig(cfg, webpackConfig, sources);
607
+ return processIndividualWebpackConfig(cfg, webpackConfig, sources, sharedCache);
597
608
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.47.0",
3
+ "version": "2.49.0",
4
4
  "license": "UNLICENSED",
5
5
  "files": [
6
6
  "/build"
@@ -16,9 +16,9 @@
16
16
  }
17
17
  },
18
18
  "devDependencies": {
19
- "@plaudit/gutenberg-api-extensions": "^2.68.1",
19
+ "@plaudit/gutenberg-api-extensions": "^2.69.0",
20
20
  "@types/browser-sync-webpack-plugin": "^2.2.5",
21
- "@types/node": "^22.15.18",
21
+ "@types/node": "^22.15.19",
22
22
  "@types/postcss-functions": "^4.0.4",
23
23
  "@types/tapable": "^2.2.7",
24
24
  "@types/webpack": "^5.28.5",
@@ -71,6 +71,8 @@
71
71
  },
72
72
  "scripts": {
73
73
  "build": "tsc",
74
+ "clean": "rm -rf build && mkdir build",
75
+ "build:clean": "pnpm run clean && pnpm run build",
74
76
  "watch": "tsc -w"
75
77
  }
76
78
  }