Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Oct 14, 2024
1 parent b3d7c67 commit 63199c6
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 1 deletion.
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 = "=2.0.0" }
swc_html = { version = "=1.0.0" }
swc_html_minifier = { version = "=1.0.0", default-features = false }
swc_node_comments = { version = "=1.0.0" }
swc_typescript = { version = "=1.0.0" }

rspack_dojang = { version = "0.1.9" }
[workspace.metadata.release]
Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export enum BuiltinPluginName {
LightningCssMinimizerRspackPlugin = 'LightningCssMinimizerRspackPlugin',
BundlerInfoRspackPlugin = 'BundlerInfoRspackPlugin',
CssExtractRspackPlugin = 'CssExtractRspackPlugin',
SwcDtsEmitRspackPlugin = 'SwcDtsEmitRspackPlugin',
JsLoaderRspackPlugin = 'JsLoaderRspackPlugin',
LazyCompilationPlugin = 'LazyCompilationPlugin'
}
Expand Down
4 changes: 4 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 @@ -174,6 +174,7 @@ pub enum BuiltinPluginName {
LightningCssMinimizerRspackPlugin,
BundlerInfoRspackPlugin,
CssExtractRspackPlugin,
SwcDtsEmitRspackPlugin,

// rspack js adapter plugins
// naming format follow XxxRspackPlugin
Expand Down Expand Up @@ -487,6 +488,9 @@ impl BuiltinPlugin {
.boxed();
plugins.push(plugin);
}
BuiltinPluginName::SwcDtsEmitRspackPlugin => {
rspack_loader_swc::
}
BuiltinPluginName::JsLoaderRspackPlugin => {
plugins
.push(JsLoaderRspackPlugin::new(downcast_into::<JsLoaderRunner>(self.options)?).boxed());
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"
17 changes: 17 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,15 @@

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

use std::default::Default;

use compiler::{IntoJsAst, SwcCompiler};
use options::SwcCompilerOptionsWithAdditional;
pub use options::SwcLoaderJsOptions;
pub use plugin::PluginCssExtract;
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,6 +20,7 @@ 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 transformer::IdentCollector;

Expand Down Expand Up @@ -121,6 +124,19 @@ 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 dts_code = to_code_with_comments(
Some(&built.comments),
&program.as_module().expect("cannot reach"),
);
loader_context
.parse_meta
.entry("swc-loader-dts-extract".into())
.and_modify(|v| v.push_str(&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 +146,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
9 changes: 9 additions & 0 deletions crates/rspack_loader_swc/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::sync::Arc;

use rspack_hook::{plugin, plugin_hook};

#[plugin]
#[derive(Debug)]
pub struct PluginCssExtract {
pub(crate) options: Arc<CssExtractOptions>,
}
2 changes: 1 addition & 1 deletion packages/rspack/src/builtin-plugin/css-extract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "@rspack/binding";

import { join } from "node:path";
import type { Compiler } from "../..";
import type { Compiler } from "../../Compiler";
import { MODULE_TYPE } from "./loader";
import { PLUGIN_NAME } from "./utils";

Expand Down
13 changes: 13 additions & 0 deletions packages/rspack/src/builtin-plugin/swc-dts-emit/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BuiltinPluginName } from "@rspack/binding";
import { Compiler } from "../../Compiler";

export interface SwcDtsEmitRspackPluginOptions {}

export class SwcDtsEmitRspackPlugin {
apply(compiler: Compiler) {
compiler.__internal__registerBuiltinPlugin({
name: BuiltinPluginName.SwcDtsEmitRspackPlugin,
options: {}
});
}
}

0 comments on commit 63199c6

Please sign in to comment.