Skip to content

Commit

Permalink
feat: remove styled-components, emotion and relay in builtin swc-load…
Browse files Browse the repository at this point in the history
…er (#6862)

* feat: init

* fix

* fix

* doc

* fix
  • Loading branch information
h-a-n-a authored Jun 20, 2024
1 parent 3570eb0 commit d73b4fa
Show file tree
Hide file tree
Showing 37 changed files with 80 additions and 390 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/rspack_loader_swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ base64 = { version = "0.22" }
dashmap = { version = "5.5.3" }
either = "1"
jsonc-parser = { version = "0.23.0", features = ["serde"] }
once_cell = { workspace = true }
rspack_ast = { path = "../rspack_ast" }
rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
Expand Down
15 changes: 1 addition & 14 deletions crates/rspack_loader_swc/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use rspack_swc_visitors::{
EmotionOptions, ImportOptions, PreactOptions, RawEmotionOptions, RawImportOptions,
RawPreactOptions, RawRelayOptions, RawStyledComponentsOptions, RelayOptions,
StyledComponentsOptions,
};
use rspack_swc_visitors::{ImportOptions, PreactOptions, RawImportOptions, RawPreactOptions};
use serde::Deserialize;
use swc_config::config_types::BoolConfig;
use swc_core::base::config::{
Expand All @@ -13,31 +9,22 @@ use swc_core::base::config::{
#[derive(Default, Deserialize, Debug)]
#[serde(rename_all = "camelCase", default)]
pub struct RawRspackExperiments {
pub relay: Option<RawRelayOptions>,
pub styled_components: Option<RawStyledComponentsOptions>,
pub import: Option<Vec<RawImportOptions>>,
pub emotion: Option<RawEmotionOptions>,
pub preact: Option<RawPreactOptions>,
}

#[derive(Default, Debug)]
pub(crate) struct RspackExperiments {
pub(crate) relay: Option<RelayOptions>,
pub(crate) styled_components: Option<StyledComponentsOptions>,
pub(crate) import: Option<Vec<ImportOptions>>,
pub(crate) emotion: Option<EmotionOptions>,
pub(crate) preact: Option<PreactOptions>,
}

impl From<RawRspackExperiments> for RspackExperiments {
fn from(value: RawRspackExperiments) -> Self {
Self {
relay: value.relay.map(|v| v.into()),
styled_components: value.styled_components.map(|v| v.into()),
import: value
.import
.map(|i| i.into_iter().map(|v| v.into()).collect()),
emotion: value.emotion,
preact: value.preact,
}
}
Expand Down
56 changes: 10 additions & 46 deletions crates/rspack_loader_swc/src/transformer.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use std::path::{Path, PathBuf};
use std::path::Path;
use std::sync::Arc;

use either::Either;
use once_cell::sync::Lazy;
use rspack_core::CompilerOptions;
use rspack_swc_visitors::{styled_components, PreactOptions, StyledComponentsOptions};
use rspack_swc_visitors::PreactOptions;
use swc_core::atoms::Atom;
use swc_core::common::collections::AHashMap;
use swc_core::common::{
chain,
comments::{Comments, NoopComments},
Mark, SourceMap,
};
use swc_core::common::{BytePos, FileName};
use swc_core::common::BytePos;
use swc_core::common::{chain, comments::Comments, Mark, SourceMap};
use swc_core::ecma::ast::Ident;
use swc_core::ecma::visit::{noop_visit_type, Visit};
use swc_core::ecma::{transforms::base::pass::noop, visit::Fold};
Expand Down Expand Up @@ -40,52 +35,21 @@ macro_rules! either {

#[allow(clippy::too_many_arguments)]
pub(crate) fn transform<'a>(
resource_path: &'a Path,
rspack_options: &'a CompilerOptions,
comments: Option<&'a dyn Comments>,
_resource_path: &'a Path,
_rspack_options: &'a CompilerOptions,
_comments: Option<&'a dyn Comments>,
_top_level_mark: Mark,
unresolved_mark: Mark,
cm: Arc<SourceMap>,
_unresolved_mark: Mark,
_cm: Arc<SourceMap>,
content: &'a String,
rspack_experiments: &'a RspackExperiments,
) -> impl Fold + 'a {
use rspack_swc_visitors::EmotionOptions;
let content_hash = Lazy::new(|| xxh32(content.as_bytes(), 0));

chain!(
either!(rspack_experiments.emotion, |options: &EmotionOptions| {
rspack_swc_visitors::emotion(
options.clone(),
resource_path,
*content_hash,
cm.clone(),
comments,
)
}),
either!(
rspack_experiments.styled_components,
|options: &StyledComponentsOptions| {
styled_components(
FileName::Real(resource_path.into()),
(*content_hash).into(),
options.clone(),
NoopComments,
)
}
),
either!(rspack_experiments.relay, |options| {
rspack_swc_visitors::relay(
options,
resource_path,
PathBuf::from(AsRef::<Path>::as_ref(&rspack_options.context)),
unresolved_mark,
)
}),
either!(rspack_experiments.import, |options| {
rspack_swc_visitors::import(options)
}),
either!(rspack_experiments.preact, |options: &PreactOptions| {
rspack_swc_visitors::preact(options.clone(), (*content_hash).to_string())
rspack_swc_visitors::preact(options.clone(), xxh32(content.as_bytes(), 0).to_string())
}),
)
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

1 comment on commit d73b4fa

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ✅ success
rspress ❌ failure
rsbuild ❌ failure
compat ✅ success
examples ❌ failure

Please sign in to comment.