Skip to main content

stylex_path_resolver/resolvers/
tests.rs

1use std::{
2  env,
3  path::{Path, PathBuf},
4};
5
6use path_clean::PathClean;
7
8#[allow(dead_code)]
9fn get_root_dir(test_path: &Path) -> PathBuf {
10  if env::var("original_root_dir").is_err() {
11    unsafe { env::set_var("original_root_dir", env::current_dir().unwrap()) };
12  }
13
14  let new_cwd = PathBuf::from(env::var("original_root_dir").unwrap())
15    .join("fixtures")
16    .join(test_path)
17    .clean();
18
19  env::set_current_dir(&new_cwd).expect("Failed to set current directory");
20
21  new_cwd
22}
23
24#[allow(dead_code)]
25fn fixture(test_path: &PathBuf, part: &str) -> PathBuf {
26  PathBuf::from(
27    env::var("original_root_dir").unwrap_or(env::current_dir().unwrap().display().to_string()),
28  )
29  .join("fixtures")
30  .join(test_path)
31  .join(part)
32  .clean()
33}
34
35#[cfg(test)]
36mod resolve_path_pnpm_tests {
37  use crate::resolvers::{
38    resolve_path,
39    tests::{fixture, get_root_dir},
40  };
41
42  use std::{collections::HashMap, path::PathBuf};
43
44  #[test]
45  fn resolve_work_dir_packages() {
46    let test_path = PathBuf::from("workspace-pnpm");
47
48    assert_eq!(
49      resolve_path(
50        fixture(&test_path, "test/index.js").as_path(),
51        get_root_dir(&test_path).as_path(),
52        &mut HashMap::default(),
53      ),
54      "test/index.js"
55    );
56
57    assert_eq!(
58      resolve_path(
59        fixture(&test_path, "index.js").as_path(),
60        get_root_dir(&test_path).as_path(),
61        &mut HashMap::default(),
62      ),
63      "index.js"
64    );
65  }
66
67  #[test]
68  #[should_panic(expected = "Path resolution failed: index.jsx")]
69  fn resolve_work_dir_not_existed_packages() {
70    let test_path = PathBuf::from("workspace-pnpm");
71
72    resolve_path(
73      fixture(&test_path, "index.jsx").as_path(),
74      get_root_dir(&test_path).as_path(),
75      &mut HashMap::default(),
76    );
77  }
78
79  #[test]
80  fn external_package_with_namespace() {
81    let test_path = PathBuf::from("workspace-pnpm");
82
83    assert_eq!(
84      resolve_path(
85        fixture(
86          &test_path,
87          "node_modules/@stylex/open-props/lib/colors.stylex.js"
88        )
89        .as_path(),
90        get_root_dir(&test_path).as_path(),
91        &mut HashMap::default(),
92      ),
93      "node_modules/@stylex/open-props/lib/colors.stylex.js"
94    );
95  }
96
97  #[test]
98  #[should_panic(
99    expected = "Path resolution failed: node_modules/@stylex/open-props/lib/spaces.stylex.js"
100  )]
101  fn resolve_work_dir_not_existed_external_package_file_with_namespace() {
102    let test_path = PathBuf::from("workspace-pnpm");
103
104    resolve_path(
105      fixture(
106        &test_path,
107        "node_modules/@stylex/open-props/lib/spaces.stylex.js",
108      )
109      .as_path(),
110      get_root_dir(&test_path).as_path(),
111      &mut HashMap::default(),
112    );
113  }
114
115  #[test]
116  #[should_panic(
117    expected = "Path resolution failed: node_modules/@stylex/close-props/lib/colors.stylex.js"
118  )]
119  fn resolve_work_dir_not_existed_external_package_with_namespace() {
120    let test_path = PathBuf::from("workspace-pnpm");
121
122    resolve_path(
123      fixture(
124        &test_path,
125        "node_modules/@stylex/close-props/lib/colors.stylex.js",
126      )
127      .as_path(),
128      get_root_dir(&test_path).as_path(),
129      &mut HashMap::default(),
130    );
131  }
132
133  #[test]
134  fn external_package_without_namespace() {
135    let test_path = PathBuf::from("workspace-pnpm");
136
137    assert_eq!(
138      resolve_path(
139        fixture(&test_path, "node_modules/stylex-lib/colors.stylex.js").as_path(),
140        get_root_dir(&test_path).as_path(),
141        &mut HashMap::default(),
142      ),
143      "node_modules/stylex-lib/colors.stylex.js"
144    );
145  }
146
147  #[test]
148  fn external_pnpm_package_file() {
149    assert_eq!(
150      resolve_path(
151        fixture(& PathBuf::from("workspace-pnpm"), "../../node_modules/.pnpm/@stylexjs+open-props@0.7.5/node_modules/@stylexjs/open-props/lib/colors.stylex.js").as_path(),
152        get_root_dir(& PathBuf::from("workspace-pnpm")).as_path(),
153        &mut HashMap::default(),
154
155      ),
156      "node_modules/@stylexjs/open-props/lib/colors.stylex.js"
157    );
158  }
159
160  #[test]
161  fn external_npm_package_file() {
162    assert_eq!(
163      resolve_path(
164        fixture(
165          &PathBuf::from("workspace-pnpm"),
166          "../../node_modules/@stylexjs/open-props/lib/colors.stylex.js"
167        )
168        .as_path(),
169        get_root_dir(&PathBuf::from("workspace-pnpm")).as_path(),
170        &mut HashMap::default(),
171      ),
172      "node_modules/@stylexjs/open-props/lib/colors.stylex.js"
173    );
174  }
175
176  #[test]
177  fn external_yarn_pnp_package_file() {
178    assert_eq!(
179      resolve_path(
180        fixture(& PathBuf::from("workspace-pnpm"), "../../app/node_modules/.yarn/__virtual__/swc-virtual-123123/node_modules/@stylexjs/open-props/lib/colors.stylex.js").as_path(),
181        get_root_dir(& PathBuf::from("workspace-pnpm")).as_path(),
182        &mut HashMap::default(),
183
184      ),
185      "node_modules/@stylexjs/open-props/lib/colors.stylex.js"
186    );
187  }
188
189  #[test]
190  fn workspace_package_without_namespace() {
191    let test_path = PathBuf::from("workspace-pnpm");
192    let local_package_test_path = PathBuf::from("");
193
194    assert_eq!(
195      resolve_path(
196        fixture(
197          &local_package_test_path,
198          "packages/stylex-lib/colors.stylex.js"
199        )
200        .as_path(),
201        get_root_dir(&test_path).as_path(),
202        &mut HashMap::default(),
203      ),
204      "node_modules/stylex-lib/colors.stylex.js"
205    );
206  }
207
208  #[test]
209  fn workspace_package_with_namespace() {
210    let test_path = PathBuf::from("workspace-pnpm");
211    let local_package_test_path = PathBuf::from("");
212
213    assert_eq!(
214      resolve_path(
215        fixture(
216          &local_package_test_path,
217          "packages/@stylex/theme-lib/colors.stylex.js"
218        )
219        .as_path(),
220        get_root_dir(&test_path).as_path(),
221        &mut HashMap::default(),
222      ),
223      "node_modules/@stylex/theme-lib/colors.stylex.js"
224    );
225  }
226
227  #[test]
228  fn workspace_package_main_dist_with_namespace() {
229    let test_path = PathBuf::from("workspace-pnpm");
230    let local_package_test_path = PathBuf::from("");
231
232    assert_eq!(
233      resolve_path(
234        fixture(
235          &local_package_test_path,
236          "packages/@stylex/theme-lib-main-dist/dist/colors.stylex.js"
237        )
238        .as_path(),
239        get_root_dir(&test_path).as_path(),
240        &mut HashMap::default(),
241      ),
242      "node_modules/@stylex/theme-lib-main-dist/dist/colors.stylex.js"
243    );
244  }
245}
246
247#[cfg(test)]
248mod resolve_path_npm_tests {
249  use crate::resolvers::{
250    resolve_path,
251    tests::{fixture, get_root_dir},
252  };
253
254  use std::{collections::HashMap, path::PathBuf};
255
256  #[test]
257  fn resolve_work_dir_packages() {
258    let test_path = PathBuf::from("workspace-npm/apps/web");
259
260    assert_eq!(
261      resolve_path(
262        fixture(&test_path, "test/index.js").as_path(),
263        get_root_dir(&test_path).as_path(),
264        &mut HashMap::default(),
265      ),
266      "test/index.js"
267    );
268
269    assert_eq!(
270      resolve_path(
271        fixture(&test_path, "index.js").as_path(),
272        get_root_dir(&test_path).as_path(),
273        &mut HashMap::default(),
274      ),
275      "index.js"
276    );
277
278    assert!(get_root_dir(&test_path).join("node_modules").exists());
279  }
280
281  #[test]
282  #[should_panic(expected = "Path resolution failed: index.jsx")]
283  fn resolve_work_dir_not_existed_packages() {
284    let test_path = PathBuf::from("workspace-npm/apps/web");
285
286    resolve_path(
287      fixture(&test_path, "index.jsx").as_path(),
288      get_root_dir(&test_path).as_path(),
289      &mut HashMap::default(),
290    );
291  }
292
293  #[test]
294  fn external_package_with_namespace() {
295    let test_path = PathBuf::from("workspace-npm");
296
297    assert_eq!(
298      resolve_path(
299        fixture(
300          &test_path,
301          "node_modules/@stylex/open-props/lib/colors.stylex.js"
302        )
303        .as_path(),
304        get_root_dir(&test_path).as_path(),
305        &mut HashMap::default(),
306      ),
307      "node_modules/@stylex/open-props/lib/colors.stylex.js"
308    );
309  }
310
311  #[test]
312  #[should_panic(
313    expected = "Path resolution failed: node_modules/@stylex/open-props/lib/spaces.stylex.js"
314  )]
315  fn resolve_work_dir_not_existed_external_package_file_with_namespace() {
316    let test_path = PathBuf::from("workspace-npm/apps/web");
317
318    resolve_path(
319      fixture(
320        &test_path,
321        "node_modules/@stylex/open-props/lib/spaces.stylex.js",
322      )
323      .as_path(),
324      get_root_dir(&test_path).as_path(),
325      &mut HashMap::default(),
326    );
327  }
328
329  #[test]
330  #[should_panic(
331    expected = "Path resolution failed: node_modules/@stylex/close-props/lib/colors.stylex.js"
332  )]
333  fn resolve_work_dir_not_existed_external_package_with_namespace() {
334    let test_path = PathBuf::from("workspace-npm/apps/web");
335
336    resolve_path(
337      fixture(
338        &test_path,
339        "node_modules/@stylex/close-props/lib/colors.stylex.js",
340      )
341      .as_path(),
342      get_root_dir(&test_path).as_path(),
343      &mut HashMap::default(),
344    );
345  }
346
347  #[test]
348  fn external_package_without_namespace() {
349    let test_path = PathBuf::from("workspace-npm");
350
351    assert_eq!(
352      resolve_path(
353        fixture(&test_path, "node_modules/stylex-lib/colors.stylex.js").as_path(),
354        get_root_dir(&test_path).as_path(),
355        &mut HashMap::default(),
356      ),
357      "node_modules/stylex-lib/colors.stylex.js"
358    );
359  }
360
361  #[test]
362  fn external_pnpm_package_file() {
363    assert_eq!(
364      resolve_path(
365        fixture(& PathBuf::from("workspace-npm/apps/web"), "../../node_modules/.pnpm/@stylexjs+open-props@0.7.5/node_modules/@stylexjs/open-props/lib/colors.stylex.js").as_path(),
366        get_root_dir(& PathBuf::from("workspace-pnpm")).as_path(),
367        &mut HashMap::default(),
368
369      ),
370      "node_modules/@stylexjs/open-props/lib/colors.stylex.js"
371    );
372  }
373
374  #[test]
375  fn external_npm_package_file() {
376    assert_eq!(
377      resolve_path(
378        fixture(
379          &PathBuf::from("workspace-npm/apps/web"),
380          "../../node_modules/@stylexjs/open-props/lib/colors.stylex.js"
381        )
382        .as_path(),
383        get_root_dir(&PathBuf::from("workspace-npm/apps/web")).as_path(),
384        &mut HashMap::default(),
385      ),
386      "node_modules/@stylexjs/open-props/lib/colors.stylex.js"
387    );
388  }
389
390  #[test]
391  fn external_yarn_pnp_package_file() {
392    assert_eq!(
393      resolve_path(
394        fixture(& PathBuf::from("workspace-npm/apps/web"), "../../app/node_modules/.yarn/__virtual__/swc-virtual-123123/node_modules/@stylexjs/open-props/lib/colors.stylex.js").as_path(),
395        get_root_dir(& PathBuf::from("workspace-npm/apps/web")).as_path(),
396        &mut HashMap::default(),
397
398      ),
399      "node_modules/@stylexjs/open-props/lib/colors.stylex.js"
400    );
401  }
402
403  #[test]
404  fn workspace_package_without_namespace() {
405    let test_path = PathBuf::from("workspace-npm/apps/web");
406    let local_package_test_path = PathBuf::from("");
407
408    assert_eq!(
409      resolve_path(
410        fixture(
411          &local_package_test_path,
412          "packages/stylex-lib/colors.stylex.js"
413        )
414        .as_path(),
415        get_root_dir(&test_path).as_path(),
416        &mut HashMap::default(),
417      ),
418      "../../node_modules/stylex-lib/colors.stylex.js"
419    );
420  }
421
422  #[test]
423  fn workspace_package_with_namespace() {
424    let test_path = PathBuf::from("workspace-npm/apps/web");
425    let local_package_test_path = PathBuf::from("");
426
427    assert_eq!(
428      resolve_path(
429        fixture(
430          &local_package_test_path,
431          "packages/@stylex/theme-lib/colors.stylex.js"
432        )
433        .as_path(),
434        get_root_dir(&test_path).as_path(),
435        &mut HashMap::default(),
436      ),
437      "../../node_modules/@stylex/theme-lib/colors.stylex.js"
438    );
439  }
440
441  #[test]
442  fn workspace_package_main_dist_with_namespace() {
443    let test_path = PathBuf::from("workspace-npm/apps/web");
444    let local_package_test_path = PathBuf::from("");
445
446    assert_eq!(
447      resolve_path(
448        fixture(
449          &local_package_test_path,
450          "packages/@stylex/theme-lib-main-dist/colors.stylex.js"
451        )
452        .as_path(),
453        get_root_dir(&test_path).as_path(),
454        &mut HashMap::default(),
455      ),
456      "../../node_modules/@stylex/theme-lib-main-dist/dist/colors.stylex.js"
457    );
458  }
459}
460
461#[cfg(test)]
462mod resolve_path_exports_tests {
463
464  use crate::resolvers::{
465    resolve_file_path, resolve_path,
466    tests::{fixture, get_root_dir},
467  };
468  use rustc_hash::FxHashMap;
469
470  use std::{collections::HashMap, path::PathBuf};
471
472  #[test]
473  fn external_package_main_exports() {
474    let test_path = PathBuf::from("exports");
475
476    assert_eq!(
477      resolve_path(
478        fixture(
479          &test_path,
480          "node_modules/stylex-lib-dist-main/dist/index.jsx"
481        )
482        .as_path(),
483        get_root_dir(&test_path).as_path(),
484        &mut HashMap::default(),
485      ),
486      "node_modules/stylex-lib-dist-main/dist/index.jsx"
487    );
488  }
489
490  #[test]
491  fn external_package_module_exports() {
492    let test_path = PathBuf::from("exports");
493    assert_eq!(
494      resolve_path(
495        fixture(
496          &test_path,
497          "node_modules/stylex-lib-dist-module/dist/index.jsx"
498        )
499        .as_path(),
500        get_root_dir(&test_path).as_path(),
501        &mut HashMap::default(),
502      ),
503      "node_modules/stylex-lib-dist-module/dist/index.jsx"
504    );
505  }
506
507  #[test]
508  fn external_package_exports() {
509    let test_path = PathBuf::from("exports");
510
511    assert_eq!(
512      resolve_path(
513        fixture(
514          &test_path,
515          "node_modules/stylex-lib-dist-exports/dist/index.js"
516        )
517        .as_path(),
518        get_root_dir(&test_path).as_path(),
519        &mut HashMap::default(),
520      ),
521      "node_modules/stylex-lib-dist-exports/dist/index.js"
522    );
523  }
524
525  #[test]
526  fn external_package_commonjs_and_esm_exports() {
527    let test_path = PathBuf::from("exports");
528
529    assert_eq!(
530      resolve_path(
531        fixture(
532          &test_path,
533          "node_modules/stylex-lib-dist-exports-commonjs-esm/dist/index.js"
534        )
535        .as_path(),
536        get_root_dir(&test_path).as_path(),
537        &mut HashMap::default(),
538      ),
539      "node_modules/stylex-lib-dist-exports-commonjs-esm/dist/index.js"
540    );
541
542    assert_eq!(
543      resolve_path(
544        fixture(
545          &test_path,
546          "node_modules/stylex-lib-dist-exports-commonjs-esm/dist/colors.stylex.cjs"
547        )
548        .as_path(),
549        get_root_dir(&test_path).as_path(),
550        &mut HashMap::default(),
551      ),
552      "node_modules/stylex-lib-dist-exports-commonjs-esm/dist/colors.stylex.cjs"
553    );
554  }
555
556  #[test]
557  fn external_package_exports_with_main() {
558    let test_path = PathBuf::from("exports");
559
560    assert_eq!(
561      resolve_path(
562        fixture(
563          &test_path,
564          "node_modules/stylex-lib-dist-exports-with-main/dist/index.js"
565        )
566        .as_path(),
567        get_root_dir(&test_path).as_path(),
568        &mut HashMap::default(),
569      ),
570      "node_modules/stylex-lib-dist-exports-with-main/dist/index.js"
571    );
572  }
573
574  #[test]
575  fn external_package_exports_with_wildcard() {
576    let test_path = PathBuf::from("exports");
577
578    assert_eq!(
579      resolve_path(
580        fixture(
581          &test_path,
582          "node_modules/stylex-lib-dist-exports-with-wildcard/src/index.ts"
583        )
584        .as_path(),
585        get_root_dir(&test_path).as_path(),
586        &mut HashMap::default(),
587      ),
588      "node_modules/stylex-lib-dist-exports-with-wildcard/src/index.ts"
589    );
590
591    assert_eq!(
592      resolve_path(
593        fixture(
594          &test_path,
595          "node_modules/stylex-lib-dist-exports-with-wildcard/src/colors.stylex.ts"
596        )
597        .as_path(),
598        get_root_dir(&test_path).as_path(),
599        &mut HashMap::default(),
600      ),
601      "node_modules/stylex-lib-dist-exports-with-wildcard/src/colors.stylex.ts"
602    );
603
604    assert_eq!(
605      resolve_path(
606        fixture(
607          &test_path,
608          "node_modules/stylex-lib-dist-exports-with-wildcard/src/icons/arrow-right.tsx"
609        )
610        .as_path(),
611        get_root_dir(&test_path).as_path(),
612        &mut HashMap::default(),
613      ),
614      "node_modules/stylex-lib-dist-exports-with-wildcard/src/icons/arrow-right.tsx"
615    );
616  }
617
618  #[test]
619  fn resolve_file_path_with_wildcard_exports() {
620    let test_path = PathBuf::from("exports");
621    let root_path = get_root_dir(&test_path).display().to_string();
622    let source_file_path = format!("{}/test.js", root_path);
623    let aliases = FxHashMap::default();
624
625    // Test root entry point (. -> ./src/index.ts)
626    let expected_root = format!(
627      "{}/{}",
628      root_path, "node_modules/stylex-lib-dist-exports-with-wildcard/src/index.ts"
629    );
630    assert_eq!(
631      resolve_file_path(
632        "stylex-lib-dist-exports-with-wildcard",
633        source_file_path.as_str(),
634        root_path.as_str(),
635        &aliases,
636        &mut HashMap::default(),
637      )
638      .unwrap_or_default()
639      .display()
640      .to_string(),
641      expected_root
642    );
643
644    // Test wildcard export (./* -> ./src/*.ts)
645    let expected_colors = format!(
646      "{}/{}",
647      root_path, "node_modules/stylex-lib-dist-exports-with-wildcard/src/colors.stylex.ts"
648    );
649    assert_eq!(
650      resolve_file_path(
651        "stylex-lib-dist-exports-with-wildcard/colors.stylex",
652        source_file_path.as_str(),
653        root_path.as_str(),
654        &aliases,
655        &mut HashMap::default(),
656      )
657      .unwrap_or_default()
658      .display()
659      .to_string(),
660      expected_colors
661    );
662
663    // Test nested wildcard export (./icons/* -> ./src/icons/*.tsx)
664    let expected_icon = format!(
665      "{}/{}",
666      root_path, "node_modules/stylex-lib-dist-exports-with-wildcard/src/icons/arrow-right.tsx"
667    );
668    assert_eq!(
669      resolve_file_path(
670        "stylex-lib-dist-exports-with-wildcard/icons/arrow-right",
671        source_file_path.as_str(),
672        root_path.as_str(),
673        &aliases,
674        &mut HashMap::default(),
675      )
676      .unwrap_or_default()
677      .display()
678      .to_string(),
679      expected_icon
680    );
681  }
682
683  #[test]
684  fn workspace_package_main_exports() {
685    let test_path = PathBuf::from("exports");
686    let local_package_test_path = PathBuf::from("");
687
688    assert_eq!(
689      resolve_path(
690        fixture(
691          &local_package_test_path,
692          "packages/stylex-lib-dist-main-local/dist/index.jsx"
693        )
694        .as_path(),
695        get_root_dir(&test_path).as_path(),
696        &mut HashMap::default(),
697      ),
698      "node_modules/stylex-lib-dist-main-local/dist/index.jsx"
699    );
700  }
701
702  #[test]
703  #[should_panic(
704    expected = "Resolve path must be a file, but got: fixtures/packages/stylex-lib-dist-main-local"
705  )]
706  fn resolve_work_dir_not_existed_workspace_package_main_exports() {
707    let test_path = PathBuf::from("exports");
708    let local_package_test_path = PathBuf::from("");
709
710    resolve_path(
711      fixture(
712        &local_package_test_path,
713        "packages/stylex-lib-dist-main-local",
714      )
715      .as_path(),
716      get_root_dir(&test_path).as_path(),
717      &mut HashMap::default(),
718    );
719  }
720
721  #[test]
722  fn workspace_package_module_exports() {
723    let test_path = PathBuf::from("exports");
724    let local_package_test_path = PathBuf::from("");
725
726    assert_eq!(
727      resolve_path(
728        fixture(
729          &local_package_test_path,
730          "packages/stylex-lib-dist-module-local/dist/index.jsx"
731        )
732        .as_path(),
733        get_root_dir(&test_path).as_path(),
734        &mut HashMap::default(),
735      ),
736      "node_modules/stylex-lib-dist-module-local/dist/index.jsx"
737    );
738  }
739
740  #[test]
741  fn workspace_package_exports() {
742    let test_path = PathBuf::from("exports");
743    let local_package_test_path = PathBuf::from("");
744
745    assert_eq!(
746      resolve_path(
747        fixture(
748          &test_path,
749          "node_modules/stylex-lib-dist-exports/dist/index.js"
750        )
751        .as_path(),
752        get_root_dir(&test_path).as_path(),
753        &mut HashMap::default(),
754      ),
755      "node_modules/stylex-lib-dist-exports/dist/index.js"
756    );
757
758    assert_eq!(
759      resolve_path(
760        fixture(
761          &test_path,
762          "node_modules/stylex-lib-dist-exports-with-main/dist/index.js"
763        )
764        .as_path(),
765        get_root_dir(&test_path).as_path(),
766        &mut HashMap::default(),
767      ),
768      "node_modules/stylex-lib-dist-exports-with-main/dist/index.js"
769    );
770
771    assert_eq!(
772      resolve_path(
773        fixture(
774          &local_package_test_path,
775          "packages/stylex-lib-dist-exports-local/dist/index.js"
776        )
777        .as_path(),
778        get_root_dir(&test_path).as_path(),
779        &mut HashMap::default(),
780      ),
781      "node_modules/stylex-lib-dist-exports-local/dist/index.js"
782    );
783
784    assert_eq!(
785      resolve_path(
786        fixture(
787          &local_package_test_path,
788          "packages/stylex-lib-dist-exports-local/dist/colors.stylex.js"
789        )
790        .as_path(),
791        get_root_dir(&test_path).as_path(),
792        &mut HashMap::default(),
793      ),
794      "node_modules/stylex-lib-dist-exports-local/dist/colors.stylex.js",
795    );
796  }
797
798  #[test]
799  #[should_panic(
800    expected = "Resolve path must be a file, but got: fixtures/packages/stylex-lib-dist-module-local"
801  )]
802  fn resolve_path_not_a_file_workspace_package_module_exports() {
803    let test_path = PathBuf::from("exports");
804    let local_package_test_path = PathBuf::from("");
805
806    resolve_path(
807      fixture(
808        &local_package_test_path,
809        "packages/stylex-lib-dist-module-local",
810      )
811      .as_path(),
812      get_root_dir(&test_path).as_path(),
813      &mut HashMap::default(),
814    );
815  }
816
817  #[test]
818  #[should_panic(
819    expected = "Resolve path must be a file, but got: fixtures/packages/stylex-lib-dist-exports/colors.stylex"
820  )]
821  fn resolve_work_dir_not_existed_workspace_package_exports() {
822    let test_path = PathBuf::from("exports");
823    let local_package_test_path = PathBuf::from("");
824
825    resolve_path(
826      fixture(
827        &local_package_test_path,
828        "packages/stylex-lib-dist-exports/colors.stylex",
829      )
830      .as_path(),
831      get_root_dir(&test_path).as_path(),
832      &mut HashMap::default(),
833    );
834  }
835
836  #[test]
837  #[should_panic(
838    expected = "Resolve path must be a file, but got: fixtures/packages/stylex-lib-dist-exports-with-main/colors.stylex"
839  )]
840  fn resolve_work_dir_not_existed_workspace_package_exports_with_main() {
841    let test_path = PathBuf::from("exports");
842    let local_package_test_path = PathBuf::from("");
843
844    resolve_path(
845      fixture(
846        &local_package_test_path,
847        "packages/stylex-lib-dist-exports-with-main/colors.stylex",
848      )
849      .as_path(),
850      get_root_dir(&test_path).as_path(),
851      &mut HashMap::default(),
852    );
853  }
854
855  #[test]
856  #[should_panic(
857    expected = "Resolve path must be a file, but got: fixtures/exports/node_modules/stylex-lib-dist-exports/colors.stylex"
858  )]
859  fn resolve_work_dir_not_existed_external_package_exports() {
860    let test_path = PathBuf::from("exports");
861
862    resolve_path(
863      fixture(
864        &test_path,
865        "node_modules/stylex-lib-dist-exports/colors.stylex",
866      )
867      .as_path(),
868      get_root_dir(&test_path).as_path(),
869      &mut HashMap::default(),
870    );
871  }
872
873  #[test]
874  #[should_panic(
875    expected = "Resolve path must be a file, but got: fixtures/exports/node_modules/stylex-lib-dist-exports-with-main/colors.stylex"
876  )]
877  fn resolve_work_dir_not_existed_external_package_exports_with_main() {
878    let test_path = PathBuf::from("exports");
879
880    resolve_path(
881      fixture(
882        &test_path,
883        "node_modules/stylex-lib-dist-exports-with-main/colors.stylex",
884      )
885      .as_path(),
886      get_root_dir(&test_path).as_path(),
887      &mut HashMap::default(),
888    );
889  }
890
891  #[test]
892  #[should_panic(
893    expected = "Resolve path must be a file, but got: fixtures/exports/node_modules/stylex-lib-dist-exports"
894  )]
895  fn failed_resolve_root_package_path() {
896    let test_path = PathBuf::from("exports");
897
898    assert_eq!(
899      resolve_path(
900        fixture(&test_path, "node_modules/stylex-lib-dist-exports").as_path(),
901        get_root_dir(&test_path).as_path(),
902        &mut HashMap::default(),
903      ),
904      "node_modules/stylex-lib-dist-exports/dist/index.js"
905    );
906  }
907
908  #[test]
909  #[should_panic(
910    expected = "Resolve path must be a file, but got: fixtures/exports/node_modules/stylex-lib-dist-exports"
911  )]
912  fn failed_resolve_root_package_path_with_main() {
913    let test_path = PathBuf::from("exports");
914
915    assert_eq!(
916      resolve_path(
917        fixture(&test_path, "node_modules/stylex-lib-dist-exports-with-main").as_path(),
918        get_root_dir(&test_path).as_path(),
919        &mut HashMap::default(),
920      ),
921      "node_modules/stylex-lib-dist-exports-with-main/dist/index.js"
922    );
923  }
924
925  #[test]
926  #[should_panic(
927    expected = "Resolve path must be a file, but got: fixtures/exports/node_modules/stylex-lib-dist-exports/colors.stylex"
928  )]
929  fn failed_resolve_package_exports_dir_path() {
930    let test_path = PathBuf::from("exports");
931
932    resolve_path(
933      fixture(
934        &test_path,
935        "node_modules/stylex-lib-dist-exports/colors.stylex",
936      )
937      .as_path(),
938      get_root_dir(&test_path).as_path(),
939      &mut HashMap::default(),
940    );
941  }
942
943  #[test]
944  #[should_panic(
945    expected = "Resolve path must be a file, but got: fixtures/exports/node_modules/stylex-lib-dist-exports-with-main/colors.stylex"
946  )]
947  fn failed_resolve_package_exports_dir_path_with_main() {
948    let test_path = PathBuf::from("exports");
949
950    resolve_path(
951      fixture(
952        &test_path,
953        "node_modules/stylex-lib-dist-exports-with-main/colors.stylex",
954      )
955      .as_path(),
956      get_root_dir(&test_path).as_path(),
957      &mut HashMap::default(),
958    );
959  }
960
961  #[test]
962  #[should_panic(
963    expected = "Resolve path must be a file, but got: fixtures/packages/stylex-lib-dist-exports-local"
964  )]
965  fn failed_resolve_local_package_root_dir_path() {
966    let test_path = PathBuf::from("exports");
967    let local_package_test_path = PathBuf::from("");
968
969    resolve_path(
970      fixture(
971        &local_package_test_path,
972        "packages/stylex-lib-dist-exports-local",
973      )
974      .as_path(),
975      get_root_dir(&test_path).as_path(),
976      &mut HashMap::default(),
977    );
978  }
979
980  #[test]
981  fn resolve_work_dir_existed_local_package_exports_path() {
982    let test_path = PathBuf::from("exports");
983    let local_package_test_path = PathBuf::from("");
984
985    let expected_result = "node_modules/stylex-lib-dist-exports-local/dist/colors.stylex.js";
986
987    assert_eq!(
988      resolve_path(
989        fixture(
990          &local_package_test_path,
991          "packages/stylex-lib-dist-exports-local/colors.stylex.js",
992        )
993        .as_path(),
994        get_root_dir(&test_path).as_path(),
995        &mut HashMap::default(),
996      ),
997      expected_result
998    );
999  }
1000}
1001#[cfg(test)]
1002mod resolve_path_application_pnpm_tests {
1003  use path_clean::PathClean;
1004  use rustc_hash::FxHashMap;
1005
1006  use crate::resolvers::{resolve_file_path, tests::get_root_dir};
1007
1008  use std::{collections::HashMap, path::PathBuf};
1009
1010  #[test]
1011  fn resolve_regular_local_import_from_src() {
1012    let test_path = PathBuf::from("application-pnpm");
1013
1014    let import_path_str = "../colors.stylex.js";
1015    let source_file_path = format!(
1016      "{}/src/pages/home.js",
1017      get_root_dir(&test_path).as_path().display()
1018    );
1019    let root_path = get_root_dir(&test_path).display().to_string();
1020    let aliases = Default::default();
1021
1022    let expected_result = format!("{}/{}", root_path, "src/colors.stylex.js");
1023
1024    assert_eq!(
1025      resolve_file_path(
1026        import_path_str,
1027        source_file_path.as_str(),
1028        root_path.as_str(),
1029        &aliases,
1030        &mut HashMap::default(),
1031      )
1032      .unwrap_or_default()
1033      .display()
1034      .to_string(),
1035      expected_result
1036    );
1037  }
1038
1039  #[test]
1040  fn resolve_regular_local_import_from_same_level_directory() {
1041    let test_path = PathBuf::from("application-pnpm");
1042
1043    let import_path_str = "../components/button.js";
1044    let source_file_path = format!(
1045      "{}/src/pages/home.js",
1046      get_root_dir(&test_path).as_path().display()
1047    );
1048    let root_path = get_root_dir(&test_path).display().to_string();
1049    let aliases = Default::default();
1050
1051    let expected_result = format!("{}/{}", root_path, "src/components/button.js");
1052
1053    assert_eq!(
1054      resolve_file_path(
1055        import_path_str,
1056        source_file_path.as_str(),
1057        root_path.as_str(),
1058        &aliases,
1059        &mut HashMap::default(),
1060      )
1061      .unwrap_or_default()
1062      .display()
1063      .to_string(),
1064      expected_result
1065    );
1066  }
1067
1068  #[test]
1069  fn resolve_regular_local_import_from_alias() {
1070    let test_path = PathBuf::from("application-pnpm");
1071
1072    let import_path_str = "@/components/button.js";
1073    let source_file_path = format!(
1074      "{}/src/pages/home.js",
1075      get_root_dir(&test_path).as_path().display()
1076    );
1077    let root_path = get_root_dir(&test_path).display().to_string();
1078    let mut aliases = FxHashMap::default();
1079    aliases.insert("@/*".to_string(), vec![format!("{}/src/*", root_path)]);
1080
1081    let expected_result = format!("{}/{}", root_path, "src/components/button.js");
1082
1083    assert_eq!(
1084      resolve_file_path(
1085        import_path_str,
1086        source_file_path.as_str(),
1087        root_path.as_str(),
1088        &aliases,
1089        &mut HashMap::default(),
1090      )
1091      .unwrap_or_default()
1092      .display()
1093      .to_string(),
1094      expected_result
1095    );
1096  }
1097
1098  #[test]
1099  fn resolve_regular_local_import_from_root_no_alias() {
1100    let test_path = PathBuf::from("application-pnpm");
1101
1102    let import_path_str = "/src/colors.stylex.js";
1103    let source_file_path = format!(
1104      "{}/src/pages/home.js",
1105      get_root_dir(&test_path).as_path().display()
1106    );
1107    let root_path = get_root_dir(&test_path).display().to_string();
1108    let aliases = Default::default();
1109    let expected_result = format!("{root_path}/src/colors.stylex.js");
1110
1111    assert_eq!(
1112      resolve_file_path(
1113        import_path_str,
1114        source_file_path.as_str(),
1115        root_path.as_str(),
1116        &aliases,
1117        &mut HashMap::default(),
1118      )
1119      .unwrap_or_default()
1120      .display()
1121      .to_string(),
1122      expected_result
1123    );
1124  }
1125
1126  #[test]
1127  fn resolve_regular_local_import_from_root_with_alias() {
1128    let test_path = PathBuf::from("application-pnpm");
1129
1130    let import_path_str = "/src/colors.stylex.js";
1131    let source_file_path = format!(
1132      "{}/src/pages/home.js",
1133      get_root_dir(&test_path).as_path().display()
1134    );
1135    let root_path = get_root_dir(&test_path).display().to_string();
1136    let mut aliases = FxHashMap::default();
1137    aliases.insert("/*".to_string(), vec![format!("{root_path}/*")]);
1138
1139    let expected_result = format!("{root_path}/src/colors.stylex.js");
1140
1141    assert_eq!(
1142      resolve_file_path(
1143        import_path_str,
1144        source_file_path.as_str(),
1145        root_path.as_str(),
1146        &aliases,
1147        &mut HashMap::default(),
1148      )
1149      .unwrap_or_default()
1150      .display()
1151      .to_string(),
1152      expected_result
1153    );
1154  }
1155
1156  #[test]
1157  fn resolve_regular_local_import_from_src_alias() {
1158    let test_path = PathBuf::from("application-pnpm");
1159
1160    let import_path_str = "/colors.stylex.js";
1161    let source_file_path = format!(
1162      "{}/src/pages/home.js",
1163      get_root_dir(&test_path).as_path().display()
1164    );
1165    let root_path = get_root_dir(&test_path).display().to_string();
1166    let mut aliases = FxHashMap::default();
1167    aliases.insert("/*".to_string(), vec![format!("{root_path}/src/*")]);
1168
1169    let expected_result = format!("{root_path}/src/colors.stylex.js");
1170
1171    assert_eq!(
1172      resolve_file_path(
1173        import_path_str,
1174        source_file_path.as_str(),
1175        root_path.as_str(),
1176        &aliases,
1177        &mut HashMap::default(),
1178      )
1179      .unwrap_or_default()
1180      .display()
1181      .to_string(),
1182      expected_result
1183    );
1184  }
1185
1186  #[test]
1187  fn resolve_regular_local_import_from_workspace_alias() {
1188    let test_path = PathBuf::from("workspace-pnpm");
1189
1190    let import_path_str = "@/components/button";
1191    let source_file_path = format!(
1192      "{}/src/pages/home.js",
1193      get_root_dir(&test_path).as_path().display()
1194    );
1195    let root_path = get_root_dir(&test_path).display().to_string();
1196    let mut aliases = FxHashMap::default();
1197    aliases.insert(
1198      "@/*".to_string(),
1199      vec![format!(
1200        "{}",
1201        PathBuf::from(&root_path)
1202          .join("../application-pnpm/src/*")
1203          .clean()
1204          .to_string_lossy()
1205      )],
1206    );
1207
1208    let expected_result = format!(
1209      "{}",
1210      PathBuf::from(&root_path)
1211        .join("../application-pnpm/src/components/button.js")
1212        .clean()
1213        .to_string_lossy(),
1214    );
1215
1216    assert_eq!(
1217      resolve_file_path(
1218        import_path_str,
1219        source_file_path.as_str(),
1220        root_path.as_str(),
1221        &aliases,
1222        &mut HashMap::default(),
1223      )
1224      .unwrap_or_default()
1225      .display()
1226      .to_string(),
1227      expected_result
1228    );
1229  }
1230
1231  #[test]
1232  fn resolve_regular_external_import() {
1233    let test_path = PathBuf::from("application-pnpm");
1234
1235    let import_path_str = "stylex-lib-dist-main";
1236    let source_file_path = format!(
1237      "{}/src/pages/home.js",
1238      get_root_dir(&test_path).as_path().display()
1239    );
1240    let root_path = get_root_dir(&test_path).display().to_string();
1241    let aliases = FxHashMap::default();
1242
1243    let expected_result = format!(
1244      "{}/{}",
1245      root_path, "node_modules/stylex-lib-dist-main/dist/index.jsx"
1246    );
1247
1248    assert_eq!(
1249      resolve_file_path(
1250        import_path_str,
1251        source_file_path.as_str(),
1252        root_path.as_str(),
1253        &aliases,
1254        &mut HashMap::default(),
1255      )
1256      .unwrap_or_default()
1257      .display()
1258      .to_string(),
1259      expected_result
1260    );
1261  }
1262
1263  #[test]
1264  fn resolve_regular_external_import_with_exports_dist() {
1265    let test_path = PathBuf::from("application-pnpm");
1266
1267    let import_path_str = "stylex-lib-dist-exports-with-main/colors.stylex";
1268    let source_file_path = format!(
1269      "{}/src/pages/home.js",
1270      get_root_dir(&test_path).as_path().display()
1271    );
1272    let root_path = get_root_dir(&test_path).display().to_string();
1273    let aliases = FxHashMap::default();
1274
1275    let expected_result = format!(
1276      "{}/{}",
1277      root_path, "node_modules/stylex-lib-dist-exports-with-main/dist/colors.stylex.js",
1278    );
1279    assert_eq!(
1280      resolve_file_path(
1281        import_path_str,
1282        source_file_path.as_str(),
1283        root_path.as_str(),
1284        &aliases,
1285        &mut HashMap::default(),
1286      )
1287      .unwrap_or_default()
1288      .display()
1289      .to_string(),
1290      expected_result
1291    );
1292  }
1293
1294  #[test]
1295  fn resolve_package_with_pnpm_path() {
1296    let test_path = PathBuf::from("application-pnpm");
1297
1298    let import_path_str = "stylex-lib-pnpm";
1299    let source_file_path = format!(
1300      "{}/src/pages/home.js",
1301      get_root_dir(&test_path).as_path().display()
1302    );
1303    let root_path = get_root_dir(&test_path).display().to_string();
1304    let aliases = FxHashMap::default();
1305
1306    let expected_result = format!(
1307      "{}/{}",
1308      root_path,
1309      "node_modules/.pnpm/stylex-lib-pnpm@0.1.0/node_modules/stylex-lib-pnpm/dist/index.jsx"
1310    );
1311
1312    assert_eq!(
1313      resolve_file_path(
1314        import_path_str,
1315        source_file_path.as_str(),
1316        root_path.as_str(),
1317        &aliases,
1318        &mut HashMap::default(),
1319      )
1320      .unwrap_or_default()
1321      .display()
1322      .to_string(),
1323      expected_result
1324    );
1325  }
1326
1327  #[test]
1328  fn resolve_organisation_package_with_pnpm_path() {
1329    let test_path = PathBuf::from("application-pnpm");
1330
1331    let import_path_str = "@stylex/lib-exports-pnpm/colors.stylex";
1332    let source_file_path = format!(
1333      "{}/src/pages/home.js",
1334      get_root_dir(&test_path).as_path().display()
1335    );
1336    let root_path = get_root_dir(&test_path).display().to_string();
1337    let aliases = FxHashMap::default();
1338
1339    let expected_result = format!(
1340      "{}/{}",
1341      root_path,
1342      "node_modules/.pnpm/@stylex+lib-exports-pnpm@0.1.0/node_modules/@stylex/lib-exports-pnpm/dist/colors.stylex.js"
1343    );
1344
1345    assert_eq!(
1346      resolve_file_path(
1347        import_path_str,
1348        source_file_path.as_str(),
1349        root_path.as_str(),
1350        &aliases,
1351        &mut HashMap::default(),
1352      )
1353      .unwrap_or_default()
1354      .display()
1355      .to_string(),
1356      expected_result
1357    );
1358  }
1359
1360  #[test]
1361  fn resolve_organisation_package_with_pnpm_with_same_path() {
1362    let test_path = PathBuf::from("application-pnpm");
1363
1364    let import_path_str = "@stylex/lib-exports-pnpm/colors.stylex";
1365    let source_file_path = format!(
1366      "{}/src/pages/home.js",
1367      get_root_dir(&test_path).as_path().display()
1368    );
1369    let root_path = get_root_dir(&test_path).display().to_string();
1370    let aliases = FxHashMap::default();
1371
1372    let expected_result = format!(
1373      "{}/{}",
1374      root_path,
1375      "node_modules/.pnpm/@stylex+lib-exports-pnpm@0.1.0/node_modules/@stylex/lib-exports-pnpm/dist/colors.stylex.js"
1376    );
1377
1378    assert_eq!(
1379      resolve_file_path(
1380        import_path_str,
1381        source_file_path.as_str(),
1382        root_path.as_str(),
1383        &aliases,
1384        &mut HashMap::default(),
1385      )
1386      .unwrap_or_default()
1387      .display()
1388      .to_string(),
1389      expected_result
1390    );
1391  }
1392}
1393
1394#[cfg(test)]
1395mod resolve_path_application_npm_tests {
1396  use path_clean::PathClean;
1397  use rustc_hash::FxHashMap;
1398
1399  use crate::resolvers::{resolve_file_path, tests::get_root_dir};
1400
1401  use std::{collections::HashMap, path::PathBuf};
1402
1403  #[test]
1404  fn resolve_regular_local_import_from_src() {
1405    let test_path = PathBuf::from("application-npm/apps/web");
1406
1407    let import_path_str = "../colors.stylex.js";
1408    let source_file_path = format!(
1409      "{}/src/pages/home.js",
1410      get_root_dir(&test_path).as_path().display()
1411    );
1412    let root_path = get_root_dir(&test_path).display().to_string();
1413    let aliases = Default::default();
1414
1415    let expected_result = format!("{}/{}", root_path, "src/colors.stylex.js");
1416
1417    assert_eq!(
1418      resolve_file_path(
1419        import_path_str,
1420        source_file_path.as_str(),
1421        root_path.as_str(),
1422        &aliases,
1423        &mut HashMap::default(),
1424      )
1425      .unwrap_or_default()
1426      .display()
1427      .to_string(),
1428      expected_result
1429    );
1430  }
1431
1432  #[test]
1433  fn resolve_regular_local_import_from_same_level_directory() {
1434    let test_path = PathBuf::from("application-npm/apps/web");
1435
1436    let import_path_str = "../components/button.js";
1437    let source_file_path = format!(
1438      "{}/src/pages/home.js",
1439      get_root_dir(&test_path).as_path().display()
1440    );
1441    let root_path = get_root_dir(&test_path).display().to_string();
1442    let aliases = Default::default();
1443
1444    let expected_result = format!("{}/{}", root_path, "src/components/button.js");
1445
1446    assert_eq!(
1447      resolve_file_path(
1448        import_path_str,
1449        source_file_path.as_str(),
1450        root_path.as_str(),
1451        &aliases,
1452        &mut HashMap::default(),
1453      )
1454      .unwrap_or_default()
1455      .display()
1456      .to_string(),
1457      expected_result
1458    );
1459  }
1460
1461  #[test]
1462  fn resolve_regular_local_import_from_alias() {
1463    let test_path = PathBuf::from("application-npm/apps/web");
1464
1465    let import_path_str = "@/components/button.js";
1466    let source_file_path = format!(
1467      "{}/src/pages/home.js",
1468      get_root_dir(&test_path).as_path().display()
1469    );
1470    let root_path = get_root_dir(&test_path).display().to_string();
1471    let mut aliases = FxHashMap::default();
1472    aliases.insert("@/*".to_string(), vec![format!("{}/src/*", root_path)]);
1473
1474    let expected_result = format!("{}/{}", root_path, "src/components/button.js");
1475
1476    assert_eq!(
1477      resolve_file_path(
1478        import_path_str,
1479        source_file_path.as_str(),
1480        root_path.as_str(),
1481        &aliases,
1482        &mut HashMap::default(),
1483      )
1484      .unwrap_or_default()
1485      .display()
1486      .to_string(),
1487      expected_result
1488    );
1489  }
1490
1491  #[test]
1492  fn resolve_regular_local_import_from_workspace_alias() {
1493    let test_path = PathBuf::from("workspace-npm/apps/web");
1494
1495    let import_path_str = "@/components/button";
1496    let source_file_path = format!(
1497      "{}/src/pages/home.js",
1498      get_root_dir(&test_path).as_path().display()
1499    );
1500    let root_path = get_root_dir(&test_path).display().to_string();
1501    let mut aliases = FxHashMap::default();
1502    aliases.insert(
1503      "@/*".to_string(),
1504      vec![format!(
1505        "{}",
1506        PathBuf::from(&root_path)
1507          .join("../../../application-npm/apps/web/src/*")
1508          .clean()
1509          .to_string_lossy()
1510      )],
1511    );
1512
1513    let expected_result = format!(
1514      "{}",
1515      PathBuf::from(&root_path)
1516        .join("../../../application-npm/apps/web/src/components/button.js")
1517        .clean()
1518        .to_string_lossy(),
1519    );
1520
1521    assert_eq!(
1522      resolve_file_path(
1523        import_path_str,
1524        source_file_path.as_str(),
1525        root_path.as_str(),
1526        &aliases,
1527        &mut HashMap::default(),
1528      )
1529      .unwrap_or_default()
1530      .display()
1531      .to_string(),
1532      expected_result
1533    );
1534  }
1535
1536  #[test]
1537  fn resolve_regular_external_import() {
1538    let test_path = PathBuf::from("application-npm/apps/web");
1539
1540    let import_path_str = "stylex-lib-dist-main";
1541    let source_file_path = format!(
1542      "{}/src/pages/home.js",
1543      get_root_dir(&test_path).as_path().display()
1544    );
1545    let root_path = get_root_dir(&test_path).display().to_string();
1546    let aliases = FxHashMap::default();
1547
1548    let expected_result = format!(
1549      "{}/{}",
1550      root_path.replace("/apps/web", ""),
1551      "node_modules/stylex-lib-dist-main/dist/index.jsx"
1552    );
1553
1554    assert_eq!(
1555      resolve_file_path(
1556        import_path_str,
1557        source_file_path.as_str(),
1558        root_path.as_str(),
1559        &aliases,
1560        &mut HashMap::default(),
1561      )
1562      .unwrap_or_default()
1563      .display()
1564      .to_string(),
1565      expected_result
1566    );
1567  }
1568
1569  #[test]
1570  fn resolve_regular_external_import_with_exports_dist() {
1571    let test_path = PathBuf::from("application-npm/apps/web");
1572
1573    let import_path_str = "stylex-lib-dist-exports-with-main/colors.stylex";
1574    let source_file_path = format!(
1575      "{}/src/pages/home.js",
1576      get_root_dir(&test_path).as_path().display()
1577    );
1578    let root_path = get_root_dir(&test_path)
1579      .display()
1580      .to_string()
1581      .replace("/apps/web", "");
1582    let aliases = FxHashMap::default();
1583
1584    let expected_result = format!(
1585      "{}/{}",
1586      root_path, "node_modules/stylex-lib-dist-exports-with-main/dist/colors.stylex.js",
1587    );
1588
1589    assert_eq!(
1590      resolve_file_path(
1591        import_path_str,
1592        source_file_path.as_str(),
1593        root_path.as_str(),
1594        &aliases,
1595        &mut HashMap::default(),
1596      )
1597      .unwrap_or_default()
1598      .display()
1599      .to_string(),
1600      expected_result
1601    );
1602  }
1603}
1604#[cfg(test)]
1605mod resolve_path_aliases_tests {
1606  use rustc_hash::FxHashMap;
1607
1608  use crate::resolvers::possible_aliased_paths;
1609
1610  use std::path::PathBuf;
1611
1612  #[test]
1613  fn get_import_path_when_no_aliases() {
1614    assert_eq!(
1615      possible_aliased_paths("@stylexjs/stylex", &FxHashMap::default()),
1616      vec![PathBuf::from("@stylexjs/stylex")]
1617    );
1618  }
1619
1620  #[test]
1621  fn get_import_path_when_right_aliase() {
1622    assert_eq!(
1623      possible_aliased_paths(
1624        "@/components/button",
1625        &[("#/app/*".to_string(), vec![format!("{}/src/*", "root")])]
1626          .iter()
1627          .cloned()
1628          .collect::<FxHashMap<String, Vec<String>>>()
1629      ),
1630      vec![PathBuf::from("@/components/button")]
1631    );
1632  }
1633
1634  #[test]
1635  fn get_import_path_with_aliases() {
1636    assert_eq!(
1637      possible_aliased_paths(
1638        "@/components/button",
1639        &[("@/*".to_string(), vec!["/src/*".to_string()])]
1640          .iter()
1641          .cloned()
1642          .collect::<FxHashMap<String, Vec<String>>>()
1643      ),
1644      vec![
1645        PathBuf::from("@/components/button"),
1646        PathBuf::from("/src/components/button"),
1647      ]
1648    );
1649
1650    assert_eq!(
1651      possible_aliased_paths(
1652        "@/components/button",
1653        &[("@/*".to_string(), vec!["../../buttons/*".to_string()])]
1654          .iter()
1655          .cloned()
1656          .collect::<FxHashMap<String, Vec<String>>>()
1657      ),
1658      vec![
1659        PathBuf::from("@/components/button"),
1660        PathBuf::from("../../buttons/components/button"),
1661      ]
1662    );
1663  }
1664
1665  #[test]
1666  fn get_import_path_with_aliases_with_ext() {
1667    assert_eq!(
1668      possible_aliased_paths(
1669        "@/components/button.js",
1670        &[("@/*".to_string(), vec!["/src/*".to_string()])]
1671          .iter()
1672          .cloned()
1673          .collect::<FxHashMap<String, Vec<String>>>()
1674      ),
1675      vec![
1676        PathBuf::from("@/components/button.js"),
1677        PathBuf::from("/src/components/button.js"),
1678      ]
1679    );
1680  }
1681
1682  #[test]
1683  fn get_import_path_with_aliases_with_stylex_ext() {
1684    assert_eq!(
1685      possible_aliased_paths(
1686        "@/colors.stylex",
1687        &[("@/*".to_string(), vec!["/src/*".to_string()])]
1688          .iter()
1689          .cloned()
1690          .collect::<FxHashMap<String, Vec<String>>>()
1691      ),
1692      vec![
1693        PathBuf::from("@/colors.stylex"),
1694        PathBuf::from("/src/colors.stylex"),
1695      ]
1696    );
1697  }
1698}
1699
1700#[cfg(test)]
1701mod resolve_file_path_aliases_tests {
1702  use rustc_hash::FxHashMap;
1703  use std::{collections::HashMap, path::PathBuf};
1704
1705  use crate::resolvers::{resolve_file_path, tests::get_root_dir};
1706
1707  #[test]
1708  fn resolve_aliased_import_with_stylex_extension() {
1709    let test_path = PathBuf::from("application-pnpm");
1710
1711    // Import like @/colors.stylex should resolve to src/colors.stylex.js
1712    let import_path_str = "@/colors.stylex";
1713    let source_file_path = format!(
1714      "{}/src/pages/home.js",
1715      get_root_dir(&test_path).as_path().display()
1716    );
1717    let root_path = get_root_dir(&test_path).display().to_string();
1718    let mut aliases = FxHashMap::default();
1719    aliases.insert("@/*".to_string(), vec![format!("{}/src/*", root_path)]);
1720
1721    let expected_result = format!("{}/{}", root_path, "src/colors.stylex.js");
1722
1723    assert_eq!(
1724      resolve_file_path(
1725        import_path_str,
1726        source_file_path.as_str(),
1727        root_path.as_str(),
1728        &aliases,
1729        &mut HashMap::default(),
1730      )
1731      .unwrap_or_default()
1732      .display()
1733      .to_string(),
1734      expected_result
1735    );
1736  }
1737
1738  #[test]
1739  fn resolve_aliased_import_with_js_extension() {
1740    let test_path = PathBuf::from("application-pnpm");
1741
1742    let import_path_str = "@/components/button.js";
1743    let source_file_path = format!(
1744      "{}/src/pages/home.js",
1745      get_root_dir(&test_path).as_path().display()
1746    );
1747    let root_path = get_root_dir(&test_path).display().to_string();
1748    let mut aliases = FxHashMap::default();
1749    aliases.insert("@/*".to_string(), vec![format!("{}/src/*", root_path)]);
1750
1751    let expected_result = format!("{}/{}", root_path, "src/components/button.js");
1752
1753    assert_eq!(
1754      resolve_file_path(
1755        import_path_str,
1756        source_file_path.as_str(),
1757        root_path.as_str(),
1758        &aliases,
1759        &mut HashMap::default(),
1760      )
1761      .unwrap_or_default()
1762      .display()
1763      .to_string(),
1764      expected_result
1765    );
1766  }
1767
1768  #[test]
1769  fn resolve_aliased_import_without_extension() {
1770    let test_path = PathBuf::from("application-pnpm");
1771
1772    // Import without extension should try to add extensions
1773    let import_path_str = "@/components/button";
1774    let source_file_path = format!(
1775      "{}/src/pages/home.js",
1776      get_root_dir(&test_path).as_path().display()
1777    );
1778    let root_path = get_root_dir(&test_path).display().to_string();
1779    let mut aliases = FxHashMap::default();
1780    aliases.insert("@/*".to_string(), vec![format!("{}/src/*", root_path)]);
1781
1782    let expected_result = format!("{}/{}", root_path, "src/components/button.js");
1783
1784    assert_eq!(
1785      resolve_file_path(
1786        import_path_str,
1787        source_file_path.as_str(),
1788        root_path.as_str(),
1789        &aliases,
1790        &mut HashMap::default(),
1791      )
1792      .unwrap_or_default()
1793      .display()
1794      .to_string(),
1795      expected_result
1796    );
1797  }
1798}
1799
1800#[cfg(test)]
1801mod resolve_nested_external_imports_tests {
1802  use rustc_hash::FxHashMap;
1803
1804  use crate::{
1805    package_json::find_closest_node_modules,
1806    resolvers::{resolve_file_path, tests::get_root_dir},
1807  };
1808
1809  use std::{collections::HashMap, path::PathBuf};
1810
1811  #[test]
1812  fn resolve_regular_nested_import() {
1813    let test_path = PathBuf::from("exports/node_modules/stylex-lib-dist-main");
1814
1815    let import_path_str = "stylex-lib-dist-exports-with-main/colors.stylex";
1816    let source_file_path = format!(
1817      "{}/dist/index.jsx",
1818      get_root_dir(&test_path).as_path().display()
1819    );
1820    let root_path = get_root_dir(&test_path)
1821      .display()
1822      .to_string()
1823      .replace("/node_modules/stylex-lib-dist-main", "");
1824    let aliases = FxHashMap::default();
1825
1826    let expected_result = format!(
1827      "{}/{}",
1828      root_path, "node_modules/stylex-lib-dist-exports-with-main/dist/colors.stylex.js"
1829    );
1830
1831    assert_eq!(
1832      resolve_file_path(
1833        import_path_str,
1834        source_file_path.as_str(),
1835        root_path.as_str(),
1836        &aliases,
1837        &mut HashMap::default(),
1838      )
1839      .unwrap_or_default()
1840      .display()
1841      .to_string(),
1842      expected_result
1843    );
1844  }
1845
1846  #[test]
1847  fn resolve_nested_import_with_exports_and_nested_node_modules() {
1848    let test_path = PathBuf::from("exports/node_modules/stylex-lib-dist-main");
1849
1850    let import_path_str = "stylex-lib-dist-exports/colors.stylex";
1851    let source_file_path = format!(
1852      "{}/dist/index.jsx",
1853      get_root_dir(&test_path).as_path().display()
1854    );
1855    let root_path = get_root_dir(&test_path)
1856      .display()
1857      .to_string()
1858      .replace("/node_modules/stylex-lib-dist-main", "");
1859    let aliases = FxHashMap::default();
1860
1861    let expected_result = format!(
1862      "{}/{}",
1863      root_path, "node_modules/stylex-lib-dist-exports/dist/colors.stylex.js"
1864    );
1865
1866    assert_eq!(
1867      resolve_file_path(
1868        import_path_str,
1869        source_file_path.as_str(),
1870        root_path.as_str(),
1871        &aliases,
1872        &mut HashMap::default(),
1873      )
1874      .unwrap_or_default()
1875      .display()
1876      .to_string(),
1877      expected_result
1878    );
1879
1880    let test_nested_package_path = &get_root_dir(&test_path);
1881
1882    let closest_node_modules = find_closest_node_modules(test_nested_package_path);
1883
1884    assert_eq!(
1885      closest_node_modules
1886        .unwrap_or_default()
1887        .display()
1888        .to_string(),
1889      test_nested_package_path
1890        .join("node_modules")
1891        .to_string_lossy()
1892    );
1893  }
1894
1895  #[test]
1896  fn resolve_commonjs_exports() {
1897    let test_path = PathBuf::from("exports");
1898
1899    let import_path_str = "stylex-lib-dist-exports-commonjs-esm/colors.stylex";
1900    let source_file_path = format!(
1901      "{}/dist/index.jsx",
1902      get_root_dir(&test_path).as_path().display()
1903    );
1904    let root_path = get_root_dir(&test_path)
1905      .display()
1906      .to_string()
1907      .replace("/node_modules/stylex-lib-dist-exports-commonjs-esm", "");
1908    let aliases = FxHashMap::default();
1909
1910    let expected_result = format!(
1911      "{}/{}",
1912      root_path, "node_modules/stylex-lib-dist-exports-commonjs-esm/dist/colors.stylex.cjs"
1913    );
1914
1915    assert_eq!(
1916      resolve_file_path(
1917        import_path_str,
1918        source_file_path.as_str(),
1919        root_path.as_str(),
1920        &aliases,
1921        &mut HashMap::default(),
1922      )
1923      .unwrap_or_default()
1924      .display()
1925      .to_string(),
1926      expected_result
1927    );
1928
1929    let test_nested_package_path = &get_root_dir(&test_path);
1930
1931    let closest_node_modules = find_closest_node_modules(test_nested_package_path);
1932
1933    assert_eq!(
1934      closest_node_modules
1935        .unwrap_or_default()
1936        .display()
1937        .to_string(),
1938      test_nested_package_path
1939        .join("node_modules")
1940        .to_string_lossy()
1941    );
1942  }
1943
1944  #[test]
1945  fn resolve_esm_exports() {
1946    let test_path = PathBuf::from("exports");
1947
1948    let import_path_str = "stylex-lib-dist-exports-commonjs-esm";
1949    let source_file_path = format!(
1950      "{}/dist/index.jsx",
1951      get_root_dir(&test_path).as_path().display()
1952    );
1953    let root_path = get_root_dir(&test_path)
1954      .display()
1955      .to_string()
1956      .replace("/node_modules/stylex-lib-dist-exports-commonjs-esm", "");
1957    let aliases = FxHashMap::default();
1958
1959    let expected_result = format!(
1960      "{}/{}",
1961      root_path, "node_modules/stylex-lib-dist-exports-commonjs-esm/dist/index.cjs"
1962    );
1963
1964    assert_eq!(
1965      resolve_file_path(
1966        import_path_str,
1967        source_file_path.as_str(),
1968        root_path.as_str(),
1969        &aliases,
1970        &mut HashMap::default(),
1971      )
1972      .unwrap_or_default()
1973      .display()
1974      .to_string(),
1975      expected_result
1976    );
1977
1978    let test_nested_package_path = &get_root_dir(&test_path);
1979
1980    let closest_node_modules = find_closest_node_modules(test_nested_package_path);
1981
1982    assert_eq!(
1983      closest_node_modules
1984        .unwrap_or_default()
1985        .display()
1986        .to_string(),
1987      test_nested_package_path
1988        .join("node_modules")
1989        .to_string_lossy()
1990    );
1991  }
1992}