Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(turbopack): Improve analyzer of tree-shaking pass #70017

Merged
merged 15 commits into from
Sep 16, 2024
Merged
3 changes: 2 additions & 1 deletion turbopack/crates/turbopack-ecmascript/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ pub async fn minify(path: Vc<FileSystemPath>, code: Vc<Code>) -> Result<Vc<Code>
let program = match parser.parse_program() {
Ok(program) => program,
Err(err) => {
err.into_diagnostic(handler).emit();
// TODO should emit an issue
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
bail!(
"failed to parse source code\n{err:?}\n{}",
"failed to parse source code\n{}",
code.source_code().to_str()?
)
}
Expand Down
39 changes: 27 additions & 12 deletions turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use serde::{Deserialize, Serialize};
use swc_core::{
common::DUMMY_SP,
ecma::ast::{
self, ComputedPropName, Expr, ExprStmt, Ident, KeyValueProp, Lit, MemberExpr, MemberProp,
ModuleItem, ObjectLit, Program, Prop, PropName, PropOrSpread, Script, Stmt, Str,
self, AssignTarget, ComputedPropName, Expr, ExprStmt, Ident, KeyValueProp, Lit, MemberExpr,
MemberProp, ModuleItem, ObjectLit, Program, Prop, PropName, PropOrSpread, Script,
SimpleAssignTarget, Stmt, Str,
},
quote, quote_expr,
};
Expand All @@ -28,7 +29,7 @@ use super::base::ReferencedAsset;
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
code_gen::{CodeGenerateable, CodeGeneration},
create_visitor,
create_visitor, magic_identifier,
references::esm::base::insert_hoisted_stmt,
};

Expand Down Expand Up @@ -497,12 +498,17 @@ impl CodeGenerateable for EsmExports {
"(() => { throw new Error(\"Failed binding. See build errors!\"); })" as Expr,
)),
EsmExport::LocalBinding(name, mutable) => {
let local = if name == "default" {
magic_identifier::mangle("default export").into()
} else {
name.clone()
};
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
if *mutable {
Some(quote!(
"([() => $local, ($new) => $local = $new])" as Expr,
local = Ident::new((name as &str).into(), DUMMY_SP, Default::default()),
local = Ident::new(local.as_str().into(), DUMMY_SP, Default::default()),
new = Ident::new(
format!("{name}_new_value").into(),
format!("new_{name}").into(),
DUMMY_SP,
Default::default()
),
Expand All @@ -518,9 +524,13 @@ impl CodeGenerateable for EsmExports {
let referenced_asset =
ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?;
referenced_asset.get_ident().await?.map(|ident| {
let expr = Expr::Member(MemberExpr {
let expr = MemberExpr {
span: DUMMY_SP,
obj: Box::new(Expr::Ident(Ident::new(ident.into(), DUMMY_SP, Default::default()))),
obj: Box::new(Expr::Ident(Ident::new(
ident.into(),
DUMMY_SP,
Default::default(),
))),
prop: MemberProp::Computed(ComputedPropName {
span: DUMMY_SP,
expr: Box::new(Expr::Lit(Lit::Str(Str {
Expand All @@ -529,17 +539,22 @@ impl CodeGenerateable for EsmExports {
raw: None,
}))),
}),
});
};
if *mutable {
quote!(
"([() => $expr, ($new) => $expr = $new])" as Expr,
expr: Expr = expr,
new = Ident::new(format!("{name}_new_value").into(), DUMMY_SP, Default::default()),
"([() => $expr, ($new) => $lhs = $new])" as Expr,
expr: Expr = Expr::Member(expr.clone()),
lhs: AssignTarget = AssignTarget::Simple(SimpleAssignTarget::Member(expr)),
new = Ident::new(
format!("new_{name}").into(),
DUMMY_SP,
Default::default()
),
)
} else {
quote!(
"(() => $expr)" as Expr,
expr: Expr = expr,
expr: Expr = Expr::Member(expr),
)
}
})
Expand Down
27 changes: 26 additions & 1 deletion turbopack/crates/turbopack-ecmascript/src/swc_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{cell::RefCell, collections::HashMap, mem::take};
use swc_core::{
base::SwcComments,
common::{
comments::{Comment, Comments},
comments::{Comment, CommentKind, Comments},
BytePos,
},
};
Expand Down Expand Up @@ -130,6 +130,31 @@ impl Comments for ImmutableComments {
panic!("Comments are immutable after parsing")
}

fn has_flag(&self, pos: BytePos, flag: &str) -> bool {
self.with_leading(pos, |cmts| {
for c in cmts {
if c.kind == CommentKind::Block {
for line in c.text.lines() {
// jsdoc
let line = line.trim_start_matches(['*', ' ']);
let line = line.trim();

//
if line.len() == (flag.len() + 5)
&& (line.starts_with("#__") || line.starts_with("@__"))
&& line.ends_with("__")
&& flag == &line[3..line.len() - 2]
{
return true;
}
}
}
}

false
})
}

fn with_leading<F, Ret>(&self, pos: BytePos, f: F) -> Ret
where
Self: Sized,
Expand Down
17 changes: 11 additions & 6 deletions turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use turbopack_core::{

use super::{
chunk_item::EcmascriptModulePartChunkItem, get_part_id, part_of_module, split, split_module,
Key, SplitResult,
Key, PartId, SplitResult,
};
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
Expand All @@ -27,7 +27,7 @@ use crate::{
#[turbo_tasks::value]
pub struct EcmascriptModulePartAsset {
pub full_module: Vc<EcmascriptModuleAsset>,
pub(crate) part: Vc<ModulePart>,
pub part: Vc<ModulePart>,
}

#[turbo_tasks::value_impl]
Expand All @@ -40,10 +40,10 @@ impl EcmascriptParsable for EcmascriptModulePartAsset {
let split_data = split(this.full_module.ident(), this.full_module.source(), parsed);
Ok(part_of_module(split_data, this.part))
}

#[turbo_tasks::function]
async fn parse_original(self: Vc<Self>) -> Result<Vc<ParseResult>> {
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
Ok(self.await?.full_module.parse_original())
let this = self.await?;
Ok(this.full_module.parse_original())
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -198,11 +198,16 @@ impl Module for EcmascriptModulePartAsset {

let mut assets = deps
.iter()
.map(|&part_id| {
.map(|part_id| {
Ok(Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
ModulePart::internal(part_id),
match part_id {
PartId::Internal(part_id) => ModulePart::internal(*part_id),
PartId::Export(name) => ModulePart::export(name.clone()),
PartId::ModuleEvaluation => ModulePart::evaluation(),
PartId::Exports => ModulePart::exports(),
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
},
)),
Vc::cell("ecmascript module part".into()),
)))
Expand Down
Loading
Loading