@plaudit/webpack-extensions 2.61.2 → 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.
@@ -1,21 +1,125 @@
1
1
  import { AssetInfo, Compilation } from "webpack";
2
- export declare class Expr {
3
- private readonly value;
4
- private readonly raw;
2
+ export declare abstract class Expr {
5
3
  static readonly jsonToPHPConverter: (obj: unknown, parentIndent?: string) => string;
6
4
  static readonly convertJsonToPHP: (obj: unknown) => string;
7
- constructor(value: string, raw?: boolean);
5
+ protected constructor();
6
+ abstract toString(): string;
7
+ typeName(): string;
8
+ static isset(...checks: (Var | ArrayAccess | ObjectAccess)[]): Call;
9
+ static call(name: string, args: ConstructorParameters<typeof Call>[1]): Call;
10
+ }
11
+ export interface EnclosableExpression extends Expr {
12
+ toEnclosedForm(): string;
13
+ not(): Not;
14
+ }
15
+ export interface PotentiallyWriteableExpression extends EnclosableExpression {
16
+ readonly potentiallyWritable: true;
17
+ }
18
+ export declare function isEnclosableExpression(a: unknown): a is EnclosableExpression;
19
+ export declare function isPotentiallyWriteableExpression(a: unknown): a is PotentiallyWriteableExpression;
20
+ export declare abstract class AbstractEnclosableExpression extends Expr implements EnclosableExpression {
21
+ abstract toEnclosedForm(): string;
22
+ not(): Not;
23
+ }
24
+ export declare abstract class ParenthesesEnclosableExpression extends AbstractEnclosableExpression {
25
+ toEnclosedForm(): string;
26
+ }
27
+ export declare abstract class EnclosedExpression extends AbstractEnclosableExpression {
28
+ toEnclosedForm(): string;
29
+ }
30
+ export declare class Literal extends Expr {
31
+ readonly expression: string;
32
+ constructor(expression: string);
33
+ toString(): string;
34
+ static make(expression: string, enclosed: true): EnclosedLiteral;
35
+ static make(expression: string, enclosureOpen: string, encloserClose: string): EnclosableLiteral;
36
+ static make(expression: string, enclosed?: false | undefined): Literal;
37
+ static name(expression: string): EnclosedLiteral;
38
+ }
39
+ export declare class EnclosedLiteral extends Literal implements EnclosableExpression {
40
+ constructor(expression: string);
41
+ toEnclosedForm(): string;
42
+ not(): Not;
43
+ }
44
+ export declare class EnclosableLiteral extends Literal implements EnclosableExpression {
45
+ private readonly enclosureOpen;
46
+ private readonly enclosureClose;
47
+ constructor(expression: string, enclosureOpen: string, enclosureClose: string);
48
+ toEnclosedForm(): string;
49
+ not(): Not;
50
+ }
51
+ export declare class Var extends EnclosedExpression implements PotentiallyWriteableExpression {
52
+ readonly name: string;
53
+ readonly potentiallyWritable = true;
54
+ constructor(name: string);
55
+ toString(): string;
56
+ }
57
+ export declare class Op extends ParenthesesEnclosableExpression {
58
+ readonly operator: string;
59
+ readonly args: readonly unknown[];
60
+ constructor(operator: string, ...args: unknown[]);
61
+ toString(): string;
62
+ static binary(left: unknown, operator: string, right: unknown): Op;
63
+ static join(...args: unknown[]): Op;
64
+ }
65
+ export declare class Not extends ParenthesesEnclosableExpression {
66
+ readonly expression: EnclosableExpression;
67
+ constructor(expression: EnclosableExpression);
68
+ toString(): string;
69
+ }
70
+ type AtLeastOneArray<T> = [T, ...T[]];
71
+ type ReadonlyAtLeastOneArray<T> = readonly [T, ...T[]];
72
+ export type Accessor = Expr | string | number;
73
+ export type AccessorOrAccessorArray = Accessor | AtLeastOneArray<Accessor>;
74
+ export declare abstract class Access extends EnclosedExpression implements PotentiallyWriteableExpression {
75
+ readonly target: EnclosableExpression;
76
+ readonly potentiallyWritable = true;
77
+ readonly accesses: ReadonlyAtLeastOneArray<Accessor>;
78
+ constructor(target: EnclosableExpression, accesses: AccessorOrAccessorArray);
79
+ abstract endsWithCall(): boolean;
80
+ abstract endsWithPotentiallyWritableExpression(): boolean;
81
+ }
82
+ export declare class ArrayAccess extends Access {
8
83
  toString(): string;
84
+ endsWithCall(): boolean;
85
+ endsWithPotentiallyWritableExpression(): boolean;
86
+ }
87
+ export declare class ObjectAccess extends Access {
88
+ static readonly validObjectKey: RegExp;
89
+ toString(): string;
90
+ endsWithCall(): boolean;
91
+ endsWithPotentiallyWritableExpression(): boolean;
92
+ }
93
+ export declare class Call extends EnclosedExpression implements PotentiallyWriteableExpression {
94
+ readonly callee: string | EnclosableExpression;
95
+ readonly args: readonly unknown[];
96
+ readonly potentiallyWritable = true;
97
+ constructor(callee: string | EnclosableExpression, args: readonly unknown[]);
98
+ toString(): string;
99
+ }
100
+ type Assignable = EnclosedExpression & PotentiallyWriteableExpression;
101
+ export declare class Assignment extends ParenthesesEnclosableExpression {
102
+ readonly value: unknown;
103
+ readonly push: boolean;
104
+ readonly assignees: ReadonlyAtLeastOneArray<Assignable>;
105
+ constructor(assignees: Assignable, value: unknown, push: true);
106
+ constructor(assignees: Assignable | AtLeastOneArray<Assignable>, value: unknown, push?: false | undefined);
107
+ toString(): string;
108
+ }
109
+ export declare abstract class Constants {
110
+ static readonly __FILE__: EnclosedLiteral;
111
+ static readonly __DIR__: EnclosedLiteral;
112
+ static readonly ABSPATH: EnclosedLiteral;
9
113
  }
10
114
  export type ActionOrFilterArgs = {
11
115
  priority?: number | Expr;
12
116
  functionArgParameters?: string[];
13
- useVars?: string[];
117
+ useVars?: Var[];
14
118
  accountForAlreadyDoing?: boolean | Parameters<PHPWriter['call']>[1];
15
119
  };
16
120
  export type FunctionCreationArgs = {
17
121
  includeExistenceCheck?: boolean;
18
- useVars?: string[];
122
+ useVars?: Var[];
19
123
  returnType?: string;
20
124
  assignToName?: boolean;
21
125
  scopeActionDescription?: string;
@@ -37,31 +141,33 @@ export declare class PHPWriter {
37
141
  outdent(): this;
38
142
  setIndentation(level: number): this;
39
143
  append(...lines: (string | Expr)[]): this;
40
- assign(variable: string | string[], expression: unknown | Expr, opts?: {
144
+ assign(assignee: ConstructorParameters<typeof Assignment>[0] | string, expression: unknown, opts?: {
41
145
  chain?: boolean;
42
146
  return?: boolean;
43
147
  }): this;
44
148
  return(expression: unknown | Expr): this;
45
- static(variable: string, opts?: {
46
- initializer?: unknown | Expr;
149
+ static(variable: Var | string, opts?: {
150
+ initializer?: unknown | Literal;
47
151
  withTest?: boolean | 'chainable';
48
152
  }): this;
49
153
  linebreak(): this;
50
- call(func: string, args: unknown[], opts?: {
154
+ call(func: string | EnclosableExpression, args: unknown[], opts?: {
51
155
  chain?: boolean;
52
- assignTo?: string | string[];
156
+ assignTo?: ConstructorParameters<typeof Assignment>[0];
53
157
  return?: boolean;
54
158
  }): this;
55
159
  action(name: string | Expr, contents: (writer: PHPWriter) => void, args?: ActionOrFilterArgs): this;
56
160
  filter(name: string | Expr, contents: (writer: PHPWriter) => void, args?: ActionOrFilterArgs): this;
57
161
  actionOrFilter(type: 'action' | 'filter', name: string | Expr, contents: (writer: PHPWriter) => void, args: ActionOrFilterArgs): this;
58
- if(condition: string): this;
59
- elseIf(condition: string): this;
162
+ if(condition: Expr): this;
163
+ elseIf(condition: Expr): this;
60
164
  else(): this;
61
165
  endIf(): this;
62
- function(name: true, parameters: string[], body: (writer: PHPWriter) => void, args?: FunctionCreationArgs): string;
63
- function(name: string | false, parameters: string[], body: (writer: PHPWriter) => void, args?: FunctionCreationArgs): this;
64
- generateFunctionName(asVariable?: boolean): string;
166
+ function(name: true, parameters: (string | Var)[], body: (writer: PHPWriter) => void, args?: FunctionCreationArgs): Var;
167
+ function(name: string | Var | false, parameters: (string | Var)[], body: (writer: PHPWriter) => void, args?: FunctionCreationArgs): this;
168
+ generateFunctionName(asVariable: true): Var;
169
+ generateFunctionName(asVariable?: false | undefined): string;
170
+ generateFunctionName(asVariable?: boolean): string | Var;
65
171
  closePHP(): this;
66
172
  openPHP(): this;
67
173
  namespace(namespace: string): this;
@@ -89,4 +195,11 @@ export declare class PHPWriter {
89
195
  includeNamespaceAndUse?: boolean;
90
196
  }): string;
91
197
  emitAsset(compilation: Compilation, file: string, assetInfo?: AssetInfo): void;
198
+ /**
199
+ * @param expr Unlike most other methods on this class, this is treated as literal *regardless of the argument's type*
200
+ * @param opts
201
+ * @private
202
+ */
203
+ private appendWithPossibleReturnAndChain;
92
204
  }
205
+ export {};
@@ -3,23 +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
- 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();
15
207
  this.value = value;
16
- 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
+ }
17
217
  }
18
218
  toString() {
19
- 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(" = ");
20
227
  }
21
228
  }
22
- 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;
23
236
  class PHPWriter {
24
237
  inlineFirstLine;
25
238
  scopeStack;
@@ -62,35 +275,34 @@ class PHPWriter {
62
275
  }
63
276
  return this;
64
277
  }
65
- assign(variable, expression, opts = {}) {
66
- let lineComponents;
67
- 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)) {
68
283
  if (this.scopeStack.length > 0) {
69
- 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));
70
285
  }
71
- lineComponents = [`${variable} =`];
72
286
  }
73
287
  else {
74
- if (this.scopeStack.length > 0) {
75
- 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);
76
290
  }
77
- lineComponents = variable.map(v => `${v} =`);
78
- }
79
- if (opts.return) {
80
- lineComponents.splice(0, 0, "return");
81
291
  }
82
- lineComponents.push(Expr.convertJsonToPHP(expression));
83
- return this.append(!opts.chain ? lineComponents.join(" ") + ';' : lineComponents.join(" "));
292
+ return this.appendWithPossibleReturnAndChain(new Assignment(assignee, expression), opts);
84
293
  }
85
294
  return(expression) {
86
295
  return this.append(`return ${Expr.convertJsonToPHP(expression)};`);
87
296
  }
88
297
  static(variable, opts = {}) {
298
+ if (typeof variable === 'string') {
299
+ variable = new Var(variable);
300
+ }
89
301
  const initializer = opts.initializer ? Expr.convertJsonToPHP(opts.initializer) : "null";
90
- this.append(`static ${variable} = ${initializer};`);
302
+ this.append(`static ${variable.toEnclosedForm()} = ${initializer};`);
91
303
  if (opts.withTest) {
92
- this.if(`${variable} !== ${initializer}`)
93
- .append(`return ${variable};`);
304
+ this.if(Op.binary(variable, "!==", new Literal(initializer)))
305
+ .append(`return ${Expr.convertJsonToPHP(variable)};`);
94
306
  if (opts.withTest !== 'chainable') {
95
307
  this.endIf();
96
308
  }
@@ -102,16 +314,11 @@ class PHPWriter {
102
314
  return this;
103
315
  }
104
316
  call(func, args, opts = {}) {
105
- const functionCall = `${func}(${args.map(Expr.convertJsonToPHP).join(", ")})`;
317
+ const functionCall = new Call(typeof func === 'string' ? Literal.name(func) : func, args);
106
318
  if (opts.assignTo) {
107
- return this.assign(opts.assignTo, new Expr(functionCall), opts);
319
+ return this.assign(opts.assignTo, functionCall, opts);
108
320
  }
109
- const lineComponents = [];
110
- if (opts.return) {
111
- lineComponents.push("return");
112
- }
113
- lineComponents.push(functionCall);
114
- return this.append(!opts.chain ? lineComponents.join(" ") + ';' : lineComponents.join(" "));
321
+ return this.appendWithPossibleReturnAndChain(functionCall, opts);
115
322
  }
116
323
  action(name, contents, args = {}) {
117
324
  return this.actionOrFilter('action', name, contents, args);
@@ -131,7 +338,8 @@ class PHPWriter {
131
338
  const functionName = functionWriter.function(true, functionArgParameters, contents, { useVars, scopeActionDescription: `closing the function call for the ${name} ${type}.`, assignToName: true });
132
339
  this.append(functionWriter.toString().trim());
133
340
  declarationFunctionText = functionName + "(...)";
134
- 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);
135
343
  }
136
344
  else {
137
345
  functionWriter.function(false, functionArgParameters, contents, { useVars, scopeActionDescription: `closing the function call for the ${name} ${type}.` });
@@ -185,18 +393,18 @@ class PHPWriter {
185
393
  name = this.generateFunctionName(args.assignToName);
186
394
  }
187
395
  if (args.includeExistenceCheck) {
188
- this.if(`!function_exists(${Expr.convertJsonToPHP(name)})`);
396
+ this.if(Expr.call('function_exists', [name]).not());
189
397
  }
190
398
  else {
191
399
  this.openPHP();
192
400
  }
193
- if (name !== false && args.assignToName) {
401
+ if (name instanceof Var) {
194
402
  if (this.scopeStack.length > 0) {
195
- this.scopeStack[this.scopeStack.length - 1].push(name);
403
+ this.scopeStack[this.scopeStack.length - 1].push(name.name);
196
404
  }
197
405
  }
198
- const declarationComponents = name === false ? [] : (args.assignToName ? [name, '='] : ["function"]);
199
- 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(", ")})`;
200
408
  if (args.useVars?.length) {
201
409
  let useVars = `use (${args.useVars.join(", ")})`;
202
410
  if (args.returnType) {
@@ -226,10 +434,10 @@ class PHPWriter {
226
434
  let count = 0;
227
435
  let name;
228
436
  do {
229
- name = `${asVariable ? '$' : ''}plaudit_webpack_extensions__generated_function__${count++}`;
437
+ name = `plaudit_webpack_extensions__generated_function__${count++}`;
230
438
  } while (this.allocatedGeneratedFunctionNames.has(name));
231
439
  this.allocatedGeneratedFunctionNames.add(name);
232
- return name;
440
+ return asVariable ? new Var(name) : name;
233
441
  }
234
442
  closePHP() {
235
443
  if (!this.printingInPHP) {
@@ -278,10 +486,13 @@ class PHPWriter {
278
486
  if (!scope?.length) {
279
487
  return this;
280
488
  }
281
- 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) {
282
492
  this.allocatedGeneratedFunctionNames.delete(scopeItem);
283
493
  }
284
- return this.call("unset", scope.map(v => new Expr(v)));
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(", ")});`);
285
496
  }
286
497
  /**
287
498
  * Pops the top scope from the stack WITHOUT calling unset for the variables that were assigned within it.
@@ -343,5 +554,14 @@ class PHPWriter {
343
554
  const contents = this.toString() + "\n";
344
555
  compilation[file in compilation.assets ? 'updateAsset' : 'emitAsset'](file, new webpack_1.sources.RawSource(contents), { size: Buffer.byteLength(contents), ...assetInfo });
345
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
+ }
346
566
  }
347
567
  exports.PHPWriter = PHPWriter;
@@ -8,21 +8,22 @@ const node_path_1 = __importDefault(require("node:path"));
8
8
  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
- const BlockJSONManagingPlugin_1 = require("./plugins/BlockJSONManagingPlugin");
12
11
  const BrowserSyncPlugin_1 = require("./plugins/BrowserSyncPlugin");
12
+ const dependency_extraction_webpack_plugin_config_builder_1 = require("./plugins/dependency-extraction-webpack-plugin-config-builder");
13
13
  const ExtensionsConfigFileGeneratorPlugin_1 = require("./plugins/ExtensionsConfigFileGeneratorPlugin");
14
+ const ExtensionsConfigFileGeneratorPluginV1_1 = require("./plugins/ExtensionsConfigFileGeneratorPluginV1");
14
15
  const MiniCSSExtractPluginErrorCleaner_1 = require("./plugins/MiniCSSExtractPluginErrorCleaner");
15
16
  const PackageConfigSanityChecker_1 = require("./plugins/PackageConfigSanityChecker");
16
17
  const PlainEntrypointsConfigFileGeneratorPlugin_1 = require("./plugins/PlainEntrypointsConfigFileGeneratorPlugin");
18
+ const PlainEntrypointsStyleBlockJSONPlugin_1 = require("./plugins/PlainEntrypointsStyleBlockJSONPlugin");
17
19
  const SpecialAssetHandlingPlugin_1 = require("./plugins/SpecialAssetHandlingPlugin");
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)) {
@@ -377,7 +378,8 @@ function buildCommonPluginConfig(srcRoot, scriptExtension, webpackConfig, dest,
377
378
  handleDisablingTSCheckerIfNecessary(srcRoot, scriptExtension, plugins);
378
379
  const removeEmptyScriptsPlugin = new webpack_remove_empty_scripts_1.default({
379
380
  stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS,
380
- extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss']
381
+ extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss'],
382
+ ignore: /block\.json/
381
383
  });
382
384
  plugins.push(removeEmptyScriptsPlugin, new MiniCSSExtractPluginErrorCleaner_1.MiniCSSExtractPluginErrorCleaner());
383
385
  plugins.push(new UnifiedLoaderGenerator_1.UnifiedLoaderGenerator(config));
@@ -390,7 +392,7 @@ function buildCommonPluginConfig(srcRoot, scriptExtension, webpackConfig, dest,
390
392
  else {
391
393
  const localAssumeGlobalizedPlauditLibraries = dest.assumeGlobalizedPlauditLibraries ?? assumeGlobalizedPlauditLibraries;
392
394
  const wantsGroupedDepData = combineAssetMetadata
393
- && ((!processingModules && sourceType === "blocks" /* SourceType.blocks */)
395
+ && ((sourceType === "blocks" /* SourceType.blocks */)
394
396
  || (extensionsVersion > 1 && sourceType === "extensions" /* SourceType.extensions */)
395
397
  || (plainEntrypointsVersion > 1 && sourceType === "plain" /* SourceType.plain */));
396
398
  const builtDependencyExtractionWebpackPlugin = (0, dependency_extraction_webpack_plugin_config_builder_1.makeDependencyExtractionPlugin)(externals, localAssumeGlobalizedPlauditLibraries, wantsGroupedDepData, dest.externalize);
@@ -465,7 +467,6 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
465
467
  }
466
468
  case "plain" /* SourceType.plain */:
467
469
  if (!srcIsDirectory) {
468
- //TODO: We need to filter this when in directory mode as well
469
470
  const primarySrcRoot = typeof srcRoot === 'string' ? srcRoot : srcRoot[0];
470
471
  if (!scriptExtension.test(primarySrcRoot) && !shared_1.styleExtension.test(primarySrcRoot)) {
471
472
  return undefined;
@@ -481,22 +482,25 @@ function processIndividualWebpackConfig(config, webpackConfig, sources) {
481
482
  }
482
483
  function makeBlocksWebpackConfig(config, commonConfig, webpackConfig, dest, src, srcRoot, plugins) {
483
484
  const { processingModules } = commonConfig;
484
- const blockJSONManagingPlugin = new BlockJSONManagingPlugin_1.BlockJSONManagingPlugin(config.standaloneBlocks, processingModules, dest.destination);
485
+ const blockJSONManagingPlugin = new PlainEntrypointsStyleBlockJSONPlugin_1.PlainEntrypointsStyleBlockJSONPlugin(config, dest.destination, plugins.find(p => p instanceof webpack_remove_empty_scripts_1.default));
485
486
  plugins.push(blockJSONManagingPlugin);
486
487
  if (config.processTranslationConfigs && !processingModules) {
487
- plugins.push(new WPMLConfigBuilder_1.WPMLConfigBuilderPlugin(blockJSONManagingPlugin, dest.destination));
488
+ plugins.push(new WPMLConfigBuilder_1.WPMLConfigBuilderPlugin(dest.destination));
488
489
  }
489
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);
490
491
  }
491
492
  function makeExtensionsWebpackConfig(config, commonConfig, webpackConfig, dest, src, srcRoot, plugins) {
492
- 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);
493
497
  const entry = async () => {
494
498
  const rawEntrypoints = [];
495
499
  for await (const dirent of await promises_1.default.opendir(srcRoot)) {
496
500
  if (dirent.isFile() && !dirent.name.startsWith("~")) {
497
501
  if (commonConfig.scriptExtension.test(dirent.name) || shared_1.styleExtension.test(dirent.name)) {
498
502
  const file = (0, common_config_helpers_1.joinPossiblyAbsolutePaths)(srcRoot, dirent.name);
499
- 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 } }]);
500
504
  }
501
505
  }
502
506
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.61.2",
3
+ "version": "2.62.1",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "files": [
6
6
  "/build"