Skip to content

Commit

Permalink
feat(next-swc): Update swc crates (#35996)
Browse files Browse the repository at this point in the history
* Move

* Adjust

* publish = false

* Tree

* Move tests

* fixup

* Split `emotion`

* Split `styled_jsx`

* Split `styled_jsx`

* fmt

* `--fix`

* clippy

* Update crates

* fixup

* publish

* Bump

* Rename

* authors

* Update

* More update

* Oh

* Update test refs

* Update test refs

* Update again

* Fix
  • Loading branch information
kdy1 committed Apr 11, 2022
1 parent 0110973 commit 16f7084
Show file tree
Hide file tree
Showing 265 changed files with 4,467 additions and 317 deletions.
248 changes: 146 additions & 102 deletions packages/next-swc/Cargo.lock

Large diffs are not rendered by default.

24 changes: 11 additions & 13 deletions packages/next-swc/crates/core/Cargo.toml
Expand Up @@ -2,37 +2,35 @@
edition = "2018"
name = "next-swc"
version = "0.0.0"
publish = false

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
base64 = "0.13"
byteorder = "1"
chrono = "0.4"
easy-error = "1.0.0"
either = "1"
fxhash = "0.2.1"
once_cell = "1.8.0"
pathdiff = "0.2.0"
radix_fmt = "1"
regex = "1.5"
serde = "1"
serde_json = "1"
styled_components = "0.23.0"
swc = "0.161.1"
swc_emotion = {path="../emotion"}
styled_components = {path="../styled_components"}
styled_jsx = {path="../styled_jsx"}
modularize_imports = {path="../modularize_imports"}
swc = "0.164.0"
swc_atoms = "0.2.11"
swc_common = { version = "0.17.18", features = ["concurrent", "sourcemap"] }
swc_css = "0.103.0"
swc_common = { version = "0.17.19", features = ["concurrent", "sourcemap"] }
swc_ecma_loader = { version = "0.29.0", features = ["node", "lru"] }
swc_ecmascript = { version = "0.140.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
swc_node_base = "0.5.1"
swc_ecmascript = { version = "0.143.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
swc_cached = "0.1.1"
swc_css_prefixer = "0.99.3"
tracing = {version = "0.1.28", features = ["release_max_level_off"]}
handlebars = "4.2.1"
tracing = { version = "0.1.32", features = ["release_max_level_off"] }


[dev-dependencies]
swc_ecma_transforms_testing = "0.75.0"
swc_ecma_transforms_testing = "0.77.0"
testing = "0.19.1"
walkdir = "2.3.2"
1 change: 1 addition & 0 deletions packages/next-swc/crates/core/src/hook_optimizer.rs
Expand Up @@ -104,6 +104,7 @@ fn get_object_pattern(array_pattern: &ArrayPat) -> Pat {
key: PropName::Num(Number {
value: i as f64,
span: DUMMY_SP,
raw: None,
}),
value: Box::new(elem.clone()),
})
Expand Down
7 changes: 2 additions & 5 deletions packages/next-swc/crates/core/src/lib.rs
Expand Up @@ -48,9 +48,7 @@ use swc_ecmascript::visit::Fold;
pub mod amp_attributes;
mod auto_cjs;
pub mod disallow_re_export_all_in_page;
pub mod emotion;
pub mod hook_optimizer;
pub mod modularize_imports;
pub mod next_dynamic;
pub mod next_ssg;
pub mod page_config;
Expand All @@ -59,7 +57,6 @@ pub mod react_remove_properties;
pub mod relay;
pub mod remove_console;
pub mod shake_exports;
pub mod styled_jsx;
mod top_level_binding_collector;

#[derive(Clone, Debug, Deserialize)]
Expand Down Expand Up @@ -103,7 +100,7 @@ pub struct TransformOptions {
pub shake_exports: Option<shake_exports::Config>,

#[serde(default)]
pub emotion: Option<emotion::EmotionOptions>,
pub emotion: Option<swc_emotion::EmotionOptions>,

#[serde(default)]
pub modularize_imports: Option<modularize_imports::Config>,
Expand Down Expand Up @@ -188,7 +185,7 @@ pub fn custom_before_pass<'a, C: Comments + 'a>(
}
if let FileName::Real(path) = &file.name {
path.to_str().map(|_| {
Either::Left(emotion::EmotionTransformer::new(
Either::Left(swc_emotion::EmotionTransformer::new(
config.clone(),
path,
cm,
Expand Down
18 changes: 1 addition & 17 deletions packages/next-swc/crates/core/tests/errors.rs
@@ -1,6 +1,6 @@
use next_swc::{
disallow_re_export_all_in_page::disallow_re_export_all_in_page, next_dynamic::next_dynamic,
next_ssg::next_ssg, styled_jsx::styled_jsx,
next_ssg::next_ssg,
};
use std::path::PathBuf;
use swc_common::FileName;
Expand Down Expand Up @@ -44,22 +44,6 @@ fn next_dynamic_errors(input: PathBuf) {
);
}

#[fixture("tests/errors/styled-jsx/**/input.js")]
fn styled_jsx_errors(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
let file_name = match input.to_str().unwrap().contains("ts-with-css-resolve") {
true => FileName::Real(PathBuf::from("/some-project/src/some-file.ts")),
false => FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
};

test_fixture_allowing_error(
syntax(),
&|t| styled_jsx(t.cm.clone(), file_name.clone()),
&input,
&output,
);
}

#[fixture("tests/errors/next-ssg/**/input.js")]
fn next_ssg_errors(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
Expand Down
153 changes: 3 additions & 150 deletions packages/next-swc/crates/core/tests/fixture.rs
@@ -1,25 +1,19 @@
use next_swc::{
amp_attributes::amp_attributes,
emotion::{self, EmotionOptions},
modularize_imports::modularize_imports,
next_dynamic::next_dynamic,
next_ssg::next_ssg,
page_config::page_config_test,
react_remove_properties::remove_properties,
relay::{relay, Config as RelayConfig, RelayLanguageConfig},
remove_console::remove_console,
shake_exports::{shake_exports, Config as ShakeExportsConfig},
styled_jsx::styled_jsx,
};
use std::path::PathBuf;
use swc_common::{chain, comments::SingleThreadedComments, FileName, Mark, Span, DUMMY_SP};
use swc_common::{chain, comments::SingleThreadedComments, FileName, Mark};
use swc_ecma_transforms_testing::{test, test_fixture};
use swc_ecmascript::{
parser::{EsConfig, Syntax, TsConfig},
transforms::{
react::{jsx, Runtime},
resolver,
},
parser::{EsConfig, Syntax},
transforms::react::jsx,
};
use testing::fixture;

Expand All @@ -30,13 +24,6 @@ fn syntax() -> Syntax {
})
}

fn ts_syntax() -> Syntax {
Syntax::Typescript(TsConfig {
tsx: true,
..Default::default()
})
}

#[fixture("tests/fixture/amp/**/input.js")]
fn amp_attributes_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
Expand Down Expand Up @@ -120,56 +107,6 @@ fn next_ssg_fixture(input: PathBuf) {
);
}

#[fixture("tests/fixture/styled-jsx/**/input.js")]
fn styled_jsx_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|t| {
chain!(
resolver(),
styled_jsx(
t.cm.clone(),
FileName::Real(PathBuf::from("/some-project/src/some-file.js"))
)
)
},
&input,
&output,
);

test_fixture(
syntax(),
&|t| {
// `resolver` uses `Mark` which is stored in a thread-local storage (namely
// swc_common::GLOBALS), and this loop will make `Mark` to be different from the
// invocation above.
//
// 1000 is used because in future I (kdy1) may optimize logic of resolver.
for _ in 0..1000 {
let _mark = Mark::fresh(Mark::root());
}

chain!(
resolver(),
styled_jsx(
t.cm.clone(),
FileName::Real(PathBuf::from("/some-project/src/some-file.js"))
)
)
},
&input,
&output,
);
}

pub struct DropSpan;
impl swc_ecmascript::visit::VisitMut for DropSpan {
fn visit_mut_span(&mut self, span: &mut Span) {
*span = DUMMY_SP
}
}

#[fixture("tests/fixture/page-config/**/input.js")]
fn page_config_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
Expand Down Expand Up @@ -272,87 +209,3 @@ fn shake_exports_fixture_default(input: PathBuf) {
&output,
);
}

#[fixture("tests/fixture/emotion/*/input.tsx")]
fn next_emotion_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.ts");
test_fixture(
ts_syntax(),
&|tr| {
let top_level_mark = Mark::fresh(Mark::root());
let jsx = jsx::<SingleThreadedComments>(
tr.cm.clone(),
Some(tr.comments.as_ref().clone()),
swc_ecmascript::transforms::react::Options {
next: false,
runtime: Some(Runtime::Automatic),
throw_if_namespace: false,
development: false,
use_builtins: true,
use_spread: true,
..Default::default()
},
top_level_mark,
);
chain!(
emotion::emotion(
EmotionOptions {
enabled: Some(true),
sourcemap: Some(true),
auto_label: Some(true),
..Default::default()
},
&PathBuf::from("input.ts"),
tr.cm.clone(),
tr.comments.as_ref().clone(),
),
jsx
)
},
&input,
&output,
);
}

#[fixture("tests/fixture/modularize-imports/**/input.js")]
fn modularize_imports_fixture(input: PathBuf) {
use next_swc::modularize_imports::PackageConfig;
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|_tr| {
modularize_imports(next_swc::modularize_imports::Config {
packages: vec![
(
"react-bootstrap".to_string(),
PackageConfig {
transform: "react-bootstrap/lib/{{member}}".into(),
prevent_full_import: false,
skip_default_conversion: false,
},
),
(
"my-library/?(((\\w*)?/?)*)".to_string(),
PackageConfig {
transform: "my-library/{{ matches.[1] }}/{{member}}".into(),
prevent_full_import: false,
skip_default_conversion: false,
},
),
(
"my-library-2".to_string(),
PackageConfig {
transform: "my-library-2/{{ camelCase member }}".into(),
prevent_full_import: false,
skip_default_conversion: true,
},
),
]
.into_iter()
.collect(),
})
},
&input,
&output,
);
}
2 changes: 1 addition & 1 deletion packages/next-swc/crates/core/tests/full.rs
Expand Up @@ -31,7 +31,7 @@ fn test(input: &Path, minify: bool) {
swc: swc::config::Options {
swcrc: true,
is_module: swc::config::IsModule::Bool(true),
output_path: Some(output.to_path_buf()),
output_path: Some(output.clone()),

config: swc::config::Config {
jsc: swc::config::JscConfig {
Expand Down
27 changes: 27 additions & 0 deletions packages/next-swc/crates/emotion/Cargo.toml
@@ -0,0 +1,27 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
edition = "2018"
description = "AST Transforms for emotion"
license = "Apache-2.0"
name = "swc_emotion"
repository = "https://github.com/vercel/next.js.git"
version = "0.2.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
base64 = "0.13"
byteorder = "1"
fxhash = "0.2.1"
once_cell = "1.8.0"
radix_fmt = "1"
regex = "1.5"
serde = "1"
sourcemap = "6.0.1"
swc_atoms = "0.2.11"
swc_common = { version = "0.17.19", features = ["concurrent", "sourcemap"] }
swc_ecmascript = { version = "0.143.0", features = ["codegen", "utils", "visit"] }

[dev-dependencies]
swc_ecma_transforms_testing = "0.77.0"
testing = "0.19.1"
Expand Up @@ -46,8 +46,8 @@ mod test {
let s2 = "abcdeg";
for i in 0..5 {
assert_eq!(
murmurhash2(&s1[i..5].as_bytes(), 0),
murmurhash2(&s2[i..5].as_bytes(), 0)
murmurhash2(s1[i..5].as_bytes(), 0),
murmurhash2(s2[i..5].as_bytes(), 0)
);
}
}
Expand Down
Expand Up @@ -6,14 +6,14 @@ use fxhash::FxHashMap;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use swc::sourcemap::{RawToken, SourceMap as RawSourcemap};
use sourcemap::{RawToken, SourceMap as RawSourcemap};
use swc_atoms::JsWord;
use swc_common::comments::Comments;
use swc_common::util::take::Take;
use swc_common::{BytePos, SourceMap, DUMMY_SP};
use swc_ecmascript::ast::{
ArrayLit, JSXAttr, JSXAttrName, JSXAttrOrSpread, JSXAttrValue, JSXElementName, JSXExpr,
JSXExprContainer, JSXObject,
ArrayLit, CallExpr, JSXAttr, JSXAttrName, JSXAttrOrSpread, JSXAttrValue, JSXElementName,
JSXExpr, JSXExprContainer, JSXObject,
};
use swc_ecmascript::utils::ident::IdentLike;
use swc_ecmascript::utils::{ExprFactory, Id};
Expand All @@ -23,7 +23,7 @@ use swc_ecmascript::{
MemberProp, ObjectLit, Pat, Prop, PropName, PropOrSpread, Tpl, VarDeclarator,
},
codegen::util::SourceMapperExt,
visit::{swc_ecma_ast::CallExpr, Fold, FoldWith},
visit::{Fold, FoldWith},
};

mod hash;
Expand Down

0 comments on commit 16f7084

Please sign in to comment.