diff --git a/crates/parcel_plugin_transformer_js/src/transformer/conversion/dependency_kind.rs b/crates/parcel_plugin_transformer_js/src/transformer/conversion/dependency_kind.rs index c4b02543dbf..e8154bc0f4d 100644 --- a/crates/parcel_plugin_transformer_js/src/transformer/conversion/dependency_kind.rs +++ b/crates/parcel_plugin_transformer_js/src/transformer/conversion/dependency_kind.rs @@ -42,8 +42,10 @@ pub(crate) fn convert_specifier_type( #[cfg(test)] mod test { - use crate::transformer::test_helpers::run_swc_core_transform; - use parcel_js_swc_core::DependencyKind; + use crate::transformer::test_helpers::{ + make_test_swc_config, run_swc_core_transform, run_swc_core_transform_with_config, + }; + use parcel_js_swc_core::{Config, DependencyKind}; use super::*; @@ -59,6 +61,36 @@ mod test { assert_eq!(convert_specifier_type(&dependency), SpecifierType::Esm); } + #[test] + fn test_deferred_for_display_dependency_kind() { + let dependency = get_last_dependency_with_config(Config { + tier_imports: true, + ..make_test_swc_config( + r#" + const x = importDeferredForDisplay('other'); + "#, + ) + }); + assert_eq!(dependency.kind, DependencyKind::DeferredForDisplayImport); + assert_eq!(convert_priority(&dependency), Priority::Tier); + assert_eq!(convert_specifier_type(&dependency), SpecifierType::Esm); + } + + #[test] + fn test_deferred_dependency_kind() { + let dependency = get_last_dependency_with_config(Config { + tier_imports: true, + ..make_test_swc_config( + r#" + const x = importDeferred('other'); + "#, + ) + }); + assert_eq!(dependency.kind, DependencyKind::DeferredImport); + assert_eq!(convert_priority(&dependency), Priority::Tier); + assert_eq!(convert_specifier_type(&dependency), SpecifierType::Esm); + } + #[test] fn test_dynamic_import_dependency_kind() { let dependency = get_last_dependency( @@ -171,4 +203,10 @@ mod test { let swc_output = run_swc_core_transform(source); swc_output.dependencies.last().unwrap().clone() } + + /// Run the SWC transformer with a config and return the last dependency descriptor listed + fn get_last_dependency_with_config(config: Config) -> parcel_js_swc_core::DependencyDescriptor { + let swc_output = run_swc_core_transform_with_config(config); + swc_output.dependencies.last().unwrap().clone() + } } diff --git a/crates/parcel_plugin_transformer_js/src/transformer/test_helpers.rs b/crates/parcel_plugin_transformer_js/src/transformer/test_helpers.rs index c89c1518bf8..7b3da009ff3 100644 --- a/crates/parcel_plugin_transformer_js/src/transformer/test_helpers.rs +++ b/crates/parcel_plugin_transformer_js/src/transformer/test_helpers.rs @@ -6,6 +6,12 @@ pub(crate) fn run_swc_core_transform(source: &str) -> TransformResult { swc_output } +/// Parse a file with the `parcel_js_swc_core` parser for testing and specify a config +pub(crate) fn run_swc_core_transform_with_config(config: Config) -> TransformResult { + let swc_output = parcel_js_swc_core::transform(config, None).unwrap(); + swc_output +} + /// SWC configuration for testing pub(crate) fn make_test_swc_config(source: &str) -> Config { Config { diff --git a/packages/transformers/js/core/src/dependency_collector.rs b/packages/transformers/js/core/src/dependency_collector.rs index 3912be20bf2..49ddfeb1fbb 100644 --- a/packages/transformers/js/core/src/dependency_collector.rs +++ b/packages/transformers/js/core/src/dependency_collector.rs @@ -441,10 +441,15 @@ impl<'a> Fold for DependencyCollector<'a> { Callee::Import(_) => DependencyKind::DynamicImport, Callee::Expr(expr) => { match &**expr { - Ident(ident) if ident.sym.to_string().as_str() == "importDeferredForDisplay" => { + Ident(ident) + if self.config.tier_imports + && ident.sym.to_string().as_str() == "importDeferredForDisplay" => + { DependencyKind::DeferredForDisplayImport } - Ident(ident) if ident.sym.to_string().as_str() == "importDeferred" => { + Ident(ident) + if self.config.tier_imports && ident.sym.to_string().as_str() == "importDeferred" => + { DependencyKind::DeferredImport } Ident(ident) => {