@plaudit/webpack-extensions 2.37.0 → 2.38.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.
@@ -195,8 +195,8 @@ class BlockJSONManagingPlugin {
195
195
  compilation[name in compilation.assets ? 'updateAsset' : 'emitAsset'](name, new webpack_1.sources.RawSource(JSON.stringify(json, undefined, " ")));
196
196
  blockDirConfigData[node_path_1.default.dirname(name)] = json;
197
197
  }
198
- const sortedBlockDirConfigData = Object.fromEntries(Object.entries(blockDirConfigData)
199
- .sort(([a], [b]) => a.localeCompare(b)));
198
+ const sortedBlockDirConfigData = Object.fromEntries([["__metadata", { version: 1 }], ...Object.entries(blockDirConfigData)
199
+ .sort(([a], [b]) => a.localeCompare(b))]);
200
200
  compilation.emitAsset("blockdir.config", new webpack_1.sources.RawSource((0, php_serializer_1.default)(sortedBlockDirConfigData, { excludedKeys: ["$schema"] })));
201
201
  });
202
202
  });
@@ -0,0 +1,9 @@
1
+ import DependencyExtractionWebpackPlugin from "@wordpress/dependency-extraction-webpack-plugin";
2
+ export type IndividualExternalDepConfig = string | {
3
+ import?: string | [string, ...string[]];
4
+ handle: string;
5
+ };
6
+ export type Externals = {
7
+ [dep: string]: IndividualExternalDepConfig;
8
+ };
9
+ export declare function makeDependencyExtractionPlugin(externals: Externals | undefined, assumeGlobalizedPlauditLibraries: boolean): false | DependencyExtractionWebpackPlugin;
@@ -0,0 +1,95 @@
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
+ exports.makeDependencyExtractionPlugin = makeDependencyExtractionPlugin;
7
+ const dependency_extraction_webpack_plugin_1 = __importDefault(require("@wordpress/dependency-extraction-webpack-plugin"));
8
+ function makeDependencyExtractionPlugin(externals, assumeGlobalizedPlauditLibraries) {
9
+ if (!externals) {
10
+ if (assumeGlobalizedPlauditLibraries) {
11
+ return new dependency_extraction_webpack_plugin_1.default({
12
+ requestToExternal: plauditRequestToExternal,
13
+ requestToHandle: plauditRequestToHandle
14
+ });
15
+ }
16
+ return false;
17
+ }
18
+ const exactExternals = {};
19
+ const suffixExternals = [];
20
+ for (const [key, value] of Object.entries(externals)) {
21
+ const normalizedValue = typeof value !== 'string' && value.import === undefined ? { ...value, import: key } : value;
22
+ if (key.startsWith("*/")) {
23
+ suffixExternals.push([key.substring(1), typeof normalizedValue === 'string' ? { import: normalizedValue, handle: key.substring(2) } : normalizedValue]);
24
+ }
25
+ else {
26
+ exactExternals[key] = normalizedValue;
27
+ }
28
+ }
29
+ const resolvePossibleExternal = (request) => {
30
+ if (request in exactExternals) {
31
+ return exactExternals[request];
32
+ }
33
+ for (const [key, value] of suffixExternals) {
34
+ if (request.endsWith(key)) {
35
+ return value;
36
+ }
37
+ }
38
+ return undefined;
39
+ };
40
+ const baselineRequestToExternal = request => {
41
+ const possibleExternal = resolvePossibleExternal(request);
42
+ if (typeof possibleExternal === 'string') {
43
+ return possibleExternal;
44
+ }
45
+ else if (possibleExternal !== undefined) {
46
+ return possibleExternal['import'];
47
+ }
48
+ return undefined;
49
+ };
50
+ const baselineRequestToHandle = request => {
51
+ const possibleExternal = resolvePossibleExternal(request);
52
+ if (possibleExternal !== undefined && typeof possibleExternal !== 'string') {
53
+ return possibleExternal.handle;
54
+ }
55
+ return undefined;
56
+ };
57
+ return assumeGlobalizedPlauditLibraries
58
+ ? new dependency_extraction_webpack_plugin_1.default({
59
+ requestToExternal: curryRequestHandlers(baselineRequestToExternal, plauditRequestToExternal),
60
+ requestToHandle: curryRequestHandlers(baselineRequestToHandle, plauditRequestToHandle),
61
+ })
62
+ : new dependency_extraction_webpack_plugin_1.default({
63
+ requestToExternal: baselineRequestToExternal,
64
+ requestToHandle: baselineRequestToHandle,
65
+ });
66
+ }
67
+ const PLAUDIT_NAMESPACE = '@plaudit/';
68
+ const EXTERNALIZABLE_PLAUDIT_LIBRARIES = ['@plaudit/library-extensions'];
69
+ function plauditRequestToExternal(request) {
70
+ if (EXTERNALIZABLE_PLAUDIT_LIBRARIES.includes(request)) {
71
+ return ['plaudit', camelCaseDash(request.substring(PLAUDIT_NAMESPACE.length))];
72
+ }
73
+ else if (request.startsWith('@plaudit/gutenberg-api-extensions')) {
74
+ return ['plaudit', 'gutenbergApiExtensions'];
75
+ }
76
+ return undefined;
77
+ }
78
+ function plauditRequestToHandle(request) {
79
+ if (EXTERNALIZABLE_PLAUDIT_LIBRARIES.includes(request)) {
80
+ return 'plaudit-' + request.substring(PLAUDIT_NAMESPACE.length);
81
+ }
82
+ else if (request.startsWith('@plaudit/gutenberg-api-extensions')) {
83
+ return 'plaudit-gutenberg-api-extensions';
84
+ }
85
+ return undefined;
86
+ }
87
+ function curryRequestHandlers(primary, secondary) {
88
+ return (...args) => primary(...args) ?? secondary(...args);
89
+ }
90
+ /**
91
+ * Copied from @wordpress/dependency-extraction-webpack-plugin for consistency
92
+ */
93
+ function camelCaseDash(string) {
94
+ return string.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
95
+ }
@@ -1,3 +1,4 @@
1
+ import { type Externals } from "./wordpress-scripts-wrapper/dependency-extraction-webpack-plugin-config-builder";
1
2
  import type { Options as PostcssFunctionsOptions } from "postcss-functions";
2
3
  import type { Configuration } from "webpack";
3
4
  interface AdvancedOutputConfig {
@@ -5,14 +6,8 @@ interface AdvancedOutputConfig {
5
6
  withLegacyBlocksIn?: string | undefined;
6
7
  additionalDependencies?: string[];
7
8
  directoryLayout?: 'blocks' | 'extensions';
9
+ assumeGlobalizedPlauditLibraries?: boolean;
8
10
  }
9
- type IndividualExternalDepConfig = string | {
10
- import?: string | [string, ...string[]];
11
- handle: string;
12
- };
13
- type Externals = {
14
- [dep: string]: IndividualExternalDepConfig;
15
- };
16
11
  type PlauditWordpressWebpackConfig = {
17
12
  standaloneBlocks?: boolean;
18
13
  variables?: Record<string, any>;
@@ -23,6 +18,7 @@ type PlauditWordpressWebpackConfig = {
23
18
  functions?: (variables: (name: string) => unknown) => PostcssFunctionsOptions['functions'];
24
19
  };
25
20
  externals?: Externals;
21
+ assumeGlobalizedPlauditLibraries?: boolean;
26
22
  };
27
23
  declare const _default: (config: PlauditWordpressWebpackConfig, webpackConfig?: Configuration[] | Configuration) => Configuration[];
28
24
  export = _default;
@@ -6,6 +6,7 @@ const node_fs_1 = __importDefault(require("node:fs"));
6
6
  const node_path_1 = __importDefault(require("node:path"));
7
7
  const AdditionalDependencyInjectorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/AdditionalDependencyInjectorPlugin"));
8
8
  const BlockJSONManagingPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/BlockJSONManagingPlugin"));
9
+ const dependency_extraction_webpack_plugin_config_builder_1 = require("./wordpress-scripts-wrapper/dependency-extraction-webpack-plugin-config-builder");
9
10
  const ExtensionsConfigFileGeneratorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/ExtensionsConfigFileGeneratorPlugin"));
10
11
  const MiniCSSExtractPluginErrorCleaner_1 = __importDefault(require("./wordpress-scripts-wrapper/MiniCSSExtractPluginErrorCleaner"));
11
12
  const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
@@ -233,49 +234,6 @@ function parseEntrypointsJSON(dir) {
233
234
  });
234
235
  }
235
236
  }
236
- function makeDependencyExtractionPlugin(externals) {
237
- const exactExternals = {};
238
- const suffixExternals = [];
239
- for (const [key, value] of Object.entries(externals)) {
240
- const normalizedValue = typeof value !== 'string' && value.import === undefined ? { ...value, import: key } : value;
241
- if (key.startsWith("*/")) {
242
- suffixExternals.push([key.substring(1), typeof normalizedValue === 'string' ? { import: normalizedValue, handle: key.substring(2) } : normalizedValue]);
243
- }
244
- else {
245
- exactExternals[key] = normalizedValue;
246
- }
247
- }
248
- const resolvePossibleExternal = (request) => {
249
- if (request in exactExternals) {
250
- return exactExternals[request];
251
- }
252
- for (const [key, value] of suffixExternals) {
253
- if (request.endsWith(key)) {
254
- return value;
255
- }
256
- }
257
- return undefined;
258
- };
259
- return new dependency_extraction_webpack_plugin_1.default({
260
- requestToExternal(request) {
261
- const possibleExternal = resolvePossibleExternal(request);
262
- if (typeof possibleExternal === 'string') {
263
- return possibleExternal;
264
- }
265
- else if (possibleExternal !== undefined) {
266
- return possibleExternal['import'];
267
- }
268
- return undefined;
269
- },
270
- requestToHandle(request) {
271
- const possibleExternal = resolvePossibleExternal(request);
272
- if (possibleExternal !== undefined && typeof possibleExternal !== 'string') {
273
- return possibleExternal.handle;
274
- }
275
- return undefined;
276
- }
277
- });
278
- }
279
237
  function processIndividualWebpackConfig(config, webpackConfig, sources) {
280
238
  let scriptExtension; // This is only used in non-block contexts. It might not actually be necessary, but it is good to have
281
239
  let entrypointFields;
@@ -288,7 +246,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
288
246
  scriptExtension = scriptWithoutModuleExtension;
289
247
  entrypointFields = ["editorStyle", "viewStyle", "style", "editorScript", "viewScript", "script"];
290
248
  }
291
- const { standaloneBlocks, variablesFilePath, verbose, externals } = config;
249
+ const { standaloneBlocks, variablesFilePath, verbose, externals, assumeGlobalizedPlauditLibraries } = config;
292
250
  let currentVariables = config.currentVariables;
293
251
  const fixedRules = [
294
252
  replaceDefaultURLProcessing,
@@ -351,13 +309,14 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
351
309
  if (srcIsDirectory && (typeof dest !== 'string' && dest.directoryLayout === 'extensions')) {
352
310
  plugins.push(new ExtensionsConfigFileGeneratorPlugin_1.default(srcRoot));
353
311
  }
354
- if (externals) {
355
- const pluginIndex = plugins.findIndex(plugin => plugin instanceof dependency_extraction_webpack_plugin_1.default);
356
- if (pluginIndex === -1) {
357
- console.error("Cannot apply externals when they have been disabled via CLI flag.");
358
- }
359
- else {
360
- plugins[pluginIndex] = makeDependencyExtractionPlugin(externals);
312
+ const pluginIndex = plugins.findIndex(plugin => plugin instanceof dependency_extraction_webpack_plugin_1.default);
313
+ if (pluginIndex === -1) {
314
+ console.error("Cannot apply externals when they have been disabled via CLI flag. This will greatly increase bundle size and will likely cause the build to file");
315
+ }
316
+ else {
317
+ const builtDependencyExtractionWebpackPlugin = (0, dependency_extraction_webpack_plugin_config_builder_1.makeDependencyExtractionPlugin)(externals, (typeof dest !== 'string' ? dest.assumeGlobalizedPlauditLibraries : undefined) ?? assumeGlobalizedPlauditLibraries);
318
+ if (builtDependencyExtractionWebpackPlugin) {
319
+ plugins[pluginIndex] = builtDependencyExtractionWebpackPlugin;
361
320
  }
362
321
  }
363
322
  if (process.argv.includes('--browser-sync') || process.env['BROWSER_SYNC'] === 'true') {
@@ -510,13 +469,13 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
510
469
  }
511
470
  module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
512
471
  testForDuplicatedEntryPaths(config);
513
- const { standaloneBlocks = false, stats = 'errors-warnings', variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true', postcss = {}, externals } = config;
472
+ const { standaloneBlocks = false, stats = 'errors-warnings', variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true', postcss = {}, externals, assumeGlobalizedPlauditLibraries = true } = config;
514
473
  let variablesFilePath = undefined;
515
474
  const currentVariables = rawVariables ?? {};
516
475
  if (!rawVariables) {
517
476
  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];
518
477
  }
519
- const cfg = { currentVariables, postcss, standaloneBlocks, stats, variablesFilePath, verbose, externals };
478
+ const cfg = { currentVariables, postcss, standaloneBlocks, stats, variablesFilePath, verbose, externals, assumeGlobalizedPlauditLibraries };
520
479
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
521
480
  if (Array.isArray(webpackConfig)) {
522
481
  return webpackConfig.toSorted((a, b) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.37.0",
3
+ "version": "2.38.0",
4
4
  "license": "UNLICENSED",
5
5
  "scripts": {
6
6
  "prepublishOnly": "rm -rf build && mkdir build && tsc",