Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Nov 1, 2024
1 parent f91522c commit 82083d0
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 2 deletions.
6 changes: 6 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 @@ -85,6 +85,7 @@ swc_error_reporters = { version = "=3.0.0" }
swc_html = { version = "=3.0.0" }
swc_html_minifier = { version = "=3.0.0", default-features = false }
swc_node_comments = { version = "=2.0.0" }
swc_typescript = { version = "=2.0.0" }

rspack_dojang = { version = "0.1.9" }
[workspace.metadata.release]
Expand Down
8 changes: 8 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ export enum BuiltinPluginName {
LightningCssMinimizerRspackPlugin = 'LightningCssMinimizerRspackPlugin',
BundlerInfoRspackPlugin = 'BundlerInfoRspackPlugin',
CssExtractRspackPlugin = 'CssExtractRspackPlugin',
SwcDtsEmitRspackPlugin = 'SwcDtsEmitRspackPlugin',
JsLoaderRspackPlugin = 'JsLoaderRspackPlugin',
LazyCompilationPlugin = 'LazyCompilationPlugin'
}
Expand Down Expand Up @@ -1896,6 +1897,13 @@ export interface RawStatsOptions {
colors: boolean
}

export interface RawSwcDtsEmitRspackPluginOptions {
rootDir?: string
outDir?: string
include?: string
mode?: string
}

export interface RawSwcJsMinimizerOptions {
compress: any
mangle: any
Expand Down
10 changes: 10 additions & 0 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod raw_mf;
mod raw_progress;
mod raw_runtime_chunk;
mod raw_size_limits;
mod raw_swc_dts_emit;
mod raw_swc_js_minimizer;

use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown};
Expand Down Expand Up @@ -91,6 +92,7 @@ use self::{
raw_mf::{RawConsumeSharedPluginOptions, RawContainerReferencePluginOptions, RawProvideOptions},
raw_runtime_chunk::RawRuntimeChunkOptions,
raw_size_limits::RawSizeLimitsPluginOptions,
raw_swc_dts_emit::RawSwcDtsEmitRspackPluginOptions,
};
use crate::{
plugins::JsLoaderRspackPlugin, JsLoaderRunner, RawContextReplacementPluginOptions,
Expand Down Expand Up @@ -176,6 +178,7 @@ pub enum BuiltinPluginName {
LightningCssMinimizerRspackPlugin,
BundlerInfoRspackPlugin,
CssExtractRspackPlugin,
SwcDtsEmitRspackPlugin,

// rspack js adapter plugins
// naming format follow XxxRspackPlugin
Expand Down Expand Up @@ -492,6 +495,13 @@ impl BuiltinPlugin {
.boxed();
plugins.push(plugin);
}
BuiltinPluginName::SwcDtsEmitRspackPlugin => {
let plugin = rspack_loader_swc::PluginSwcDtsEmit::new(
downcast_into::<RawSwcDtsEmitRspackPluginOptions>(self.options)?.try_into()?,
)
.boxed();
plugins.push(plugin);
}
BuiltinPluginName::JsLoaderRspackPlugin => {
plugins
.push(JsLoaderRspackPlugin::new(downcast_into::<JsLoaderRunner>(self.options)?).boxed());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use napi_derive::napi;
use rspack_error::Result;
use rspack_loader_swc::SwcDtsEmitOptions;

#[napi(object, object_to_js = false)]
pub struct RawSwcDtsEmitRspackPluginOptions {
pub root_dir: Option<String>,
pub out_dir: Option<String>,
pub include: Option<String>,
pub mode: Option<String>,
}

impl TryFrom<RawSwcDtsEmitRspackPluginOptions> for SwcDtsEmitOptions {
type Error = rspack_error::Error;

fn try_from(value: RawSwcDtsEmitRspackPluginOptions) -> Result<Self> {
Ok(SwcDtsEmitOptions {
root_dir: value
.root_dir
.ok_or(rspack_error::error!("Failed to get 'root_dir'"))?,
out_dir: value
.out_dir
.ok_or(rspack_error::error!("Failed to get 'out_dir'"))?,
include: value
.include
.ok_or(rspack_error::error!("Failed to get 'include'"))?,
mode: value
.mode
.ok_or(rspack_error::error!("Failed to get 'mode'"))?,
})
}
}
1 change: 1 addition & 0 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ impl Module for ConcatenatedModule {
json_data: Default::default(),
top_level_declarations: Some(Default::default()),
module_concatenation_bailout: Default::default(),
parse_meta: Default::default(),
};
self.clear_diagnostics();

Expand Down
5 changes: 4 additions & 1 deletion crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rspack_sources::Source;
use rspack_util::atom::Atom;
use rspack_util::ext::{AsAny, DynHash};
use rspack_util::source_map::ModuleSourceMapConfig;
use rustc_hash::FxHashSet as HashSet;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use crate::concatenated_module::ConcatenatedModule;
use crate::dependencies_block::dependencies_block_update_hash;
Expand Down Expand Up @@ -56,6 +56,8 @@ pub struct BuildInfo {
pub json_data: Option<JsonValue>,
pub top_level_declarations: Option<HashSet<Atom>>,
pub module_concatenation_bailout: Option<String>,

pub parse_meta: HashMap<String, String>,
}

impl Default for BuildInfo {
Expand All @@ -74,6 +76,7 @@ impl Default for BuildInfo {
json_data: None,
top_level_declarations: None,
module_concatenation_bailout: None,
parse_meta: HashMap::default(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ impl Module for NormalModule {
}

build_info.cacheable = loader_result.cacheable;
build_info.parse_meta = loader_result.parse_meta.clone();
build_info.file_dependencies = loader_result.file_dependencies;
build_info.context_dependencies = loader_result.context_dependencies;
build_info.missing_dependencies = loader_result.missing_dependencies;
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_loader_swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@ async-trait = { workspace = true }
base64 = { version = "0.22" }
dashmap = { workspace = true }
either = { workspace = true }
glob = { workspace = true }
jsonc-parser = { version = "0.26.0", features = ["serde"] }
rspack_ast = { version = "0.1.0", path = "../rspack_ast" }
rspack_core = { version = "0.1.0", path = "../rspack_core" }
rspack_error = { version = "0.1.0", path = "../rspack_error" }
rspack_hook = { version = "0.1.0", path = "../rspack_hook" }
rspack_loader_runner = { version = "0.1.0", path = "../rspack_loader_runner" }
rspack_paths = { version = "0.1.0", path = "../rspack_paths" }
rspack_plugin_javascript = { version = "0.1.0", path = "../rspack_plugin_javascript" }
rspack_util = { version = "0.1.0", path = "../rspack_util" }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
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 }
tracing = { workspace = true }
url = "2.5.0"
27 changes: 27 additions & 0 deletions crates/rspack_loader_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

mod compiler;
mod options;
mod plugin;
mod transformer;

use std::default::Default;
use std::sync::Arc;

use compiler::{IntoJsAst, SwcCompiler};
use options::SwcCompilerOptionsWithAdditional;
pub use options::SwcLoaderJsOptions;
pub use plugin::{PluginSwcDtsEmit, SwcDtsEmitOptions};
use rspack_core::{rspack_sources::SourceMap, Mode, RunnerContext};
use rspack_error::{error, AnyhowError, Diagnostic, Result};
use rspack_loader_runner::{Identifiable, Identifier, Loader, LoaderContext};
Expand All @@ -18,7 +21,9 @@ use rspack_util::source_map::SourceMapKind;
use swc_config::{config_types::MergingOption, merge::Merge};
use swc_core::base::config::SourceMapsConfig;
use swc_core::base::config::{InputSourceMap, OutputCharset, TransformConfig};
use swc_core::ecma::codegen::to_code_with_comments;
use swc_core::ecma::visit::VisitWith;
use swc_typescript::fast_dts::FastDts;
use transformer::IdentCollector;

#[derive(Debug)]
Expand Down Expand Up @@ -81,6 +86,8 @@ impl SwcLoader {
swc_options
};

let filename = swc_options.filename.clone();

let source_map_kind: SourceMapKind = match swc_options.config.source_maps {
Some(SourceMapsConfig::Bool(false)) => SourceMapKind::empty(),
_ => loader_context.context.module_source_map_kind,
Expand Down Expand Up @@ -121,6 +128,25 @@ impl SwcLoader {
inline_script: Some(false),
keep_comments: Some(true),
};
let emit_dts = built.syntax.typescript() && built.emit_isolated_dts;

let program = &built.program;
if emit_dts && program.is_module() {
let mut module = program.clone().expect_module();
let mut checker = FastDts::new(Arc::new(swc_core::common::FileName::Custom(
filename.clone(),
)));
let issues = checker.transform(&mut module);
for issue in issues {
error!(issue);
}
let dts_code = to_code_with_comments(Some(&built.comments), &module);
loader_context
.parse_meta
.entry(String::from("swc-dts-emit-plugin") + &filename[..])
.and_modify(|v| v.push_str(&dts_code))
.or_insert(dts_code);
}

let program = tokio::task::block_in_place(|| c.transform(built).map_err(AnyhowError::from))?;
if source_map_kind.enabled() {
Expand All @@ -130,6 +156,7 @@ impl SwcLoader {
program.visit_with(&mut v);
codegen_options.source_map_config.names = v.names;
}

let ast = c.into_js_ast(program);
let TransformOutput { code, map } = ast::stringify(&ast, codegen_options)?;

Expand Down
Loading

0 comments on commit 82083d0

Please sign in to comment.