Skip to content

Commit

Permalink
feat(swc, swc_ecma_transofmrs_module): added outFileExtension module …
Browse files Browse the repository at this point in the history
…option
  • Loading branch information
hanseltime committed Dec 6, 2024
1 parent 8900bb3 commit 8865cee
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 22 deletions.
47 changes: 33 additions & 14 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,22 +1400,39 @@ impl ModuleConfig {

let base_url = base_url.to_path_buf();
let resolver = match config {
None => build_resolver(base_url, paths, false),
None => build_resolver(base_url, paths, false, &"js".to_string()),
Some(ModuleConfig::Es6(config)) | Some(ModuleConfig::NodeNext(config)) => {
build_resolver(base_url, paths, config.resolve_fully)
}
Some(ModuleConfig::CommonJs(config)) => {
build_resolver(base_url, paths, config.resolve_fully)
}
Some(ModuleConfig::Umd(config)) => {
build_resolver(base_url, paths, config.config.resolve_fully)
}
Some(ModuleConfig::Amd(config)) => {
build_resolver(base_url, paths, config.config.resolve_fully)
}
Some(ModuleConfig::SystemJs(config)) => {
build_resolver(base_url, paths, config.resolve_fully)
build_resolver(
base_url,
paths,
config.resolve_fully,
&config.config.out_file_extension,
)
}
Some(ModuleConfig::CommonJs(config)) => build_resolver(
base_url,
paths,
config.resolve_fully,
&config.out_file_extension,
),
Some(ModuleConfig::Umd(config)) => build_resolver(
base_url,
paths,
config.config.resolve_fully,
&config.config.out_file_extension,
),
Some(ModuleConfig::Amd(config)) => build_resolver(
base_url,
paths,
config.config.resolve_fully,
&config.config.out_file_extension,
),
Some(ModuleConfig::SystemJs(config)) => build_resolver(
base_url,
paths,
config.resolve_fully,
&config.config.out_file_extension,
),
};

Some((base, resolver))
Expand Down Expand Up @@ -1733,6 +1750,7 @@ fn build_resolver(
mut base_url: PathBuf,
paths: CompiledPaths,
resolve_fully: bool,
file_extension: &String,
) -> SwcImportResolver {
static CACHE: Lazy<DashMap<(PathBuf, CompiledPaths, bool), SwcImportResolver, ARandomState>> =
Lazy::new(Default::default);
Expand Down Expand Up @@ -1772,6 +1790,7 @@ fn build_resolver(
swc_ecma_transforms::modules::path::Config {
base_dir: Some(base_url.clone()),
resolve_fully,
file_extension: file_extension.to_owned(),
},
);
Arc::new(r)
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_ecma_transforms_module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use serde::{Deserialize, Serialize};
use swc_common::{Span, SyntaxContext};
use util::Config;

pub use self::{amd::amd, common_js::common_js, system_js::system_js, umd::umd};

Expand All @@ -26,6 +27,8 @@ pub mod umd;
pub struct EsModuleConfig {
#[serde(default)]
pub resolve_fully: bool,
#[serde(flatten, default)]
pub config: Config,
}

type SpanCtx = (Span, SyntaxContext);
32 changes: 24 additions & 8 deletions crates/swc_ecma_transforms_module/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use swc_ecma_loader::resolve::{Resolution, Resolve};
use swc_ecma_utils::{quote_ident, ExprFactory};
use tracing::{debug, info, warn, Level};

use crate::util::default_js_ext;

#[derive(Default)]
pub enum Resolver {
Real {
Expand Down Expand Up @@ -96,10 +98,21 @@ where
config: Config,
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct Config {
pub base_dir: Option<PathBuf>,
pub resolve_fully: bool,
pub file_extension: String,
}

impl Default for Config {
fn default() -> Config {
Config {
file_extension: default_js_ext(),
resolve_fully: bool::default(),
base_dir: Option::default(),
}
}
}

impl<R> NodeImportResolver<R>
Expand Down Expand Up @@ -162,13 +175,13 @@ where
};

let is_resolved_as_non_js = if let Some(ext) = target_path.extension() {
ext != "js"
ext.to_string_lossy() != self.config.file_extension
} else {
false
};

let is_resolved_as_js = if let Some(ext) = target_path.extension() {
ext == "js"
ext.to_string_lossy() == self.config.file_extension
} else {
false
};
Expand All @@ -191,27 +204,30 @@ where
// Resolved: `./foo/index.js`

if self.config.resolve_fully {
target_path.set_file_name("index.js");
target_path.set_file_name(format!("index.{}", self.config.file_extension));
} else {
target_path.set_file_name("index");
}
} else if is_resolved_as_index && is_resolved_as_js && orig_filename != "index.js" {
} else if is_resolved_as_index
&& is_resolved_as_js
&& orig_filename != format!("index{}", self.config.file_extension)
{
// Import: `./foo`
// Resolved: `./foo/index.js`

target_path.pop();
} else if is_resolved_as_non_js && self.config.resolve_fully && file_stem_matches {
target_path.set_extension("js");
target_path.set_extension(self.config.file_extension.clone());
} else if !is_resolved_as_js && !is_resolved_as_index && !is_exact {
target_path.set_file_name(orig_filename);
} else if is_resolved_as_non_js && is_exact {
if let Some(ext) = Path::new(orig_filename).extension() {
target_path.set_extension(ext);
} else {
target_path.set_extension("js");
target_path.set_extension(self.config.file_extension.clone());
}
} else if self.config.resolve_fully && is_resolved_as_non_js {
target_path.set_extension("js");
target_path.set_extension(self.config.file_extension.clone());
} else if is_resolved_as_non_js && is_resolved_as_index {
if orig_filename == "index" {
target_path.set_extension("");
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_transforms_module/src/system_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use swc_ecma_utils::{
};
use swc_ecma_visit::{fold_pass, standard_only_fold, Fold, FoldWith, VisitWith};

pub use super::util::Config as InnerConfig;
use crate::{
path::Resolver,
top_level_this::top_level_this,
Expand All @@ -21,6 +22,9 @@ pub struct Config {

#[serde(default)]
pub resolve_fully: bool,

#[serde(flatten, default)]
pub config: InnerConfig,
}

struct SystemJs {
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_transforms_module/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ pub struct Config {

#[serde(default)]
pub resolve_fully: bool,

#[serde(default = "default_js_ext")]
pub out_file_extension: String,
}

pub fn default_js_ext() -> String {
"js".to_string()
}

impl Default for Config {
Expand All @@ -61,6 +68,7 @@ impl Default for Config {
ignore_dynamic: false,
preserve_import_meta: false,
resolve_fully: false,
out_file_extension: "js".to_string(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_transforms_module/tests/path_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use swc_ecma_parser::Syntax;
use swc_ecma_transforms_module::{
path::{ImportResolver, NodeImportResolver},
rewriter::import_rewriter,
util::default_js_ext,
};
use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig};
use testing::run_test2;
Expand Down Expand Up @@ -106,6 +107,7 @@ fn paths_resolver(base_dir: &Path, rules: Vec<(String, Vec<String>)>) -> JscPath
swc_ecma_transforms_module::path::Config {
base_dir: Some(base_dir),
resolve_fully: true,
file_extension: default_js_ext(),
},
)
}
Expand Down

0 comments on commit 8865cee

Please sign in to comment.