@plaudit/webpack-extensions 2.62.0 → 2.62.2

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.
@@ -3,25 +3,235 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PHPWriter = exports.Expr = void 0;
6
+ exports.PHPWriter = exports.Constants = exports.Assignment = exports.Call = exports.ObjectAccess = exports.ArrayAccess = exports.Access = exports.Not = exports.Op = exports.Var = exports.EnclosableLiteral = exports.EnclosedLiteral = exports.Literal = exports.EnclosedExpression = exports.ParenthesesEnclosableExpression = exports.AbstractEnclosableExpression = exports.Expr = void 0;
7
+ exports.isEnclosableExpression = isEnclosableExpression;
8
+ exports.isPotentiallyWriteableExpression = isPotentiallyWriteableExpression;
7
9
  const json_to_php_but_with____injection_1 = __importDefault(require("./json-to-php-but-with-__-injection"));
8
10
  const webpack_1 = require("webpack");
9
11
  class Expr {
10
- value;
11
- raw;
12
12
  static jsonToPHPConverter = json_to_php_but_with____injection_1.default.make({ shortArraySyntax: true });
13
13
  static convertJsonToPHP = (obj) => obj instanceof Expr ? obj.toString() : Expr.jsonToPHPConverter(obj);
14
- static __FILE__ = new Expr("__FILE__");
15
- static __DIR__ = new Expr("__DIR__");
16
- constructor(value, raw = true) {
14
+ constructor() { }
15
+ typeName() {
16
+ return this.constructor.name;
17
+ }
18
+ static isset(...checks) {
19
+ return Expr.call("isset", checks);
20
+ }
21
+ static call(name, args) {
22
+ return new Call(Literal.name(name), args);
23
+ }
24
+ }
25
+ exports.Expr = Expr;
26
+ function isEnclosableExpression(a) {
27
+ return a instanceof Expr && 'toEnclosedForm' in a && typeof a.toEnclosedForm === 'function';
28
+ }
29
+ function isPotentiallyWriteableExpression(a) {
30
+ return isEnclosableExpression(a) && 'potentiallyWritable' in a && a.potentiallyWritable === true;
31
+ }
32
+ class AbstractEnclosableExpression extends Expr {
33
+ not() {
34
+ return new Not(this);
35
+ }
36
+ }
37
+ exports.AbstractEnclosableExpression = AbstractEnclosableExpression;
38
+ class ParenthesesEnclosableExpression extends AbstractEnclosableExpression {
39
+ toEnclosedForm() {
40
+ return "(" + this.toString() + ")";
41
+ }
42
+ }
43
+ exports.ParenthesesEnclosableExpression = ParenthesesEnclosableExpression;
44
+ class EnclosedExpression extends AbstractEnclosableExpression {
45
+ toEnclosedForm() {
46
+ return this.toString();
47
+ }
48
+ }
49
+ exports.EnclosedExpression = EnclosedExpression;
50
+ class Literal extends Expr {
51
+ expression;
52
+ constructor(expression) {
53
+ super();
54
+ this.expression = expression;
55
+ }
56
+ toString() {
57
+ return this.expression;
58
+ }
59
+ static make(expression, enclosed = false, enclosureClose) {
60
+ if (typeof enclosed === 'string') {
61
+ return new EnclosableLiteral(expression, enclosed, enclosureClose ?? enclosed);
62
+ }
63
+ if (enclosed) {
64
+ return new EnclosedLiteral(expression);
65
+ }
66
+ return new Literal(expression);
67
+ }
68
+ static name(expression) {
69
+ return new EnclosedLiteral(expression);
70
+ }
71
+ }
72
+ exports.Literal = Literal;
73
+ class EnclosedLiteral extends Literal {
74
+ constructor(expression) {
75
+ super(expression);
76
+ }
77
+ toEnclosedForm() {
78
+ return this.toString();
79
+ }
80
+ not() {
81
+ return new Not(this);
82
+ }
83
+ }
84
+ exports.EnclosedLiteral = EnclosedLiteral;
85
+ class EnclosableLiteral extends Literal {
86
+ enclosureOpen;
87
+ enclosureClose;
88
+ constructor(expression, enclosureOpen, enclosureClose) {
89
+ super(expression);
90
+ this.enclosureOpen = enclosureOpen;
91
+ this.enclosureClose = enclosureClose;
92
+ }
93
+ toEnclosedForm() {
94
+ return this.enclosureOpen + this.toString() + this.enclosureClose;
95
+ }
96
+ not() {
97
+ return new Not(this);
98
+ }
99
+ }
100
+ exports.EnclosableLiteral = EnclosableLiteral;
101
+ class Var extends EnclosedExpression {
102
+ name;
103
+ potentiallyWritable = true;
104
+ constructor(name) {
105
+ super();
106
+ this.name = name;
107
+ }
108
+ toString() {
109
+ return "$" + this.name;
110
+ }
111
+ }
112
+ exports.Var = Var;
113
+ class Op extends ParenthesesEnclosableExpression {
114
+ operator;
115
+ args;
116
+ constructor(operator, ...args) {
117
+ super();
118
+ this.operator = operator;
119
+ this.args = args;
120
+ }
121
+ toString() {
122
+ return this.args
123
+ .map(a => isEnclosableExpression(a) ? a.toEnclosedForm() : Expr.convertJsonToPHP(a))
124
+ .join(this.operator);
125
+ }
126
+ static binary(left, operator, right) {
127
+ return new Op(operator, left, right);
128
+ }
129
+ static join(...args) {
130
+ return new Op(".", ...args);
131
+ }
132
+ }
133
+ exports.Op = Op;
134
+ class Not extends ParenthesesEnclosableExpression {
135
+ expression;
136
+ constructor(expression) {
137
+ super();
138
+ this.expression = expression;
139
+ }
140
+ toString() {
141
+ return "!" + this.expression.toEnclosedForm();
142
+ }
143
+ }
144
+ exports.Not = Not;
145
+ class Access extends EnclosedExpression {
146
+ target;
147
+ potentiallyWritable = true;
148
+ accesses;
149
+ constructor(target, accesses) {
150
+ super();
151
+ this.target = target;
152
+ this.accesses = Array.isArray(accesses) ? accesses : [accesses];
153
+ }
154
+ }
155
+ exports.Access = Access;
156
+ class ArrayAccess extends Access {
157
+ toString() {
158
+ return this.target.toEnclosedForm() + this.accesses.map(a => `[${Expr.convertJsonToPHP(a)}]`).join("");
159
+ }
160
+ endsWithCall() {
161
+ return false;
162
+ }
163
+ endsWithPotentiallyWritableExpression() {
164
+ return true;
165
+ }
166
+ }
167
+ exports.ArrayAccess = ArrayAccess;
168
+ class ObjectAccess extends Access {
169
+ static validObjectKey = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
170
+ toString() {
171
+ return [this.target.toString(), ...this.accesses.map(a => {
172
+ if ((typeof a === 'string' && ObjectAccess.validObjectKey.test(a)) || a instanceof Var || a instanceof Call) {
173
+ return a;
174
+ }
175
+ return "{" + Expr.convertJsonToPHP(a) + "}";
176
+ })].join("->");
177
+ }
178
+ endsWithCall() {
179
+ return this.accesses[this.accesses.length - 1] instanceof Call;
180
+ }
181
+ endsWithPotentiallyWritableExpression() {
182
+ return isPotentiallyWriteableExpression(this.accesses[this.accesses.length - 1]);
183
+ }
184
+ }
185
+ exports.ObjectAccess = ObjectAccess;
186
+ class Call extends EnclosedExpression {
187
+ callee;
188
+ args;
189
+ potentiallyWritable = true;
190
+ constructor(callee, args) {
191
+ super();
192
+ this.callee = callee;
193
+ this.args = args;
194
+ }
195
+ toString() {
196
+ const callee = typeof this.callee === 'string' ? Expr.convertJsonToPHP(this.callee) : this.callee.toEnclosedForm();
197
+ return `${callee}(${this.args.map(Expr.convertJsonToPHP).join(", ")})`;
198
+ }
199
+ }
200
+ exports.Call = Call;
201
+ class Assignment extends ParenthesesEnclosableExpression {
202
+ value;
203
+ push;
204
+ assignees;
205
+ constructor(assignees, value, push = false) {
206
+ super();
17
207
  this.value = value;
18
- this.raw = raw;
208
+ this.push = push;
209
+ this.assignees = Array.isArray(assignees) ? assignees : [assignees];
210
+ if (this.assignees.some(a => a instanceof Access && !a.endsWithPotentiallyWritableExpression())) {
211
+ throw new Error("Cannot assign a value to an access chain that does not end in a writable state");
212
+ }
213
+ if (!this.push && this.assignees.some(a => a instanceof Access && a.endsWithCall())) {
214
+ throw new Error("Cannot assign a value to an access chain that ends in a function call unless that 'assignment' is a push");
215
+ }
19
216
  }
20
217
  toString() {
21
- return this.raw ? this.value : Expr.jsonToPHPConverter(this.value);
218
+ //TODO: Validate that we're in push mode if assigning to a call
219
+ if (!this.push && this.assignees.some(a => a instanceof Call)) {
220
+ }
221
+ const assignmentValue = isEnclosableExpression(this.value) ? this.value.toEnclosedForm() : Expr.convertJsonToPHP(this.value);
222
+ if (this.push) {
223
+ return this.assignees[0].toEnclosedForm() + "[] = " + assignmentValue;
224
+ }
225
+ return [...this.assignees.map(t => t.toEnclosedForm()), assignmentValue].join(" = ");
22
226
  }
23
227
  }
24
- exports.Expr = Expr;
228
+ exports.Assignment = Assignment;
229
+ class Constants {
230
+ static __FILE__ = Literal.name("__FILE__");
231
+ static __DIR__ = Literal.name("__DIR__");
232
+ static ABSPATH = Literal.name("ABSPATH");
233
+ }
234
+ exports.Constants = Constants;
25
235
  class PHPWriter {
26
236
  inlineFirstLine;
27
237
  scopeStack;
@@ -64,35 +274,34 @@ class PHPWriter {
64
274
  }
65
275
  return this;
66
276
  }
67
- assign(variable, expression, opts = {}) {
68
- let lineComponents;
69
- if (typeof variable === 'string') {
277
+ assign(assignee, expression, opts = {}) {
278
+ if (typeof assignee === 'string') {
279
+ assignee = new Var(assignee);
280
+ }
281
+ if (Array.isArray(assignee)) {
70
282
  if (this.scopeStack.length > 0) {
71
- this.scopeStack[this.scopeStack.length - 1].push(variable);
283
+ this.scopeStack[this.scopeStack.length - 1].push(...assignee.filter(v => v instanceof Var).map(v => v.name));
72
284
  }
73
- lineComponents = [`${variable} =`];
74
285
  }
75
286
  else {
76
- if (this.scopeStack.length > 0) {
77
- this.scopeStack[this.scopeStack.length - 1].push(...variable);
287
+ if (this.scopeStack.length > 0 && assignee instanceof Var) {
288
+ this.scopeStack[this.scopeStack.length - 1].push(assignee.name);
78
289
  }
79
- lineComponents = variable.map(v => `${v} =`);
80
- }
81
- if (opts.return) {
82
- lineComponents.splice(0, 0, "return");
83
290
  }
84
- lineComponents.push(Expr.convertJsonToPHP(expression));
85
- return this.append(!opts.chain ? lineComponents.join(" ") + ';' : lineComponents.join(" "));
291
+ return this.appendWithPossibleReturnAndChain(new Assignment(assignee, expression), opts);
86
292
  }
87
293
  return(expression) {
88
294
  return this.append(`return ${Expr.convertJsonToPHP(expression)};`);
89
295
  }
90
296
  static(variable, opts = {}) {
297
+ if (typeof variable === 'string') {
298
+ variable = new Var(variable);
299
+ }
91
300
  const initializer = opts.initializer ? Expr.convertJsonToPHP(opts.initializer) : "null";
92
- this.append(`static ${variable} = ${initializer};`);
301
+ this.append(`static ${variable.toEnclosedForm()} = ${initializer};`);
93
302
  if (opts.withTest) {
94
- this.if(`${variable} !== ${initializer}`)
95
- .append(`return ${variable};`);
303
+ this.if(Op.binary(variable, "!==", new Literal(initializer)))
304
+ .append(`return ${Expr.convertJsonToPHP(variable)};`);
96
305
  if (opts.withTest !== 'chainable') {
97
306
  this.endIf();
98
307
  }
@@ -104,16 +313,11 @@ class PHPWriter {
104
313
  return this;
105
314
  }
106
315
  call(func, args, opts = {}) {
107
- const functionCall = `${func}(${args.map(Expr.convertJsonToPHP).join(", ")})`;
316
+ const functionCall = new Call(typeof func === 'string' ? Literal.name(func) : func, args);
108
317
  if (opts.assignTo) {
109
- return this.assign(opts.assignTo, new Expr(functionCall), opts);
318
+ return this.assign(opts.assignTo, functionCall, opts);
110
319
  }
111
- const lineComponents = [];
112
- if (opts.return) {
113
- lineComponents.push("return");
114
- }
115
- lineComponents.push(functionCall);
116
- return this.append(!opts.chain ? lineComponents.join(" ") + ';' : lineComponents.join(" "));
320
+ return this.appendWithPossibleReturnAndChain(functionCall, opts);
117
321
  }
118
322
  action(name, contents, args = {}) {
119
323
  return this.actionOrFilter('action', name, contents, args);
@@ -133,7 +337,8 @@ class PHPWriter {
133
337
  const functionName = functionWriter.function(true, functionArgParameters, contents, { useVars, scopeActionDescription: `closing the function call for the ${name} ${type}.`, assignToName: true });
134
338
  this.append(functionWriter.toString().trim());
135
339
  declarationFunctionText = functionName + "(...)";
136
- this.if(`doing_${type}(${Expr.convertJsonToPHP(name)})`).call(functionName, accountForAlreadyDoing === true ? [] : accountForAlreadyDoing);
340
+ this.if(Expr.call(`doing_${type}`, [name]))
341
+ .call(functionName, accountForAlreadyDoing === true ? [] : accountForAlreadyDoing);
137
342
  }
138
343
  else {
139
344
  functionWriter.function(false, functionArgParameters, contents, { useVars, scopeActionDescription: `closing the function call for the ${name} ${type}.` });
@@ -187,18 +392,18 @@ class PHPWriter {
187
392
  name = this.generateFunctionName(args.assignToName);
188
393
  }
189
394
  if (args.includeExistenceCheck) {
190
- this.if(`!function_exists(${Expr.convertJsonToPHP(name)})`);
395
+ this.if(Expr.call('function_exists', [name]).not());
191
396
  }
192
397
  else {
193
398
  this.openPHP();
194
399
  }
195
- if (name !== false && args.assignToName) {
400
+ if (name instanceof Var) {
196
401
  if (this.scopeStack.length > 0) {
197
- this.scopeStack[this.scopeStack.length - 1].push(name);
402
+ this.scopeStack[this.scopeStack.length - 1].push(name.name);
198
403
  }
199
404
  }
200
- const declarationComponents = name === false ? [] : (args.assignToName ? [name, '='] : ["function"]);
201
- let nameAndParameters = `${!name || args.assignToName ? 'function' : name}(${parameters.join(", ")})`;
405
+ const declarationComponents = name === false ? [] : (name instanceof Var ? [name, '='] : ["function"]);
406
+ let nameAndParameters = `${!name || name instanceof Var ? 'function' : name}(${parameters.join(", ")})`;
202
407
  if (args.useVars?.length) {
203
408
  let useVars = `use (${args.useVars.join(", ")})`;
204
409
  if (args.returnType) {
@@ -228,10 +433,10 @@ class PHPWriter {
228
433
  let count = 0;
229
434
  let name;
230
435
  do {
231
- name = `${asVariable ? '$' : ''}plaudit_webpack_extensions__generated_function__${count++}`;
436
+ name = `plaudit_webpack_extensions__generated_function__${count++}`;
232
437
  } while (this.allocatedGeneratedFunctionNames.has(name));
233
438
  this.allocatedGeneratedFunctionNames.add(name);
234
- return name;
439
+ return asVariable ? new Var(name) : name;
235
440
  }
236
441
  closePHP() {
237
442
  if (!this.printingInPHP) {
@@ -280,10 +485,13 @@ class PHPWriter {
280
485
  if (!scope?.length) {
281
486
  return this;
282
487
  }
283
- for (const scopeItem of scope) {
488
+ const uniquenessSet = new Set();
489
+ const uniqueScopeItems = scope.filter(si => !uniquenessSet.has(si) && uniquenessSet.add(si));
490
+ for (const scopeItem of uniqueScopeItems) {
284
491
  this.allocatedGeneratedFunctionNames.delete(scopeItem);
285
492
  }
286
- return this.append(`unset(${scope.join(", ")});`); // This is equivalent to calling this.call("unset", scope.map(v => new Expr(v))), but creates fewer objects
493
+ // This is equivalent to calling this.call("unset", scope.map(v => new Var(v))), but creates fewer objects
494
+ return this.append(`unset(${uniqueScopeItems.map(usi => "$" + usi).join(", ")});`);
287
495
  }
288
496
  /**
289
497
  * Pops the top scope from the stack WITHOUT calling unset for the variables that were assigned within it.
@@ -345,5 +553,14 @@ class PHPWriter {
345
553
  const contents = this.toString() + "\n";
346
554
  compilation[file in compilation.assets ? 'updateAsset' : 'emitAsset'](file, new webpack_1.sources.RawSource(contents), { size: Buffer.byteLength(contents), ...assetInfo });
347
555
  }
556
+ /**
557
+ * @param expr Unlike most other methods on this class, this is treated as literal *regardless of the argument's type*
558
+ * @param opts
559
+ * @private
560
+ */
561
+ appendWithPossibleReturnAndChain(expr, opts) {
562
+ const res = (opts.return ? "return " : "") + expr.toString();
563
+ return this.append(!opts.chain ? res + ';' : res);
564
+ }
348
565
  }
349
566
  exports.PHPWriter = PHPWriter;
@@ -9,7 +9,9 @@ const shared_1 = require("./shared");
9
9
  const common_config_helpers_1 = require("./utils/common-config-helpers");
10
10
  const AdditionalDependencyInjectorPlugin_1 = require("./plugins/AdditionalDependencyInjectorPlugin");
11
11
  const BrowserSyncPlugin_1 = require("./plugins/BrowserSyncPlugin");
12
+ const dependency_extraction_webpack_plugin_config_builder_1 = require("./plugins/dependency-extraction-webpack-plugin-config-builder");
12
13
  const ExtensionsConfigFileGeneratorPlugin_1 = require("./plugins/ExtensionsConfigFileGeneratorPlugin");
14
+ const ExtensionsConfigFileGeneratorPluginV1_1 = require("./plugins/ExtensionsConfigFileGeneratorPluginV1");
13
15
  const MiniCSSExtractPluginErrorCleaner_1 = require("./plugins/MiniCSSExtractPluginErrorCleaner");
14
16
  const PackageConfigSanityChecker_1 = require("./plugins/PackageConfigSanityChecker");
15
17
  const PlainEntrypointsConfigFileGeneratorPlugin_1 = require("./plugins/PlainEntrypointsConfigFileGeneratorPlugin");
@@ -18,11 +20,10 @@ const SpecialAssetHandlingPlugin_1 = require("./plugins/SpecialAssetHandlingPlug
18
20
  const VariablesJSMonitorPlugin_1 = require("./plugins/VariablesJSMonitorPlugin");
19
21
  const WPMLConfigBuilder_1 = require("./plugins/WPMLConfigBuilder");
20
22
  const static_configs_1 = require("./plugins/static-configs");
21
- const dependency_extraction_webpack_plugin_config_builder_1 = require("./plugins/dependency-extraction-webpack-plugin-config-builder");
23
+ const UnifiedLoaderGenerator_1 = require("./plugins/UnifiedLoaderGenerator");
22
24
  const dependency_extraction_webpack_plugin_1 = __importDefault(require("@wordpress/dependency-extraction-webpack-plugin"));
23
25
  const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
24
26
  const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
25
- const UnifiedLoaderGenerator_1 = require("./plugins/UnifiedLoaderGenerator");
26
27
  function resolveLegacyBlockScriptsInFolder(folder) {
27
28
  const blockScriptEntrypoints = [];
28
29
  for (const blockDir of node_fs_1.default.readdirSync(folder)) {
@@ -431,17 +432,6 @@ function commonConfigProcessingPrep(config, webpackConfig) {
431
432
  }
432
433
  });
433
434
  }
434
- fixedRules.push({
435
- test: /block\.json$/i,
436
- resourceQuery: /for-resource-tracking/i,
437
- type: 'asset/resource',
438
- generator: {
439
- binary: false,
440
- filename(pathData) {
441
- return `${node_path_1.default.dirname(pathData.runtime)}/block[ext]`;
442
- }
443
- }
444
- });
445
435
  return {
446
436
  entrypointFields, fixedRules, processingModules, scriptExtension,
447
437
  updateCurrentVariables: (value) => currentVariables = value
@@ -500,14 +490,17 @@ function makeBlocksWebpackConfig(config, commonConfig, webpackConfig, dest, src,
500
490
  return (0, common_config_helpers_1.commonMakeWebpackConfig)(config, commonConfig, webpackConfig, true, dest, src, srcRoot, (0, common_config_helpers_1.resolveEntryFromDirectory)(commonConfig, srcRoot, dest), plugins);
501
491
  }
502
492
  function makeExtensionsWebpackConfig(config, commonConfig, webpackConfig, dest, src, srcRoot, plugins) {
503
- plugins.push(new ExtensionsConfigFileGeneratorPlugin_1.ExtensionsConfigFileGeneratorPlugin(config, srcRoot, dest.destination));
493
+ const plugin = config.extensionsVersion > 1
494
+ ? new ExtensionsConfigFileGeneratorPlugin_1.ExtensionsConfigFileGeneratorPlugin(config, srcRoot, dest.destination)
495
+ : new ExtensionsConfigFileGeneratorPluginV1_1.ExtensionsConfigFileGeneratorPluginV1(dest.destination);
496
+ plugins.push(plugin);
504
497
  const entry = async () => {
505
498
  const rawEntrypoints = [];
506
499
  for await (const dirent of await promises_1.default.opendir(srcRoot)) {
507
500
  if (dirent.isFile() && !dirent.name.startsWith("~")) {
508
501
  if (commonConfig.scriptExtension.test(dirent.name) || shared_1.styleExtension.test(dirent.name)) {
509
502
  const file = (0, common_config_helpers_1.joinPossiblyAbsolutePaths)(srcRoot, dirent.name);
510
- rawEntrypoints.push([node_path_1.default.join(dest.destination, node_path_1.default.basename(file, node_path_1.default.extname(file))), file]);
503
+ rawEntrypoints.push([node_path_1.default.join(dest.destination, node_path_1.default.basename(file, node_path_1.default.extname(file))), { import: file, library: { type: plugin.libraryType } }]);
511
504
  }
512
505
  }
513
506
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.62.0",
3
+ "version": "2.62.2",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "files": [
6
6
  "/build"