@plaudit/webpack-extensions 2.26.0 → 2.28.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.
@@ -49,6 +49,7 @@ class ExtensionsConfigFileGeneratorPlugin {
49
49
  if (!stats.modules) {
50
50
  throw new Error("Stats did not include modules despite them being requested");
51
51
  }
52
+ const regex = /^(.+?)-((?:editor-|view-|)(?:style|script|script-module))\.(?:css|m?js)$/i;
52
53
  const mapping = {};
53
54
  for (const asset of stats.assets) {
54
55
  if ((asset.chunks?.length ?? -1) > 1 || !asset.size) {
@@ -58,21 +59,9 @@ class ExtensionsConfigFileGeneratorPlugin {
58
59
  if ((match = /^(.+?)-setup.php$/i.exec(asset.name)) && match[1]) {
59
60
  (mapping[match[1]] ?? (mapping[match[1]] = [{}]))[1] = `${asset.name}`;
60
61
  }
61
- else if ((match = /^(.+?)-(editor-style|style|view-script|editor-script|script)\.(?:css|m?js)$/i.exec(asset.name)) && match[1] && match[2]) {
62
+ else if ((match = regex.exec(asset.name)) && match[1] && match[2]) {
62
63
  const resourceInfo = (mapping[match[1]] ?? (mapping[match[1]] = [{}]))[0];
63
- let key;
64
- if (match[2] === "editor-script") {
65
- key = "editorScript";
66
- }
67
- else if (match[2] === "view-script") {
68
- key = "viewScript";
69
- }
70
- else if (match[2] === "editor-style") {
71
- key = "editorStyle";
72
- }
73
- else {
74
- key = match[2];
75
- }
64
+ const key = match[2].replace(/-[sm]/gi, chars => chars.substring(1).toUpperCase());
76
65
  (resourceInfo[key] ?? (resourceInfo[key] = [])).push([`plaudit_block-extension_${match[1]}-${match[2]}`, asset.name]);
77
66
  }
78
67
  }
@@ -15,6 +15,12 @@ type PlauditWordpressWebpackConfig = {
15
15
  postcss?: {
16
16
  functions?: (variables: (name: string) => unknown) => PostcssFunctionsOptions['functions'];
17
17
  };
18
+ externals?: {
19
+ [dep: string]: string | {
20
+ import: string;
21
+ handle: string;
22
+ };
23
+ };
18
24
  };
19
25
  declare const _default: (config: PlauditWordpressWebpackConfig, webpackConfig?: Configuration[] | Configuration) => Configuration[];
20
26
  export = _default;
@@ -10,6 +10,7 @@ const ExtensionsConfigFileGeneratorPlugin_1 = __importDefault(require("./wordpre
10
10
  const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
11
11
  const BrowserSyncPlugin_1 = require("./wordpress-scripts-wrapper/BrowserSyncPlugin");
12
12
  const static_configs_1 = require("./wordpress-scripts-wrapper/static-configs");
13
+ const dependency_extraction_webpack_plugin_1 = __importDefault(require("@wordpress/dependency-extraction-webpack-plugin"));
13
14
  const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
14
15
  const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
15
16
  const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
@@ -224,7 +225,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
224
225
  scriptExtension = scriptWithoutModuleExtension;
225
226
  entrypointFields = ["editorStyle", "viewStyle", "style", "editorScript", "viewScript", "script"];
226
227
  }
227
- const { standaloneBlocks, variablesFilePath, verbose } = config;
228
+ const { standaloneBlocks, variablesFilePath, verbose, externals } = config;
228
229
  let currentVariables = config.currentVariables;
229
230
  replaceDefaultURLProcessing(webpackConfig);
230
231
  const fixedRules = injectPostcssConfigOverrides(webpackConfig, name => currentVariables[name], config.postcss.functions ?? (() => ({})), verbose);
@@ -281,6 +282,37 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
281
282
  if (srcIsDirectory && (typeof dest !== 'string' && dest.directoryLayout === 'extensions')) {
282
283
  plugins.push(new ExtensionsConfigFileGeneratorPlugin_1.default(srcRoot));
283
284
  }
285
+ if (externals) {
286
+ const pluginIndex = plugins.findIndex(plugin => plugin instanceof dependency_extraction_webpack_plugin_1.default);
287
+ if (pluginIndex === -1) {
288
+ console.error("Cannot apply externals when they have been disabled via CLI flag.");
289
+ }
290
+ else {
291
+ plugins[pluginIndex] = new dependency_extraction_webpack_plugin_1.default({
292
+ requestToExternal(request) {
293
+ const possibleExternal = externals[request];
294
+ if (typeof possibleExternal === 'string') {
295
+ return possibleExternal;
296
+ }
297
+ else if (possibleExternal !== undefined) {
298
+ return possibleExternal['import'];
299
+ }
300
+ else {
301
+ return undefined;
302
+ }
303
+ },
304
+ requestToHandle(request) {
305
+ const possibleExternal = externals[request];
306
+ if (possibleExternal !== undefined && typeof possibleExternal !== 'string') {
307
+ return possibleExternal['handle'];
308
+ }
309
+ else {
310
+ return request;
311
+ }
312
+ }
313
+ });
314
+ }
315
+ }
284
316
  if (process.argv.includes('--browser-sync') || process.env['BROWSER_SYNC'] === 'true') {
285
317
  plugins.push(new BrowserSyncPlugin_1.BrowserSyncPlugin());
286
318
  }
@@ -423,13 +455,13 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
423
455
  }
424
456
  module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
425
457
  testForDuplicatedEntryPaths(config);
426
- const { standaloneBlocks = false, stats = 'errors-warnings', variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true', postcss = {} } = config;
458
+ const { standaloneBlocks = false, stats = 'errors-warnings', variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true', postcss = {}, externals } = config;
427
459
  let variablesFilePath = undefined;
428
460
  const currentVariables = rawVariables ?? {};
429
461
  if (!rawVariables) {
430
462
  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];
431
463
  }
432
- const cfg = { currentVariables, postcss, standaloneBlocks, stats, variablesFilePath, verbose };
464
+ const cfg = { currentVariables, postcss, standaloneBlocks, stats, variablesFilePath, verbose, externals };
433
465
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
434
466
  if (Array.isArray(webpackConfig)) {
435
467
  return webpackConfig.flatMap(wpCfg => processIndividualWebpackConfig(cfg, wpCfg, sources));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.26.0",
3
+ "version": "2.28.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -29,7 +29,7 @@
29
29
  "postcss-load-config": "^4.0.2",
30
30
  "postcss-loader": "^7.3.4",
31
31
  "ts-node": "^10.9.2",
32
- "typescript": "^5.5.3"
32
+ "typescript": "^5.6.2"
33
33
  },
34
34
  "dependencies": {
35
35
  "@plaudit/postcss-color-function": "^5.0.0",