|
1 | 1 | use crate::resolve::{is_remote_url, DependencyDescriptor, InlineStyle, Resolver};
|
2 |
| -use crate::resolve_fold::create_aleph_pack_var_decl; |
| 2 | +use crate::resolve_fold::create_aleph_pack_var_decl_member; |
3 | 3 |
|
4 | 4 | use sha1::{Digest, Sha1};
|
5 | 5 | use std::{cell::RefCell, rc::Rc};
|
@@ -371,12 +371,11 @@ impl Fold for AlephJsxBuiltinModuleResolveFold {
|
371 | 371 | if resolver.bundle_mode {
|
372 | 372 | items.push(ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl {
|
373 | 373 | span: DUMMY_SP,
|
374 |
| - kind: VarDeclKind::Var, |
| 374 | + kind: VarDeclKind::Const, |
375 | 375 | declare: false,
|
376 |
| - decls: vec![create_aleph_pack_var_decl( |
377 |
| - id, |
| 376 | + decls: vec![create_aleph_pack_var_decl_member( |
378 | 377 | fixed_url.as_str(),
|
379 |
| - Some("default"), |
| 378 | + vec![(id, Some("default".into()))], |
380 | 379 | )],
|
381 | 380 | }))));
|
382 | 381 | } else {
|
@@ -431,3 +430,126 @@ fn rename_builtin_tag(name: &str) -> String {
|
431 | 430 | }
|
432 | 431 | "__ALEPH_".to_owned() + name.as_str()
|
433 | 432 | }
|
| 433 | + |
| 434 | +#[cfg(test)] |
| 435 | +mod tests { |
| 436 | + use crate::resolve::DependencyDescriptor; |
| 437 | + use crate::swc::st; |
| 438 | + |
| 439 | + #[test] |
| 440 | + fn resolve_jsx_builtin_tags() { |
| 441 | + let source = r#" |
| 442 | + import React from "https://esm.sh/react" |
| 443 | + export default function Index() { |
| 444 | + return ( |
| 445 | + <> |
| 446 | + <head> |
| 447 | + <title>Hello World!</title> |
| 448 | + <link rel="stylesheet" href="../style/index.css" /> |
| 449 | + </head> |
| 450 | + <a href="/about">About</a> |
| 451 | + <a href="https://github.com">About</a> |
| 452 | + <a href="/about" target="_blank">About</a> |
| 453 | + <script src="ga.js"></script> |
| 454 | + <script>{` |
| 455 | + function gtag() { |
| 456 | + dataLayer.push(arguments) |
| 457 | + } |
| 458 | + window.dataLayer = window.dataLayer || []; |
| 459 | + gtag("js", new Date()); |
| 460 | + gtag("config", "G-1234567890"); |
| 461 | + `}</script> |
| 462 | + </> |
| 463 | + ) |
| 464 | + } |
| 465 | + "#; |
| 466 | + let (code, resolver) = st("/pages/index.tsx", source, false); |
| 467 | + assert!(code.contains( |
| 468 | + "import __ALEPH_Anchor from \"../-/deno.land/x/[email protected]/framework/react/anchor.js\"" |
| 469 | + )); |
| 470 | + assert!(code.contains( |
| 471 | + "import __ALEPH_Head from \"../-/deno.land/x/[email protected]/framework/react/head.js\"" |
| 472 | + )); |
| 473 | + assert!(code.contains( |
| 474 | + "import __ALEPH_Stylelink from \"../-/deno.land/x/[email protected]/framework/react/stylelink.js\"" |
| 475 | + )); |
| 476 | + assert!(code.contains( |
| 477 | + "import __ALEPH_Script from \"../-/deno.land/x/[email protected]/framework/react/script.js\"" |
| 478 | + )); |
| 479 | + assert!(code.contains("React.createElement(\"a\",")); |
| 480 | + assert!(code.contains("React.createElement(__ALEPH_Anchor,")); |
| 481 | + assert!(code.contains("React.createElement(__ALEPH_Head,")); |
| 482 | + assert!(code.contains("React.createElement(__ALEPH_Stylelink,")); |
| 483 | + assert!(code.contains("href: \"/style/index.css\"")); |
| 484 | + assert!(code.contains( |
| 485 | + format!( |
| 486 | + "import \"../style/index.css.js#{}@000000\"", |
| 487 | + "/style/index.css" |
| 488 | + ) |
| 489 | + .as_str() |
| 490 | + )); |
| 491 | + assert!(code.contains("React.createElement(__ALEPH_Script,")); |
| 492 | + let r = resolver.borrow_mut(); |
| 493 | + assert_eq!( |
| 494 | + r.dep_graph, |
| 495 | + vec![ |
| 496 | + DependencyDescriptor { |
| 497 | + specifier: "https://esm.sh/react".into(), |
| 498 | + is_dynamic: false, |
| 499 | + }, |
| 500 | + DependencyDescriptor { |
| 501 | + specifier: "/style/index.css".into(), |
| 502 | + is_dynamic: false, |
| 503 | + }, |
| 504 | + DependencyDescriptor { |
| 505 | + specifier : "https://deno.land/x/[email protected]/framework/react/head.ts".into (), |
| 506 | + is_dynamic: false, |
| 507 | + }, |
| 508 | + DependencyDescriptor { |
| 509 | + specifier : "https://deno.land/x/[email protected]/framework/react/stylelink.ts".into (), |
| 510 | + is_dynamic: false, |
| 511 | + }, |
| 512 | + DependencyDescriptor { |
| 513 | + specifier : "https://deno.land/x/[email protected]/framework/react/anchor.ts".into (), |
| 514 | + is_dynamic: false, |
| 515 | + }, |
| 516 | + DependencyDescriptor { |
| 517 | + specifier : "https://deno.land/x/[email protected]/framework/react/script.ts".into (), |
| 518 | + is_dynamic: false, |
| 519 | + } |
| 520 | + ] |
| 521 | + ); |
| 522 | + } |
| 523 | + |
| 524 | + #[test] |
| 525 | + fn resolve_inlie_style() { |
| 526 | + let source = r#" |
| 527 | + export default function Index() { |
| 528 | + const [color, setColor] = useState('white'); |
| 529 | +
|
| 530 | + return ( |
| 531 | + <> |
| 532 | + <style>{` |
| 533 | + :root { |
| 534 | + --color: ${color}; |
| 535 | + } |
| 536 | + `}</style> |
| 537 | + <style>{` |
| 538 | + h1 { |
| 539 | + font-size: 12px; |
| 540 | + } |
| 541 | + `}</style> |
| 542 | + </> |
| 543 | + ) |
| 544 | + } |
| 545 | + "#; |
| 546 | + let (code, resolver) = st("/pages/index.tsx", source, false); |
| 547 | + assert!(code.contains( |
| 548 | + "import __ALEPH_Style from \"../-/deno.land/x/[email protected]/framework/react/style.js\"" |
| 549 | + )); |
| 550 | + assert!(code.contains("React.createElement(__ALEPH_Style,")); |
| 551 | + assert!(code.contains("__styleId: \"inline-style-")); |
| 552 | + let r = resolver.borrow_mut(); |
| 553 | + assert!(r.inline_styles.len() == 2); |
| 554 | + } |
| 555 | +} |
0 commit comments