Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: support loader options to change package import #37

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/loader_barrel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
lazy_static = "1.4.0"
serde_json = "1.0.100"
serde_json = { workspace = true }
futures = { workspace = true }
regex = { workspace = true }
anyhow = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/loader_compilation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ rspack_regex = { path = "../.rspack_crates/rspack_regex" }
rspack_util = { path = "../.rspack_crates/rspack_util" }
regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = "1.0.100"
serde_json = { workspace = true }
swc_config = { workspace = true }
swc_core = { workspace = true, features = [
"base",
Expand All @@ -34,6 +34,7 @@ swc_env_replacement = { path = "../swc_env_replacement" }
swc_keep_export = { path = "../swc_keep_export" }
swc_remove_export = { path = "../swc_remove_export" }
swc_named_import_transform = { path = "../swc_named_import_transform" }
swc_change_package_import = { path = "../swc_change_package_import" }

[dev-dependencies]
indexmap = { workspace = true }
Expand Down
10 changes: 9 additions & 1 deletion crates/loader_compilation/src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::Path;

use anyhow::{Context, Error};
use either::Either;
use serde::Deserialize;
Expand All @@ -9,6 +8,7 @@ use swc_env_replacement::env_replacement;
use swc_keep_export::keep_export;
use swc_named_import_transform::{named_import_transform, TransformConfig};
use swc_remove_export::remove_export;
use swc_change_package_import::{change_package_import, Config as ImportConfig, SpecificConfigs};

macro_rules! either {
($config:expr, $f:expr) => {
Expand Down Expand Up @@ -85,6 +85,7 @@ pub struct TransformFeatureOptions {
pub keep_export: Option<Vec<String>>,
pub remove_export: Option<Vec<String>>,
pub optimize_import: Option<Vec<String>>,
pub import_config: Option<Vec<SpecificConfigs>>,
}

pub(crate) fn transform<'a>(
Expand All @@ -98,6 +99,13 @@ pub(crate) fn transform<'a>(
packages: options.clone(),
})
}),
either!(
feature_options.import_config,
|options: &Vec<SpecificConfigs>| {
let import_config = options.to_vec();
change_package_import(import_config.into_iter().map(ImportConfig::SpecificConfig).collect())
}
),
either!(
Some(&vec!["@uni/env".to_string(), "universal-env".to_string()]),
|options: &Vec<String>| { env_replacement(options.clone()) }
Expand Down
Loading
Loading