@lwc/babel-plugin-component 9.0.4-alpha.0 → 9.0.4-alpha.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.
- package/README.md +2 -2
- package/dist/constants.d.ts +2 -1
- package/dist/dedupe-imports.d.ts +4 -0
- package/dist/{index.cjs → index.cjs.js} +128 -79
- package/dist/index.js +125 -76
- package/dist/private-method-transform.d.ts +2 -5
- package/dist/types.d.ts +1 -0
- package/package.json +4 -9
package/README.md
CHANGED
|
@@ -21,8 +21,8 @@ This babel plugin does the following transform:
|
|
|
21
21
|
## Usage
|
|
22
22
|
|
|
23
23
|
```js
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const babel = require('@babel/core');
|
|
25
|
+
const lwcPlugin = require('@lwc/babel-plugin-component');
|
|
26
26
|
|
|
27
27
|
const source = `
|
|
28
28
|
import { LightningElement } from 'lwc';
|
package/dist/constants.d.ts
CHANGED
|
@@ -25,7 +25,8 @@ declare const API_VERSION_KEY = "apiVersion";
|
|
|
25
25
|
declare const COMPONENT_CLASS_ID = "__lwc_component_class_internal";
|
|
26
26
|
declare const PRIVATE_METHOD_PREFIX = "__lwc_component_class_internal_private_";
|
|
27
27
|
declare const PRIVATE_METHOD_METADATA_KEY = "__lwcTransformedPrivateMethods";
|
|
28
|
+
declare const ENABLE_PRIVATE_METHODS_KEY = "enablePrivateMethods";
|
|
28
29
|
declare const SYNTHETIC_ELEMENT_INTERNALS_KEY = "enableSyntheticElementInternals";
|
|
29
30
|
declare const COMPONENT_FEATURE_FLAG_KEY = "componentFeatureFlag";
|
|
30
|
-
export { DECORATOR_TYPES, LWC_PACKAGE_ALIAS, LWC_PACKAGE_EXPORTS, LWC_COMPONENT_PROPERTIES, REGISTER_COMPONENT_ID, REGISTER_DECORATORS_ID, TEMPLATE_KEY, COMPONENT_NAME_KEY, API_VERSION_KEY, COMPONENT_CLASS_ID, PRIVATE_METHOD_PREFIX, PRIVATE_METHOD_METADATA_KEY, SYNTHETIC_ELEMENT_INTERNALS_KEY, COMPONENT_FEATURE_FLAG_KEY, };
|
|
31
|
+
export { DECORATOR_TYPES, LWC_PACKAGE_ALIAS, LWC_PACKAGE_EXPORTS, LWC_COMPONENT_PROPERTIES, REGISTER_COMPONENT_ID, REGISTER_DECORATORS_ID, TEMPLATE_KEY, COMPONENT_NAME_KEY, API_VERSION_KEY, COMPONENT_CLASS_ID, PRIVATE_METHOD_PREFIX, PRIVATE_METHOD_METADATA_KEY, ENABLE_PRIVATE_METHODS_KEY, SYNTHETIC_ELEMENT_INTERNALS_KEY, COMPONENT_FEATURE_FLAG_KEY, };
|
|
31
32
|
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var path = require('path');
|
|
9
9
|
var helperModuleImports = require('@babel/helper-module-imports');
|
|
10
10
|
var shared = require('@lwc/shared');
|
|
11
11
|
var errors = require('@lwc/errors');
|
|
@@ -43,6 +43,7 @@ const API_VERSION_KEY = 'apiVersion';
|
|
|
43
43
|
const COMPONENT_CLASS_ID = '__lwc_component_class_internal';
|
|
44
44
|
const PRIVATE_METHOD_PREFIX = '__lwc_component_class_internal_private_';
|
|
45
45
|
const PRIVATE_METHOD_METADATA_KEY = '__lwcTransformedPrivateMethods';
|
|
46
|
+
const ENABLE_PRIVATE_METHODS_KEY = 'enablePrivateMethods';
|
|
46
47
|
const SYNTHETIC_ELEMENT_INTERNALS_KEY = 'enableSyntheticElementInternals';
|
|
47
48
|
const COMPONENT_FEATURE_FLAG_KEY = 'componentFeatureFlag';
|
|
48
49
|
|
|
@@ -53,8 +54,8 @@ const COMPONENT_FEATURE_FLAG_KEY = 'componentFeatureFlag';
|
|
|
53
54
|
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
54
55
|
*/
|
|
55
56
|
function getBaseName(classPath) {
|
|
56
|
-
const ext =
|
|
57
|
-
return
|
|
57
|
+
const ext = path.extname(classPath);
|
|
58
|
+
return path.basename(classPath, ext);
|
|
58
59
|
}
|
|
59
60
|
function importDefaultTemplate(path, state) {
|
|
60
61
|
const { filename } = state.file.opts;
|
|
@@ -125,6 +126,9 @@ function component ({ types: t }) {
|
|
|
125
126
|
if (state.opts.enableSyntheticElementInternals === true) {
|
|
126
127
|
properties.push(t.objectProperty(t.identifier(SYNTHETIC_ELEMENT_INTERNALS_KEY), t.booleanLiteral(true)));
|
|
127
128
|
}
|
|
129
|
+
if (state.opts.enablePrivateMethods === true) {
|
|
130
|
+
properties.push(t.objectProperty(t.identifier(ENABLE_PRIVATE_METHODS_KEY), t.booleanLiteral(true)));
|
|
131
|
+
}
|
|
128
132
|
const registerComponentExpression = t.callExpression(registerComponentId, [
|
|
129
133
|
node,
|
|
130
134
|
t.objectExpression(properties),
|
|
@@ -1163,6 +1167,57 @@ function decorators({ types: t }) {
|
|
|
1163
1167
|
};
|
|
1164
1168
|
}
|
|
1165
1169
|
|
|
1170
|
+
/*
|
|
1171
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
1172
|
+
* All rights reserved.
|
|
1173
|
+
* SPDX-License-Identifier: MIT
|
|
1174
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
1175
|
+
*/
|
|
1176
|
+
function defaultImport(t, specifiers) {
|
|
1177
|
+
const defaultImport = specifiers.find((s) => t.isImportDefaultSpecifier(s));
|
|
1178
|
+
return defaultImport && defaultImport.local.name;
|
|
1179
|
+
}
|
|
1180
|
+
function dedupeImports ({ types: t }) {
|
|
1181
|
+
return function (path) {
|
|
1182
|
+
const body = path.get('body');
|
|
1183
|
+
const importStatements = body.filter((s) => s.isImportDeclaration());
|
|
1184
|
+
const visited = new Map();
|
|
1185
|
+
importStatements.forEach((importPath) => {
|
|
1186
|
+
const sourceLiteral = importPath.node.source;
|
|
1187
|
+
// If the import is of the type import * as X, just ignore it since we can't dedupe
|
|
1188
|
+
if (importPath.node.specifiers.some((_) => t.isImportNamespaceSpecifier(_))) {
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
// If we have seen the same source, we will try to dedupe it
|
|
1192
|
+
if (visited.has(sourceLiteral.value)) {
|
|
1193
|
+
const visitedImport = visited.get(sourceLiteral.value);
|
|
1194
|
+
const visitedSpecifiers = visitedImport.node.specifiers;
|
|
1195
|
+
const visitedDefaultImport = defaultImport(t, visitedSpecifiers);
|
|
1196
|
+
// We merge all the named imports unless is a default with the same name
|
|
1197
|
+
let canImportBeRemoved = true;
|
|
1198
|
+
importPath.node.specifiers.forEach((s) => {
|
|
1199
|
+
if (visitedDefaultImport && t.isImportDefaultSpecifier(s)) {
|
|
1200
|
+
if (visitedDefaultImport !== s.local.name) {
|
|
1201
|
+
canImportBeRemoved = false;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
else {
|
|
1205
|
+
visitedSpecifiers.push(s);
|
|
1206
|
+
}
|
|
1207
|
+
});
|
|
1208
|
+
if (canImportBeRemoved) {
|
|
1209
|
+
importPath.remove();
|
|
1210
|
+
}
|
|
1211
|
+
// We need to sort the imports due to a bug in babel where default must be first
|
|
1212
|
+
visitedSpecifiers.sort((a) => (t.isImportDefaultSpecifier(a) ? -1 : 1));
|
|
1213
|
+
}
|
|
1214
|
+
else {
|
|
1215
|
+
visited.set(sourceLiteral.value, importPath);
|
|
1216
|
+
}
|
|
1217
|
+
});
|
|
1218
|
+
};
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1166
1221
|
/*
|
|
1167
1222
|
* Copyright (c) 2023, salesforce.com, inc.
|
|
1168
1223
|
* All rights reserved.
|
|
@@ -1286,84 +1341,76 @@ const METHOD_KIND = 'method';
|
|
|
1286
1341
|
* private methods are converted to regular methods before decorator and class
|
|
1287
1342
|
* property processing.
|
|
1288
1343
|
*
|
|
1289
|
-
* Uses
|
|
1290
|
-
*
|
|
1291
|
-
* A direct ClassPrivateMethod visitor would replace nodes that the reverse transform
|
|
1292
|
-
* immediately converts back, creating an infinite loop. The manual traverse ensures
|
|
1293
|
-
* all forward replacements complete before the reverse visitor sees any ClassMethod.
|
|
1344
|
+
* Uses the `pre` lifecycle hook to run all transformations in a single pass
|
|
1345
|
+
* before the visitor phase, guaranteeing the traversal executes exactly once.
|
|
1294
1346
|
*/
|
|
1295
1347
|
function privateMethodTransform({ types: t, }) {
|
|
1296
1348
|
return {
|
|
1297
|
-
visitor: {
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
}, methodState);
|
|
1324
|
-
return;
|
|
1325
|
-
}
|
|
1326
|
-
const node = methodPath.node;
|
|
1327
|
-
// Reject private methods with decorators (e.g. @api, @track, @wire)
|
|
1328
|
-
if (node.decorators && node.decorators.length > 0) {
|
|
1329
|
-
handleError(methodPath, {
|
|
1330
|
-
errorInfo: errors.DecoratorErrors.DECORATOR_ON_PRIVATE_METHOD,
|
|
1331
|
-
}, methodState);
|
|
1332
|
-
return;
|
|
1333
|
-
}
|
|
1334
|
-
const privateName = key.node.id.name;
|
|
1335
|
-
const transformedName = `${PRIVATE_METHOD_PREFIX}${privateName}`;
|
|
1336
|
-
const keyReplacement = t.identifier(transformedName);
|
|
1337
|
-
// Create a new ClassMethod node to replace the ClassPrivateMethod
|
|
1338
|
-
// https://babeljs.io/docs/babel-types#classmethod
|
|
1339
|
-
const classMethod = t.classMethod(METHOD_KIND, keyReplacement, node.params, node.body, node.computed, node.static, node.generator, node.async);
|
|
1340
|
-
copyMethodMetadata(node, classMethod);
|
|
1341
|
-
// Replace the entire ClassPrivateMethod node with the new ClassMethod node
|
|
1342
|
-
// (we can't just replace the key of type PrivateName with type Identifier)
|
|
1343
|
-
methodPath.replaceWith(classMethod);
|
|
1344
|
-
transformedNames.add(transformedName);
|
|
1345
|
-
},
|
|
1346
|
-
MemberExpression(memberPath) {
|
|
1347
|
-
const property = memberPath.node.property;
|
|
1348
|
-
if (t.isPrivateName(property)) {
|
|
1349
|
-
const baseName = property.id.name;
|
|
1350
|
-
if (privateMethodBaseNames.has(baseName)) {
|
|
1351
|
-
const prefixedName = `${PRIVATE_METHOD_PREFIX}${baseName}`;
|
|
1352
|
-
memberPath
|
|
1353
|
-
.get('property')
|
|
1354
|
-
.replaceWith(t.identifier(prefixedName));
|
|
1355
|
-
}
|
|
1356
|
-
}
|
|
1357
|
-
},
|
|
1358
|
-
ClassPrivateProperty(propPath, propState) {
|
|
1359
|
-
handleError(propPath, {
|
|
1349
|
+
visitor: {},
|
|
1350
|
+
pre() {
|
|
1351
|
+
const state = this;
|
|
1352
|
+
const programPath = state.file.path;
|
|
1353
|
+
const transformedNames = new Set();
|
|
1354
|
+
// Phase 1: Collect base names of all private methods (kind: 'method')
|
|
1355
|
+
// so that Phase 2 can transform invocations even for forward references
|
|
1356
|
+
// (call site visited before the method definition).
|
|
1357
|
+
const privateMethodBaseNames = new Set();
|
|
1358
|
+
programPath.traverse({
|
|
1359
|
+
ClassPrivateMethod(methodPath) {
|
|
1360
|
+
const key = methodPath.get('key');
|
|
1361
|
+
if (key.isPrivateName() && methodPath.node.kind === METHOD_KIND) {
|
|
1362
|
+
privateMethodBaseNames.add(key.node.id.name);
|
|
1363
|
+
}
|
|
1364
|
+
},
|
|
1365
|
+
});
|
|
1366
|
+
// Phase 2: Transform definitions and invocations
|
|
1367
|
+
programPath.traverse({
|
|
1368
|
+
ClassPrivateMethod(methodPath, methodState) {
|
|
1369
|
+
const key = methodPath.get('key');
|
|
1370
|
+
if (!key.isPrivateName()) {
|
|
1371
|
+
return;
|
|
1372
|
+
}
|
|
1373
|
+
if (methodPath.node.kind !== METHOD_KIND) {
|
|
1374
|
+
handleError(methodPath, {
|
|
1360
1375
|
errorInfo: errors.DecoratorErrors.UNSUPPORTED_PRIVATE_MEMBER,
|
|
1361
|
-
messageArgs: ['
|
|
1362
|
-
},
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1376
|
+
messageArgs: ['accessor methods'],
|
|
1377
|
+
}, methodState);
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
const node = methodPath.node;
|
|
1381
|
+
if (node.decorators && node.decorators.length > 0) {
|
|
1382
|
+
handleError(methodPath, {
|
|
1383
|
+
errorInfo: errors.DecoratorErrors.DECORATOR_ON_PRIVATE_METHOD,
|
|
1384
|
+
}, methodState);
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
const privateName = key.node.id.name;
|
|
1388
|
+
const transformedName = `${PRIVATE_METHOD_PREFIX}${privateName}`;
|
|
1389
|
+
const keyReplacement = t.identifier(transformedName);
|
|
1390
|
+
const classMethod = t.classMethod(METHOD_KIND, keyReplacement, node.params, node.body, node.computed, node.static, node.generator, node.async);
|
|
1391
|
+
copyMethodMetadata(node, classMethod);
|
|
1392
|
+
methodPath.replaceWith(classMethod);
|
|
1393
|
+
transformedNames.add(transformedName);
|
|
1394
|
+
},
|
|
1395
|
+
PrivateName(privatePath) {
|
|
1396
|
+
const baseName = privatePath.node.id.name;
|
|
1397
|
+
if (!privateMethodBaseNames.has(baseName)) {
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
const parentPath = privatePath.parentPath;
|
|
1401
|
+
if (parentPath.isMemberExpression()) {
|
|
1402
|
+
const prefixedName = `${PRIVATE_METHOD_PREFIX}${baseName}`;
|
|
1403
|
+
privatePath.replaceWith(t.identifier(prefixedName));
|
|
1404
|
+
}
|
|
1405
|
+
},
|
|
1406
|
+
ClassPrivateProperty(propPath, propState) {
|
|
1407
|
+
handleError(propPath, {
|
|
1408
|
+
errorInfo: errors.DecoratorErrors.UNSUPPORTED_PRIVATE_MEMBER,
|
|
1409
|
+
messageArgs: ['fields'],
|
|
1410
|
+
}, propState);
|
|
1411
|
+
},
|
|
1412
|
+
}, state);
|
|
1413
|
+
state.file.metadata[PRIVATE_METHOD_METADATA_KEY] = transformedNames;
|
|
1367
1414
|
},
|
|
1368
1415
|
};
|
|
1369
1416
|
}
|
|
@@ -1499,6 +1546,8 @@ function LwcClassTransform(api) {
|
|
|
1499
1546
|
exit(path) {
|
|
1500
1547
|
const engineImportSpecifiers = getEngineImportSpecifiers(path);
|
|
1501
1548
|
removeImportedDecoratorSpecifiers(engineImportSpecifiers);
|
|
1549
|
+
// Will eventually be removed to eliminate unnecessary complexity. Rollup already does this for us.
|
|
1550
|
+
dedupeImports(api)(path);
|
|
1502
1551
|
},
|
|
1503
1552
|
},
|
|
1504
1553
|
Import: transformDynamicImports,
|
|
@@ -1512,5 +1561,5 @@ function LwcClassTransform(api) {
|
|
|
1512
1561
|
exports.LwcPrivateMethodTransform = privateMethodTransform;
|
|
1513
1562
|
exports.LwcReversePrivateMethodTransform = reversePrivateMethodTransform;
|
|
1514
1563
|
exports.default = LwcClassTransform;
|
|
1515
|
-
/** version: 9.0.4-alpha.
|
|
1516
|
-
//# sourceMappingURL=index.cjs.map
|
|
1564
|
+
/** version: 9.0.4-alpha.2 */
|
|
1565
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Copyright (c) 2026 Salesforce, Inc.
|
|
3
3
|
*/
|
|
4
|
-
import { extname, basename } from '
|
|
4
|
+
import { extname, basename } from 'path';
|
|
5
5
|
import { addNamed, addDefault } from '@babel/helper-module-imports';
|
|
6
6
|
import { getAPIVersionFromNumber, generateCustomElementTagName, DISALLOWED_PROP_SET, AMBIGUOUS_PROP_SET, isAPIFeatureEnabled, LWC_VERSION_COMMENT } from '@lwc/shared';
|
|
7
7
|
import { generateCompilerDiagnostic, DiagnosticLevel, generateErrorMessage, DecoratorErrors, CompilerMetrics, LWCClassErrors } from '@lwc/errors';
|
|
@@ -39,6 +39,7 @@ const API_VERSION_KEY = 'apiVersion';
|
|
|
39
39
|
const COMPONENT_CLASS_ID = '__lwc_component_class_internal';
|
|
40
40
|
const PRIVATE_METHOD_PREFIX = '__lwc_component_class_internal_private_';
|
|
41
41
|
const PRIVATE_METHOD_METADATA_KEY = '__lwcTransformedPrivateMethods';
|
|
42
|
+
const ENABLE_PRIVATE_METHODS_KEY = 'enablePrivateMethods';
|
|
42
43
|
const SYNTHETIC_ELEMENT_INTERNALS_KEY = 'enableSyntheticElementInternals';
|
|
43
44
|
const COMPONENT_FEATURE_FLAG_KEY = 'componentFeatureFlag';
|
|
44
45
|
|
|
@@ -121,6 +122,9 @@ function component ({ types: t }) {
|
|
|
121
122
|
if (state.opts.enableSyntheticElementInternals === true) {
|
|
122
123
|
properties.push(t.objectProperty(t.identifier(SYNTHETIC_ELEMENT_INTERNALS_KEY), t.booleanLiteral(true)));
|
|
123
124
|
}
|
|
125
|
+
if (state.opts.enablePrivateMethods === true) {
|
|
126
|
+
properties.push(t.objectProperty(t.identifier(ENABLE_PRIVATE_METHODS_KEY), t.booleanLiteral(true)));
|
|
127
|
+
}
|
|
124
128
|
const registerComponentExpression = t.callExpression(registerComponentId, [
|
|
125
129
|
node,
|
|
126
130
|
t.objectExpression(properties),
|
|
@@ -1159,6 +1163,57 @@ function decorators({ types: t }) {
|
|
|
1159
1163
|
};
|
|
1160
1164
|
}
|
|
1161
1165
|
|
|
1166
|
+
/*
|
|
1167
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
1168
|
+
* All rights reserved.
|
|
1169
|
+
* SPDX-License-Identifier: MIT
|
|
1170
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
1171
|
+
*/
|
|
1172
|
+
function defaultImport(t, specifiers) {
|
|
1173
|
+
const defaultImport = specifiers.find((s) => t.isImportDefaultSpecifier(s));
|
|
1174
|
+
return defaultImport && defaultImport.local.name;
|
|
1175
|
+
}
|
|
1176
|
+
function dedupeImports ({ types: t }) {
|
|
1177
|
+
return function (path) {
|
|
1178
|
+
const body = path.get('body');
|
|
1179
|
+
const importStatements = body.filter((s) => s.isImportDeclaration());
|
|
1180
|
+
const visited = new Map();
|
|
1181
|
+
importStatements.forEach((importPath) => {
|
|
1182
|
+
const sourceLiteral = importPath.node.source;
|
|
1183
|
+
// If the import is of the type import * as X, just ignore it since we can't dedupe
|
|
1184
|
+
if (importPath.node.specifiers.some((_) => t.isImportNamespaceSpecifier(_))) {
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
// If we have seen the same source, we will try to dedupe it
|
|
1188
|
+
if (visited.has(sourceLiteral.value)) {
|
|
1189
|
+
const visitedImport = visited.get(sourceLiteral.value);
|
|
1190
|
+
const visitedSpecifiers = visitedImport.node.specifiers;
|
|
1191
|
+
const visitedDefaultImport = defaultImport(t, visitedSpecifiers);
|
|
1192
|
+
// We merge all the named imports unless is a default with the same name
|
|
1193
|
+
let canImportBeRemoved = true;
|
|
1194
|
+
importPath.node.specifiers.forEach((s) => {
|
|
1195
|
+
if (visitedDefaultImport && t.isImportDefaultSpecifier(s)) {
|
|
1196
|
+
if (visitedDefaultImport !== s.local.name) {
|
|
1197
|
+
canImportBeRemoved = false;
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
else {
|
|
1201
|
+
visitedSpecifiers.push(s);
|
|
1202
|
+
}
|
|
1203
|
+
});
|
|
1204
|
+
if (canImportBeRemoved) {
|
|
1205
|
+
importPath.remove();
|
|
1206
|
+
}
|
|
1207
|
+
// We need to sort the imports due to a bug in babel where default must be first
|
|
1208
|
+
visitedSpecifiers.sort((a) => (t.isImportDefaultSpecifier(a) ? -1 : 1));
|
|
1209
|
+
}
|
|
1210
|
+
else {
|
|
1211
|
+
visited.set(sourceLiteral.value, importPath);
|
|
1212
|
+
}
|
|
1213
|
+
});
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1162
1217
|
/*
|
|
1163
1218
|
* Copyright (c) 2023, salesforce.com, inc.
|
|
1164
1219
|
* All rights reserved.
|
|
@@ -1282,84 +1337,76 @@ const METHOD_KIND = 'method';
|
|
|
1282
1337
|
* private methods are converted to regular methods before decorator and class
|
|
1283
1338
|
* property processing.
|
|
1284
1339
|
*
|
|
1285
|
-
* Uses
|
|
1286
|
-
*
|
|
1287
|
-
* A direct ClassPrivateMethod visitor would replace nodes that the reverse transform
|
|
1288
|
-
* immediately converts back, creating an infinite loop. The manual traverse ensures
|
|
1289
|
-
* all forward replacements complete before the reverse visitor sees any ClassMethod.
|
|
1340
|
+
* Uses the `pre` lifecycle hook to run all transformations in a single pass
|
|
1341
|
+
* before the visitor phase, guaranteeing the traversal executes exactly once.
|
|
1290
1342
|
*/
|
|
1291
1343
|
function privateMethodTransform({ types: t, }) {
|
|
1292
1344
|
return {
|
|
1293
|
-
visitor: {
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
}, methodState);
|
|
1320
|
-
return;
|
|
1321
|
-
}
|
|
1322
|
-
const node = methodPath.node;
|
|
1323
|
-
// Reject private methods with decorators (e.g. @api, @track, @wire)
|
|
1324
|
-
if (node.decorators && node.decorators.length > 0) {
|
|
1325
|
-
handleError(methodPath, {
|
|
1326
|
-
errorInfo: DecoratorErrors.DECORATOR_ON_PRIVATE_METHOD,
|
|
1327
|
-
}, methodState);
|
|
1328
|
-
return;
|
|
1329
|
-
}
|
|
1330
|
-
const privateName = key.node.id.name;
|
|
1331
|
-
const transformedName = `${PRIVATE_METHOD_PREFIX}${privateName}`;
|
|
1332
|
-
const keyReplacement = t.identifier(transformedName);
|
|
1333
|
-
// Create a new ClassMethod node to replace the ClassPrivateMethod
|
|
1334
|
-
// https://babeljs.io/docs/babel-types#classmethod
|
|
1335
|
-
const classMethod = t.classMethod(METHOD_KIND, keyReplacement, node.params, node.body, node.computed, node.static, node.generator, node.async);
|
|
1336
|
-
copyMethodMetadata(node, classMethod);
|
|
1337
|
-
// Replace the entire ClassPrivateMethod node with the new ClassMethod node
|
|
1338
|
-
// (we can't just replace the key of type PrivateName with type Identifier)
|
|
1339
|
-
methodPath.replaceWith(classMethod);
|
|
1340
|
-
transformedNames.add(transformedName);
|
|
1341
|
-
},
|
|
1342
|
-
MemberExpression(memberPath) {
|
|
1343
|
-
const property = memberPath.node.property;
|
|
1344
|
-
if (t.isPrivateName(property)) {
|
|
1345
|
-
const baseName = property.id.name;
|
|
1346
|
-
if (privateMethodBaseNames.has(baseName)) {
|
|
1347
|
-
const prefixedName = `${PRIVATE_METHOD_PREFIX}${baseName}`;
|
|
1348
|
-
memberPath
|
|
1349
|
-
.get('property')
|
|
1350
|
-
.replaceWith(t.identifier(prefixedName));
|
|
1351
|
-
}
|
|
1352
|
-
}
|
|
1353
|
-
},
|
|
1354
|
-
ClassPrivateProperty(propPath, propState) {
|
|
1355
|
-
handleError(propPath, {
|
|
1345
|
+
visitor: {},
|
|
1346
|
+
pre() {
|
|
1347
|
+
const state = this;
|
|
1348
|
+
const programPath = state.file.path;
|
|
1349
|
+
const transformedNames = new Set();
|
|
1350
|
+
// Phase 1: Collect base names of all private methods (kind: 'method')
|
|
1351
|
+
// so that Phase 2 can transform invocations even for forward references
|
|
1352
|
+
// (call site visited before the method definition).
|
|
1353
|
+
const privateMethodBaseNames = new Set();
|
|
1354
|
+
programPath.traverse({
|
|
1355
|
+
ClassPrivateMethod(methodPath) {
|
|
1356
|
+
const key = methodPath.get('key');
|
|
1357
|
+
if (key.isPrivateName() && methodPath.node.kind === METHOD_KIND) {
|
|
1358
|
+
privateMethodBaseNames.add(key.node.id.name);
|
|
1359
|
+
}
|
|
1360
|
+
},
|
|
1361
|
+
});
|
|
1362
|
+
// Phase 2: Transform definitions and invocations
|
|
1363
|
+
programPath.traverse({
|
|
1364
|
+
ClassPrivateMethod(methodPath, methodState) {
|
|
1365
|
+
const key = methodPath.get('key');
|
|
1366
|
+
if (!key.isPrivateName()) {
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
if (methodPath.node.kind !== METHOD_KIND) {
|
|
1370
|
+
handleError(methodPath, {
|
|
1356
1371
|
errorInfo: DecoratorErrors.UNSUPPORTED_PRIVATE_MEMBER,
|
|
1357
|
-
messageArgs: ['
|
|
1358
|
-
},
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1372
|
+
messageArgs: ['accessor methods'],
|
|
1373
|
+
}, methodState);
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1376
|
+
const node = methodPath.node;
|
|
1377
|
+
if (node.decorators && node.decorators.length > 0) {
|
|
1378
|
+
handleError(methodPath, {
|
|
1379
|
+
errorInfo: DecoratorErrors.DECORATOR_ON_PRIVATE_METHOD,
|
|
1380
|
+
}, methodState);
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
const privateName = key.node.id.name;
|
|
1384
|
+
const transformedName = `${PRIVATE_METHOD_PREFIX}${privateName}`;
|
|
1385
|
+
const keyReplacement = t.identifier(transformedName);
|
|
1386
|
+
const classMethod = t.classMethod(METHOD_KIND, keyReplacement, node.params, node.body, node.computed, node.static, node.generator, node.async);
|
|
1387
|
+
copyMethodMetadata(node, classMethod);
|
|
1388
|
+
methodPath.replaceWith(classMethod);
|
|
1389
|
+
transformedNames.add(transformedName);
|
|
1390
|
+
},
|
|
1391
|
+
PrivateName(privatePath) {
|
|
1392
|
+
const baseName = privatePath.node.id.name;
|
|
1393
|
+
if (!privateMethodBaseNames.has(baseName)) {
|
|
1394
|
+
return;
|
|
1395
|
+
}
|
|
1396
|
+
const parentPath = privatePath.parentPath;
|
|
1397
|
+
if (parentPath.isMemberExpression()) {
|
|
1398
|
+
const prefixedName = `${PRIVATE_METHOD_PREFIX}${baseName}`;
|
|
1399
|
+
privatePath.replaceWith(t.identifier(prefixedName));
|
|
1400
|
+
}
|
|
1401
|
+
},
|
|
1402
|
+
ClassPrivateProperty(propPath, propState) {
|
|
1403
|
+
handleError(propPath, {
|
|
1404
|
+
errorInfo: DecoratorErrors.UNSUPPORTED_PRIVATE_MEMBER,
|
|
1405
|
+
messageArgs: ['fields'],
|
|
1406
|
+
}, propState);
|
|
1407
|
+
},
|
|
1408
|
+
}, state);
|
|
1409
|
+
state.file.metadata[PRIVATE_METHOD_METADATA_KEY] = transformedNames;
|
|
1363
1410
|
},
|
|
1364
1411
|
};
|
|
1365
1412
|
}
|
|
@@ -1495,6 +1542,8 @@ function LwcClassTransform(api) {
|
|
|
1495
1542
|
exit(path) {
|
|
1496
1543
|
const engineImportSpecifiers = getEngineImportSpecifiers(path);
|
|
1497
1544
|
removeImportedDecoratorSpecifiers(engineImportSpecifiers);
|
|
1545
|
+
// Will eventually be removed to eliminate unnecessary complexity. Rollup already does this for us.
|
|
1546
|
+
dedupeImports(api)(path);
|
|
1498
1547
|
},
|
|
1499
1548
|
},
|
|
1500
1549
|
Import: transformDynamicImports,
|
|
@@ -1506,5 +1555,5 @@ function LwcClassTransform(api) {
|
|
|
1506
1555
|
}
|
|
1507
1556
|
|
|
1508
1557
|
export { privateMethodTransform as LwcPrivateMethodTransform, reversePrivateMethodTransform as LwcReversePrivateMethodTransform, LwcClassTransform as default };
|
|
1509
|
-
/** version: 9.0.4-alpha.
|
|
1558
|
+
/** version: 9.0.4-alpha.2 */
|
|
1510
1559
|
//# sourceMappingURL=index.js.map
|
|
@@ -8,11 +8,8 @@ import type { PluginObj } from '@babel/core';
|
|
|
8
8
|
* private methods are converted to regular methods before decorator and class
|
|
9
9
|
* property processing.
|
|
10
10
|
*
|
|
11
|
-
* Uses
|
|
12
|
-
*
|
|
13
|
-
* A direct ClassPrivateMethod visitor would replace nodes that the reverse transform
|
|
14
|
-
* immediately converts back, creating an infinite loop. The manual traverse ensures
|
|
15
|
-
* all forward replacements complete before the reverse visitor sees any ClassMethod.
|
|
11
|
+
* Uses the `pre` lifecycle hook to run all transformations in a single pass
|
|
12
|
+
* before the visitor phase, guaranteeing the traversal executes exactly once.
|
|
16
13
|
*/
|
|
17
14
|
export default function privateMethodTransform({ types: t, }: BabelAPI): PluginObj<LwcBabelPluginPass>;
|
|
18
15
|
//# sourceMappingURL=private-method-transform.d.ts.map
|
package/dist/types.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export interface LwcBabelPluginOptions {
|
|
|
14
14
|
instrumentation?: InstrumentationObject;
|
|
15
15
|
apiVersion?: number;
|
|
16
16
|
enableSyntheticElementInternals?: boolean;
|
|
17
|
+
enablePrivateMethods?: boolean;
|
|
17
18
|
componentFeatureFlagModulePath?: string;
|
|
18
19
|
}
|
|
19
20
|
export interface LwcBabelPluginPass extends PluginPass {
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"You can safely modify dependencies, devDependencies, keywords, etc., but other props will be overwritten."
|
|
5
5
|
],
|
|
6
6
|
"name": "@lwc/babel-plugin-component",
|
|
7
|
-
"version": "9.0.4-alpha.
|
|
7
|
+
"version": "9.0.4-alpha.2",
|
|
8
8
|
"description": "Babel plugin to transform a LWC module",
|
|
9
9
|
"keywords": [
|
|
10
10
|
"lwc"
|
|
@@ -19,22 +19,17 @@
|
|
|
19
19
|
"url": "https://github.com/salesforce/lwc/issues"
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
|
-
"type": "module",
|
|
23
22
|
"publishConfig": {
|
|
24
23
|
"access": "public"
|
|
25
24
|
},
|
|
26
|
-
"engines": {
|
|
27
|
-
"node": ">=16.6.0"
|
|
28
|
-
},
|
|
29
25
|
"volta": {
|
|
30
26
|
"extends": "../../../package.json"
|
|
31
27
|
},
|
|
32
|
-
"main": "dist/index.js",
|
|
28
|
+
"main": "dist/index.cjs.js",
|
|
33
29
|
"module": "dist/index.js",
|
|
34
30
|
"types": "dist/index.d.ts",
|
|
35
31
|
"files": [
|
|
36
32
|
"dist/**/*.js",
|
|
37
|
-
"dist/**/*.cjs",
|
|
38
33
|
"dist/**/*.d.ts"
|
|
39
34
|
],
|
|
40
35
|
"scripts": {
|
|
@@ -52,8 +47,8 @@
|
|
|
52
47
|
},
|
|
53
48
|
"dependencies": {
|
|
54
49
|
"@babel/helper-module-imports": "7.28.6",
|
|
55
|
-
"@lwc/errors": "9.0.4-alpha.
|
|
56
|
-
"@lwc/shared": "9.0.4-alpha.
|
|
50
|
+
"@lwc/errors": "9.0.4-alpha.2",
|
|
51
|
+
"@lwc/shared": "9.0.4-alpha.2",
|
|
57
52
|
"line-column": "~1.0.2"
|
|
58
53
|
},
|
|
59
54
|
"devDependencies": {
|