Skip to content

Commit

Permalink
refactor(next-core): remove ast cloning in custom transform (#49560)
Browse files Browse the repository at this point in the history
### What?

- closes WEB-1024.

Minor refactoring to avoid explicit AST cloning. Still visitors are
using fold though.

### Turbopack changes

* vercel/turbo#4869
* vercel/turbo#4879
* vercel/turbo#4881
  • Loading branch information
kwonoj committed May 10, 2023
1 parent 11a7117 commit 1ed7544
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 89 deletions.
70 changes: 37 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ swc_core = { version = "0.75.41" }
testing = { version = "0.33.4" }

# Turbo crates
turbo-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230508.2" }
turbo-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230510.1" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230508.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230510.1" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230508.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230510.1" }

# General Deps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ pub use next_dynamic::get_next_dynamic_transform_rule;
pub use next_font::get_next_font_transform_rule;
pub use next_strip_page_exports::get_next_pages_transforms_rule;
pub use relay::get_relay_transform_plugin;
use swc_core::{
common::util::take::Take,
ecma::ast::{Module, ModuleItem, Program},
};
use turbo_binding::turbopack::{
core::reference_type::{ReferenceType, UrlReferenceSubType},
turbopack::module_options::{ModuleRule, ModuleRuleCondition, ModuleRuleEffect, ModuleType},
Expand Down Expand Up @@ -53,18 +49,3 @@ pub(crate) fn module_rule_match_js_no_url() -> ModuleRuleCondition {
]),
])
}

pub(crate) fn unwrap_module_program(program: &mut Program) -> Program {
match program {
Program::Module(module) => Program::Module(module.take()),
Program::Script(s) => Program::Module(Module {
span: s.span,
body: s
.body
.iter()
.map(|stmt| ModuleItem::Stmt(stmt.clone()))
.collect(),
shebang: s.shebang.clone(),
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,14 @@ impl ModularizeImportsTransformer {

#[async_trait]
impl CustomTransformer for ModularizeImportsTransformer {
async fn transform(
&self,
program: &mut Program,
_ctx: &TransformContext<'_>,
) -> Result<Option<Program>> {
async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy()));
*program = p.fold_with(&mut modularize_imports(
turbo_binding::swc::custom_transform::modularize_imports::Config {
packages: self.packages.clone(),
},
));

Ok(None)
Ok(())
}
}

0 comments on commit 1ed7544

Please sign in to comment.