Skip to content

Commit

Permalink
Sqaush: #70017
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Sep 15, 2024
1 parent d66a827 commit a4cb1b1
Show file tree
Hide file tree
Showing 30 changed files with 6,367 additions and 3,088 deletions.
6 changes: 3 additions & 3 deletions turbopack/crates/turbopack-ecmascript/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ pub async fn minify(path: Vc<FileSystemPath>, code: Vc<Code>) -> Result<Vc<Code>
None,
);
let mut parser = Parser::new_from(lexer);
// TODO should use our own handler that emits issues instead.

let program = try_with_handler(cm.clone(), Default::default(), |handler| {
GLOBALS.set(&Default::default(), || {
let program = match parser.parse_program() {
Ok(program) => program,
Err(err) => {
// TODO should emit an issue
err.into_diagnostic(handler).emit();
bail!(
"failed to parse source code\n{err:?}\n{}",
"failed to parse source code\n{}",
code.source_code().to_str()?
)
}
Expand Down
40 changes: 28 additions & 12 deletions turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
borrow::Cow,
collections::{BTreeMap, HashSet},
ops::ControlFlow,
};
Expand All @@ -9,8 +10,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 +30,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 +499,17 @@ impl CodeGenerateable for EsmExports {
"(() => { throw new Error(\"Failed binding. See build errors!\"); })" as Expr,
)),
EsmExport::LocalBinding(name, mutable) => {
let local = if name == "default" {
Cow::Owned(magic_identifier::mangle("default export"))
} else {
Cow::Borrowed(name.as_str())
};
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.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 +525,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 +540,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
23 changes: 14 additions & 9 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,15 +40,14 @@ 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>> {
Ok(self.await?.full_module.parse_original())
fn parse_original(&self) -> Result<Vc<ParseResult>> {
Ok(self.full_module.parse_original())
}

#[turbo_tasks::function]
async fn ty(self: Vc<Self>) -> Result<Vc<EcmascriptModuleAssetType>> {
Ok(self.await?.full_module.ty())
async fn ty(&self) -> Result<Vc<EcmascriptModuleAssetType>> {
Ok(self.full_module.ty())
}
}

Expand Down Expand Up @@ -198,11 +197,17 @@ 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()),
_ => unreachable!(
"PartId other than Internal and Export should not be used here"
),
},
)),
Vc::cell("ecmascript module part".into()),
)))
Expand Down
Loading

0 comments on commit a4cb1b1

Please sign in to comment.