@plumeria/turbopack-loader 8.0.0 → 8.0.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/dist/index.d.ts CHANGED
@@ -6,8 +6,5 @@ interface LoaderContext {
6
6
  addDependency: (path: string) => void;
7
7
  clearDependencies: () => void;
8
8
  }
9
- declare global {
10
- var __plumeriaVirtualCssInitialized: boolean | undefined;
11
- }
12
9
  export default function loader(this: LoaderContext, source: string): Promise<void>;
13
10
  export {};
package/dist/index.js CHANGED
@@ -9,17 +9,10 @@ const fs_1 = __importDefault(require("fs"));
9
9
  const path_1 = __importDefault(require("path"));
10
10
  const zss_engine_1 = require("zss-engine");
11
11
  const utils_1 = require("@plumeria/utils");
12
- const isProduction = process.env.NODE_ENV === 'production';
13
- const VIRTUAL_FILE_PATH = path_1.default.resolve(__dirname, '..', 'zero-virtual.css');
14
- if (isProduction) {
15
- fs_1.default.writeFileSync(VIRTUAL_FILE_PATH, '/** Placeholder file */\n', 'utf-8');
16
- }
17
- else if (!global.__plumeriaVirtualCssInitialized) {
18
- global.__plumeriaVirtualCssInitialized = true;
19
- fs_1.default.writeFileSync(VIRTUAL_FILE_PATH, '/** Placeholder file */\n', 'utf-8');
20
- }
21
12
  async function loader(source) {
22
13
  const callback = this.async();
14
+ const isProduction = process.env.NODE_ENV === 'production';
15
+ const VIRTUAL_FILE_PATH = path_1.default.resolve(__dirname, '..', 'zero-virtual.css');
23
16
  if (this.resourcePath.includes('node_modules') ||
24
17
  !source.includes('@plumeria/core')) {
25
18
  return callback(null, source);
@@ -31,6 +24,10 @@ async function loader(source) {
31
24
  tsx: true,
32
25
  target: 'es2022',
33
26
  });
27
+ const leadingLen = (0, utils_1.getLeadingCommentLength)(source);
28
+ const sourceBuffer = Buffer.from(source, 'utf-8');
29
+ const leadingBytes = Buffer.byteLength(source.slice(0, leadingLen), 'utf-8');
30
+ const baseByteOffset = ast.span.start - leadingBytes;
34
31
  for (const node of ast.body) {
35
32
  if (node.type === 'ImportDeclaration') {
36
33
  const sourcePath = node.source.value;
@@ -243,12 +240,12 @@ async function loader(source) {
243
240
  hashMap,
244
241
  isExported,
245
242
  initSpan: {
246
- start: node.init.span.start - ast.span.start,
247
- end: node.init.span.end - ast.span.start,
243
+ start: node.init.span.start - baseByteOffset,
244
+ end: node.init.span.end - baseByteOffset,
248
245
  },
249
246
  declSpan: {
250
- start: declSpan.start - ast.span.start,
251
- end: declSpan.end - ast.span.start,
247
+ start: declSpan.start - baseByteOffset,
248
+ end: declSpan.end - baseByteOffset,
252
249
  },
253
250
  };
254
251
  }
@@ -278,12 +275,12 @@ async function loader(source) {
278
275
  hashMap,
279
276
  isExported,
280
277
  initSpan: {
281
- start: node.init.span.start - ast.span.start,
282
- end: node.init.span.end - ast.span.start,
278
+ start: node.init.span.start - baseByteOffset,
279
+ end: node.init.span.end - baseByteOffset,
283
280
  },
284
281
  declSpan: {
285
- start: declSpan.start - ast.span.start,
286
- end: declSpan.end - ast.span.start,
282
+ start: declSpan.start - baseByteOffset,
283
+ end: declSpan.end - baseByteOffset,
287
284
  },
288
285
  };
289
286
  }
@@ -304,12 +301,12 @@ async function loader(source) {
304
301
  hashMap: scannedTables.createAtomicMapTable[hash],
305
302
  isExported,
306
303
  initSpan: {
307
- start: node.init.span.start - ast.span.start,
308
- end: node.init.span.end - ast.span.start,
304
+ start: node.init.span.start - baseByteOffset,
305
+ end: node.init.span.end - baseByteOffset,
309
306
  },
310
307
  declSpan: {
311
- start: declSpan.start - ast.span.start,
312
- end: declSpan.end - ast.span.start,
308
+ start: declSpan.start - baseByteOffset,
309
+ end: declSpan.end - baseByteOffset,
313
310
  },
314
311
  };
315
312
  }
@@ -374,8 +371,8 @@ async function loader(source) {
374
371
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
375
372
  scannedTables.keyframesObjectTable[hash] = obj;
376
373
  replacements.push({
377
- start: node.span.start - ast.span.start,
378
- end: node.span.end - ast.span.start,
374
+ start: node.span.start - baseByteOffset,
375
+ end: node.span.end - baseByteOffset,
379
376
  content: JSON.stringify(`kf-${hash}`),
380
377
  });
381
378
  }
@@ -386,8 +383,8 @@ async function loader(source) {
386
383
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
387
384
  scannedTables.viewTransitionObjectTable[hash] = obj;
388
385
  replacements.push({
389
- start: node.span.start - ast.span.start,
390
- end: node.span.end - ast.span.start,
386
+ start: node.span.start - baseByteOffset,
387
+ end: node.span.end - baseByteOffset,
391
388
  content: JSON.stringify(`vt-${hash}`),
392
389
  });
393
390
  }
@@ -404,8 +401,8 @@ async function loader(source) {
404
401
  }
405
402
  const prefix = propName === 'createTheme' ? 'tm-' : 'st-';
406
403
  replacements.push({
407
- start: node.span.start - ast.span.start,
408
- end: node.span.end - ast.span.start,
404
+ start: node.span.start - baseByteOffset,
405
+ end: node.span.end - baseByteOffset,
409
406
  content: JSON.stringify(`${prefix}${hash}`),
410
407
  });
411
408
  }
@@ -436,8 +433,8 @@ async function loader(source) {
436
433
  }
437
434
  if (atomMap) {
438
435
  replacements.push({
439
- start: node.span.start - ast.span.start,
440
- end: node.span.end - ast.span.start,
436
+ start: node.span.start - baseByteOffset,
437
+ end: node.span.end - baseByteOffset,
441
438
  content: JSON.stringify(atomMap),
442
439
  });
443
440
  }
@@ -450,8 +447,8 @@ async function loader(source) {
450
447
  const atomicMap = scannedTables.createAtomicMapTable[themeHash];
451
448
  if (atomicMap && atomicMap && atomicMap[propName]) {
452
449
  replacements.push({
453
- start: node.span.start - ast.span.start,
454
- end: node.span.end - ast.span.start,
450
+ start: node.span.start - baseByteOffset,
451
+ end: node.span.end - baseByteOffset,
455
452
  content: JSON.stringify(atomicMap[propName]),
456
453
  });
457
454
  }
@@ -464,8 +461,8 @@ async function loader(source) {
464
461
  const staticObj = scannedTables.createStaticObjectTable[staticHash];
465
462
  if (staticObj && staticObj[propName] !== undefined) {
466
463
  replacements.push({
467
- start: node.span.start - ast.span.start,
468
- end: node.span.end - ast.span.start,
464
+ start: node.span.start - baseByteOffset,
465
+ end: node.span.end - baseByteOffset,
469
466
  content: JSON.stringify(staticObj[propName]),
470
467
  });
471
468
  }
@@ -480,8 +477,8 @@ async function loader(source) {
480
477
  const styleInfo = localCreateStyles[node.value];
481
478
  if (styleInfo) {
482
479
  replacements.push({
483
- start: node.span.start - ast.span.start,
484
- end: node.span.end - ast.span.start,
480
+ start: node.span.start - baseByteOffset,
481
+ end: node.span.end - baseByteOffset,
485
482
  content: JSON.stringify(styleInfo.hashMap),
486
483
  });
487
484
  return;
@@ -503,8 +500,8 @@ async function loader(source) {
503
500
  }
504
501
  });
505
502
  replacements.push({
506
- start: node.span.start - ast.span.start,
507
- end: node.span.end - ast.span.start,
503
+ start: node.span.start - baseByteOffset,
504
+ end: node.span.end - baseByteOffset,
508
505
  content: JSON.stringify(hashMap),
509
506
  });
510
507
  }
@@ -517,8 +514,8 @@ async function loader(source) {
517
514
  const atomicMap = scannedTables.createAtomicMapTable[themeHash];
518
515
  if (atomicMap) {
519
516
  replacements.push({
520
- start: node.span.start - ast.span.start,
521
- end: node.span.end - ast.span.start,
517
+ start: node.span.start - baseByteOffset,
518
+ end: node.span.end - baseByteOffset,
522
519
  content: JSON.stringify(atomicMap),
523
520
  });
524
521
  return;
@@ -532,8 +529,8 @@ async function loader(source) {
532
529
  const staticObj = scannedTables.createStaticObjectTable[staticHash];
533
530
  if (staticObj) {
534
531
  replacements.push({
535
- start: node.span.start - ast.span.start,
536
- end: node.span.end - ast.span.start,
532
+ start: node.span.start - baseByteOffset,
533
+ end: node.span.end - baseByteOffset,
537
534
  content: JSON.stringify(staticObj),
538
535
  });
539
536
  }
@@ -665,9 +662,11 @@ async function loader(source) {
665
662
  if (!groupVariants)
666
663
  continue;
667
664
  const currentGroupId = ++groupIdCounter;
668
- const valStart = valExpr.span.start - ast.span.start;
669
- const valEnd = valExpr.span.end - ast.span.start;
670
- const valSource = source.substring(valStart, valEnd);
665
+ const valStart = valExpr.span.start - baseByteOffset;
666
+ const valEnd = valExpr.span.end - baseByteOffset;
667
+ const valSource = sourceBuffer
668
+ .subarray(valStart, valEnd)
669
+ .toString('utf-8');
671
670
  if (valExpr.type === 'StringLiteral') {
672
671
  if (groupVariants[valExpr.value]) {
673
672
  baseStyle = (0, utils_1.deepMerge)(baseStyle, groupVariants[valExpr.value]);
@@ -691,9 +690,11 @@ async function loader(source) {
691
690
  }
692
691
  continue;
693
692
  }
694
- const argStart = arg.span.start - ast.span.start;
695
- const argEnd = arg.span.end - ast.span.start;
696
- const argSource = source.substring(argStart, argEnd);
693
+ const argStart = arg.span.start - baseByteOffset;
694
+ const argEnd = arg.span.end - baseByteOffset;
695
+ const argSource = sourceBuffer
696
+ .subarray(argStart, argEnd)
697
+ .toString('utf-8');
697
698
  if (utils_1.t.isStringLiteral(arg)) {
698
699
  if (variantObj[arg.value]) {
699
700
  baseStyle = (0, utils_1.deepMerge)(baseStyle, variantObj[arg.value]);
@@ -760,9 +761,9 @@ async function loader(source) {
760
761
  }
761
762
  }
762
763
  const getSource = (node) => {
763
- const start = node.span.start - ast.span.start;
764
- const end = node.span.end - ast.span.start;
765
- return source.substring(start, end);
764
+ const start = node.span.start - baseByteOffset;
765
+ const end = node.span.end - baseByteOffset;
766
+ return sourceBuffer.subarray(start, end).toString('utf-8');
766
767
  };
767
768
  const collectConditions = (node, currentTestStrings = []) => {
768
769
  const staticStyle = resolveStyleObject(node);
@@ -834,8 +835,8 @@ async function loader(source) {
834
835
  const records = (0, utils_1.getStyleRecords)(baseStyle);
835
836
  const className = records.map((r) => r.hash).join(' ');
836
837
  replacements.push({
837
- start: node.span.start - ast.span.start,
838
- end: node.span.end - ast.span.start,
838
+ start: node.span.start - baseByteOffset,
839
+ end: node.span.end - baseByteOffset,
839
840
  content: JSON.stringify(className),
840
841
  });
841
842
  }
@@ -958,9 +959,9 @@ async function loader(source) {
958
959
  const fClass = processBranch(c.falsy);
959
960
  let testStr = c.testString;
960
961
  if (!testStr && c.test) {
961
- const start = c.test.span.start - ast.span.start;
962
- const end = c.test.span.end - ast.span.start;
963
- testStr = source.substring(start, end);
962
+ const start = c.test.span.start - baseByteOffset;
963
+ const end = c.test.span.end - baseByteOffset;
964
+ testStr = sourceBuffer.subarray(start, end).toString('utf-8');
964
965
  }
965
966
  classParts.push(`(${testStr} ? ${tClass} : ${fClass})`);
966
967
  });
@@ -984,9 +985,11 @@ async function loader(source) {
984
985
  }
985
986
  else {
986
987
  const firstTest = options[0].test;
987
- const firstStart = firstTest.span.start - ast.span.start;
988
- const firstEnd = firstTest.span.end - ast.span.start;
989
- commonTestExpr = source.substring(firstStart, firstEnd);
988
+ const firstStart = firstTest.span.start - baseByteOffset;
989
+ const firstEnd = firstTest.span.end - baseByteOffset;
990
+ commonTestExpr = sourceBuffer
991
+ .subarray(firstStart, firstEnd)
992
+ .toString('utf-8');
990
993
  }
991
994
  options.forEach((opt) => {
992
995
  if (opt.valueName && opt.truthy) {
@@ -1020,9 +1023,9 @@ async function loader(source) {
1020
1023
  conflictStd.forEach((c) => {
1021
1024
  let testStr = c.testString;
1022
1025
  if (!testStr && c.test) {
1023
- const start = c.test.span.start - ast.span.start;
1024
- const end = c.test.span.end - ast.span.start;
1025
- testStr = source.substring(start, end);
1026
+ const start = c.test.span.start - baseByteOffset;
1027
+ const end = c.test.span.end - baseByteOffset;
1028
+ testStr = sourceBuffer.subarray(start, end).toString('utf-8');
1026
1029
  }
1027
1030
  dimensions.push({
1028
1031
  type: 'std',
@@ -1044,9 +1047,11 @@ async function loader(source) {
1044
1047
  }
1045
1048
  else {
1046
1049
  const firstTest = opts[0].test;
1047
- const firstStart = firstTest.span.start - ast.span.start;
1048
- const firstEnd = firstTest.span.end - ast.span.start;
1049
- commonTestExpr = source.substring(firstStart, firstEnd);
1050
+ const firstStart = firstTest.span.start - baseByteOffset;
1051
+ const firstEnd = firstTest.span.end - baseByteOffset;
1052
+ commonTestExpr = sourceBuffer
1053
+ .subarray(firstStart, firstEnd)
1054
+ .toString('utf-8');
1050
1055
  }
1051
1056
  }
1052
1057
  const options = opts.map((opt) => ({
@@ -1103,8 +1108,8 @@ async function loader(source) {
1103
1108
  }
1104
1109
  const replacement = classParts.length > 0 ? classParts.join(' + " " + ') : '""';
1105
1110
  replacements.push({
1106
- start: node.span.start - ast.span.start,
1107
- end: node.span.end - ast.span.start,
1111
+ start: node.span.start - baseByteOffset,
1112
+ end: node.span.end - baseByteOffset,
1108
1113
  content: replacement,
1109
1114
  });
1110
1115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/turbopack-loader",
3
- "version": "8.0.0",
3
+ "version": "8.0.2",
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": "^8.0.0"
25
+ "@plumeria/utils": "^8.0.2"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@swc/core": "1.15.8",