vite-plugin-smart-prefetch 0.1.0 → 0.3.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.cjs CHANGED
@@ -681,10 +681,14 @@ var MarkovChainTrainer = class {
681
681
 
682
682
  // src/plugin/config-generator.ts
683
683
  var _ConfigGenerator = class _ConfigGenerator {
684
- constructor(manifest, manualRules = {}, debug = false) {
684
+ constructor(manifest, manualRules = {}, debug = false, vite) {
685
+ this.vite = null;
686
+ this.isDev = false;
685
687
  this.manifest = manifest;
686
688
  this.manualRules = manualRules;
687
689
  this.debug = debug;
690
+ this.vite = vite || null;
691
+ this.isDev = !!vite;
688
692
  }
689
693
  /**
690
694
  * Generate final prefetch configuration with chunk mappings
@@ -702,6 +706,7 @@ var _ConfigGenerator = class _ConfigGenerator {
702
706
  }
703
707
  }
704
708
  const mergedModel = this.mergeManualRules(model);
709
+ const routePatterns = this.extractRoutePatterns(Object.keys(mergedModel.routes));
705
710
  const config = {
706
711
  version: model.version,
707
712
  generatedAt: model.generatedAt,
@@ -712,6 +717,8 @@ var _ConfigGenerator = class _ConfigGenerator {
712
717
  threshold: model.config.threshold,
713
718
  maxPrefetch: model.config.maxPrefetch
714
719
  },
720
+ routePatterns,
721
+ // NEW: Include route patterns for dynamic matching
715
722
  routes: {},
716
723
  chunks: {}
717
724
  };
@@ -737,6 +744,8 @@ var _ConfigGenerator = class _ConfigGenerator {
737
744
  prefetchTargets.push({
738
745
  ...target,
739
746
  chunk: chunkFile,
747
+ chunk_prod: chunkFile,
748
+ // NEW: Include production chunk path
740
749
  imports
741
750
  // Include dependency chunks (resolved to file paths)
742
751
  });
@@ -772,6 +781,8 @@ var _ConfigGenerator = class _ConfigGenerator {
772
781
  segmentPrefetchTargets.push({
773
782
  ...target,
774
783
  chunk: chunkFile,
784
+ chunk_prod: chunkFile,
785
+ // NEW: Include production chunk path
775
786
  imports
776
787
  });
777
788
  config.chunks[target.route] = chunkFile;
@@ -786,8 +797,11 @@ var _ConfigGenerator = class _ConfigGenerator {
786
797
  console.log(` \u{1F465} Segment configs for ${sourceRoute}: ${Object.keys(segmentConfigs).join(", ")}`);
787
798
  }
788
799
  }
800
+ const patterns = routePatterns[sourceRoute] || [sourceRoute];
789
801
  if (prefetchTargets.length > 0 || Object.keys(segmentConfigs).length > 0) {
790
802
  config.routes[sourceRoute] = {
803
+ patterns,
804
+ // NEW: Include patterns for dynamic route matching
791
805
  prefetch: prefetchTargets,
792
806
  ...Object.keys(segmentConfigs).length > 0 && {
793
807
  segments: segmentConfigs
@@ -967,6 +981,7 @@ var _ConfigGenerator = class _ConfigGenerator {
967
981
  ].filter(Boolean);
968
982
  for (let i = 0; i < patterns.length; i++) {
969
983
  const pattern = patterns[i];
984
+ if (!pattern) continue;
970
985
  const entry = this.manifest[pattern];
971
986
  if (entry) {
972
987
  return entry.file;
@@ -1103,6 +1118,65 @@ var _ConfigGenerator = class _ConfigGenerator {
1103
1118
  missing
1104
1119
  };
1105
1120
  }
1121
+ /**
1122
+ * Extract route patterns for dynamic route matching
1123
+ * Groups routes by their base path and includes pattern variants
1124
+ * E.g., /purchase-order and /purchase-order/:id become patterns for matching
1125
+ */
1126
+ extractRoutePatterns(routes) {
1127
+ const patterns = {};
1128
+ for (const route of routes) {
1129
+ if (route.includes(":") || route.includes("$")) {
1130
+ patterns[route] = [route];
1131
+ const base = route.split(/[:$]/)[0];
1132
+ if (base && base !== route) {
1133
+ if (!patterns[base]) {
1134
+ patterns[base] = [base, route];
1135
+ } else if (!patterns[base].includes(route)) {
1136
+ patterns[base].push(route);
1137
+ }
1138
+ }
1139
+ } else {
1140
+ patterns[route] = [route];
1141
+ }
1142
+ }
1143
+ if (this.debug && Object.keys(patterns).length > 0) {
1144
+ console.log(`
1145
+ \u{1F50D} Extracted route patterns for dynamic matching:`);
1146
+ Object.entries(patterns).forEach(([route, patternList]) => {
1147
+ if (patternList.length > 1) {
1148
+ console.log(` ${route} \u2192 [${patternList.join(", ")}]`);
1149
+ }
1150
+ });
1151
+ }
1152
+ return patterns;
1153
+ }
1154
+ /**
1155
+ * Match a pathname against route patterns
1156
+ * Supports both static routes and dynamic routes with parameters
1157
+ */
1158
+ matchRoutePattern(pathname, pattern) {
1159
+ const patternRegex = new RegExp(
1160
+ "^" + pattern.replace(/:[^/]+/g, "[^/]+").replace(/\$[^/]+/g, "[^/]+").replace(/\*/g, ".*") + // Match wildcard
1161
+ "$"
1162
+ );
1163
+ return patternRegex.test(pathname);
1164
+ }
1165
+ /**
1166
+ * Find matching route pattern for a given pathname
1167
+ * Used by hooks to match actual navigation paths to config routes
1168
+ */
1169
+ findMatchingPattern(pathname, routes) {
1170
+ for (const [route, config] of Object.entries(routes)) {
1171
+ const patterns = config.patterns || [route];
1172
+ for (const pattern of patterns) {
1173
+ if (this.matchRoutePattern(pathname, pattern)) {
1174
+ return route;
1175
+ }
1176
+ }
1177
+ }
1178
+ return null;
1179
+ }
1106
1180
  /**
1107
1181
  * Generate segment-specific prefetch configurations
1108
1182
  * Creates one config per user segment/role
@@ -1463,7 +1537,7 @@ function smartPrefetch(options = {}) {
1463
1537
  imports: []
1464
1538
  };
1465
1539
  });
1466
- const generator = new ConfigGenerator(devManifest, manualRules, debug);
1540
+ const generator = new ConfigGenerator(devManifest, manualRules, debug, server);
1467
1541
  const finalConfig = generator.generate(prefetchModel);
1468
1542
  res.setHeader("Content-Type", "application/json");
1469
1543
  res.setHeader("Access-Control-Allow-Origin", "*");