@plaudit/webpack-extensions 2.36.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.
@@ -73,6 +73,7 @@ class BlockJSONManagingPlugin {
73
73
  }
74
74
  }
75
75
  }
76
+ //TODO: Fix modules support when running in non-dev mode
76
77
  const blockDirConfigData = {};
77
78
  for (const [name, sourceDir] of currentBlockJSONAssets) {
78
79
  const assetContents = this.processingModules ? node_fs_1.default.readFileSync(testableCompilationAssets[name], 'utf8') : compilation.assets[name]?.buffer().toString();
@@ -194,8 +195,8 @@ class BlockJSONManagingPlugin {
194
195
  compilation[name in compilation.assets ? 'updateAsset' : 'emitAsset'](name, new webpack_1.sources.RawSource(JSON.stringify(json, undefined, " ")));
195
196
  blockDirConfigData[node_path_1.default.dirname(name)] = json;
196
197
  }
197
- const sortedBlockDirConfigData = Object.fromEntries(Object.entries(blockDirConfigData)
198
- .sort(([a], [b]) => a.localeCompare(b)));
198
+ const sortedBlockDirConfigData = Object.fromEntries([["__metadata", { version: 1 }], ...Object.entries(blockDirConfigData)
199
+ .sort(([a], [b]) => a.localeCompare(b))]);
199
200
  compilation.emitAsset("blockdir.config", new webpack_1.sources.RawSource((0, php_serializer_1.default)(sortedBlockDirConfigData, { excludedKeys: ["$schema"] })));
200
201
  });
201
202
  });
@@ -0,0 +1,4 @@
1
+ import type { Compiler, WebpackPluginInstance } from "webpack";
2
+ export default class MiniCSSExtractPluginErrorCleaner implements WebpackPluginInstance {
3
+ apply(compiler: Compiler): void;
4
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class MiniCSSExtractPluginErrorCleaner {
4
+ apply(compiler) {
5
+ compiler.hooks.done.tap("CleanupMiniCssExtractPlugin", stats => {
6
+ if (stats.hasErrors()) {
7
+ if (!stats.compilation.errors.every(error => error.message.includes("mini-css-extract-plugin")) && stats.compilation.errors.length % 2 === 0) {
8
+ stats.compilation.errors = stats.compilation.errors.filter(error => !error.message.includes("mini-css-extract-plugin"));
9
+ }
10
+ }
11
+ });
12
+ }
13
+ }
14
+ exports.default = MiniCSSExtractPluginErrorCleaner;
@@ -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,7 +6,9 @@ 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"));
11
+ const MiniCSSExtractPluginErrorCleaner_1 = __importDefault(require("./wordpress-scripts-wrapper/MiniCSSExtractPluginErrorCleaner"));
10
12
  const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
11
13
  const BrowserSyncPlugin_1 = require("./wordpress-scripts-wrapper/BrowserSyncPlugin");
12
14
  const static_configs_1 = require("./wordpress-scripts-wrapper/static-configs");
@@ -17,9 +19,6 @@ const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-e
17
19
  function joinPossiblyAbsolutePaths(...paths) {
18
20
  return paths.reduce((res, p) => !res || node_path_1.default.isAbsolute(p) ? p : node_path_1.default.join(res, p), '') || '.';
19
21
  }
20
- function test(content, map, meta) {
21
- this.resourcePath;
22
- }
23
22
  function mapToRealEntrypoints(entrypoint, dir, mapper = (entrypoint) => entrypoint, lazyDependent) {
24
23
  return (Array.isArray(entrypoint) ? entrypoint : [entrypoint])
25
24
  .map(ep => joinPossiblyAbsolutePaths(dir, mapper(ep)))
@@ -235,49 +234,6 @@ function parseEntrypointsJSON(dir) {
235
234
  });
236
235
  }
237
236
  }
238
- function makeDependencyExtractionPlugin(externals) {
239
- const exactExternals = {};
240
- const suffixExternals = [];
241
- for (const [key, value] of Object.entries(externals)) {
242
- const normalizedValue = typeof value !== 'string' && value.import === undefined ? { ...value, import: key } : value;
243
- if (key.startsWith("*/")) {
244
- suffixExternals.push([key.substring(1), typeof normalizedValue === 'string' ? { import: normalizedValue, handle: key.substring(2) } : normalizedValue]);
245
- }
246
- else {
247
- exactExternals[key] = normalizedValue;
248
- }
249
- }
250
- const resolvePossibleExternal = (request) => {
251
- if (request in exactExternals) {
252
- return exactExternals[request];
253
- }
254
- for (const [key, value] of suffixExternals) {
255
- if (request.endsWith(key)) {
256
- return value;
257
- }
258
- }
259
- return undefined;
260
- };
261
- return new dependency_extraction_webpack_plugin_1.default({
262
- requestToExternal(request) {
263
- const possibleExternal = resolvePossibleExternal(request);
264
- if (typeof possibleExternal === 'string') {
265
- return possibleExternal;
266
- }
267
- else if (possibleExternal !== undefined) {
268
- return possibleExternal['import'];
269
- }
270
- return undefined;
271
- },
272
- requestToHandle(request) {
273
- const possibleExternal = resolvePossibleExternal(request);
274
- if (possibleExternal !== undefined && typeof possibleExternal !== 'string') {
275
- return possibleExternal.handle;
276
- }
277
- return undefined;
278
- }
279
- });
280
- }
281
237
  function processIndividualWebpackConfig(config, webpackConfig, sources) {
282
238
  let scriptExtension; // This is only used in non-block contexts. It might not actually be necessary, but it is good to have
283
239
  let entrypointFields;
@@ -290,7 +246,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
290
246
  scriptExtension = scriptWithoutModuleExtension;
291
247
  entrypointFields = ["editorStyle", "viewStyle", "style", "editorScript", "viewScript", "script"];
292
248
  }
293
- const { standaloneBlocks, variablesFilePath, verbose, externals } = config;
249
+ const { standaloneBlocks, variablesFilePath, verbose, externals, assumeGlobalizedPlauditLibraries } = config;
294
250
  let currentVariables = config.currentVariables;
295
251
  const fixedRules = [
296
252
  replaceDefaultURLProcessing,
@@ -342,7 +298,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
342
298
  plugins.push(new webpack_remove_empty_scripts_1.default({
343
299
  stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS,
344
300
  extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss']
345
- }));
301
+ }), new MiniCSSExtractPluginErrorCleaner_1.default());
346
302
  if (variablesFilePath) {
347
303
  plugins.push(new VariablesJSMonitorPlugin_1.default(variablesFilePath));
348
304
  }
@@ -353,13 +309,14 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
353
309
  if (srcIsDirectory && (typeof dest !== 'string' && dest.directoryLayout === 'extensions')) {
354
310
  plugins.push(new ExtensionsConfigFileGeneratorPlugin_1.default(srcRoot));
355
311
  }
356
- if (externals) {
357
- const pluginIndex = plugins.findIndex(plugin => plugin instanceof dependency_extraction_webpack_plugin_1.default);
358
- if (pluginIndex === -1) {
359
- console.error("Cannot apply externals when they have been disabled via CLI flag.");
360
- }
361
- else {
362
- 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;
363
320
  }
364
321
  }
365
322
  if (process.argv.includes('--browser-sync') || process.env['BROWSER_SYNC'] === 'true') {
@@ -512,13 +469,13 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
512
469
  }
513
470
  module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
514
471
  testForDuplicatedEntryPaths(config);
515
- 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;
516
473
  let variablesFilePath = undefined;
517
474
  const currentVariables = rawVariables ?? {};
518
475
  if (!rawVariables) {
519
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];
520
477
  }
521
- const cfg = { currentVariables, postcss, standaloneBlocks, stats, variablesFilePath, verbose, externals };
478
+ const cfg = { currentVariables, postcss, standaloneBlocks, stats, variablesFilePath, verbose, externals, assumeGlobalizedPlauditLibraries };
522
479
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
523
480
  if (Array.isArray(webpackConfig)) {
524
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.36.0",
3
+ "version": "2.38.0",
4
4
  "license": "UNLICENSED",
5
5
  "scripts": {
6
6
  "prepublishOnly": "rm -rf build && mkdir build && tsc",