@plaudit/webpack-extensions 2.62.0 → 2.62.1

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,236 @@ 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.push) {
211
+ if (this.assignees.some(a => a instanceof Access && !a.endsWithPotentiallyWritableExpression())) {
212
+ throw new Error("Cannot write to an access chain that does not end with a writable access");
213
+ }
214
+ }
215
+ else {
216
+ }
19
217
  }
20
218
  toString() {
21
- return this.raw ? this.value : Expr.jsonToPHPConverter(this.value);
219
+ //TODO: Validate that we're in push mode if assigning to a call
220
+ if (!this.push && this.assignees.some(a => a instanceof Call)) {
221
+ }
222
+ const assignmentValue = isEnclosableExpression(this.value) ? this.value.toEnclosedForm() : Expr.convertJsonToPHP(this.value);
223
+ if (this.push) {
224
+ return this.assignees[0].toEnclosedForm() + "[] = " + assignmentValue;
225
+ }
226
+ return [...this.assignees.map(t => t.toEnclosedForm()), assignmentValue].join(" = ");
22
227
  }
23
228
  }
24
- exports.Expr = Expr;
229
+ exports.Assignment = Assignment;
230
+ class Constants {
231
+ static __FILE__ = Literal.name("__FILE__");
232
+ static __DIR__ = Literal.name("__DIR__");
233
+ static ABSPATH = Literal.name("ABSPATH");
234
+ }
235
+ exports.Constants = Constants;
25
236
  class PHPWriter {
26
237
  inlineFirstLine;
27
238
  scopeStack;
@@ -64,35 +275,34 @@ class PHPWriter {
64
275
  }
65
276
  return this;
66
277
  }
67
- assign(variable, expression, opts = {}) {
68
- let lineComponents;
69
- if (typeof variable === 'string') {
278
+ assign(assignee, expression, opts = {}) {
279
+ if (typeof assignee === 'string') {
280
+ assignee = new Var(assignee);
281
+ }
282
+ if (Array.isArray(assignee)) {
70
283
  if (this.scopeStack.length > 0) {
71
- this.scopeStack[this.scopeStack.length - 1].push(variable);
284
+ this.scopeStack[this.scopeStack.length - 1].push(...assignee.filter(v => v instanceof Var).map(v => v.name));
72
285
  }
73
- lineComponents = [`${variable} =`];
74
286
  }
75
287
  else {
76
- if (this.scopeStack.length > 0) {
77
- this.scopeStack[this.scopeStack.length - 1].push(...variable);
288
+ if (this.scopeStack.length > 0 && assignee instanceof Var) {
289
+ this.scopeStack[this.scopeStack.length - 1].push(assignee.name);
78
290
  }
79
- lineComponents = variable.map(v => `${v} =`);
80
- }
81
- if (opts.return) {
82
- lineComponents.splice(0, 0, "return");
83
291
  }
84
- lineComponents.push(Expr.convertJsonToPHP(expression));
85
- return this.append(!opts.chain ? lineComponents.join(" ") + ';' : lineComponents.join(" "));
292
+ return this.appendWithPossibleReturnAndChain(new Assignment(assignee, expression), opts);
86
293
  }
87
294
  return(expression) {
88
295
  return this.append(`return ${Expr.convertJsonToPHP(expression)};`);
89
296
  }
90
297
  static(variable, opts = {}) {
298
+ if (typeof variable === 'string') {
299
+ variable = new Var(variable);
300
+ }
91
301
  const initializer = opts.initializer ? Expr.convertJsonToPHP(opts.initializer) : "null";
92
- this.append(`static ${variable} = ${initializer};`);
302
+ this.append(`static ${variable.toEnclosedForm()} = ${initializer};`);
93
303
  if (opts.withTest) {
94
- this.if(`${variable} !== ${initializer}`)
95
- .append(`return ${variable};`);
304
+ this.if(Op.binary(variable, "!==", new Literal(initializer)))
305
+ .append(`return ${Expr.convertJsonToPHP(variable)};`);
96
306
  if (opts.withTest !== 'chainable') {
97
307
  this.endIf();
98
308
  }
@@ -104,16 +314,11 @@ class PHPWriter {
104
314
  return this;
105
315
  }
106
316
  call(func, args, opts = {}) {
107
- const functionCall = `${func}(${args.map(Expr.convertJsonToPHP).join(", ")})`;
317
+ const functionCall = new Call(typeof func === 'string' ? Literal.name(func) : func, args);
108
318
  if (opts.assignTo) {
109
- return this.assign(opts.assignTo, new Expr(functionCall), opts);
319
+ return this.assign(opts.assignTo, functionCall, opts);
110
320
  }
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(" "));
321
+ return this.appendWithPossibleReturnAndChain(functionCall, opts);
117
322
  }
118
323
  action(name, contents, args = {}) {
119
324
  return this.actionOrFilter('action', name, contents, args);
@@ -133,7 +338,8 @@ class PHPWriter {
133
338
  const functionName = functionWriter.function(true, functionArgParameters, contents, { useVars, scopeActionDescription: `closing the function call for the ${name} ${type}.`, assignToName: true });
134
339
  this.append(functionWriter.toString().trim());
135
340
  declarationFunctionText = functionName + "(...)";
136
- this.if(`doing_${type}(${Expr.convertJsonToPHP(name)})`).call(functionName, accountForAlreadyDoing === true ? [] : accountForAlreadyDoing);
341
+ this.if(Expr.call(`doing_${type}`, [name]))
342
+ .call(functionName, accountForAlreadyDoing === true ? [] : accountForAlreadyDoing);
137
343
  }
138
344
  else {
139
345
  functionWriter.function(false, functionArgParameters, contents, { useVars, scopeActionDescription: `closing the function call for the ${name} ${type}.` });
@@ -187,18 +393,18 @@ class PHPWriter {
187
393
  name = this.generateFunctionName(args.assignToName);
188
394
  }
189
395
  if (args.includeExistenceCheck) {
190
- this.if(`!function_exists(${Expr.convertJsonToPHP(name)})`);
396
+ this.if(Expr.call('function_exists', [name]).not());
191
397
  }
192
398
  else {
193
399
  this.openPHP();
194
400
  }
195
- if (name !== false && args.assignToName) {
401
+ if (name instanceof Var) {
196
402
  if (this.scopeStack.length > 0) {
197
- this.scopeStack[this.scopeStack.length - 1].push(name);
403
+ this.scopeStack[this.scopeStack.length - 1].push(name.name);
198
404
  }
199
405
  }
200
- const declarationComponents = name === false ? [] : (args.assignToName ? [name, '='] : ["function"]);
201
- let nameAndParameters = `${!name || args.assignToName ? 'function' : name}(${parameters.join(", ")})`;
406
+ const declarationComponents = name === false ? [] : (name instanceof Var ? [name, '='] : ["function"]);
407
+ let nameAndParameters = `${!name || name instanceof Var ? 'function' : name}(${parameters.join(", ")})`;
202
408
  if (args.useVars?.length) {
203
409
  let useVars = `use (${args.useVars.join(", ")})`;
204
410
  if (args.returnType) {
@@ -228,10 +434,10 @@ class PHPWriter {
228
434
  let count = 0;
229
435
  let name;
230
436
  do {
231
- name = `${asVariable ? '$' : ''}plaudit_webpack_extensions__generated_function__${count++}`;
437
+ name = `plaudit_webpack_extensions__generated_function__${count++}`;
232
438
  } while (this.allocatedGeneratedFunctionNames.has(name));
233
439
  this.allocatedGeneratedFunctionNames.add(name);
234
- return name;
440
+ return asVariable ? new Var(name) : name;
235
441
  }
236
442
  closePHP() {
237
443
  if (!this.printingInPHP) {
@@ -280,10 +486,13 @@ class PHPWriter {
280
486
  if (!scope?.length) {
281
487
  return this;
282
488
  }
283
- for (const scopeItem of scope) {
489
+ const uniquenessSet = new Set();
490
+ const uniqueScopeItems = scope.filter(si => !uniquenessSet.has(si) && uniquenessSet.add(si));
491
+ for (const scopeItem of uniqueScopeItems) {
284
492
  this.allocatedGeneratedFunctionNames.delete(scopeItem);
285
493
  }
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
494
+ // This is equivalent to calling this.call("unset", scope.map(v => new Var(v))), but creates fewer objects
495
+ return this.append(`unset(${uniqueScopeItems.map(usi => "$" + usi).join(", ")});`);
287
496
  }
288
497
  /**
289
498
  * Pops the top scope from the stack WITHOUT calling unset for the variables that were assigned within it.
@@ -345,5 +554,14 @@ class PHPWriter {
345
554
  const contents = this.toString() + "\n";
346
555
  compilation[file in compilation.assets ? 'updateAsset' : 'emitAsset'](file, new webpack_1.sources.RawSource(contents), { size: Buffer.byteLength(contents), ...assetInfo });
347
556
  }
557
+ /**
558
+ * @param expr Unlike most other methods on this class, this is treated as literal *regardless of the argument's type*
559
+ * @param opts
560
+ * @private
561
+ */
562
+ appendWithPossibleReturnAndChain(expr, opts) {
563
+ const res = (opts.return ? "return " : "") + expr.toString();
564
+ return this.append(!opts.chain ? res + ';' : res);
565
+ }
348
566
  }
349
567
  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.1",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "files": [
6
6
  "/build"