Skip to content

Commit

Permalink
chore(rust): cargo update + fixups
Browse files Browse the repository at this point in the history
note that typescript will now shortcut enums
  • Loading branch information
wmertens committed Apr 26, 2024
1 parent 760eec9 commit 1cdf6ef
Show file tree
Hide file tree
Showing 111 changed files with 1,200 additions and 1,362 deletions.
992 changes: 490 additions & 502 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/qwik/src/optimizer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ categories = ["development-tools", "development-tools::cargo-plugins"]
crate-type = ["rlib"]

[dependencies]
swc_ecmascript = { version = "0.227.16", features = ["codegen", "utils", "visit", "parser", "transforms", "typescript", "react", "optimization"] }
swc_common = { version = "0.31.4", features = ["sourcemap"] }
swc_atoms = "0.5.3"
swc_ecmascript = { version = "*", features = ["codegen", "utils", "visit", "parser", "transforms", "typescript", "react", "optimization"] }
swc_common = { version = "*", features = ["sourcemap"] }
swc_atoms = "*"
serde = "1.0.160"
serde_bytes = "0.11.9"
serde_json = "1.0.96"
Expand Down
3 changes: 2 additions & 1 deletion packages/qwik/src/optimizer/core/src/add_side_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ impl<'a> VisitMut for SideEffectVisitor<'a> {
node.body.insert(
0,
ast::ModuleItem::ModuleDecl(ast::ModuleDecl::Import(ast::ImportDecl {
asserts: None,
with: None,
phase: Default::default(),
span: DUMMY_SP,
specifiers: vec![],
type_only: false,
Expand Down
15 changes: 7 additions & 8 deletions packages/qwik/src/optimizer/core/src/code_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn new_module(ctx: NewModuleCtx) -> Result<(ast::Module, SingleThreadedComme

let has_scoped_idents = ctx.need_transform && !ctx.scoped_idents.is_empty();
let use_lexical_scope = if has_scoped_idents {
let new_local = id!(private_ident!(&USE_LEXICAL_SCOPE.clone()));
let new_local = id!(private_ident!(&*USE_LEXICAL_SCOPE.clone()));
module
.body
.push(create_synthetic_named_import(&new_local, ctx.core_module));
Expand Down Expand Up @@ -93,7 +93,8 @@ pub fn new_module(ctx: NewModuleCtx) -> Result<(ast::Module, SingleThreadedComme
ast::ImportDecl {
span: DUMMY_SP,
type_only: false,
asserts: import.asserts.clone(),
with: import.asserts.clone(),
phase: Default::default(),
src: Box::new(ast::Str {
span: DUMMY_SP,
value: fix_path(
Expand Down Expand Up @@ -121,7 +122,8 @@ pub fn new_module(ctx: NewModuleCtx) -> Result<(ast::Module, SingleThreadedComme
ast::ImportDecl {
span: DUMMY_SP,
type_only: false,
asserts: None,
with: None,
phase: Default::default(),
src: Box::new(ast::Str {
span: DUMMY_SP,
value: fix_path(
Expand Down Expand Up @@ -241,10 +243,7 @@ pub fn generate_entries(
let hooks: Vec<&HookAnalysis> = output.modules.iter().flat_map(|m| &m.hook).collect();
for hook in hooks {
if let Some(ref e) = hook.entry {
entries_map
.entry(e.as_ref())
.or_insert_with(Vec::new)
.push(hook);
entries_map.entry(e.as_ref()).or_default().push(hook);
}
}

Expand Down Expand Up @@ -293,7 +292,7 @@ fn new_entry_module(
ast::NamedExport {
span: DUMMY_SP,
type_only: false,
asserts: None,
with: None,
src: Some(Box::new(ast::Str {
span: DUMMY_SP,
value: JsWord::from(src),
Expand Down
12 changes: 6 additions & 6 deletions packages/qwik/src/optimizer/core/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct GlobalCollect {
in_export_decl: bool,
}

pub fn global_collect(module: &ast::Module) -> GlobalCollect {
pub fn global_collect(program: &ast::Program) -> GlobalCollect {
let mut collect = GlobalCollect {
synthetic: vec![],
imports: HashMap::with_capacity(16),
Expand All @@ -62,7 +62,7 @@ pub fn global_collect(module: &ast::Module) -> GlobalCollect {

in_export_decl: false,
};
module.visit_with(&mut collect);
program.visit_with(&mut collect);
collect
}

Expand Down Expand Up @@ -93,7 +93,7 @@ impl GlobalCollect {
.cloned()
.map_or_else(
|| {
let local = id!(private_ident!(specifier));
let local = id!(private_ident!(specifier.clone()));
self.add_import(
local.clone(),
Import {
Expand Down Expand Up @@ -175,7 +175,7 @@ impl Visit for GlobalCollect {
specifier: imported,
kind: ImportKind::Named,
synthetic: false,
asserts: node.asserts.clone(),
asserts: node.with.clone(),
},
);
}
Expand All @@ -187,7 +187,7 @@ impl Visit for GlobalCollect {
specifier: js_word!("default"),
kind: ImportKind::Default,
synthetic: false,
asserts: node.asserts.clone(),
asserts: node.with.clone(),
},
);
}
Expand All @@ -199,7 +199,7 @@ impl Visit for GlobalCollect {
specifier: "*".into(),
kind: ImportKind::All,
synthetic: false,
asserts: node.asserts.clone(),
asserts: node.with.clone(),
},
);
}
Expand Down
7 changes: 1 addition & 6 deletions packages/qwik/src/optimizer/core/src/inlined_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,7 @@ pub fn render_expr(expr: &ast::Expr) -> String {
let mut buf = Vec::new();
let source_map = Lrc::new(SourceMap::default());
let writer = Box::new(JsWriter::new(Lrc::clone(&source_map), "\n", &mut buf, None));
let config = swc_ecmascript::codegen::Config {
minify: true,
target: ast::EsVersion::latest(),
ascii_only: false,
omit_last_semi: false,
};
let config = swc_ecmascript::codegen::Config::default().with_minify(true);
let mut emitter = swc_ecmascript::codegen::Emitter {
cfg: config,
comments: None,
Expand Down
3 changes: 0 additions & 3 deletions packages/qwik/src/optimizer/core/src/package_json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub use crate::entry_strategy::EntryStrategy;
pub use crate::parse::{ErrorBuffer, HookAnalysis, MinifyMode, TransformModule, TransformOutput};

#[cfg(feature = "fs")]
pub fn find_modules(
src_dir: &std::path::Path,
Expand Down
91 changes: 35 additions & 56 deletions packages/qwik/src/optimizer/core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl Emitter for ErrorBuffer {
pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, anyhow::Error> {
let source_map = Lrc::new(SourceMap::default());
let path_data = parse_path(config.relative_path, config.src_dir)?;
let module = parse(
let result = parse(
config.code,
&path_data,
config.root_dir,
Expand All @@ -218,8 +218,8 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a

let origin: JsWord = path_data.rel_path.to_slash_lossy().into();

match module {
Ok((main_module, comments, is_type_script, is_jsx)) => {
match result {
Ok((program, comments, is_type_script, is_jsx)) => {
let extension = match (transpile_ts, transpile_jsx, is_type_script, is_jsx) {
(true, true, _, _) => JsWord::from("js"),
(true, false, _, true) => JsWord::from("jsx"),
Expand All @@ -240,35 +240,20 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();

let mut main_module = main_module;
let mut program = program;

if let Some(strip_exports) = config.strip_exports {
let mut visitor = StripExportsVisitor::new(strip_exports);
main_module.visit_mut_with(&mut visitor);
program.visit_mut_with(&mut visitor);
}

let mut did_transform = false;

// Transpile JSX
if transpile_ts && is_type_script {
did_transform = true;
main_module = if is_jsx {
main_module.fold_with(&mut typescript::strip_with_jsx(
Lrc::clone(&source_map),
typescript::Config {
pragma: Some("h".to_string()),
pragma_frag: Some("Fragment".to_string()),
..Default::default()
},
Some(&comments),
top_level_mark,
))
} else {
main_module.fold_with(&mut typescript::strip(top_level_mark))
}
program.visit_mut_with(&mut typescript::strip(top_level_mark))
}

// Transpile JSX
if transpile_jsx && is_jsx {
did_transform = true;
let mut react_options = react::Options::default();
Expand All @@ -278,7 +263,7 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
react_options.runtime = Some(react::Runtime::Automatic);
react_options.import_source = Some("@builder.io/qwik".to_string());
};
main_module = main_module.fold_with(&mut react::react(
program.visit_mut_with(&mut react::react(
Lrc::clone(&source_map),
Some(&comments),
react_options,
Expand All @@ -288,27 +273,23 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
}

// Resolve with mark
main_module.visit_mut_with(&mut resolver(
program.visit_mut_with(&mut resolver(
unresolved_mark,
top_level_mark,
is_type_script && !transpile_ts,
));
// Collect import/export metadata
let mut collect = global_collect(&main_module);
let mut collect = global_collect(&program);

transform_props_destructuring(
&mut main_module,
&mut collect,
&config.core_module,
);
transform_props_destructuring(&mut program, &mut collect, &config.core_module);

// Replace const values
if let Some(is_server) = config.is_server {
if config.mode != EmitMode::Lib {
let is_dev = config.mode == EmitMode::Dev;
let mut const_replacer =
ConstReplacerVisitor::new(is_server, is_dev, &collect);
main_module.visit_mut_with(&mut const_replacer);
program.visit_mut_with(&mut const_replacer);
}
}
let mut qwik_transform = QwikTransform::new(QwikTransformOptions {
Expand All @@ -330,14 +311,14 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
});

// Run main transform
main_module = main_module.fold_with(&mut qwik_transform);
program = program.fold_with(&mut qwik_transform);

let mut treeshaker = Treeshaker::new();

if config.minify != MinifyMode::None {
main_module.visit_mut_with(&mut treeshaker.marker);
program.visit_mut_with(&mut treeshaker.marker);

main_module = main_module.fold_with(&mut simplify::simplifier(
program = program.fold_with(&mut simplify::simplifier(
unresolved_mark,
simplify::Config {
dce: simplify::dce::Config {
Expand All @@ -352,17 +333,17 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
config.entry_strategy,
EntryStrategy::Inline | EntryStrategy::Hoist
) {
main_module.visit_mut_with(&mut SideEffectVisitor::new(
program.visit_mut_with(&mut SideEffectVisitor::new(
&qwik_transform.options.global_collect,
&path_data,
config.src_dir,
));
} else if config.minify != MinifyMode::None
&& matches!(config.is_server, Some(false))
{
main_module.visit_mut_with(&mut treeshaker.cleaner);
program.visit_mut_with(&mut treeshaker.cleaner);
if treeshaker.cleaner.did_drop {
main_module = main_module.fold_with(&mut simplify::simplifier(
program = program.fold_with(&mut simplify::simplifier(
unresolved_mark,
simplify::Config {
dce: simplify::dce::Config {
Expand All @@ -374,8 +355,8 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
));
}
}
main_module.visit_mut_with(&mut hygiene_with_config(Default::default()));
main_module.visit_mut_with(&mut fixer(None));
program.visit_mut_with(&mut hygiene_with_config(Default::default()));
program.visit_mut_with(&mut fixer(None));

let hooks = qwik_transform.hooks;
let mut modules: Vec<TransformModule> = Vec::with_capacity(hooks.len() + 10);
Expand Down Expand Up @@ -449,13 +430,16 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
});
}

let (code, map) = emit_source_code(
Lrc::clone(&source_map),
Some(comments),
&main_module,
config.root_dir,
config.source_maps,
)?;
let (code, map) = match program {
ast::Program::Module(ref modu) => emit_source_code(
Lrc::clone(&source_map),
Some(comments),
modu,
config.root_dir,
config.source_maps,
)?,
_ => (String::new(), None),
};

let a = if did_transform && !config.preserve_filenames {
[&path_data.file_stem, ".", &extension].concat()
Expand Down Expand Up @@ -506,7 +490,7 @@ fn parse(
path_data: &PathData,
root_dir: Option<&Path>,
source_map: Lrc<SourceMap>,
) -> PResult<(ast::Module, SingleThreadedComments, bool, bool)> {
) -> PResult<(ast::Program, SingleThreadedComments, bool, bool)> {
let sm_path = if let Some(root_dir) = root_dir {
pathdiff::diff_paths(path_data.abs_path.clone(), root_dir).unwrap()
} else {
Expand Down Expand Up @@ -538,9 +522,9 @@ fn parse(
);

let mut parser = Parser::new_from(lexer);
match parser.parse_module() {
match parser.parse_program() {
Err(err) => Err(err),
Ok(module) => Ok((module, comments, is_type_script, is_jsx)),
Ok(result) => Ok((result, comments, is_type_script, is_jsx)),
}
}

Expand All @@ -562,7 +546,7 @@ fn parse_filename(path_data: &PathData) -> (bool, bool) {
pub fn emit_source_code(
source_map: Lrc<SourceMap>,
comments: Option<SingleThreadedComments>,
program: &ast::Module,
module: &ast::Module,
root_dir: Option<&Path>,
source_maps: bool,
) -> Result<(String, Option<String>), Error> {
Expand All @@ -579,19 +563,14 @@ pub fn emit_source_code(
None
},
));
let config = swc_ecmascript::codegen::Config {
minify: false,
target: ast::EsVersion::latest(),
ascii_only: false,
omit_last_semi: false,
};
let config = swc_ecmascript::codegen::Config::default();
let mut emitter = swc_ecmascript::codegen::Emitter {
cfg: config,
comments: Some(&comments),
cm: Lrc::clone(&source_map),
wr: writer,
};
emitter.emit_module(program)?;
emitter.emit_module(module)?;
}

let mut map_buf = vec![];
Expand Down
Loading

0 comments on commit 1cdf6ef

Please sign in to comment.