@plumeria/turbopack-loader 15.1.3 → 16.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +246 -44
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -125,6 +125,7 @@ async function loader(source) {
|
|
|
125
125
|
const createThemeImportMap = {};
|
|
126
126
|
const createStaticImportMap = {};
|
|
127
127
|
const plumeriaAliases = {};
|
|
128
|
+
const localImports = {};
|
|
128
129
|
(0, utils_1.traverse)(ast, {
|
|
129
130
|
ImportDeclaration({ node }) {
|
|
130
131
|
const sourcePath = node.source.value;
|
|
@@ -153,6 +154,7 @@ async function loader(source) {
|
|
|
153
154
|
: specifier.local.value;
|
|
154
155
|
const localName = specifier.local.value;
|
|
155
156
|
const uniqueKey = `${actualPath}-${importedName}`;
|
|
157
|
+
localImports[localName] = { actualPath, importedName };
|
|
156
158
|
if (scannedTables.staticTable[uniqueKey]) {
|
|
157
159
|
importMap[localName] = scannedTables.staticTable[uniqueKey];
|
|
158
160
|
}
|
|
@@ -267,6 +269,19 @@ async function loader(source) {
|
|
|
267
269
|
mergedCreateStaticHashTable[key] = createStaticImportMap[key];
|
|
268
270
|
}
|
|
269
271
|
const localCreateStyles = {};
|
|
272
|
+
const localStyleAliases = {};
|
|
273
|
+
const checkStyleAliasAssignment = (decl) => {
|
|
274
|
+
if (!utils_1.t.isIdentifier(decl.id) || !decl.init)
|
|
275
|
+
return;
|
|
276
|
+
const init = decl.init;
|
|
277
|
+
if (utils_1.t.isMemberExpression(init) && utils_1.t.isIdentifier(init.object)) {
|
|
278
|
+
const objName = init.object.value;
|
|
279
|
+
if (localCreateStyles[objName] !== undefined ||
|
|
280
|
+
mergedCreateTable[objName] !== undefined) {
|
|
281
|
+
localStyleAliases[decl.id.value] = init;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
};
|
|
270
285
|
const replacements = [];
|
|
271
286
|
const processedDecls = new Set();
|
|
272
287
|
const idSpans = new Set();
|
|
@@ -382,42 +397,6 @@ async function loader(source) {
|
|
|
382
397
|
}
|
|
383
398
|
}
|
|
384
399
|
}
|
|
385
|
-
else if (propName === 'variants' &&
|
|
386
|
-
utils_1.t.isObjectExpression(init.arguments[0].expression)) {
|
|
387
|
-
if (utils_1.t.isIdentifier(node.id)) {
|
|
388
|
-
idSpans.add(node.id.span.start);
|
|
389
|
-
}
|
|
390
|
-
const obj = (0, utils_1.objectExpressionToObject)(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable, (name) => {
|
|
391
|
-
if (localCreateStyles[name]) {
|
|
392
|
-
return localCreateStyles[name].obj;
|
|
393
|
-
}
|
|
394
|
-
if (mergedCreateTable[name]) {
|
|
395
|
-
const hash = mergedCreateTable[name];
|
|
396
|
-
if (scannedTables.createObjectTable[hash]) {
|
|
397
|
-
return scannedTables.createObjectTable[hash];
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
return undefined;
|
|
401
|
-
});
|
|
402
|
-
const { hashMap } = (0, utils_1.processVariants)(obj);
|
|
403
|
-
if (utils_1.t.isIdentifier(node.id)) {
|
|
404
|
-
localCreateStyles[node.id.value] = {
|
|
405
|
-
name: node.id.value,
|
|
406
|
-
type: 'variant',
|
|
407
|
-
obj,
|
|
408
|
-
hashMap,
|
|
409
|
-
isExported,
|
|
410
|
-
initSpan: {
|
|
411
|
-
start: init.span.start - baseByteOffset,
|
|
412
|
-
end: init.span.end - baseByteOffset,
|
|
413
|
-
},
|
|
414
|
-
declSpan: {
|
|
415
|
-
start: declSpan.start - baseByteOffset,
|
|
416
|
-
end: declSpan.end - baseByteOffset,
|
|
417
|
-
},
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
400
|
else if (propName === 'createTheme' &&
|
|
422
401
|
init.arguments.length >= 2 &&
|
|
423
402
|
utils_1.t.isObjectExpression(init.arguments[1].expression)) {
|
|
@@ -544,6 +523,7 @@ async function loader(source) {
|
|
|
544
523
|
node.declaration.declarations.forEach((decl) => {
|
|
545
524
|
checkVariantAssignment(decl);
|
|
546
525
|
registerStyle(decl, node.span, true);
|
|
526
|
+
checkStyleAliasAssignment(decl);
|
|
547
527
|
});
|
|
548
528
|
}
|
|
549
529
|
},
|
|
@@ -553,6 +533,7 @@ async function loader(source) {
|
|
|
553
533
|
node.declarations.forEach((decl) => {
|
|
554
534
|
checkVariantAssignment(decl);
|
|
555
535
|
registerStyle(decl, node.span, false);
|
|
536
|
+
checkStyleAliasAssignment(decl);
|
|
556
537
|
});
|
|
557
538
|
},
|
|
558
539
|
CallExpression({ node }) {
|
|
@@ -639,6 +620,42 @@ async function loader(source) {
|
|
|
639
620
|
},
|
|
640
621
|
});
|
|
641
622
|
const jsxOpeningElementMap = new Map();
|
|
623
|
+
const componentParamNames = new Set();
|
|
624
|
+
for (const node of ast.body) {
|
|
625
|
+
let declarations = [];
|
|
626
|
+
if (utils_1.t.isVariableDeclaration(node)) {
|
|
627
|
+
declarations = node.declarations;
|
|
628
|
+
}
|
|
629
|
+
else if (utils_1.t.isExportDeclaration(node) &&
|
|
630
|
+
utils_1.t.isVariableDeclaration(node.declaration)) {
|
|
631
|
+
declarations = node.declaration.declarations;
|
|
632
|
+
}
|
|
633
|
+
for (const decl of declarations) {
|
|
634
|
+
if (!utils_1.t.isIdentifier(decl.id) || !decl.init)
|
|
635
|
+
continue;
|
|
636
|
+
const init = decl.init;
|
|
637
|
+
if (init.type !== 'ArrowFunctionExpression' &&
|
|
638
|
+
init.type !== 'FunctionExpression')
|
|
639
|
+
continue;
|
|
640
|
+
if (init.params.length > 0) {
|
|
641
|
+
const p = init.params[0];
|
|
642
|
+
if (utils_1.t.isIdentifier(p)) {
|
|
643
|
+
componentParamNames.add(p.value);
|
|
644
|
+
}
|
|
645
|
+
else if (p.pat && utils_1.t.isIdentifier(p.pat)) {
|
|
646
|
+
componentParamNames.add(p.pat.value);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
const excludeSubtreeSpans = (n) => {
|
|
652
|
+
if (!n || typeof n !== 'object')
|
|
653
|
+
return;
|
|
654
|
+
if (n.span) {
|
|
655
|
+
excludedSpans.add(n.span.start);
|
|
656
|
+
}
|
|
657
|
+
Object.values(n).forEach((val) => excludeSubtreeSpans(val));
|
|
658
|
+
};
|
|
642
659
|
const getSource = (node) => {
|
|
643
660
|
const start = node.span.start - baseByteOffset;
|
|
644
661
|
const end = node.span.end - baseByteOffset;
|
|
@@ -652,7 +669,7 @@ async function loader(source) {
|
|
|
652
669
|
utils_1.t.isIdentifier(expr.object) &&
|
|
653
670
|
(utils_1.t.isIdentifier(expr.property) || expr.property.type === 'Computed')) {
|
|
654
671
|
if (expr.property.type === 'Computed')
|
|
655
|
-
return
|
|
672
|
+
return null;
|
|
656
673
|
const varName = expr.object.value;
|
|
657
674
|
const propName = expr.property.value;
|
|
658
675
|
const styleInfo = localCreateStyles[varName];
|
|
@@ -689,6 +706,12 @@ async function loader(source) {
|
|
|
689
706
|
return null;
|
|
690
707
|
};
|
|
691
708
|
const buildClassParts = (args, dynamicClassParts = [], existingClass = '') => {
|
|
709
|
+
args.forEach((arg) => {
|
|
710
|
+
const expr = arg.expression;
|
|
711
|
+
if (utils_1.t.isIdentifier(expr) && localStyleAliases[expr.value]) {
|
|
712
|
+
arg.expression = localStyleAliases[expr.value];
|
|
713
|
+
}
|
|
714
|
+
});
|
|
692
715
|
const conditionals = [];
|
|
693
716
|
let groupIdCounter = 0;
|
|
694
717
|
let baseStyle = {};
|
|
@@ -711,6 +734,17 @@ async function loader(source) {
|
|
|
711
734
|
}
|
|
712
735
|
return null;
|
|
713
736
|
};
|
|
737
|
+
const resolveCreateObject = (varName) => {
|
|
738
|
+
const localStyle = localCreateStyles[varName];
|
|
739
|
+
if (localStyle?.type === 'create')
|
|
740
|
+
return localStyle.obj;
|
|
741
|
+
let hash = scannedTables.createHashTable[`${resourcePath}-${varName}`];
|
|
742
|
+
if (!hash)
|
|
743
|
+
hash = mergedCreateTable[varName];
|
|
744
|
+
if (hash && scannedTables.createObjectTable[hash])
|
|
745
|
+
return scannedTables.createObjectTable[hash];
|
|
746
|
+
return null;
|
|
747
|
+
};
|
|
714
748
|
const collectConditions = (node, currentTestStrings = []) => {
|
|
715
749
|
const staticStyle = resolveStyleObject(node);
|
|
716
750
|
if (staticStyle) {
|
|
@@ -768,6 +802,49 @@ async function loader(source) {
|
|
|
768
802
|
};
|
|
769
803
|
for (const arg of args) {
|
|
770
804
|
const expr = arg.expression;
|
|
805
|
+
if (utils_1.t.isIdentifier(expr) ||
|
|
806
|
+
(utils_1.t.isMemberExpression(expr) &&
|
|
807
|
+
utils_1.t.isIdentifier(expr.object) &&
|
|
808
|
+
componentParamNames.has(expr.object.value) &&
|
|
809
|
+
utils_1.t.isIdentifier(expr.property))) {
|
|
810
|
+
const varName = utils_1.t.isIdentifier(expr)
|
|
811
|
+
? expr.value
|
|
812
|
+
: expr.property.value;
|
|
813
|
+
const testLHS = utils_1.t.isIdentifier(expr) ? varName : getSource(expr);
|
|
814
|
+
let propPossibilities;
|
|
815
|
+
for (const key of Object.keys(scannedTables.componentPropsTable || {})) {
|
|
816
|
+
if (key.startsWith(`${resourcePath}-`)) {
|
|
817
|
+
if (scannedTables.componentPropsTable?.[key]?.[varName]) {
|
|
818
|
+
propPossibilities =
|
|
819
|
+
scannedTables.componentPropsTable?.[key]?.[varName];
|
|
820
|
+
break;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
if (propPossibilities && propPossibilities.length > 0) {
|
|
825
|
+
const currentGroupId = ++groupIdCounter;
|
|
826
|
+
const uniqueEntries = [];
|
|
827
|
+
propPossibilities.forEach((entry) => {
|
|
828
|
+
if (!uniqueEntries.some((x) => x.index === entry.index)) {
|
|
829
|
+
uniqueEntries.push(entry);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
uniqueEntries.forEach((entry) => {
|
|
833
|
+
conditionals.push({
|
|
834
|
+
test: expr,
|
|
835
|
+
testLHS,
|
|
836
|
+
testString: `${testLHS} === ${entry.index}`,
|
|
837
|
+
truthy: entry.styleObj,
|
|
838
|
+
falsy: {},
|
|
839
|
+
groupId: currentGroupId,
|
|
840
|
+
groupName: undefined,
|
|
841
|
+
valueName: String(entry.index),
|
|
842
|
+
varName,
|
|
843
|
+
});
|
|
844
|
+
});
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
771
848
|
if (utils_1.t.isCallExpression(expr) && utils_1.t.isIdentifier(expr.callee)) {
|
|
772
849
|
const varName = expr.callee.value;
|
|
773
850
|
const uniqueKey = `${resourcePath}-${varName}`;
|
|
@@ -899,6 +976,31 @@ async function loader(source) {
|
|
|
899
976
|
continue;
|
|
900
977
|
}
|
|
901
978
|
}
|
|
979
|
+
if (utils_1.t.isMemberExpression(expr) &&
|
|
980
|
+
utils_1.t.isIdentifier(expr.object) &&
|
|
981
|
+
expr.property.type === 'Computed') {
|
|
982
|
+
const varName = expr.object.value;
|
|
983
|
+
const styleObj = resolveCreateObject(varName);
|
|
984
|
+
if (styleObj) {
|
|
985
|
+
const dynExpr = expr.property.expression;
|
|
986
|
+
const dynSource = getSource(dynExpr);
|
|
987
|
+
const currentGroupId = ++groupIdCounter;
|
|
988
|
+
Object.entries(styleObj).forEach(([optionName, style]) => {
|
|
989
|
+
conditionals.push({
|
|
990
|
+
test: dynExpr,
|
|
991
|
+
testLHS: dynSource,
|
|
992
|
+
testString: `${dynSource} === '${optionName}'`,
|
|
993
|
+
truthy: style,
|
|
994
|
+
falsy: {},
|
|
995
|
+
groupId: currentGroupId,
|
|
996
|
+
groupName: undefined,
|
|
997
|
+
valueName: optionName,
|
|
998
|
+
varName,
|
|
999
|
+
});
|
|
1000
|
+
});
|
|
1001
|
+
continue;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
902
1004
|
const handled = collectConditions(expr);
|
|
903
1005
|
if (handled)
|
|
904
1006
|
continue;
|
|
@@ -1120,13 +1222,56 @@ async function loader(source) {
|
|
|
1120
1222
|
};
|
|
1121
1223
|
(0, utils_1.traverse)(ast, {
|
|
1122
1224
|
JSXOpeningElement({ node }) {
|
|
1123
|
-
|
|
1225
|
+
let tagName = '';
|
|
1226
|
+
if (node.name.type === 'Identifier') {
|
|
1227
|
+
tagName = node.name.value;
|
|
1228
|
+
}
|
|
1229
|
+
jsxOpeningElementMap.set(node.span.start, {
|
|
1230
|
+
tagName,
|
|
1231
|
+
attributes: node.attributes,
|
|
1232
|
+
});
|
|
1124
1233
|
},
|
|
1125
1234
|
MemberExpression({ node }) {
|
|
1126
|
-
if (utils_1.t.isIdentifier(node.object) &&
|
|
1235
|
+
if (utils_1.t.isIdentifier(node.object) &&
|
|
1236
|
+
(utils_1.t.isIdentifier(node.property) || node.property.type === 'Computed')) {
|
|
1127
1237
|
const varName = node.object.value;
|
|
1128
|
-
const propName = node.property.value;
|
|
1129
1238
|
const uniqueKey = `${resourcePath}-${varName}`;
|
|
1239
|
+
if (node.property.type === 'Computed') {
|
|
1240
|
+
const dynExpr = node.property.expression;
|
|
1241
|
+
const dynSource = getSource(dynExpr);
|
|
1242
|
+
const localStyle = localCreateStyles[varName];
|
|
1243
|
+
let hashMap;
|
|
1244
|
+
if (localStyle) {
|
|
1245
|
+
hashMap = localStyle.hashMap;
|
|
1246
|
+
}
|
|
1247
|
+
else {
|
|
1248
|
+
let hash = scannedTables.createHashTable[uniqueKey];
|
|
1249
|
+
if (!hash) {
|
|
1250
|
+
hash = mergedCreateTable[varName];
|
|
1251
|
+
}
|
|
1252
|
+
if (hash) {
|
|
1253
|
+
const obj = scannedTables.createObjectTable[hash];
|
|
1254
|
+
const atomicMap = scannedTables.createAtomicMapTable[hash];
|
|
1255
|
+
if (obj && atomicMap) {
|
|
1256
|
+
hashMap = {};
|
|
1257
|
+
Object.keys(obj).forEach((key) => {
|
|
1258
|
+
if (atomicMap[key]) {
|
|
1259
|
+
hashMap[key] = atomicMap[key];
|
|
1260
|
+
}
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
if (hashMap) {
|
|
1266
|
+
replacements.push({
|
|
1267
|
+
start: node.span.start - baseByteOffset,
|
|
1268
|
+
end: node.span.end - baseByteOffset,
|
|
1269
|
+
content: `((${JSON.stringify(hashMap)})[${dynSource}] || {})`,
|
|
1270
|
+
});
|
|
1271
|
+
}
|
|
1272
|
+
return;
|
|
1273
|
+
}
|
|
1274
|
+
const propName = node.property.value;
|
|
1130
1275
|
const localStyle = localCreateStyles[varName];
|
|
1131
1276
|
if (localStyle && localStyle.type === 'create') {
|
|
1132
1277
|
const atomMap = localStyle.hashMap[propName];
|
|
@@ -1273,8 +1418,65 @@ async function loader(source) {
|
|
|
1273
1418
|
}
|
|
1274
1419
|
},
|
|
1275
1420
|
JSXAttribute({ node }) {
|
|
1276
|
-
if (node.name.type !== 'Identifier'
|
|
1421
|
+
if (node.name.type !== 'Identifier')
|
|
1277
1422
|
return;
|
|
1423
|
+
const attrName = node.name.value;
|
|
1424
|
+
if (attrName !== 'styleName') {
|
|
1425
|
+
let parentTagName = '';
|
|
1426
|
+
for (const [, val] of jsxOpeningElementMap) {
|
|
1427
|
+
const found = val.attributes
|
|
1428
|
+
.filter((a) => a.type === 'JSXAttribute')
|
|
1429
|
+
.find((a) => a.span.start === node.span.start);
|
|
1430
|
+
if (found) {
|
|
1431
|
+
parentTagName = val.tagName;
|
|
1432
|
+
break;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
if (parentTagName &&
|
|
1436
|
+
parentTagName[0] === parentTagName[0].toUpperCase()) {
|
|
1437
|
+
let compKey;
|
|
1438
|
+
const imported = localImports[parentTagName];
|
|
1439
|
+
if (imported) {
|
|
1440
|
+
compKey = `${imported.actualPath}-${imported.importedName}`;
|
|
1441
|
+
}
|
|
1442
|
+
else {
|
|
1443
|
+
compKey = `${resourcePath}-${parentTagName}`;
|
|
1444
|
+
}
|
|
1445
|
+
const list = scannedTables.componentPropsTable?.[compKey]?.[attrName];
|
|
1446
|
+
if (list &&
|
|
1447
|
+
node.value &&
|
|
1448
|
+
node.value.type === 'JSXExpressionContainer') {
|
|
1449
|
+
const expr = node.value.expression;
|
|
1450
|
+
(0, utils_1.traverse)(expr, {
|
|
1451
|
+
MemberExpression({ node: subNode }) {
|
|
1452
|
+
const entry = list.find((x) => x.spanStart === subNode.span.start &&
|
|
1453
|
+
x.filePath === resourcePath);
|
|
1454
|
+
if (entry) {
|
|
1455
|
+
replacements.push({
|
|
1456
|
+
start: subNode.span.start - baseByteOffset,
|
|
1457
|
+
end: subNode.span.end - baseByteOffset,
|
|
1458
|
+
content: String(entry.index),
|
|
1459
|
+
});
|
|
1460
|
+
excludeSubtreeSpans(subNode);
|
|
1461
|
+
}
|
|
1462
|
+
},
|
|
1463
|
+
ArrayExpression({ node: subNode }) {
|
|
1464
|
+
const entry = list.find((x) => x.spanStart === subNode.span.start &&
|
|
1465
|
+
x.filePath === resourcePath);
|
|
1466
|
+
if (entry) {
|
|
1467
|
+
replacements.push({
|
|
1468
|
+
start: subNode.span.start - baseByteOffset,
|
|
1469
|
+
end: subNode.span.end - baseByteOffset,
|
|
1470
|
+
content: String(entry.index),
|
|
1471
|
+
});
|
|
1472
|
+
excludeSubtreeSpans(subNode);
|
|
1473
|
+
}
|
|
1474
|
+
},
|
|
1475
|
+
});
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
return;
|
|
1479
|
+
}
|
|
1278
1480
|
if (!node.value || node.value.type !== 'JSXExpressionContainer')
|
|
1279
1481
|
return;
|
|
1280
1482
|
const expr = node.value.expression;
|
|
@@ -1286,12 +1488,12 @@ async function loader(source) {
|
|
|
1286
1488
|
const dynamicClassParts = [];
|
|
1287
1489
|
const dynamicStyleParts = [];
|
|
1288
1490
|
let attributes = [];
|
|
1289
|
-
for (const [,
|
|
1290
|
-
const found =
|
|
1491
|
+
for (const [, val] of jsxOpeningElementMap) {
|
|
1492
|
+
const found = val.attributes
|
|
1291
1493
|
.filter((a) => a.type === 'JSXAttribute')
|
|
1292
1494
|
.find((a) => a.span.start === node.span.start);
|
|
1293
1495
|
if (found) {
|
|
1294
|
-
attributes =
|
|
1496
|
+
attributes = val.attributes;
|
|
1295
1497
|
break;
|
|
1296
1498
|
}
|
|
1297
1499
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/turbopack-loader",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "16.1.0",
|
|
4
4
|
"description": "Plumeria Turbopack-loader",
|
|
5
5
|
"author": "Refirst 11",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"zero-virtual.css"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@plumeria/utils": "^
|
|
25
|
+
"@plumeria/utils": "^16.1.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@swc/core": "1.15.43",
|