Skip to content

Commit

Permalink
feat: support loader options to change package import
Browse files Browse the repository at this point in the history
  • Loading branch information
ClarkXia committed Feb 5, 2024
1 parent 81ec13f commit 00987a8
Show file tree
Hide file tree
Showing 10 changed files with 1,137 additions and 11 deletions.
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

0 comments on commit 00987a8

Please sign in to comment.