Skip to content

Commit

Permalink
chore: bump swc (#4372)
Browse files Browse the repository at this point in the history
* chore: bump swc

* chore: update snap
  • Loading branch information
JSerFeng authored and IWANABETHATGUY committed Oct 22, 2023
1 parent 30cb400 commit c9f7691
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 159 deletions.
433 changes: 333 additions & 100 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ napi = { version = "=2.12.5" }
napi-build = { version = "=2.0.1" }
napi-derive = { version = "=2.12.3" }
napi-sys = { version = "=2.2.3" }
styled_components = { version = "=0.72.0" }
styled_components = { version = "0.77.0" }
swc_config = { version = "=0.1.7" }
swc_core = { version = "=0.83.1", default-features = false }
swc_css = { version = "=0.155.2" }
swc_ecma_minifier = { version = "=0.187.0", default-features = false }
swc_emotion = { version = "=0.42.0" }
swc_error_reporters = { version = "=0.16.1" }
swc_html = { version = "=0.131.0" }
swc_html_minifier = { version = "=0.128.0" }
swc_node_comments = { version = "=0.19.1" }
swc_core = { version = "0.86.9", default-features = false }
swc_css = { version = "0.157.1" }
swc_ecma_minifier = { version = "0.189.9", default-features = false }
swc_emotion = { version = "=0.53.0" }
swc_error_reporters = { version = "=0.17.0" }
swc_html = { version = "=0.134.10" }
swc_html_minifier = { version = "=0.131.9" }
swc_node_comments = { version = "=0.20.0" }
tikv-jemallocator = { version = "=0.5.4", features = ["disable_initial_exec_tls"] }

[profile.dev]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ console.log('something');
"./source/index.js": function (module, exports, __webpack_require__) {
var _class_call_check = __webpack_require__(/* @swc/helpers/_/_class_call_check */"../../../../../node_modules/@swc/helpers/esm/_class_call_check.js");
var _create_class = __webpack_require__(/* @swc/helpers/_/_create_class */"../../../../../node_modules/@swc/helpers/esm/_create_class.js");
var test = function test() {
var res = new Response();
return res;
};
var Response = function() {
"use strict";
function Response(mode) {
Expand All @@ -33,6 +29,10 @@ var Response = function() {
]);
return Response;
}();
function test() {
var res = new Response();
return res;
}
var result = test();
module.exports = result;
},
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod make;
mod queue;

use std::collections::hash_map::Entry;
use std::ops::Deref;
use std::{path::Path, sync::Arc};

pub use compilation::*;
Expand Down Expand Up @@ -183,7 +184,7 @@ where
SymbolRef::Declaration(d) => (d.src(), d.exported()),
SymbolRef::Indirect(i) => match i.ty {
IndirectType::Import(_, _) => (i.src(), i.indirect_id()),
IndirectType::ImportDefault(_) => (i.src(), &DEFAULT_JS_WORD),
IndirectType::ImportDefault(_) => (i.src(), DEFAULT_JS_WORD.deref()),
IndirectType::ReExport(_, _) => (i.importer(), i.id()),
_ => return,
},
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ pub mod needs_refactor {
Expr, ExprOrSpread, Id, Ident, ImportDecl, Lit, MemberExpr, MemberProp, MetaPropExpr,
MetaPropKind, ModuleExportName, NewExpr,
},
atoms::{js_word, JsWord},
atoms::JsWord,
visit::Visit,
},
};
Expand All @@ -532,7 +532,7 @@ pub mod needs_refactor {
Ident::within_ignored_ctxt(|| expr.eq_ignore_span(&IMPORT_META))
}

if matches!(&*new_expr.callee, Expr::Ident(Ident { sym: js_word!("URL"), .. }))
if matches!(&*new_expr.callee, Expr::Ident(Ident { sym, .. }) if sym == "URL")
&& let Some(args) = &new_expr.args
&& let (Some(first), Some(second)) = (args.first(), args.get(1))
&& let (
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/runtime_globals.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use bitflags::bitflags;
use swc_core::ecma::atoms::JsWordStaticSet;
use swc_core::ecma::atoms::JsWord;

bitflags! {
pub struct RuntimeGlobals: u64 {
Expand Down Expand Up @@ -307,7 +307,7 @@ impl RuntimeGlobals {
}
}

impl From<RuntimeGlobals> for string_cache::Atom<JsWordStaticSet> {
impl From<RuntimeGlobals> for JsWord {
fn from(value: RuntimeGlobals) -> Self {
value.name().into()
}
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/tree_shaking/symbol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bitflags::bitflags;
use once_cell::sync::Lazy;
use rspack_identifier::Identifier;
use serde::{Deserialize, Serialize};
use swc_core::ecma::ast::Id;
Expand Down Expand Up @@ -80,7 +81,7 @@ pub enum IndirectType {
///
ImportDefault(JsWord),
}
pub static DEFAULT_JS_WORD: JsWord = js_word!("default");
pub static DEFAULT_JS_WORD: Lazy<JsWord> = Lazy::new(|| js_word!("default"));
/// We have three kind of star symbol
/// ## import with namespace
/// ```js
Expand Down
7 changes: 2 additions & 5 deletions crates/rspack_core/src/tree_shaking/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use swc_core::common::SyntaxContext;
use swc_core::ecma::ast::{CallExpr, Callee, Expr, ExprOrSpread, Ident, Lit};
use swc_core::ecma::atoms::{js_word, JsWord};
use swc_core::ecma::atoms::JsWord;

use super::symbol::{IndirectTopLevelSymbol, StarSymbol, Symbol};
use super::visitor::SymbolRef;
Expand All @@ -20,10 +20,7 @@ pub fn get_first_string_lit_arg(e: &CallExpr) -> Option<JsWord> {
pub fn get_require_literal(e: &CallExpr, unresolved_ctxt: SyntaxContext) -> Option<JsWord> {
if e.args.len() == 1 {
if match &e.callee {
ident @ Callee::Expr(box Expr::Ident(Ident {
sym: js_word!("require"),
..
})) => {
ident @ Callee::Expr(box Expr::Ident(Ident { sym, .. })) if sym == "require" => {
// dbg!(&ident);
ident
.as_expr()
Expand Down
10 changes: 5 additions & 5 deletions crates/rspack_core/src/tree_shaking/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::Serialize;
use swc_core::common::SyntaxContext;
use swc_core::common::{util::take::Take, GLOBALS};
use swc_core::ecma::ast::*;
use swc_core::ecma::atoms::{js_word, JsWord};
use swc_core::ecma::atoms::JsWord;
use swc_core::ecma::utils::{ExprCtx, ExprExt};
use swc_core::ecma::visit::{noop_visit_type, Visit, VisitWith};
use swc_node_comments::SwcComments;
Expand Down Expand Up @@ -422,8 +422,8 @@ impl<'a> ModuleRefAnalyze<'a> {
fn check_commonjs_feature(&mut self, member_chain: &[(JsWord, SyntaxContext)]) {
if self.state.contains(AnalyzeState::ASSIGNMENT_LHS) {
match member_chain {
[(js_word!("module"), first_ctxt), (second, _), ..]
if second == "exports" && first_ctxt == &self.unresolved_ctxt => {}
[(first, first_ctxt), (second, _), ..]
if first == "module" && second == "exports" && first_ctxt == &self.unresolved_ctxt => {}
[(first, first_ctxt), ..] if first == "exports" && &self.unresolved_ctxt == first_ctxt => {}
_ => return,
}
Expand Down Expand Up @@ -1727,13 +1727,13 @@ fn is_module_exports_member_expr(expr: &Expr, unresolved_ctxt: SyntaxContext) ->
matches!(expr, Expr::Member(MemberExpr {
obj:
box Expr::Ident(Ident {
sym: js_word!("module"),
sym: obj_sym,
span: obj_span,
..
}),
prop: MemberProp::Ident(Ident { sym: prop_sym, .. }),
..
}) if obj_span.ctxt == unresolved_ctxt && prop_sym == "exports")
}) if obj_sym == "module" && obj_span.ctxt == unresolved_ctxt && prop_sym == "exports")
}

fn is_pure_decl(stmt: &Decl, unresolved_ctxt: SyntaxContext) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_css/src/swc_css_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SwcCssCompiler {
.cm
.new_source_file(FileName::Custom(path.to_string()), source);

let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
let lexer = Lexer::new(SourceFileInput::from(&*fm), None, config);
let mut parser = Parser::new(lexer, config);
let stylesheet = parser.parse_all();
stylesheet
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_plugin_javascript/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rspack_core::{ErrorSpan, ModuleType};
use rspack_error::{DiagnosticKind, Error};
use swc_core::common::{SourceFile, Span, Spanned, SyntaxContext, DUMMY_SP};
use swc_core::ecma::ast::{CallExpr, Callee, Expr, ExprOrSpread, Ident, Lit, Str};
use swc_core::ecma::atoms::{js_word, JsWord};
use swc_core::ecma::atoms::JsWord;
use swc_core::ecma::parser::Syntax;
use swc_core::ecma::parser::{EsConfig, TsConfig};

Expand Down Expand Up @@ -121,10 +121,10 @@ pub fn is_require_literal_expr(e: &CallExpr, unresolved_ctxt: &SyntaxContext) ->
matches!(
&**callee,
Expr::Ident(Ident {
sym: js_word!("require"),
sym,
span: Span { ctxt, .. },
..
}) if ctxt == unresolved_ctxt
}) if sym == "require" && ctxt == unresolved_ctxt
)
}
_ => false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use rspack_core::{CompilerOptions, ConstDependency, DependencyTemplate, ResourceData, SpanExt};
use swc_core::common::Spanned;
use swc_core::ecma::ast::{Expr, Ident, NewExpr, UnaryExpr, UnaryOp};
use swc_core::ecma::atoms::js_word;
use swc_core::ecma::visit::{noop_visit_type, Visit, VisitWith};
use url::Url;

Expand Down Expand Up @@ -123,11 +122,7 @@ impl Visit for ImportMetaScanner<'_> {

fn visit_new_expr(&mut self, new_expr: &NewExpr) {
// exclude new URL("", import.meta.url)
if let Expr::Ident(Ident {
sym: js_word!("URL"),
..
}) = &*new_expr.callee
{
if matches!(&*new_expr.callee, Expr::Ident(Ident { sym, .. }) if sym == "URL") {
return;
}
new_expr.visit_children_with(self);
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_javascript/src/visitors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub fn run_before_pass(
(options.should_transform_by_default() || syntax.typescript()) && syntax.decorators()
),
Optional::new(
swc_visitor::typescript(assumptions, top_level_mark, comments, &cm),
swc_visitor::typescript(top_level_mark, comments, &cm),
syntax.typescript()
),
builtins_additional_feature_transforms(
Expand Down
19 changes: 11 additions & 8 deletions crates/rspack_plugin_javascript/src/visitors/swc_visitor/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ fn compat_by_es_version(
),
Optional::new(compat::es2021::es2021(), es_version < EsVersion::Es2021),
Optional::new(
compat::es2020::es2020(compat::es2020::Config {
nullish_coalescing: compat::es2020::nullish_coalescing::Config {
no_document_all: assumptions.no_document_all
compat::es2020::es2020(
compat::es2020::Config {
nullish_coalescing: compat::es2020::nullish_coalescing::Config {
no_document_all: assumptions.no_document_all
},
optional_chaining: compat::es2020::optional_chaining::Config {
no_document_all: assumptions.no_document_all,
pure_getter: assumptions.pure_getters
}
},
optional_chaining: compat::es2020::optional_chaining::Config {
no_document_all: assumptions.no_document_all,
pure_getter: assumptions.pure_getters
}
}),
unresolved_mark
),
es_version < EsVersion::Es2020
),
Optional::new(compat::es2019::es2019(), es_version < EsVersion::Es2019),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::sync::Arc;

use swc_core::common::{comments::Comments, Mark, SourceMap};
use swc_core::ecma::transforms::base::Assumptions;
use swc_core::ecma::transforms::{
react::{default_pragma, default_pragma_frag},
typescript::{self, TsEnumConfig, TsImportExportAssignConfig},
typescript,
};
use swc_core::ecma::visit::Fold;

pub fn typescript<'a>(
assumptions: Assumptions,
top_level_mark: Mark,
comments: Option<&'a dyn Comments>,
cm: &Arc<SourceMap>,
Expand All @@ -24,17 +22,12 @@ pub fn typescript<'a>(
_ => TsImportExportAssignConfig::Classic,
};*/

typescript::strip_with_jsx(
typescript::tsx(
cm.clone(),
typescript::Config {
typescript::Config::default(),
typescript::TsxConfig {
pragma: Some(default_pragma()),
pragma_frag: Some(default_pragma_frag()),
ts_enum_config: TsEnumConfig {
treat_const_enum_as_enum: false,
ts_enum_is_readonly: assumptions.ts_enum_is_readonly,
},
import_export_assign_config: TsImportExportAssignConfig::Classic,
..Default::default()
},
comments,
top_level_mark,
Expand Down

0 comments on commit c9f7691

Please sign in to comment.