Skip to content

Commit

Permalink
fix: next swc loader options
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jan 10, 2025
1 parent 8450743 commit fec9b65
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ rspack_loader_lightningcss = { version = "0.2.0", path = "crates/rsp
rspack_loader_preact_refresh = { version = "0.2.0", path = "crates/rspack_loader_preact_refresh" }
rspack_loader_react_refresh = { version = "0.2.0", path = "crates/rspack_loader_react_refresh" }
rspack_loader_runner = { version = "0.2.0", path = "crates/rspack_loader_runner" }
rspack_loader_next_swc = { version = "0.2.0", path = "crates/rspack_loader_next_swc" }
rspack_loader_swc = { version = "0.2.0", path = "crates/rspack_loader_swc" }
rspack_loader_testing = { version = "0.2.0", path = "crates/rspack_loader_testing" }
rspack_macros = { version = "0.2.0", path = "crates/rspack_macros" }
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_binding_values/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rspack_loader_lightningcss = { workspace = true }
rspack_loader_preact_refresh = { workspace = true }
rspack_loader_react_refresh = { workspace = true }
rspack_loader_runner = { workspace = true }
rspack_loader_next_swc = { workspace = true }
rspack_loader_swc = { workspace = true }
rspack_loader_testing = { workspace = true }
rspack_plugin_asset = { workspace = true }
Expand Down
16 changes: 16 additions & 0 deletions crates/rspack_binding_values/src/plugins/js_loader/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rspack_error::{
};
use rspack_hook::plugin_hook;
use rspack_loader_lightningcss::{config::Config, LIGHTNINGCSS_LOADER_IDENTIFIER};
use rspack_loader_next_swc::{NextSwcLoader, NEXT_SWC_LOADER_IDENTIFIER};
use rspack_loader_preact_refresh::PREACT_REFRESH_LOADER_IDENTIFIER;
use rspack_loader_react_refresh::REACT_REFRESH_LOADER_IDENTIFIER;
use rspack_loader_swc::{SwcLoader, SWC_LOADER_IDENTIFIER};
Expand Down Expand Up @@ -81,6 +82,21 @@ pub async fn get_builtin_loader(builtin: &str, options: Option<&str>) -> Result<
return Ok(loader);
}

if builtin.starts_with(NEXT_SWC_LOADER_IDENTIFIER) {
let loader = Arc::new(
rspack_loader_next_swc::NextSwcLoader::new(options.as_ref())
.map_err(|e| {
serde_error_to_miette(
e,
options.clone(),
"failed to parse builtin:swc-loader options",
)
})?
.with_identifier(builtin.into()),
);
return Ok(loader);
}

if builtin.starts_with(LIGHTNINGCSS_LOADER_IDENTIFIER) {
let config: rspack_loader_lightningcss::config::RawConfig =
serde_json::from_str(options.as_ref()).map_err(|e| {
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_loader_next_swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ next-custom-transforms = { workspace = true }
once_cell = { workspace = true }
sugar_path = { workspace = true }
regex = { workspace = true }
preset_env_base = "1.0.0"
rustc-hash = "1.1.0"
swc_core = { workspace = true, features = ["base", "ecma_ast", "common"] }
url = "2.5.4"
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_loader_next_swc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,14 @@ impl SwcCompiler {

impl SwcCompiler {
pub fn new(resource_path: PathBuf, source: String, mut options: Options) -> Result<Self, Error> {
let unresolved_mark = Mark::new();
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
let globals = Globals::default();
GLOBALS.set(&globals, || {
let unresolved_mark = GLOBALS.set(&globals, || {
let top_level_mark = Mark::new();
let unresolved_mark = Mark::new();
options.top_level_mark = Some(top_level_mark);
options.unresolved_mark = Some(unresolved_mark);
unresolved_mark
});

let fm = cm.new_source_file(Arc::new(FileName::Real(resource_path)), source);
Expand Down
24 changes: 21 additions & 3 deletions crates/rspack_loader_next_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ mod compiler;
mod options;
mod transformer;

use std::collections::HashMap;
use std::default::Default;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use compiler::{IntoJsAst, SwcCompiler};
use next_custom_transforms::chain_transforms::{custom_before_pass, TransformOptions};
Expand All @@ -16,6 +18,8 @@ use next_custom_transforms::transforms::{
use once_cell::sync::Lazy;
use options::NextSwcLoaderJsOptions;
pub use options::SwcLoaderJsOptions;
use preset_env_base::query::QueryOrVersion;
use preset_env_base::version::Version;
use regex::Regex;
use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_core::RunnerContext;
Expand Down Expand Up @@ -209,7 +213,15 @@ impl NextSwcLoader {
..Default::default()
})
} else {
None
Some(swc_core::ecma::preset_env::Config {
targets: Some(swc_core::ecma::preset_env::Targets::HashMap(
HashMap::from_iter(vec![(
"node".to_string(),
QueryOrVersion::Version(Version::from_str("18.20.4").unwrap()),
)]),
)),
..Default::default()
})
}
};

Expand Down Expand Up @@ -283,6 +295,13 @@ impl NextSwcLoader {
..Default::default()
};

let server_components = server_components.map(|_| {
react_server_components::Config::WithOptions(react_server_components::Options {
is_react_server_layer,
dynamic_io_enabled: false,
})
});

let opts = TransformOptions {
swc: swc_options.clone(),
disable_next_ssg,
Expand All @@ -293,7 +312,7 @@ impl NextSwcLoader {
is_development,
is_server_compiler: *is_server,
prefer_esm: *esm,
server_components: server_components.map(react_server_components::Config::All),
server_components,
styled_jsx: Default::default(),
styled_components,
remove_console: None,
Expand All @@ -319,7 +338,6 @@ impl NextSwcLoader {
.map_err(AnyhowError::from)?;

let c_ref = &c;

let built = c
.parse(None, |_| {
custom_before_pass(
Expand Down

0 comments on commit fec9b65

Please sign in to comment.