Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: align output.clean with webpack #7845

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions crates/rspack_binding_options/src/options/raw_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rspack_binding_values::library::JsLibraryOptions;
use rspack_binding_values::JsFilename;
use rspack_core::{CrossOriginLoading, Environment, PathInfo};
use rspack_core::{OutputOptions, TrustedTypes};
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};

#[derive(Debug)]
#[napi(object)]
Expand Down Expand Up @@ -58,13 +59,20 @@ impl From<RawEnvironment> for Environment {
}
}

#[derive(Debug, Clone)]
#[napi(object)]
pub struct RawClean {
pub keep: Option<JsRegExp>,
}

#[derive(Debug)]
#[napi(object, object_to_js = false)]
pub struct RawOutputOptions {
pub path: String,
#[napi(ts_type = "boolean | \"verbose\"")]
pub pathinfo: Either<bool, String>,
pub clean: bool,
#[napi(ts_type = "boolean | object")]
pub clean: Either<bool, RawClean>,
#[napi(ts_type = "\"auto\" | JsFilename")]
pub public_path: JsFilename,
pub asset_module_filename: JsFilename,
Expand Down Expand Up @@ -118,10 +126,23 @@ impl TryFrom<RawOutputOptions> for OutputOptions {
Either::B(s) => PathInfo::String(s),
};

let clean = match value.clean {
Either::A(a) => {
if a {
Some(None)
} else {
None
}
}
Either::B(b) => Some(CleanOutput {
keep: s.keep.map(|keep| keep.to_rspack_regex()),
}),
};

Ok(OutputOptions {
path: value.path.into(),
pathinfo,
clean: value.clean,
clean,
public_path: value.public_path.into(),
asset_module_filename: value.asset_module_filename.into(),
wasm_loading: value.wasm_loading.as_str().into(),
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ impl Compiler {

#[instrument(name = "emit_assets", skip_all)]
pub async fn emit_assets(&mut self) -> Result<()> {
if self.options.output.clean {
// @TODO(shulaoda): use `CleanPlugin` instead
if let Some(clean) = self.options.output.clean {
if self.emitted_asset_versions.is_empty() {
self
.output_filesystem
Expand Down
8 changes: 7 additions & 1 deletion crates/rspack_core/src/options/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rspack_hash::RspackHash;
pub use rspack_hash::{HashDigest, HashFunction, HashSalt};
use rspack_macros::MergeFrom;
use rspack_paths::{AssertUtf8, Utf8Path, Utf8PathBuf};
use rspack_regex::RspackRegex;
use sugar_path::SugarPath;

use crate::{
Expand All @@ -20,11 +21,16 @@ pub enum PathInfo {
String(String),
}

#[derive(Debug)]
pub struct OutputClean {
keep: Option<RspackRegex>,
}

#[derive(Debug)]
pub struct OutputOptions {
pub path: Utf8PathBuf,
pub pathinfo: PathInfo,
pub clean: bool,
pub clean: Option<OutputClean>,
pub public_path: PublicPath,
pub asset_module_filename: Filename,
pub wasm_loading: WasmLoading,
Expand Down
Loading