Skip to content

Commit 308e7ff

Browse files
Implement native react_async_import_lift transformer (#902)
1 parent 96f1d7f commit 308e7ff

File tree

4 files changed

+499
-9
lines changed

4 files changed

+499
-9
lines changed

.changeset/violet-hats-watch.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@atlaspack/transformer-js': minor
3+
'@atlaspack/rust': minor
4+
---
5+
6+
- Implement new `react_async_import_lift` transformer
7+
- Hook up the new transformer with correct ordering in `lib.rs`

packages/transformers/js/core/src/lib.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod hoist;
1313
mod lazy_loading_transformer;
1414
mod magic_comments;
1515
mod node_replacer;
16+
mod react_async_import_lift;
1617
mod react_hooks_remover;
1718
mod static_prevaluator;
1819
mod sync_dynamic_import;
@@ -56,6 +57,7 @@ use magic_comments::MagicCommentsVisitor;
5657
use node_replacer::NodeReplacer;
5758
use path_slash::PathExt;
5859
use pathdiff::diff_paths;
60+
use react_async_import_lift::ReactAsyncImportLift;
5961
use react_hooks_remover::ReactHooksRemover;
6062
use serde::Deserialize;
6163
use serde::Serialize;
@@ -159,15 +161,18 @@ pub struct Config {
159161
pub hmr_improvements: bool,
160162
pub magic_comments: bool,
161163
pub exports_rebinding_optimisation: bool,
162-
pub sync_dynamic_import_config: Option<SyncDynamicImportConfig>,
163164
pub nested_promise_import_fix: bool,
164165
pub global_aliasing_config: Option<HashMap<String, String>>,
165166
pub enable_lazy_loading: bool,
166167
pub enable_ssr_typeof_replacement: bool,
167168
pub enable_react_hooks_removal: bool,
169+
pub enable_react_async_import_lift: bool,
170+
pub react_async_lift_by_default: bool,
171+
pub react_async_lift_report_level: String,
168172
pub enable_static_prevaluation: bool,
169173
pub enable_dead_returns_removal: bool,
170174
pub enable_unused_bindings_removal: bool,
175+
pub sync_dynamic_import_config: Option<SyncDynamicImportConfig>,
171176
}
172177

173178
#[derive(Serialize, Debug, Default)]
@@ -420,6 +425,20 @@ pub fn transform(
420425
));
421426
}
422427

428+
let module = module.apply((
429+
Optional::new(
430+
visit_mut_pass(ReactAsyncImportLift::new(global_mark, config.react_async_lift_by_default, config.react_async_lift_report_level.clone())),
431+
config.enable_react_async_import_lift && ReactAsyncImportLift::should_transform(code)
432+
),
433+
Optional::new(
434+
visit_mut_pass(
435+
SyncDynamicImport::new(Path::new(&config.filename),
436+
unresolved_mark,
437+
&config.sync_dynamic_import_config,
438+
)),
439+
config.sync_dynamic_import_config.is_some()),
440+
));
441+
423442
let mut module = {
424443
let mut passes = (
425444
Optional::new(
@@ -440,13 +459,6 @@ pub fn transform(
440459
}),
441460
config.source_type != SourceType::Script
442461
),
443-
Optional::new(
444-
visit_mut_pass(
445-
SyncDynamicImport::new(Path::new(&config.filename),
446-
unresolved_mark,
447-
&config.sync_dynamic_import_config,
448-
)),
449-
config.sync_dynamic_import_config.is_some()),
450462
Optional::new(
451463
visit_mut_pass(GlobalAliaser::with_config(unresolved_mark, &config.global_aliasing_config)),
452464
config.global_aliasing_config.is_some()

0 commit comments

Comments
 (0)