Skip to content

Commit

Permalink
feat: swcDtsExtractPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Oct 12, 2024
1 parent 0514e80 commit e10b7c4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 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 @@ -81,6 +81,7 @@ swc_error_reporters = { version = "=1.0.0" }
swc_html = { version = "=0.154.0" }
swc_html_minifier = { version = "=0.151.0", default-features = false }
swc_node_comments = { version = "=0.27.0" }
swc_typescript = { version = "=0.8.1" }

rspack_dojang = { version = "0.1.9" }
[workspace.metadata.release]
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_loader_swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ stacker = { workspace = true }
swc_config = { workspace = true }
swc_core = { workspace = true, features = ["base", "ecma_ast", "common"] }
swc_plugin_import = { version = "0.1.5", path = "../swc_plugin_import" }
swc_typescript = { workspace = true }
tokio = { workspace = true }
url = "2.5.0"
12 changes: 12 additions & 0 deletions crates/rspack_loader_swc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use swc_core::common::{
};
use swc_core::common::{BytePos, SourceFile};
use swc_core::ecma::ast::{EsVersion, Program};
use swc_core::ecma::codegen::to_code_with_comments;
use swc_core::ecma::parser::{
parse_file_as_module, parse_file_as_program, parse_file_as_script, Syntax,
};
Expand All @@ -40,6 +41,7 @@ use swc_core::{
base::{config::Options, try_with_handler},
common::Globals,
};
use swc_typescript::fast_dts::FastDts;
use url::Url;

fn minify_file_comments(
Expand Down Expand Up @@ -402,6 +404,16 @@ impl SwcCompiler {
program
}

pub fn transform_dts(&self, config: BuiltInput<impl Fold>) -> Result<String, Error> {
// let mut checker = FastDts::new();
let comments = config.comments.clone().unwrap();

let module = config.program.clone().expect_module();

let dts_code = to_code_with_comments(Some(&comments), &module);
Ok(dts_code)
}

pub fn input_source_map(
&self,
input_src_map: &InputSourceMap,
Expand Down
21 changes: 21 additions & 0 deletions crates/rspack_loader_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,31 @@ impl SwcLoader {
let ast = c.into_js_ast(program);
let TransformOutput { code, map } = ast::stringify(&ast, codegen_options)?;

let emit_dts = swc_options
.config
.jsc
.experimental
.emit_isolated_dts
.into_bool()
&& swc_options
.config
.jsc
.syntax
.is_some_and(|x| x.typescript());

if emit_dts {
let dts_code = c.transform_dts(built).unwrap();
loader_context
.parse_meta
.entry("swc-loader-dts-extract".into())
.and_modify(|v| v.push_str(&dts_code));
}

let map = map
.map(|m| SourceMap::from_json(&m))
.transpose()
.map_err(|e| error!(e.to_string()))?;
loader_context.emit_diagnostic(diagnostic);
loader_context.finish_with((code, map));

Ok(())
Expand Down

0 comments on commit e10b7c4

Please sign in to comment.