From fa2cb3e3e3f2990551c29e285fb7339f4ddc2515 Mon Sep 17 00:00:00 2001 From: Soon Date: Thu, 1 Aug 2024 19:34:49 +0800 Subject: [PATCH] refactor(rust): replace once_cell with std::sync::LazyLock (#7403) --- Cargo.lock | 16 --- crates/rspack_base64/Cargo.toml | 1 - crates/rspack_base64/src/lib.rs | 6 +- crates/rspack_binding_values/Cargo.toml | 1 - crates/rspack_binding_values/src/runtime.rs | 7 +- crates/rspack_core/src/concatenated_module.rs | 4 +- crates/rspack_core/src/context_module.rs | 14 +-- crates/rspack_core/src/exports_info.rs | 5 +- .../rspack_core/src/normal_module_factory.rs | 12 +- crates/rspack_core/src/options/filename.rs | 62 +++++------ crates/rspack_core/src/options/output.rs | 6 +- crates/rspack_core/src/resolver/mod.rs | 14 +-- crates/rspack_core/src/template.rs | 7 +- crates/rspack_core/src/utils/comment.rs | 7 +- .../src/utils/compile_boolean_matcher.rs | 6 +- .../src/utils/concatenation_scope.rs | 4 +- crates/rspack_core/src/utils/identifier.rs | 10 +- .../rspack_core/src/utils/property_access.rs | 7 +- crates/rspack_core/src/utils/property_name.rs | 8 +- crates/rspack_core/src/utils/runtime.rs | 5 +- crates/rspack_core/src/utils/to_path.rs | 11 +- crates/rspack_error/src/error.rs | 4 +- crates/rspack_ids/Cargo.toml | 1 - crates/rspack_ids/src/id_helpers.rs | 9 +- crates/rspack_loader_runner/src/loader.rs | 4 +- crates/rspack_loader_swc/src/compiler.rs | 4 +- crates/rspack_plugin_banner/Cargo.toml | 1 - crates/rspack_plugin_banner/src/lib.rs | 6 +- .../src/parser_and_generator/mod.rs | 14 ++- crates/rspack_plugin_css/src/utils.rs | 44 ++++---- crates/rspack_plugin_devtool/Cargo.toml | 1 - .../src/eval_dev_tool_module_plugin.rs | 4 +- .../src/module_filename_helpers.rs | 8 +- .../src/source_map_dev_tool_plugin.rs | 12 +- crates/rspack_plugin_externals/Cargo.toml | 1 - .../src/http_externals_plugin.rs | 15 +-- crates/rspack_plugin_externals/src/plugin.rs | 6 +- crates/rspack_plugin_extract_css/Cargo.toml | 1 - .../src/css_module.rs | 6 +- .../rspack_plugin_extract_css/src/plugin.rs | 37 ++++--- .../esm/harmony_import_dependency.rs | 6 +- .../src/parser_plugin/check_var_decl.rs | 5 +- .../src/parser_plugin/define_plugin/parser.rs | 10 +- .../src/parser_plugin/mod.rs | 8 +- .../src/parser_plugin/worker_plugin.rs | 6 +- .../src/plugin/mangle_exports_plugin.rs | 12 +- .../src/plugin/mod.rs | 7 +- .../src/plugin/side_effects_flag_plugin.rs | 6 +- .../dependency/context_dependency_helper.rs | 4 +- .../src/visitors/dependency/util.rs | 6 +- .../src/webpack_comment.rs | 9 +- .../rspack_plugin_lazy_compilation/Cargo.toml | 1 - .../src/plugin.rs | 4 +- crates/rspack_plugin_library/Cargo.toml | 1 - .../src/assign_library_plugin.rs | 6 +- .../Cargo.toml | 1 - .../src/lib.rs | 6 +- crates/rspack_plugin_mf/Cargo.toml | 1 - .../src/sharing/consume_shared_plugin.rs | 14 +-- .../src/sharing/provide_shared_plugin.rs | 10 +- .../src/lib.rs | 7 +- crates/rspack_plugin_runtime/Cargo.toml | 1 - .../src/runtime_plugin.rs | 104 +++++++++--------- crates/rspack_plugin_schemes/Cargo.toml | 1 - crates/rspack_plugin_schemes/src/data_uri.rs | 5 +- crates/rspack_plugin_split_chunks/Cargo.toml | 1 - .../src/plugin/max_size.rs | 14 +-- .../Cargo.toml | 5 +- .../src/lib.rs | 7 +- .../rspack_plugin_swc_js_minimizer/src/lib.rs | 8 +- crates/rspack_util/Cargo.toml | 1 - crates/rspack_util/src/diff_mode.rs | 6 +- crates/rspack_util/src/identifier.rs | 19 ++-- crates/rspack_util/src/test.rs | 20 ++-- 74 files changed, 355 insertions(+), 358 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f099f429bd..f56d5d63deb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3041,7 +3041,6 @@ name = "rspack_base64" version = "0.1.0" dependencies = [ "base64-simd 0.8.0", - "once_cell", "regex", ] @@ -3120,7 +3119,6 @@ dependencies = [ "heck 0.5.0", "napi-derive", "napi-h", - "once_cell", "rspack_collections", "rspack_core", "rspack_error", @@ -3266,7 +3264,6 @@ name = "rspack_ids" version = "0.1.0" dependencies = [ "itertools 0.13.0", - "once_cell", "rayon", "regex", "rspack_core", @@ -3460,7 +3457,6 @@ version = "0.1.0" dependencies = [ "async-recursion", "futures", - "once_cell", "regex", "rspack_core", "rspack_error", @@ -3526,7 +3522,6 @@ dependencies = [ "derivative", "futures", "itertools 0.13.0", - "once_cell", "pathdiff", "rayon", "regex", @@ -3581,7 +3576,6 @@ dependencies = [ name = "rspack_plugin_externals" version = "0.1.0" dependencies = [ - "once_cell", "regex", "rspack_core", "rspack_error", @@ -3595,7 +3589,6 @@ name = "rspack_plugin_extract_css" version = "0.1.0" dependencies = [ "async-trait", - "once_cell", "regex", "rspack_collections", "rspack_core", @@ -3719,7 +3712,6 @@ name = "rspack_plugin_lazy_compilation" version = "0.1.0" dependencies = [ "async-trait", - "once_cell", "rspack_collections", "rspack_core", "rspack_error", @@ -3737,7 +3729,6 @@ name = "rspack_plugin_library" version = "0.1.0" dependencies = [ "async-trait", - "once_cell", "regex", "rspack_collections", "rspack_core", @@ -3756,7 +3747,6 @@ name = "rspack_plugin_lightning_css_minimizer" version = "0.1.0" dependencies = [ "lightningcss", - "once_cell", "parcel_sourcemap", "rayon", "regex", @@ -3799,7 +3789,6 @@ dependencies = [ "async-trait", "hashlink", "itertools 0.13.0", - "once_cell", "regex", "rspack_collections", "rspack_core", @@ -3863,7 +3852,6 @@ dependencies = [ "async-trait", "indexmap 2.2.6", "itertools 0.13.0", - "once_cell", "rspack_collections", "rspack_core", "rspack_error", @@ -3892,7 +3880,6 @@ name = "rspack_plugin_schemes" version = "0.1.0" dependencies = [ "async-trait", - "once_cell", "regex", "rspack_base64", "rspack_core", @@ -3922,7 +3909,6 @@ version = "0.1.0" dependencies = [ "dashmap 5.5.3", "derivative", - "once_cell", "rayon", "regex", "rspack_collections", @@ -3940,7 +3926,6 @@ dependencies = [ name = "rspack_plugin_swc_css_minimizer" version = "0.1.0" dependencies = [ - "once_cell", "rayon", "regex", "rspack_core", @@ -4082,7 +4067,6 @@ dependencies = [ "concat-string", "dashmap 5.5.3", "indexmap 2.2.6", - "once_cell", "regex", "rustc-hash 1.1.0", "serde", diff --git a/crates/rspack_base64/Cargo.toml b/crates/rspack_base64/Cargo.toml index 0dda7559975..da803c73e28 100644 --- a/crates/rspack_base64/Cargo.toml +++ b/crates/rspack_base64/Cargo.toml @@ -8,5 +8,4 @@ version = "0.1.0" [dependencies] base64-simd = { version = "0.8.0", features = ["alloc"] } -once_cell = { workspace = true } regex = { workspace = true } diff --git a/crates/rspack_base64/src/lib.rs b/crates/rspack_base64/src/lib.rs index ddf29a2e2d8..34193235e7e 100644 --- a/crates/rspack_base64/src/lib.rs +++ b/crates/rspack_base64/src/lib.rs @@ -1,8 +1,8 @@ pub mod base64 { use std::borrow::Cow; + use std::sync::LazyLock; use base64_simd::{Base64 as Raw, Error, STANDARD}; - use once_cell::sync::Lazy; use regex::Regex; pub struct Base64(Raw); @@ -37,8 +37,8 @@ pub mod base64 { BASE64.0.decode_to_vec(data) } - static INVALID_BASE64_RE: Lazy = - Lazy::new(|| Regex::new(r"[^+/0-9A-Za-z-_]").expect("Invalid RegExp")); + static INVALID_BASE64_RE: LazyLock = + LazyLock::new(|| Regex::new(r"[^+/0-9A-Za-z-_]").expect("Invalid RegExp")); // modified from https://github.com/feross/buffer/blob/795bbb5bda1b39f1370ebd784bea6107b087e3a7/index.js#L1942 // Buffer.from in nodejs will clean base64 first, which causes some inconsistent behavior with base64_simd diff --git a/crates/rspack_binding_values/Cargo.toml b/crates/rspack_binding_values/Cargo.toml index b90d41a45df..2ba1991199b 100644 --- a/crates/rspack_binding_values/Cargo.toml +++ b/crates/rspack_binding_values/Cargo.toml @@ -10,7 +10,6 @@ futures = { workspace = true } heck = { workspace = true } napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow"] } napi-derive = { workspace = true } -once_cell = { workspace = true } rspack_collections = { path = "../rspack_collections" } rspack_core = { path = "../rspack_core" } rspack_error = { path = "../rspack_error" } diff --git a/crates/rspack_binding_values/src/runtime.rs b/crates/rspack_binding_values/src/runtime.rs index dc3686f6ead..b8768032a96 100644 --- a/crates/rspack_binding_values/src/runtime.rs +++ b/crates/rspack_binding_values/src/runtime.rs @@ -1,15 +1,16 @@ +use std::sync::LazyLock; + use heck::{ToLowerCamelCase, ToSnakeCase}; use napi_derive::napi; -use once_cell::sync::Lazy; use rspack_core::RuntimeGlobals; use rustc_hash::FxHashMap; use crate::JsChunk; -static RUNTIME_GLOBAL_MAP: Lazy<( +static RUNTIME_GLOBAL_MAP: LazyLock<( FxHashMap, FxHashMap, -)> = Lazy::new(|| { +)> = LazyLock::new(|| { let mut to_js_map = FxHashMap::default(); let mut from_js_map = FxHashMap::default(); diff --git a/crates/rspack_core/src/concatenated_module.rs b/crates/rspack_core/src/concatenated_module.rs index 1e759d8a65f..c607a3e47c5 100644 --- a/crates/rspack_core/src/concatenated_module.rs +++ b/crates/rspack_core/src/concatenated_module.rs @@ -3,7 +3,7 @@ use std::{ collections::hash_map::{DefaultHasher, Entry}, fmt::Debug, hash::{BuildHasherDefault, Hash, Hasher}, - sync::{Arc, Mutex}, + sync::{Arc, LazyLock, Mutex}, }; use dashmap::DashMap; @@ -140,7 +140,7 @@ impl ConnectionOrModuleIdent { } } -pub static REGEX: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { +pub static REGEX: LazyLock = LazyLock::new(|| { let pattern = r"\.+\/|(\/index)?\.([a-zA-Z0-9]{1,4})($|\s|\?)|\s*\+\s*\d+\s*modules"; Regex::new(pattern).expect("should construct the regex") }); diff --git a/crates/rspack_core/src/context_module.rs b/crates/rspack_core/src/context_module.rs index 9f2a2e93f58..5b32311dcb5 100644 --- a/crates/rspack_core/src/context_module.rs +++ b/crates/rspack_core/src/context_module.rs @@ -1,3 +1,4 @@ +use std::sync::LazyLock; use std::{ borrow::Cow, fs, @@ -8,7 +9,6 @@ use std::{ use indoc::formatdoc; use itertools::Itertools; -use once_cell::sync::Lazy; use regex::{Captures, Regex}; use rspack_collections::{Identifiable, Identifier}; use rspack_error::{impl_empty_diagnosable_trait, miette::IntoDiagnostic, Diagnostic, Result}; @@ -990,12 +990,12 @@ impl Hash for ContextModule { } } -static WEBPACK_CHUNK_NAME_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[index|request\]").expect("regexp init failed")); -static WEBPACK_CHUNK_NAME_INDEX_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[index\]").expect("regexp init failed")); -static WEBPACK_CHUNK_NAME_REQUEST_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[request\]").expect("regexp init failed")); +static WEBPACK_CHUNK_NAME_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[index|request\]").expect("regexp init failed")); +static WEBPACK_CHUNK_NAME_INDEX_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[index\]").expect("regexp init failed")); +static WEBPACK_CHUNK_NAME_REQUEST_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[request\]").expect("regexp init failed")); impl ContextModule { fn visit_dirs( diff --git a/crates/rspack_core/src/exports_info.rs b/crates/rspack_core/src/exports_info.rs index 3491d994f56..3e241bae747 100644 --- a/crates/rspack_core/src/exports_info.rs +++ b/crates/rspack_core/src/exports_info.rs @@ -5,9 +5,9 @@ use std::hash::Hasher; use std::sync::atomic::AtomicU32; use std::sync::atomic::Ordering::Relaxed; use std::sync::Arc; +use std::sync::LazyLock; use itertools::Itertools; -use once_cell::sync::Lazy; use rspack_collections::impl_item_ukey; use rspack_collections::Ukey; use rspack_collections::UkeyDashMap; @@ -33,7 +33,8 @@ pub trait ExportsHash { ); } -static EXPORTS_INFO_HASH: Lazy> = Lazy::new(UkeyDashMap::default); +static EXPORTS_INFO_HASH: LazyLock> = + LazyLock::new(UkeyDashMap::default); #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)] pub struct ExportsInfo(Ukey); diff --git a/crates/rspack_core/src/normal_module_factory.rs b/crates/rspack_core/src/normal_module_factory.rs index 4d06c493461..cd008bcb237 100644 --- a/crates/rspack_core/src/normal_module_factory.rs +++ b/crates/rspack_core/src/normal_module_factory.rs @@ -1,6 +1,6 @@ +use std::sync::LazyLock; use std::{borrow::Cow, sync::Arc}; -use once_cell::sync::Lazy; use regex::Regex; use rspack_error::{error, Result}; use rspack_hook::define_hook; @@ -73,15 +73,15 @@ impl ModuleFactory for NormalModuleFactory { } } -static MATCH_RESOURCE_REGEX: Lazy = - Lazy::new(|| Regex::new("^([^!]+)!=!").expect("Failed to initialize `MATCH_RESOURCE_REGEX`")); +static MATCH_RESOURCE_REGEX: LazyLock = + LazyLock::new(|| Regex::new("^([^!]+)!=!").expect("Failed to initialize `MATCH_RESOURCE_REGEX`")); -static MATCH_WEBPACK_EXT_REGEX: Lazy = Lazy::new(|| { +static MATCH_WEBPACK_EXT_REGEX: LazyLock = LazyLock::new(|| { Regex::new(r#"\.webpack\[([^\]]+)\]$"#).expect("Failed to initialize `MATCH_WEBPACK_EXT_REGEX`") }); -static ELEMENT_SPLIT_REGEX: Lazy = - Lazy::new(|| Regex::new(r"!+").expect("Failed to initialize `ELEMENT_SPLIT_REGEX`")); +static ELEMENT_SPLIT_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"!+").expect("Failed to initialize `ELEMENT_SPLIT_REGEX`")); const HYPHEN: char = '-'; const EXCLAMATION: char = '!'; diff --git a/crates/rspack_core/src/options/filename.rs b/crates/rspack_core/src/options/filename.rs index 0b46b9b36ed..a6e78d8ec1b 100644 --- a/crates/rspack_core/src/options/filename.rs +++ b/crates/rspack_core/src/options/filename.rs @@ -3,9 +3,9 @@ use std::hash::{Hash, Hasher}; use std::ops::Deref; use std::str::FromStr; use std::sync::Arc; +use std::sync::LazyLock; use std::{borrow::Cow, convert::Infallible, ptr}; -use once_cell::sync::Lazy; use regex::{Captures, NoExpand, Regex}; use rspack_error::error; use rspack_macros::MergeFrom; @@ -15,37 +15,37 @@ use rspack_util::MergeFrom; use crate::{parse_resource, AssetInfo, PathData, ResourceParsedData}; -pub static FILE_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[file\]").expect("Should generate regex")); -pub static BASE_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[base\]").expect("Should generate regex")); -pub static NAME_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[name\]").expect("Should generate regex")); -pub static PATH_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[path\]").expect("Should generate regex")); -pub static EXT_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[ext\]").expect("Should generate regex")); -pub static QUERY_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[query\]").expect("Should generate regex")); -pub static FRAGMENT_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[fragment\]").expect("Should generate regex")); -pub static ID_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[id\]").expect("Should generate regex")); -pub static RUNTIME_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[runtime\]").expect("Should generate regex")); -pub static URL_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[url\]").expect("Should generate regex")); -pub static HASH_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[hash(:(\d*))?]").expect("Invalid regex")); -pub static CHUNK_HASH_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[chunkhash(:(\d*))?]").expect("Invalid regex")); -pub static CONTENT_HASH_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[contenthash(:(\d*))?]").expect("Invalid regex")); -pub static FULL_HASH_PLACEHOLDER: Lazy = - Lazy::new(|| Regex::new(r"\[fullhash(:(\d*))?]").expect("Invalid regex")); +pub static FILE_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[file\]").expect("Should generate regex")); +pub static BASE_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[base\]").expect("Should generate regex")); +pub static NAME_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[name\]").expect("Should generate regex")); +pub static PATH_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[path\]").expect("Should generate regex")); +pub static EXT_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[ext\]").expect("Should generate regex")); +pub static QUERY_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[query\]").expect("Should generate regex")); +pub static FRAGMENT_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[fragment\]").expect("Should generate regex")); +pub static ID_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[id\]").expect("Should generate regex")); +pub static RUNTIME_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[runtime\]").expect("Should generate regex")); +pub static URL_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[url\]").expect("Should generate regex")); +pub static HASH_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[hash(:(\d*))?]").expect("Invalid regex")); +pub static CHUNK_HASH_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[chunkhash(:(\d*))?]").expect("Invalid regex")); +pub static CONTENT_HASH_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[contenthash(:(\d*))?]").expect("Invalid regex")); +pub static FULL_HASH_PLACEHOLDER: LazyLock = + LazyLock::new(|| Regex::new(r"\[fullhash(:(\d*))?]").expect("Invalid regex")); -static DATA_URI_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^data:([^;,]+)").expect("Invalid regex")); +static DATA_URI_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^data:([^;,]+)").expect("Invalid regex")); #[derive(PartialEq, Debug, Hash, Eq, Clone, PartialOrd, Ord, MergeFrom)] enum FilenameKind { diff --git a/crates/rspack_core/src/options/output.rs b/crates/rspack_core/src/options/output.rs index 57c804acf7a..a96e604a34f 100644 --- a/crates/rspack_core/src/options/output.rs +++ b/crates/rspack_core/src/options/output.rs @@ -1,3 +1,4 @@ +use std::sync::LazyLock; use std::{ borrow::Cow, fmt::Debug, @@ -8,7 +9,6 @@ use std::{ }; use derivative::Derivative; -use once_cell::sync::Lazy; use regex::Regex; use rspack_hash::RspackHash; pub use rspack_hash::{HashDigest, HashFunction, HashSalt}; @@ -185,8 +185,8 @@ pub struct PathData<'a> { pub id: Option<&'a str>, } -static PREPARE_ID_REGEX: Lazy = - Lazy::new(|| Regex::new(r"(^[.-]|[^a-zA-Z0-9_-])+").expect("invalid Regex")); +static PREPARE_ID_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"(^[.-]|[^a-zA-Z0-9_-])+").expect("invalid Regex")); impl<'a> PathData<'a> { pub fn prepare_id(v: &str) -> Cow { diff --git a/crates/rspack_core/src/resolver/mod.rs b/crates/rspack_core/src/resolver/mod.rs index 9d89c1c28f4..acaed5b5985 100644 --- a/crates/rspack_core/src/resolver/mod.rs +++ b/crates/rspack_core/src/resolver/mod.rs @@ -2,9 +2,9 @@ mod factory; mod resolver_impl; use std::borrow::Borrow; use std::fs; +use std::sync::LazyLock; use std::{fmt, path::PathBuf}; -use once_cell::sync::Lazy; use regex::Regex; use rspack_error::{Error, MietteExt}; use rspack_loader_runner::DescriptionData; @@ -19,14 +19,14 @@ use crate::{ SharedPluginDriver, }; -static RELATIVE_PATH_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^\.\.?\/").expect("should init regex")); +static RELATIVE_PATH_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^\.\.?\/").expect("should init regex")); -static PARENT_PATH_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^\.\.[\/]").expect("should init regex")); +static PARENT_PATH_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^\.\.[\/]").expect("should init regex")); -static CURRENT_DIR_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^(\.[\/])").expect("should init regex")); +static CURRENT_DIR_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^(\.[\/])").expect("should init regex")); #[derive(Debug)] pub struct ResolveArgs<'a> { diff --git a/crates/rspack_core/src/template.rs b/crates/rspack_core/src/template.rs index d76a29c1695..5919ca052ff 100644 --- a/crates/rspack_core/src/template.rs +++ b/crates/rspack_core/src/template.rs @@ -1,9 +1,10 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use regex::Regex; pub struct Template; -static COMMENT_END_REGEX: Lazy = - Lazy::new(|| Regex::new(r"\*\/").expect("should construct regex")); +static COMMENT_END_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"\*\/").expect("should construct regex")); impl Template { pub fn to_comment(str: &str) -> String { diff --git a/crates/rspack_core/src/utils/comment.rs b/crates/rspack_core/src/utils/comment.rs index 5fff0bc00a4..a0d5e5428b0 100644 --- a/crates/rspack_core/src/utils/comment.rs +++ b/crates/rspack_core/src/utils/comment.rs @@ -1,8 +1,9 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use regex::Regex; -static COMMENT_END_REGEX: Lazy = - Lazy::new(|| Regex::new(r"\*\/").expect("should init regex")); +static COMMENT_END_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"\*\/").expect("should init regex")); #[inline] pub fn to_comment(str: &str) -> String { diff --git a/crates/rspack_core/src/utils/compile_boolean_matcher.rs b/crates/rspack_core/src/utils/compile_boolean_matcher.rs index 36f749bfe01..f1aa8310833 100644 --- a/crates/rspack_core/src/utils/compile_boolean_matcher.rs +++ b/crates/rspack_core/src/utils/compile_boolean_matcher.rs @@ -1,6 +1,6 @@ use std::collections::BTreeSet; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use regex::Regex; use rustc_hash::FxHashMap as HashMap; @@ -273,8 +273,8 @@ pub(crate) fn items_to_regexp(items_arr: Vec) -> String { } } -static QUOTE_META_REG: Lazy = - Lazy::new(|| Regex::new(r"[-\[\]\\/{}()*+?.^$|]").expect("regexp init failed")); +static QUOTE_META_REG: LazyLock = + LazyLock::new(|| Regex::new(r"[-\[\]\\/{}()*+?.^$|]").expect("regexp init failed")); fn quote_meta(str: &str) -> String { QUOTE_META_REG.replace_all(str, "\\$0").to_string() diff --git a/crates/rspack_core/src/utils/concatenation_scope.rs b/crates/rspack_core/src/utils/concatenation_scope.rs index c414a6b88e6..e1b584609c7 100644 --- a/crates/rspack_core/src/utils/concatenation_scope.rs +++ b/crates/rspack_core/src/utils/concatenation_scope.rs @@ -1,7 +1,7 @@ use std::collections::hash_map::Entry; use std::sync::Arc; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use regex::Regex; use rspack_collections::IdentifierIndexMap; use swc_core::atoms::Atom; @@ -12,7 +12,7 @@ use crate::ModuleIdentifier; pub const DEFAULT_EXPORT: &str = "__WEBPACK_DEFAULT_EXPORT__"; pub const NAMESPACE_OBJECT_EXPORT: &str = "__WEBPACK_NAMESPACE_OBJECT__"; -static MODULE_REFERENCE_REGEXP: Lazy = once_cell::sync::Lazy::new(|| { +static MODULE_REFERENCE_REGEXP: LazyLock = LazyLock::new(|| { Regex::new( r"^__WEBPACK_MODULE_REFERENCE__(\d+)_([\da-f]+|ns)(_call)?(_directImport)?(?:_asiSafe(\d))?__$", ) diff --git a/crates/rspack_core/src/utils/identifier.rs b/crates/rspack_core/src/utils/identifier.rs index ed48a6f557d..51e69e2b15b 100644 --- a/crates/rspack_core/src/utils/identifier.rs +++ b/crates/rspack_core/src/utils/identifier.rs @@ -1,6 +1,6 @@ +use std::sync::LazyLock; use std::{borrow::Cow, path::Path}; -use once_cell::sync::Lazy; use regex::Regex; use rspack_util::identifier::absolute_to_request; @@ -15,10 +15,10 @@ pub fn contextify(context: impl AsRef, request: &str) -> String { .join("!") } -static IDENTIFIER_NAME_REPLACE_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^([^a-zA-Z$_])").expect("should init regex")); -static IDENTIFIER_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"[^a-zA-Z0-9$]+").expect("should init regex")); +static IDENTIFIER_NAME_REPLACE_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^([^a-zA-Z$_])").expect("should init regex")); +static IDENTIFIER_REGEXP: LazyLock = + LazyLock::new(|| Regex::new(r"[^a-zA-Z0-9$]+").expect("should init regex")); #[inline] pub fn to_identifier(v: &str) -> Cow { diff --git a/crates/rspack_core/src/utils/property_access.rs b/crates/rspack_core/src/utils/property_access.rs index d54b64d05df..d94e82c0527 100644 --- a/crates/rspack_core/src/utils/property_access.rs +++ b/crates/rspack_core/src/utils/property_access.rs @@ -1,8 +1,9 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use regex::Regex; -static SAFE_IDENTIFIER_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^[_a-zA-Z$][_a-zA-Z$0-9]*$").expect("should init regex")); +static SAFE_IDENTIFIER_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^[_a-zA-Z$][_a-zA-Z$0-9]*$").expect("should init regex")); const RESERVED_IDENTIFIER: [&str; 37] = [ "break", "case", diff --git a/crates/rspack_core/src/utils/property_name.rs b/crates/rspack_core/src/utils/property_name.rs index cf2e5b8cce4..9d74c361711 100644 --- a/crates/rspack_core/src/utils/property_name.rs +++ b/crates/rspack_core/src/utils/property_name.rs @@ -1,13 +1,13 @@ use std::borrow::Cow; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use regex::Regex; use rspack_error::{error, Result}; use rustc_hash::FxHashSet as HashSet; -pub static SAFE_IDENTIFIER: Lazy = - Lazy::new(|| Regex::new(r"^[_a-zA-Z$][_a-zA-Z$0-9]*$").expect("Invalid regexp")); -pub static RESERVED_IDENTIFIER: Lazy> = Lazy::new(|| { +pub static SAFE_IDENTIFIER: LazyLock = + LazyLock::new(|| Regex::new(r"^[_a-zA-Z$][_a-zA-Z$0-9]*$").expect("Invalid regexp")); +pub static RESERVED_IDENTIFIER: LazyLock> = LazyLock::new(|| { HashSet::from_iter([ "break", "case", diff --git a/crates/rspack_core/src/utils/runtime.rs b/crates/rspack_core/src/utils/runtime.rs index 0fbd6eec95a..dec7d993d22 100644 --- a/crates/rspack_core/src/utils/runtime.rs +++ b/crates/rspack_core/src/utils/runtime.rs @@ -1,5 +1,6 @@ +use std::sync::LazyLock; + use indexmap::IndexMap; -use once_cell::sync::Lazy; use regex::{Captures, Regex}; use rustc_hash::FxHashMap as HashMap; use rustc_hash::FxHashSet as HashSet; @@ -47,7 +48,7 @@ pub fn get_entry_runtime( } } -static HASH_REPLACERS: Lazy, &str)>> = Lazy::new(|| { +static HASH_REPLACERS: LazyLock, &str)>> = LazyLock::new(|| { vec![ (&HASH_PLACEHOLDER, "[hash]"), (&FULL_HASH_PLACEHOLDER, "[fullhash]"), diff --git a/crates/rspack_core/src/utils/to_path.rs b/crates/rspack_core/src/utils/to_path.rs index fb46080906d..fb6eddc6c30 100644 --- a/crates/rspack_core/src/utils/to_path.rs +++ b/crates/rspack_core/src/utils/to_path.rs @@ -1,11 +1,12 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use regex::Regex; -static PATH_NAME_NORMALIZE_REPLACE_REGEX: Lazy = - Lazy::new(|| Regex::new(r"[^a-zA-Z0-9_!§$()\-=^°]+").expect("regexp failed")); +static PATH_NAME_NORMALIZE_REPLACE_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"[^a-zA-Z0-9_!§$()\-=^°]+").expect("regexp failed")); -static MATCH_PADDED_HYPHENS_REPLACE_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^-|-$").expect("regexp failed")); +static MATCH_PADDED_HYPHENS_REPLACE_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^-|-$").expect("regexp failed")); pub fn to_path(str: &str) -> String { let temp = PATH_NAME_NORMALIZE_REPLACE_REGEX.replace_all(str, "-"); diff --git a/crates/rspack_error/src/error.rs b/crates/rspack_error/src/error.rs index 796fa8e960d..df55f842664 100644 --- a/crates/rspack_error/src/error.rs +++ b/crates/rspack_error/src/error.rs @@ -1,14 +1,14 @@ +use std::sync::LazyLock; use std::{fmt::Display, sync::Arc}; use miette::{Diagnostic, LabeledSpan, MietteDiagnostic, Severity, SourceCode, SourceSpan}; -use once_cell::sync::Lazy; use swc_core::common::SourceFile; use thiserror::Error; use crate::RspackSeverity; #[allow(clippy::rc_buffer)] -static EMPTY_STRING: Lazy> = Lazy::new(|| Arc::new("".to_string())); +static EMPTY_STRING: LazyLock> = LazyLock::new(|| Arc::new("".to_string())); #[derive(Debug, Error)] #[error(transparent)] diff --git a/crates/rspack_ids/Cargo.toml b/crates/rspack_ids/Cargo.toml index 8759c9e6e07..04744cf824d 100644 --- a/crates/rspack_ids/Cargo.toml +++ b/crates/rspack_ids/Cargo.toml @@ -9,7 +9,6 @@ version = "0.1.0" [dependencies] itertools = { workspace = true } -once_cell = { workspace = true } rayon = { workspace = true } regex = { workspace = true } rspack_core = { path = "../rspack_core" } diff --git a/crates/rspack_ids/src/id_helpers.rs b/crates/rspack_ids/src/id_helpers.rs index 29bb252b58e..bceb99fb950 100644 --- a/crates/rspack_ids/src/id_helpers.rs +++ b/crates/rspack_ids/src/id_helpers.rs @@ -1,3 +1,4 @@ +use std::sync::LazyLock; use std::{ borrow::Cow, cmp::Ordering, @@ -9,7 +10,6 @@ use itertools::{ EitherOrBoth::{Both, Left, Right}, Itertools, }; -use once_cell::sync::Lazy; use rayon::prelude::*; use regex::Regex; use rspack_core::{ @@ -434,9 +434,10 @@ pub fn get_full_chunk_name( full_module_names.join(",") } -static REGEX1: Lazy = Lazy::new(|| Regex::new(r"^(\.\.?/)+").expect("Invalid regex")); -static REGEX2: Lazy = - Lazy::new(|| Regex::new(r"(^[.-]|[^a-zA-Z0-9_-])+").expect("Invalid regex")); +static REGEX1: LazyLock = + LazyLock::new(|| Regex::new(r"^(\.\.?/)+").expect("Invalid regex")); +static REGEX2: LazyLock = + LazyLock::new(|| Regex::new(r"(^[.-]|[^a-zA-Z0-9_-])+").expect("Invalid regex")); pub fn request_to_id(request: &str) -> String { REGEX2 diff --git a/crates/rspack_loader_runner/src/loader.rs b/crates/rspack_loader_runner/src/loader.rs index c602a2ea549..28c8e95e08d 100644 --- a/crates/rspack_loader_runner/src/loader.rs +++ b/crates/rspack_loader_runner/src/loader.rs @@ -1,3 +1,4 @@ +use std::sync::LazyLock; use std::{ fmt::Display, ops::Deref, @@ -10,7 +11,6 @@ use std::{ use async_trait::async_trait; use derivative::Derivative; -use once_cell::sync::Lazy; use regex::Regex; use rspack_collections::{Identifiable, Identifier}; use rspack_error::Result; @@ -220,7 +220,7 @@ pub fn parse_resource(resource: &str) -> Option { }) } -static PATH_QUERY_FRAGMENT_REGEXP: Lazy = Lazy::new(|| { +static PATH_QUERY_FRAGMENT_REGEXP: LazyLock = LazyLock::new(|| { Regex::new("^((?:\u{200b}.|[^?#\u{200b}])*)(\\?(?:\u{200b}.|[^#\u{200b}])*)?(#.*)?$") .expect("Failed to initialize `PATH_QUERY_FRAGMENT_REGEXP`") }); diff --git a/crates/rspack_loader_swc/src/compiler.rs b/crates/rspack_loader_swc/src/compiler.rs index cb2a42bf960..944fef652bf 100644 --- a/crates/rspack_loader_swc/src/compiler.rs +++ b/crates/rspack_loader_swc/src/compiler.rs @@ -8,6 +8,7 @@ use std::env; use std::fs::File; use std::path::Path; +use std::sync::LazyLock; use std::{path::PathBuf, sync::Arc}; use anyhow::{anyhow, bail, Context, Error}; @@ -24,7 +25,6 @@ use swc_core::base::config::{ use swc_core::base::{sourcemap, SwcComments}; use swc_core::common::comments::{Comment, CommentKind, Comments}; use swc_core::common::errors::{Handler, HANDLER}; -use swc_core::common::sync::Lazy; use swc_core::common::{ comments::SingleThreadedComments, FileName, FilePathMapping, Mark, SourceMap, GLOBALS, }; @@ -137,7 +137,7 @@ fn load_swcrc(path: &Path) -> Result { } fn read_config(opts: &Options, name: &FileName) -> Result, Error> { - static CUR_DIR: Lazy = Lazy::new(|| { + static CUR_DIR: LazyLock = LazyLock::new(|| { if cfg!(target_arch = "wasm32") { PathBuf::new() } else { diff --git a/crates/rspack_plugin_banner/Cargo.toml b/crates/rspack_plugin_banner/Cargo.toml index 06aee1b4fdd..835d28d52b2 100644 --- a/crates/rspack_plugin_banner/Cargo.toml +++ b/crates/rspack_plugin_banner/Cargo.toml @@ -10,7 +10,6 @@ version = "0.1.0" [dependencies] async-recursion = { workspace = true } futures = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } rspack_core = { path = "../rspack_core" } rspack_error = { path = "../rspack_error" } diff --git a/crates/rspack_plugin_banner/src/lib.rs b/crates/rspack_plugin_banner/src/lib.rs index 1fba36172f5..90803bf298a 100644 --- a/crates/rspack_plugin_banner/src/lib.rs +++ b/crates/rspack_plugin_banner/src/lib.rs @@ -1,10 +1,10 @@ #![feature(let_chains)] use std::fmt::{self, Debug}; +use std::sync::LazyLock; use async_recursion::async_recursion; use futures::future::BoxFuture; -use once_cell::sync::Lazy; use regex::Regex; use rspack_core::{ rspack_sources::{BoxSource, ConcatSource, RawSource, SourceExt}, @@ -116,8 +116,8 @@ async fn match_object(obj: &BannerPluginOptions, str: &str) -> Result { Ok(true) } -static TRIALING_WHITESPACE: Lazy = - Lazy::new(|| Regex::new(r"\s+\n").expect("invalid regexp")); +static TRIALING_WHITESPACE: LazyLock = + LazyLock::new(|| Regex::new(r"\s+\n").expect("invalid regexp")); fn wrap_comment(str: &str) -> String { if !str.contains('\n') { diff --git a/crates/rspack_plugin_css/src/parser_and_generator/mod.rs b/crates/rspack_plugin_css/src/parser_and_generator/mod.rs index 6d6779f3407..6ae3bbb226e 100644 --- a/crates/rspack_plugin_css/src/parser_and_generator/mod.rs +++ b/crates/rspack_plugin_css/src/parser_and_generator/mod.rs @@ -1,7 +1,10 @@ -use std::{borrow::Cow, sync::Arc}; +use std::{ + borrow::Cow, + sync::{Arc, LazyLock}, +}; use indexmap::{IndexMap, IndexSet}; -use once_cell::sync::{Lazy, OnceCell}; +use once_cell::sync::OnceCell; use regex::Regex; use rspack_core::{ diagnostics::map_box_diagnostics_to_module_parse_diagnostics, @@ -30,8 +33,8 @@ use crate::{ }, }; -static REGEX_IS_MODULES: Lazy = - Lazy::new(|| Regex::new(r"\.module(s)?\.[^.]+$").expect("Invalid regex")); +static REGEX_IS_MODULES: LazyLock = + LazyLock::new(|| Regex::new(r"\.module(s)?\.[^.]+$").expect("Invalid regex")); pub(crate) static CSS_MODULE_SOURCE_TYPE_LIST: &[SourceType; 1] = &[SourceType::Css]; @@ -403,7 +406,8 @@ impl ParserAndGenerator for CssParserAndGenerator { let used = get_used_exports(exports, identifier, generate_context.runtime, &mg); - static RE: Lazy = Lazy::new(|| Regex::new(r#"\\"#).expect("should compile")); + static RE: LazyLock = + LazyLock::new(|| Regex::new(r#"\\"#).expect("should compile")); let module_id = RE.replace_all(&module_id, "/"); let meta_data = used diff --git a/crates/rspack_plugin_css/src/utils.rs b/crates/rspack_plugin_css/src/utils.rs index bd5d3620502..71f837822d1 100644 --- a/crates/rspack_plugin_css/src/utils.rs +++ b/crates/rspack_plugin_css/src/utils.rs @@ -2,10 +2,10 @@ use std::borrow::Cow; use std::fmt::Write; use std::hash::Hasher; use std::sync::Arc; +use std::sync::LazyLock; use heck::{ToKebabCase, ToLowerCamelCase}; use indexmap::{IndexMap, IndexSet}; -use once_cell::sync::Lazy; use regex::{Captures, Regex}; use rspack_core::rspack_sources::{ConcatSource, RawSource}; use rspack_core::{ @@ -24,12 +24,12 @@ use rustc_hash::FxHashSet as HashSet; use crate::parser_and_generator::CssExport; pub const AUTO_PUBLIC_PATH_PLACEHOLDER: &str = "__RSPACK_PLUGIN_CSS_AUTO_PUBLIC_PATH__"; -pub static AUTO_PUBLIC_PATH_PLACEHOLDER_REGEX: Lazy = - Lazy::new(|| Regex::new(AUTO_PUBLIC_PATH_PLACEHOLDER).expect("Invalid regexp")); -pub static LEADING_DIGIT_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^\d+").expect("Invalid regexp")); -pub static PREFIX_UNDERSCORE_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^[0-9_-]").expect("Invalid regexp")); +pub static AUTO_PUBLIC_PATH_PLACEHOLDER_REGEX: LazyLock = + LazyLock::new(|| Regex::new(AUTO_PUBLIC_PATH_PLACEHOLDER).expect("Invalid regexp")); +pub static LEADING_DIGIT_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^\d+").expect("Invalid regexp")); +pub static PREFIX_UNDERSCORE_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^[0-9_-]").expect("Invalid regexp")); #[derive(Debug, Clone)] pub struct LocalIdentOptions<'a> { @@ -111,8 +111,8 @@ impl LocalIdentNameRenderOptions<'_> { } } -static UNESCAPE_CSS_IDENT_REGEX: Lazy = - Lazy::new(|| Regex::new(r"([^a-zA-Z0-9_\u0081-\uffff-])").expect("invalid regex")); +static UNESCAPE_CSS_IDENT_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"([^a-zA-Z0-9_\u0081-\uffff-])").expect("invalid regex")); pub fn escape_css_ident(s: &str) -> String { UNESCAPE_CSS_IDENT_REGEX.replace_all(s, "\\$1").into_owned() @@ -282,16 +282,16 @@ pub fn css_modules_exports_to_concatenate_module_string<'a>( Ok(()) } -static STRING_MULTILINE: Lazy = - Lazy::new(|| Regex::new(r"\\[\n\r\f]").expect("Invalid RegExp")); +static STRING_MULTILINE: LazyLock = + LazyLock::new(|| Regex::new(r"\\[\n\r\f]").expect("Invalid RegExp")); -static TRIM_WHITE_SPACES: Lazy = - Lazy::new(|| Regex::new(r"(^[ \t\n\r\f]*|[ \t\n\r\f]*$)").expect("Invalid RegExp")); +static TRIM_WHITE_SPACES: LazyLock = + LazyLock::new(|| Regex::new(r"(^[ \t\n\r\f]*|[ \t\n\r\f]*$)").expect("Invalid RegExp")); -static UNESCAPE: Lazy = - Lazy::new(|| Regex::new(r"\\([0-9a-fA-F]{1,6}[ \t\n\r\f]?|[\s\S])").expect("Invalid RegExp")); +static UNESCAPE: LazyLock = + LazyLock::new(|| Regex::new(r"\\([0-9a-fA-F]{1,6}[ \t\n\r\f]?|[\s\S])").expect("Invalid RegExp")); -static DATA: Lazy = Lazy::new(|| Regex::new(r"^(?i)data:").expect("Invalid RegExp")); +static DATA: LazyLock = LazyLock::new(|| Regex::new(r"^(?i)data:").expect("Invalid RegExp")); pub fn unescape(s: &str) -> Cow { UNESCAPE.replace_all(s.as_ref(), |caps: &Captures| { @@ -314,12 +314,12 @@ pub fn unescape(s: &str) -> Cow { }) } -static WHITE_OR_BRACKET_REGEX: Lazy = - Lazy::new(|| Regex::new(r#"[\n\t ()'"\\]"#).expect("Invalid Regexp")); -static QUOTATION_REGEX: Lazy = - Lazy::new(|| Regex::new(r#"[\n"\\]"#).expect("Invalid Regexp")); -static APOSTROPHE_REGEX: Lazy = - Lazy::new(|| Regex::new(r#"[\n'\\]"#).expect("Invalid Regexp")); +static WHITE_OR_BRACKET_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r#"[\n\t ()'"\\]"#).expect("Invalid Regexp")); +static QUOTATION_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r#"[\n"\\]"#).expect("Invalid Regexp")); +static APOSTROPHE_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r#"[\n'\\]"#).expect("Invalid Regexp")); pub fn css_escape_string(s: &str) -> String { let mut count_white_or_bracket = 0; diff --git a/crates/rspack_plugin_devtool/Cargo.toml b/crates/rspack_plugin_devtool/Cargo.toml index 405c7fee2ae..cde7a7b1647 100644 --- a/crates/rspack_plugin_devtool/Cargo.toml +++ b/crates/rspack_plugin_devtool/Cargo.toml @@ -13,7 +13,6 @@ dashmap = { workspace = true } derivative = { workspace = true } futures = { workspace = true } itertools = { workspace = true } -once_cell = { workspace = true } pathdiff = { workspace = true } rayon = { workspace = true } regex = { workspace = true } diff --git a/crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs b/crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs index 5ccadaffc1a..2d556961769 100644 --- a/crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs +++ b/crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs @@ -1,8 +1,8 @@ use std::hash::Hash; +use std::sync::LazyLock; use dashmap::DashMap; use derivative::Derivative; -use once_cell::sync::Lazy; use rspack_core::{ rspack_sources::{BoxSource, RawSource, Source, SourceExt}, ApplyContext, BoxModule, ChunkInitFragments, ChunkUkey, Compilation, CompilationParams, @@ -189,7 +189,7 @@ fn encode_uri(uri: &str) -> String { encode(uri, ";/?:@&=+$,#") } -static ALWAYS_UNESCAPED: Lazy> = Lazy::new(|| { +static ALWAYS_UNESCAPED: LazyLock> = LazyLock::new(|| { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~!*'()" .chars() .collect() diff --git a/crates/rspack_plugin_devtool/src/module_filename_helpers.rs b/crates/rspack_plugin_devtool/src/module_filename_helpers.rs index 1a7f8720c09..9595601fa87 100644 --- a/crates/rspack_plugin_devtool/src/module_filename_helpers.rs +++ b/crates/rspack_plugin_devtool/src/module_filename_helpers.rs @@ -1,9 +1,9 @@ +use std::sync::LazyLock; use std::{ borrow::Cow, hash::{Hash, Hasher}, }; -use once_cell::sync::Lazy; use regex::{Captures, Regex}; use rspack_core::{contextify, Compilation, OutputOptions}; use rspack_error::Result; @@ -12,14 +12,14 @@ use rustc_hash::FxHashMap as HashMap; use crate::{ModuleFilenameTemplateFn, ModuleFilenameTemplateFnCtx, ModuleOrSource}; -static REGEXP_ALL_LOADERS_RESOURCE: Lazy = Lazy::new(|| { +static REGEXP_ALL_LOADERS_RESOURCE: LazyLock = LazyLock::new(|| { Regex::new(r"\[all-?loaders\]\[resource\]") .expect("failed to compile REGEXP_ALL_LOADERS_RESOURCE") }); -static SQUARE_BRACKET_TAG_REGEXP: Lazy = Lazy::new(|| { +static SQUARE_BRACKET_TAG_REGEXP: LazyLock = LazyLock::new(|| { Regex::new(r"\[\\*([\w-]+)\\*\]").expect("failed to compile SQUARE_BRACKET_TAG_REGEXP") }); -static REGEXP_LOADERS_RESOURCE: Lazy = Lazy::new(|| { +static REGEXP_LOADERS_RESOURCE: LazyLock = LazyLock::new(|| { Regex::new(r"\[loaders\]\[resource\]").expect("failed to compile REGEXP_LOADERS_RESOURCE") }); diff --git a/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs b/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs index 10aff0eafa8..7bc9f4132ba 100644 --- a/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs +++ b/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs @@ -1,9 +1,9 @@ +use std::sync::LazyLock; use std::{borrow::Cow, path::Path}; use derivative::Derivative; use futures::future::{join_all, BoxFuture}; use itertools::Itertools; -use once_cell::sync::Lazy; use pathdiff::diff_paths; use rayon::prelude::*; use regex::Regex; @@ -24,10 +24,12 @@ use crate::{ ModuleFilenameTemplateFn, ModuleOrSource, }; -static CSS_EXTENSION_DETECT_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"\.css($|\?)").expect("failed to compile CSS_EXTENSION_DETECT_REGEXP")); -static URL_FORMATTING_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"^\n\/\/(.*)$").expect("failed to compile URL_FORMATTING_REGEXP regex")); +static CSS_EXTENSION_DETECT_REGEXP: LazyLock = LazyLock::new(|| { + Regex::new(r"\.css($|\?)").expect("failed to compile CSS_EXTENSION_DETECT_REGEXP") +}); +static URL_FORMATTING_REGEXP: LazyLock = LazyLock::new(|| { + Regex::new(r"^\n\/\/(.*)$").expect("failed to compile URL_FORMATTING_REGEXP regex") +}); #[derive(Clone)] pub enum ModuleFilenameTemplate { diff --git a/crates/rspack_plugin_externals/Cargo.toml b/crates/rspack_plugin_externals/Cargo.toml index f789c3600dc..cb509fa36bc 100644 --- a/crates/rspack_plugin_externals/Cargo.toml +++ b/crates/rspack_plugin_externals/Cargo.toml @@ -8,7 +8,6 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -once_cell = { workspace = true } regex = { workspace = true } rspack_core = { path = "../rspack_core" } rspack_error = { path = "../rspack_error" } diff --git a/crates/rspack_plugin_externals/src/http_externals_plugin.rs b/crates/rspack_plugin_externals/src/http_externals_plugin.rs index 11c2381f538..30911451974 100644 --- a/crates/rspack_plugin_externals/src/http_externals_plugin.rs +++ b/crates/rspack_plugin_externals/src/http_externals_plugin.rs @@ -1,4 +1,5 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use regex::Regex; use rspack_core::{ BoxPlugin, ExternalItem, ExternalItemFnCtx, ExternalItemFnResult, ExternalItemValue, PluginExt, @@ -6,12 +7,12 @@ use rspack_core::{ use crate::ExternalsPlugin; -static EXTERNAL_HTTP_REQUEST: Lazy = - Lazy::new(|| Regex::new(r"^(//|https?://|#)").expect("Invalid regex")); -static EXTERNAL_HTTP_STD_REQUEST: Lazy = - Lazy::new(|| Regex::new(r"^(//|https?://|std:)").expect("Invalid regex")); -static EXTERNAL_CSS_REQUEST: Lazy = - Lazy::new(|| Regex::new(r"^\.css(\?|$)").expect("Invalid regex")); +static EXTERNAL_HTTP_REQUEST: LazyLock = + LazyLock::new(|| Regex::new(r"^(//|https?://|#)").expect("Invalid regex")); +static EXTERNAL_HTTP_STD_REQUEST: LazyLock = + LazyLock::new(|| Regex::new(r"^(//|https?://|std:)").expect("Invalid regex")); +static EXTERNAL_CSS_REQUEST: LazyLock = + LazyLock::new(|| Regex::new(r"^\.css(\?|$)").expect("Invalid regex")); pub fn http_externals_rspack_plugin(css: bool, web_async: bool) -> BoxPlugin { if web_async { diff --git a/crates/rspack_plugin_externals/src/plugin.rs b/crates/rspack_plugin_externals/src/plugin.rs index eb93260c6e4..3b97d54bc4b 100644 --- a/crates/rspack_plugin_externals/src/plugin.rs +++ b/crates/rspack_plugin_externals/src/plugin.rs @@ -1,6 +1,6 @@ use std::fmt::Debug; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use regex::Regex; use rspack_core::{ ApplyContext, BoxModule, CompilerOptions, ContextInfo, ExternalItem, ExternalItemFnCtx, @@ -11,8 +11,8 @@ use rspack_core::{ use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; -static UNSPECIFIED_EXTERNAL_TYPE_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"^[a-z0-9-]+ ").expect("Invalid regex")); +static UNSPECIFIED_EXTERNAL_TYPE_REGEXP: LazyLock = + LazyLock::new(|| Regex::new(r"^[a-z0-9-]+ ").expect("Invalid regex")); #[plugin] #[derive(Debug)] diff --git a/crates/rspack_plugin_extract_css/Cargo.toml b/crates/rspack_plugin_extract_css/Cargo.toml index 89dc45ee4ea..d4d05768993 100644 --- a/crates/rspack_plugin_extract_css/Cargo.toml +++ b/crates/rspack_plugin_extract_css/Cargo.toml @@ -7,7 +7,6 @@ version = "0.1.0" [dependencies] async-trait = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } rspack_collections = { path = "../rspack_collections" } rspack_core = { path = "../rspack_core" } diff --git a/crates/rspack_plugin_extract_css/src/css_module.rs b/crates/rspack_plugin_extract_css/src/css_module.rs index cf7140999dd..7b0cc58067f 100644 --- a/crates/rspack_plugin_extract_css/src/css_module.rs +++ b/crates/rspack_plugin_extract_css/src/css_module.rs @@ -1,7 +1,7 @@ use std::hash::Hash; use std::path::PathBuf; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use rspack_collections::{Identifiable, Identifier}; use rspack_core::rspack_sources::Source; use rspack_core::{ @@ -18,8 +18,8 @@ use rustc_hash::FxHashSet; use crate::css_dependency::CssDependency; use crate::plugin::{MODULE_TYPE, SOURCE_TYPE}; -pub(crate) static DEPENDENCY_TYPE: Lazy = - Lazy::new(|| DependencyType::Custom("mini-extract-dep")); +pub(crate) static DEPENDENCY_TYPE: LazyLock = + LazyLock::new(|| DependencyType::Custom("mini-extract-dep")); #[impl_source_map_config] #[derive(Debug)] diff --git a/crates/rspack_plugin_extract_css/src/plugin.rs b/crates/rspack_plugin_extract_css/src/plugin.rs index da812202f66..d7b6ab71cfd 100644 --- a/crates/rspack_plugin_extract_css/src/plugin.rs +++ b/crates/rspack_plugin_extract_css/src/plugin.rs @@ -1,6 +1,6 @@ +use std::sync::LazyLock; use std::{borrow::Cow, cmp::max, hash::Hash, path::PathBuf, sync::Arc}; -use once_cell::sync::Lazy; use regex::Regex; use rspack_collections::{IdentifierMap, IdentifierSet, UkeySet}; use rspack_core::{ @@ -28,28 +28,30 @@ use crate::{ }; pub static PLUGIN_NAME: &str = "css-extract-rspack-plugin"; -pub static MODULE_TYPE_STR: Lazy = Lazy::new(|| Ustr::from("css/mini-extract")); -pub static MODULE_TYPE: Lazy = Lazy::new(|| ModuleType::Custom(*MODULE_TYPE_STR)); -pub static SOURCE_TYPE: Lazy<[SourceType; 1]> = - Lazy::new(|| [SourceType::Custom(*MODULE_TYPE_STR)]); +pub static MODULE_TYPE_STR: LazyLock = LazyLock::new(|| Ustr::from("css/mini-extract")); +pub static MODULE_TYPE: LazyLock = + LazyLock::new(|| ModuleType::Custom(*MODULE_TYPE_STR)); +pub static SOURCE_TYPE: LazyLock<[SourceType; 1]> = + LazyLock::new(|| [SourceType::Custom(*MODULE_TYPE_STR)]); pub static AUTO_PUBLIC_PATH: &str = "__mini_css_extract_plugin_public_path_auto__"; -pub static AUTO_PUBLIC_PATH_RE: Lazy = - Lazy::new(|| Regex::new(AUTO_PUBLIC_PATH).expect("should compile")); +pub static AUTO_PUBLIC_PATH_RE: LazyLock = + LazyLock::new(|| Regex::new(AUTO_PUBLIC_PATH).expect("should compile")); pub static ABSOLUTE_PUBLIC_PATH: &str = "webpack:///mini-css-extract-plugin/"; -pub static ABSOLUTE_PUBLIC_PATH_RE: Lazy = - Lazy::new(|| Regex::new(ABSOLUTE_PUBLIC_PATH).expect("should compile")); +pub static ABSOLUTE_PUBLIC_PATH_RE: LazyLock = + LazyLock::new(|| Regex::new(ABSOLUTE_PUBLIC_PATH).expect("should compile")); pub static BASE_URI: &str = "webpack://"; -pub static BASE_URI_RE: Lazy = Lazy::new(|| Regex::new(BASE_URI).expect("should compile")); +pub static BASE_URI_RE: LazyLock = + LazyLock::new(|| Regex::new(BASE_URI).expect("should compile")); pub static SINGLE_DOT_PATH_SEGMENT: &str = "__mini_css_extract_plugin_single_dot_path_segment__"; -pub static SINGLE_DOT_PATH_SEGMENT_RE: Lazy = - Lazy::new(|| Regex::new(SINGLE_DOT_PATH_SEGMENT).expect("should compile")); +pub static SINGLE_DOT_PATH_SEGMENT_RE: LazyLock = + LazyLock::new(|| Regex::new(SINGLE_DOT_PATH_SEGMENT).expect("should compile")); -static STARTS_WITH_AT_IMPORT_REGEX: Lazy = - Lazy::new(|| Regex::new("^@import url").expect("should compile")); +static STARTS_WITH_AT_IMPORT_REGEX: LazyLock = + LazyLock::new(|| Regex::new("^@import url").expect("should compile")); struct CssOrderConflicts { chunk: ChunkUkey, @@ -343,8 +345,8 @@ impl PluginCssExtract { external_source.add(header); } if !module.media.is_empty() { - static MEDIA_RE: Lazy = - Lazy::new(|| Regex::new(r#";|\s*$"#).expect("should compile")); + static MEDIA_RE: LazyLock = + LazyLock::new(|| Regex::new(r#";|\s*$"#).expect("should compile")); let new_content = MEDIA_RE.replace_all(content.as_ref(), &module.media); external_source.add(RawSource::from(new_content.to_string() + "\n")); } else { @@ -719,7 +721,8 @@ fn get_undo_path(filename: &str, output_path: &str, enforce_relative: bool) -> S .unwrap_or(output_path) .to_string(); - static PATH_SEP: Lazy = Lazy::new(|| Regex::new(r#"[\\/]+"#).expect("should compile")); + static PATH_SEP: LazyLock = + LazyLock::new(|| Regex::new(r#"[\\/]+"#).expect("should compile")); for part in PATH_SEP.split(filename) { if part == ".." { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs index 02a9bf0b1d2..cb582425321 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs @@ -1,6 +1,6 @@ use std::sync::Arc; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use rspack_collections::{IdentifierDashMap, IdentifierMap, IdentifierSet}; use rspack_core::{ filter_runtime, import_statement, merge_runtime, AsContextDependency, @@ -22,8 +22,8 @@ use super::create_resource_identifier_for_esm_dependency; // Align with https://github.com/webpack/webpack/blob/51f0f0aeac072f989f8d40247f6c23a1995c5c37/lib/dependencies/HarmonyImportDependency.js#L361-L365 // This map is used to save the runtime conditions of modules and used by HarmonyAcceptDependency in hot module replacement. // It can not be saved in TemplateContext because only dependencies of rebuild modules will be templated again. -static IMPORT_EMITTED_MAP: Lazy>> = - Lazy::new(Default::default); +static IMPORT_EMITTED_MAP: LazyLock>> = + LazyLock::new(Default::default); pub fn get_import_emitted_runtime( module: &ModuleIdentifier, diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/check_var_decl.rs b/crates/rspack_plugin_javascript/src/parser_plugin/check_var_decl.rs index da2803976cf..c722a581fed 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/check_var_decl.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/check_var_decl.rs @@ -1,4 +1,5 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use rustc_hash::FxHashSet; use swc_core::common::Spanned; use swc_core::ecma::ast::{Ident, ObjectPatProp, Pat, VarDeclKind}; @@ -6,7 +7,7 @@ use swc_core::ecma::ast::{Ident, ObjectPatProp, Pat, VarDeclKind}; use super::JavascriptParserPlugin; use crate::visitors::{create_traceable_error, JavascriptParser}; -static STRICT_MODE_RESERVED_WORDS: Lazy> = Lazy::new(|| { +static STRICT_MODE_RESERVED_WORDS: LazyLock> = LazyLock::new(|| { [ "implements", "interface", diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/define_plugin/parser.rs b/crates/rspack_plugin_javascript/src/parser_plugin/define_plugin/parser.rs index eccbbd87ea5..63541d760fd 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/define_plugin/parser.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/define_plugin/parser.rs @@ -1,7 +1,7 @@ +use std::sync::LazyLock; use std::{borrow::Cow, sync::Arc}; use itertools::Itertools as _; -use once_cell::sync::Lazy; use regex::Regex; use rspack_core::{ConstDependency, RuntimeGlobals, SpanExt as _}; use rustc_hash::{FxHashMap, FxHashSet}; @@ -15,13 +15,13 @@ use crate::{ JavascriptParserPlugin, }; -static TYPEOF_OPERATOR_REGEXP: Lazy = - Lazy::new(|| Regex::new("^typeof\\s+").expect("should init `TYPEOF_OPERATOR_REGEXP`")); -static WEBPACK_REQUIRE_FUNCTION_REGEXP: Lazy = Lazy::new(|| { +static TYPEOF_OPERATOR_REGEXP: LazyLock = + LazyLock::new(|| Regex::new("^typeof\\s+").expect("should init `TYPEOF_OPERATOR_REGEXP`")); +static WEBPACK_REQUIRE_FUNCTION_REGEXP: LazyLock = LazyLock::new(|| { Regex::new("__webpack_require__\\s*(!?\\.)") .expect("should init `WEBPACK_REQUIRE_FUNCTION_REGEXP`") }); -static WEBPACK_REQUIRE_IDENTIFIER_REGEXP: Lazy = Lazy::new(|| { +static WEBPACK_REQUIRE_IDENTIFIER_REGEXP: LazyLock = LazyLock::new(|| { Regex::new("__webpack_require__").expect("should init `WEBPACK_REQUIRE_IDENTIFIER_REGEXP`") }); diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/mod.rs b/crates/rspack_plugin_javascript/src/parser_plugin/mod.rs index 9b8a1deb077..75bd7cf99dc 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/mod.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/mod.rs @@ -59,8 +59,8 @@ pub(crate) use self::use_strict_plugin::UseStrictPlugin; pub(crate) use self::webpack_included_plugin::WebpackIsIncludedPlugin; pub(crate) use self::worker_plugin::WorkerPlugin; -pub static JS_DEFAULT_KEYWORD: once_cell::sync::Lazy = - once_cell::sync::Lazy::new(|| swc_core::atoms::atom!("default")); +pub static JS_DEFAULT_KEYWORD: std::sync::LazyLock = + std::sync::LazyLock::new(|| swc_core::atoms::atom!("default")); -pub static DEFAULT_STAR_JS_WORD: once_cell::sync::Lazy = - once_cell::sync::Lazy::new(|| swc_core::atoms::atom!("*default*")); +pub static DEFAULT_STAR_JS_WORD: std::sync::LazyLock = + std::sync::LazyLock::new(|| swc_core::atoms::atom!("*default*")); diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs index 49d244b1eaa..e729816f46a 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs @@ -1,7 +1,7 @@ use std::hash::Hash; +use std::sync::LazyLock; use itertools::Itertools; -use once_cell::sync::Lazy; use regex::Regex; use rspack_core::{ AsyncDependenciesBlock, ConstDependency, DependencyLocation, EntryOptions, ErrorSpan, @@ -198,8 +198,8 @@ pub struct WorkerPlugin { pattern_syntax: FxHashMap>, } -static WORKER_FROM_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^(.+?)(\(\))?\s+from\s+(.+)$").expect("invalid regex")); +static WORKER_FROM_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^(.+?)(\(\))?\s+from\s+(.+)$").expect("invalid regex")); const WORKER_SPECIFIER_TAG: &str = "_identifier__worker_specifier_tag__"; diff --git a/crates/rspack_plugin_javascript/src/plugin/mangle_exports_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/mangle_exports_plugin.rs index 5872321bd1d..abd1e55164e 100644 --- a/crates/rspack_plugin_javascript/src/plugin/mangle_exports_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/mangle_exports_plugin.rs @@ -1,4 +1,5 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use regex::Regex; use rspack_core::{ ApplyContext, BuildMetaExportsType, Compilation, CompilationOptimizeCodeGeneration, @@ -83,10 +84,11 @@ impl Plugin for MangleExportsPlugin { fn compare_strings_numeric(a: Option<&Atom>, b: Option<&Atom>) -> std::cmp::Ordering { a.cmp(&b) } -static MANGLE_NAME_NORMAL_REG: Lazy = - Lazy::new(|| Regex::new("^[a-zA-Z0-9_$]").expect("should construct regex")); -static MANGLE_NAME_DETERMINISTIC_REG: Lazy = - Lazy::new(|| Regex::new("^[a-zA-Z_$][a-zA-Z0-9_$]|^[1-9][0-9]").expect("should construct regex")); +static MANGLE_NAME_NORMAL_REG: LazyLock = + LazyLock::new(|| Regex::new("^[a-zA-Z0-9_$]").expect("should construct regex")); +static MANGLE_NAME_DETERMINISTIC_REG: LazyLock = LazyLock::new(|| { + Regex::new("^[a-zA-Z_$][a-zA-Z0-9_$]|^[1-9][0-9]").expect("should construct regex") +}); /// Function to mangle exports information. fn mangle_exports_info( diff --git a/crates/rspack_plugin_javascript/src/plugin/mod.rs b/crates/rspack_plugin_javascript/src/plugin/mod.rs index 812e2a76e25..71abec57b7b 100644 --- a/crates/rspack_plugin_javascript/src/plugin/mod.rs +++ b/crates/rspack_plugin_javascript/src/plugin/mod.rs @@ -15,6 +15,7 @@ mod side_effects_flag_plugin; use std::borrow::Cow; use std::collections::hash_map::Entry; use std::hash::Hash; +use std::sync::LazyLock; pub use drive::*; pub use flag_dependency_exports_plugin::*; @@ -22,7 +23,6 @@ pub use flag_dependency_usage_plugin::*; use indoc::indoc; pub use mangle_exports_plugin::*; pub use module_concatenation_plugin::*; -use once_cell::sync::Lazy; use rspack_ast::javascript::Ast; use rspack_collections::{Identifier, IdentifierDashMap, IdentifierLinkedMap, IdentifierMap}; use rspack_core::concatenated_module::find_new_name; @@ -50,8 +50,9 @@ use crate::runtime::{ render_chunk_modules, render_module, render_runtime_modules, stringify_array, }; -static COMPILATION_HOOKS_MAP: Lazy>> = - Lazy::new(Default::default); +static COMPILATION_HOOKS_MAP: LazyLock< + FxDashMap>, +> = LazyLock::new(Default::default); #[derive(Debug, Clone)] struct WithHash { diff --git a/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs index 56564d81afa..c0c35dedabd 100644 --- a/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs @@ -2,8 +2,8 @@ use std::collections::VecDeque; use std::fmt::Debug; use std::path::PathBuf; use std::sync::Arc; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use rspack_collections::IdentifierSet; use rspack_core::{ BoxModule, Compilation, CompilationOptimizeDependencies, ConnectionState, FactoryMeta, @@ -328,8 +328,8 @@ impl<'a> SideEffectsFlagPluginVisitor<'a> { } } -static PURE_COMMENTS: Lazy = - Lazy::new(|| regex::Regex::new("^\\s*(#|@)__PURE__\\s*$").expect("Should create the regex")); +static PURE_COMMENTS: LazyLock = + LazyLock::new(|| regex::Regex::new("^\\s*(#|@)__PURE__\\s*$").expect("Should create the regex")); fn is_pure_call_expr( call_expr: &CallExpr, diff --git a/crates/rspack_plugin_javascript/src/visitors/dependency/context_dependency_helper.rs b/crates/rspack_plugin_javascript/src/visitors/dependency/context_dependency_helper.rs index d638e1805d3..50a3b1fcc0f 100644 --- a/crates/rspack_plugin_javascript/src/visitors/dependency/context_dependency_helper.rs +++ b/crates/rspack_plugin_javascript/src/visitors/dependency/context_dependency_helper.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; +use std::sync::LazyLock; use itertools::Itertools; -use once_cell::sync::Lazy; use regex::Regex; use rspack_core::parse_resource; use rspack_error::Severity; @@ -224,7 +224,7 @@ pub(super) fn split_context_from_prefix(prefix: String) -> (String, String) { } } -static META_REG: Lazy = Lazy::new(|| { +static META_REG: LazyLock = LazyLock::new(|| { Regex::new(r"[-\[\]\\/{}()*+?.^$|]").expect("Failed to initialize `MATCH_RESOURCE_REGEX`") }); diff --git a/crates/rspack_plugin_javascript/src/visitors/dependency/util.rs b/crates/rspack_plugin_javascript/src/visitors/dependency/util.rs index 7182e0ba398..649270ed7c6 100644 --- a/crates/rspack_plugin_javascript/src/visitors/dependency/util.rs +++ b/crates/rspack_plugin_javascript/src/visitors/dependency/util.rs @@ -141,14 +141,14 @@ pub(crate) mod expr_like { pub(crate) mod expr_matcher { use std::sync::Arc; + use std::sync::LazyLock; - use once_cell::sync::Lazy; use swc_core::common::SourceMap; use swc_core::ecma::{ast::Ident, parser::parse_file_as_expr}; use super::expr_like::*; - static PARSED_MEMBER_EXPR_CM: Lazy> = Lazy::new(Default::default); + static PARSED_MEMBER_EXPR_CM: LazyLock> = LazyLock::new(Default::default); // The usage of define_member_expr_matchers is limited in `member_expr_matcher`. // Do not extends it's usage out of this mod. @@ -157,7 +157,7 @@ pub(crate) mod expr_matcher { $($fn_name:ident: $first:expr,)* }) => { $(pub(crate) fn $fn_name(expr: &E) -> bool { - static TARGET: Lazy> = Lazy::new(|| { + static TARGET: LazyLock> = LazyLock::new(|| { let mut errors = vec![]; let fm = PARSED_MEMBER_EXPR_CM.new_source_file(Arc::new(swc_core::common::FileName::Anon), $first.to_string()); diff --git a/crates/rspack_plugin_javascript/src/webpack_comment.rs b/crates/rspack_plugin_javascript/src/webpack_comment.rs index ec86bb963d4..77592c8f9fb 100644 --- a/crates/rspack_plugin_javascript/src/webpack_comment.rs +++ b/crates/rspack_plugin_javascript/src/webpack_comment.rs @@ -1,5 +1,6 @@ +use std::sync::LazyLock; + use itertools::Itertools; -use once_cell::sync::Lazy; use regex::Captures; use rspack_error::miette::{Diagnostic, Severity}; use rspack_regex::RspackRegex; @@ -147,13 +148,13 @@ fn add_magic_comment_warning( // _6 for regexp // _7 for array // _8 for identifier -static WEBPACK_MAGIC_COMMENT_REGEXP: Lazy = Lazy::new(|| { +static WEBPACK_MAGIC_COMMENT_REGEXP: LazyLock = LazyLock::new(|| { regex::Regex::new(r#"(?P<_0>webpack[a-zA-Z\d_-]+)\s*:\s*("(?P<_1>[^"]+)"|'(?P<_2>[^']+)'|`(?P<_3>[^`]+)`|(?P<_4>[\d.-]+)|(?P<_5>true|false)|(?P<_6>/([^,]+)/([dgimsuvy]*))|\[(?P<_7>[^\]]+)|(?P<_8>([^,]+)))"#) .expect("invalid regex") }); -static WEBAPCK_EXPORT_NAME_REGEXP: Lazy = - Lazy::new(|| regex::Regex::new(r#"^["`'](\w+)["`']$"#).expect("invalid regex")); +static WEBAPCK_EXPORT_NAME_REGEXP: LazyLock = + LazyLock::new(|| regex::Regex::new(r#"^["`'](\w+)["`']$"#).expect("invalid regex")); pub fn try_extract_webpack_magic_comment( source_file: &SourceFile, diff --git a/crates/rspack_plugin_lazy_compilation/Cargo.toml b/crates/rspack_plugin_lazy_compilation/Cargo.toml index d4607f0e0d2..4554765db07 100644 --- a/crates/rspack_plugin_lazy_compilation/Cargo.toml +++ b/crates/rspack_plugin_lazy_compilation/Cargo.toml @@ -6,7 +6,6 @@ version = "0.1.0" [dependencies] async-trait = { workspace = true } -once_cell = { workspace = true } rustc-hash = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } diff --git a/crates/rspack_plugin_lazy_compilation/src/plugin.rs b/crates/rspack_plugin_lazy_compilation/src/plugin.rs index 90d648f47d8..1cb836fc08a 100644 --- a/crates/rspack_plugin_lazy_compilation/src/plugin.rs +++ b/crates/rspack_plugin_lazy_compilation/src/plugin.rs @@ -1,6 +1,6 @@ +use std::sync::LazyLock; use std::{fmt::Debug, sync::Arc}; -use once_cell::sync::Lazy; use rspack_core::{ ApplyContext, BoxModule, Compilation, CompilationParams, CompilerCompilation, CompilerOptions, DependencyType, EntryDependency, Module, ModuleFactory, ModuleFactoryCreateData, @@ -15,7 +15,7 @@ use crate::{ backend::Backend, factory::LazyCompilationDependencyFactory, module::LazyCompilationProxyModule, }; -static WEBPACK_DEV_SERVER_CLIENT_RE: Lazy = Lazy::new(|| { +static WEBPACK_DEV_SERVER_CLIENT_RE: LazyLock = LazyLock::new(|| { RspackRegex::new( r#"(webpack|rspack)[/\\]hot[/\\]|(webpack|rspack)-dev-server[/\\]client|(webpack|rspack)-hot-middleware[/\\]client"#, ) diff --git a/crates/rspack_plugin_library/Cargo.toml b/crates/rspack_plugin_library/Cargo.toml index b612afc11d9..ec0db59487c 100644 --- a/crates/rspack_plugin_library/Cargo.toml +++ b/crates/rspack_plugin_library/Cargo.toml @@ -8,7 +8,6 @@ version = "0.1.0" [dependencies] async-trait = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } rspack_collections = { path = "../rspack_collections" } rspack_core = { path = "../rspack_core" } diff --git a/crates/rspack_plugin_library/src/assign_library_plugin.rs b/crates/rspack_plugin_library/src/assign_library_plugin.rs index a665eda17bf..252f9c64d31 100644 --- a/crates/rspack_plugin_library/src/assign_library_plugin.rs +++ b/crates/rspack_plugin_library/src/assign_library_plugin.rs @@ -1,6 +1,6 @@ use std::hash::Hash; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use regex::Regex; use rspack_core::rspack_sources::SourceExt; use rspack_core::{ @@ -512,11 +512,11 @@ fn access_with_init(accessor: &[String], existing_length: usize, init_last: bool current } -static KEYWORD_REGEXP: Lazy = Lazy::new(|| { +static KEYWORD_REGEXP: LazyLock = LazyLock::new(|| { Regex::new(r"^(await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|super|switch|static|this|throw|try|true|typeof|var|void|while|with|yield)$").expect("should init regex") }); -static IDENTIFIER_REGEXP: Lazy = Lazy::new(|| { +static IDENTIFIER_REGEXP: LazyLock = LazyLock::new(|| { Regex::new(r"^[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*$") .expect("should init regex") }); diff --git a/crates/rspack_plugin_lightning_css_minimizer/Cargo.toml b/crates/rspack_plugin_lightning_css_minimizer/Cargo.toml index 389133742d7..842e88de3dc 100644 --- a/crates/rspack_plugin_lightning_css_minimizer/Cargo.toml +++ b/crates/rspack_plugin_lightning_css_minimizer/Cargo.toml @@ -9,7 +9,6 @@ version = "0.1.0" [dependencies] lightningcss = { workspace = true, features = ["sourcemap", "browserslist"] } -once_cell = { workspace = true } parcel_sourcemap = { workspace = true } rayon = { workspace = true } regex = { workspace = true } diff --git a/crates/rspack_plugin_lightning_css_minimizer/src/lib.rs b/crates/rspack_plugin_lightning_css_minimizer/src/lib.rs index 606a1654a43..fd9fb9d9a18 100644 --- a/crates/rspack_plugin_lightning_css_minimizer/src/lib.rs +++ b/crates/rspack_plugin_lightning_css_minimizer/src/lib.rs @@ -1,5 +1,6 @@ #![feature(let_chains)] +use std::sync::LazyLock; use std::{ collections::HashSet, hash::Hash, @@ -11,7 +12,6 @@ use lightningcss::{ stylesheet::{MinifyOptions, ParserFlags, ParserOptions, StyleSheet}, targets::{Browsers, Targets}, }; -use once_cell::sync::Lazy; use rayon::prelude::*; use regex::Regex; use rspack_core::{ @@ -26,8 +26,8 @@ use rspack_hook::{plugin, plugin_hook}; use rspack_regex::RspackRegex; use rspack_util::try_any_sync; -static CSS_ASSET_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"\.css(\?.*)?$").expect("Invalid RegExp")); +static CSS_ASSET_REGEXP: LazyLock = + LazyLock::new(|| Regex::new(r"\.css(\?.*)?$").expect("Invalid RegExp")); #[derive(Debug, Clone, Hash)] pub enum LightningCssMinimizerRule { diff --git a/crates/rspack_plugin_mf/Cargo.toml b/crates/rspack_plugin_mf/Cargo.toml index 71f44401604..15569e3b356 100644 --- a/crates/rspack_plugin_mf/Cargo.toml +++ b/crates/rspack_plugin_mf/Cargo.toml @@ -20,7 +20,6 @@ rspack_util = { path = "../rspack_util" } async-trait = { workspace = true } hashlink = { workspace = true } itertools = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } rustc-hash = { workspace = true } serde = { workspace = true } diff --git a/crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs b/crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs index 5556c73b24f..758fa7b5488 100644 --- a/crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs +++ b/crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs @@ -1,9 +1,9 @@ use std::path::PathBuf; +use std::sync::LazyLock; use std::sync::Mutex; use std::{fmt, path::Path, sync::Arc}; use async_trait::async_trait; -use once_cell::sync::Lazy; use regex::Regex; use rspack_core::{ ApplyContext, BoxModule, ChunkUkey, Compilation, CompilationAdditionalTreeRuntimeRequirements, @@ -49,12 +49,12 @@ impl fmt::Display for ConsumeVersion { } } -static RELATIVE_REQUEST: Lazy = - Lazy::new(|| Regex::new(r"^\.\.?(\/|$)").expect("Invalid regex")); -static ABSOLUTE_REQUEST: Lazy = - Lazy::new(|| Regex::new(r"^(\/|[A-Za-z]:\\|\\\\)").expect("Invalid regex")); -static PACKAGE_NAME: Lazy = - Lazy::new(|| Regex::new(r"^((?:@[^\\/]+[\\/])?[^\\/]+)").expect("Invalid regex")); +static RELATIVE_REQUEST: LazyLock = + LazyLock::new(|| Regex::new(r"^\.\.?(\/|$)").expect("Invalid regex")); +static ABSOLUTE_REQUEST: LazyLock = + LazyLock::new(|| Regex::new(r"^(\/|[A-Za-z]:\\|\\\\)").expect("Invalid regex")); +static PACKAGE_NAME: LazyLock = + LazyLock::new(|| Regex::new(r"^((?:@[^\\/]+[\\/])?[^\\/]+)").expect("Invalid regex")); #[derive(Debug)] struct MatchedConsumes { diff --git a/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs b/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs index e36066fed22..dda1c4a77f7 100644 --- a/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs +++ b/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs @@ -1,7 +1,7 @@ +use std::sync::LazyLock; use std::{fmt, sync::Arc}; use async_trait::async_trait; -use once_cell::sync::Lazy; use regex::Regex; use rspack_core::{ ApplyContext, BoxModule, Compilation, CompilationParams, CompilerCompilation, CompilerFinishMake, @@ -19,10 +19,10 @@ use super::{ provide_shared_module_factory::ProvideSharedModuleFactory, }; -static RELATIVE_REQUEST: Lazy = - Lazy::new(|| Regex::new(r"^(\/|[A-Za-z]:\\|\\\\|\.\.?(\/|$))").expect("Invalid regex")); -static ABSOLUTE_REQUEST: Lazy = - Lazy::new(|| Regex::new(r"^(\/|[A-Za-z]:\\|\\\\)").expect("Invalid regex")); +static RELATIVE_REQUEST: LazyLock = + LazyLock::new(|| Regex::new(r"^(\/|[A-Za-z]:\\|\\\\|\.\.?(\/|$))").expect("Invalid regex")); +static ABSOLUTE_REQUEST: LazyLock = + LazyLock::new(|| Regex::new(r"^(\/|[A-Za-z]:\\|\\\\)").expect("Invalid regex")); #[derive(Debug, Clone)] pub struct ProvideOptions { diff --git a/crates/rspack_plugin_real_content_hash/src/lib.rs b/crates/rspack_plugin_real_content_hash/src/lib.rs index 04899646609..1cf1a84f180 100644 --- a/crates/rspack_plugin_real_content_hash/src/lib.rs +++ b/crates/rspack_plugin_real_content_hash/src/lib.rs @@ -3,10 +3,11 @@ use std::{ borrow::Cow, hash::{BuildHasherDefault, Hasher}, + sync::LazyLock, }; use derivative::Derivative; -use once_cell::sync::{Lazy, OnceCell}; +use once_cell::sync::OnceCell; use rayon::prelude::*; use regex::{Captures, Regex}; use rspack_core::{ @@ -20,8 +21,8 @@ use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher}; type IndexSet = indexmap::IndexSet>; -pub static QUOTE_META: Lazy = - Lazy::new(|| Regex::new(r"[-\[\]\\/{}()*+?.^$|]").expect("Invalid regex")); +pub static QUOTE_META: LazyLock = + LazyLock::new(|| Regex::new(r"[-\[\]\\/{}()*+?.^$|]").expect("Invalid regex")); #[plugin] #[derive(Debug, Default)] diff --git a/crates/rspack_plugin_runtime/Cargo.toml b/crates/rspack_plugin_runtime/Cargo.toml index d19bbac6652..e794264c904 100644 --- a/crates/rspack_plugin_runtime/Cargo.toml +++ b/crates/rspack_plugin_runtime/Cargo.toml @@ -11,7 +11,6 @@ version = "0.1.0" async-trait = { workspace = true } indexmap = { workspace = true } itertools = { workspace = true } -once_cell = { workspace = true } rspack_collections = { path = "../rspack_collections" } rspack_core = { path = "../rspack_core" } rspack_error = { path = "../rspack_error" } diff --git a/crates/rspack_plugin_runtime/src/runtime_plugin.rs b/crates/rspack_plugin_runtime/src/runtime_plugin.rs index 4ff64e422bd..a3808b1411d 100644 --- a/crates/rspack_plugin_runtime/src/runtime_plugin.rs +++ b/crates/rspack_plugin_runtime/src/runtime_plugin.rs @@ -1,7 +1,7 @@ use std::hash::Hash; +use std::sync::LazyLock; use async_trait::async_trait; -use once_cell::sync::Lazy; use rspack_core::{ get_css_chunk_filename_template, get_js_chunk_filename_template, has_hash_placeholder, ApplyContext, ChunkLoading, ChunkUkey, Compilation, CompilationParams, @@ -27,7 +27,7 @@ use crate::runtime_module::{ RuntimeIdRuntimeModule, SystemContextRuntimeModule, }; -static GLOBALS_ON_REQUIRE: Lazy> = Lazy::new(|| { +static GLOBALS_ON_REQUIRE: LazyLock> = LazyLock::new(|| { vec![ RuntimeGlobals::CHUNK_NAME, RuntimeGlobals::RUNTIME_ID, @@ -62,57 +62,59 @@ static GLOBALS_ON_REQUIRE: Lazy> = Lazy::new(|| { ] }); -static MODULE_DEPENDENCIES: Lazy)>> = Lazy::new(|| { - vec![ - (RuntimeGlobals::MODULE_LOADED, vec![RuntimeGlobals::MODULE]), - (RuntimeGlobals::MODULE_ID, vec![RuntimeGlobals::MODULE]), - ( - RuntimeGlobals::HARMONY_MODULE_DECORATOR, - vec![RuntimeGlobals::MODULE, RuntimeGlobals::REQUIRE_SCOPE], - ), - ( - RuntimeGlobals::NODE_MODULE_DECORATOR, - vec![RuntimeGlobals::MODULE, RuntimeGlobals::REQUIRE_SCOPE], - ), - ] -}); +static MODULE_DEPENDENCIES: LazyLock)>> = + LazyLock::new(|| { + vec![ + (RuntimeGlobals::MODULE_LOADED, vec![RuntimeGlobals::MODULE]), + (RuntimeGlobals::MODULE_ID, vec![RuntimeGlobals::MODULE]), + ( + RuntimeGlobals::HARMONY_MODULE_DECORATOR, + vec![RuntimeGlobals::MODULE, RuntimeGlobals::REQUIRE_SCOPE], + ), + ( + RuntimeGlobals::NODE_MODULE_DECORATOR, + vec![RuntimeGlobals::MODULE, RuntimeGlobals::REQUIRE_SCOPE], + ), + ] + }); -static TREE_DEPENDENCIES: Lazy)>> = Lazy::new(|| { - vec![ - ( - RuntimeGlobals::COMPAT_GET_DEFAULT_EXPORT, - vec![RuntimeGlobals::DEFINE_PROPERTY_GETTERS], - ), - ( - RuntimeGlobals::CREATE_FAKE_NAMESPACE_OBJECT, - vec![ +static TREE_DEPENDENCIES: LazyLock)>> = + LazyLock::new(|| { + vec![ + ( + RuntimeGlobals::COMPAT_GET_DEFAULT_EXPORT, + vec![RuntimeGlobals::DEFINE_PROPERTY_GETTERS], + ), + ( + RuntimeGlobals::CREATE_FAKE_NAMESPACE_OBJECT, + vec![ + RuntimeGlobals::DEFINE_PROPERTY_GETTERS, + RuntimeGlobals::MAKE_NAMESPACE_OBJECT, + RuntimeGlobals::REQUIRE, + ], + ), + ( RuntimeGlobals::DEFINE_PROPERTY_GETTERS, - RuntimeGlobals::MAKE_NAMESPACE_OBJECT, - RuntimeGlobals::REQUIRE, - ], - ), - ( - RuntimeGlobals::DEFINE_PROPERTY_GETTERS, - vec![RuntimeGlobals::HAS_OWN_PROPERTY], - ), - ( - RuntimeGlobals::INITIALIZE_SHARING, - vec![RuntimeGlobals::SHARE_SCOPE_MAP], - ), - ( - RuntimeGlobals::SHARE_SCOPE_MAP, - vec![RuntimeGlobals::HAS_OWN_PROPERTY], - ), - ( - RuntimeGlobals::HARMONY_MODULE_DECORATOR, - vec![RuntimeGlobals::MODULE, RuntimeGlobals::REQUIRE_SCOPE], - ), - ( - RuntimeGlobals::NODE_MODULE_DECORATOR, - vec![RuntimeGlobals::MODULE, RuntimeGlobals::REQUIRE_SCOPE], - ), - ] -}); + vec![RuntimeGlobals::HAS_OWN_PROPERTY], + ), + ( + RuntimeGlobals::INITIALIZE_SHARING, + vec![RuntimeGlobals::SHARE_SCOPE_MAP], + ), + ( + RuntimeGlobals::SHARE_SCOPE_MAP, + vec![RuntimeGlobals::HAS_OWN_PROPERTY], + ), + ( + RuntimeGlobals::HARMONY_MODULE_DECORATOR, + vec![RuntimeGlobals::MODULE, RuntimeGlobals::REQUIRE_SCOPE], + ), + ( + RuntimeGlobals::NODE_MODULE_DECORATOR, + vec![RuntimeGlobals::MODULE, RuntimeGlobals::REQUIRE_SCOPE], + ), + ] + }); fn handle_require_scope_globals( runtime_requirements: &RuntimeGlobals, diff --git a/crates/rspack_plugin_schemes/Cargo.toml b/crates/rspack_plugin_schemes/Cargo.toml index 0403f4b6f65..105b8d13b1b 100644 --- a/crates/rspack_plugin_schemes/Cargo.toml +++ b/crates/rspack_plugin_schemes/Cargo.toml @@ -9,7 +9,6 @@ version = "0.1.0" [dependencies] async-trait = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } rspack_base64 = { path = "../rspack_base64" } rspack_core = { path = "../rspack_core" } diff --git a/crates/rspack_plugin_schemes/src/data_uri.rs b/crates/rspack_plugin_schemes/src/data_uri.rs index 8667cda6640..6f6d48424c7 100644 --- a/crates/rspack_plugin_schemes/src/data_uri.rs +++ b/crates/rspack_plugin_schemes/src/data_uri.rs @@ -1,4 +1,5 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use regex::Regex; use rspack_core::{ ApplyContext, CompilerOptions, Content, ModuleFactoryCreateData, @@ -8,7 +9,7 @@ use rspack_core::{ use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; -static URI_REGEX: Lazy = Lazy::new(|| { +static URI_REGEX: LazyLock = LazyLock::new(|| { Regex::new(r"(?i)^data:([^;,]+)?((?:;[^;,]+)*?)(?:;(base64))?,(.*)$").expect("Invalid Regex") }); diff --git a/crates/rspack_plugin_split_chunks/Cargo.toml b/crates/rspack_plugin_split_chunks/Cargo.toml index caa735f5219..f5b83aec015 100644 --- a/crates/rspack_plugin_split_chunks/Cargo.toml +++ b/crates/rspack_plugin_split_chunks/Cargo.toml @@ -10,7 +10,6 @@ version = "0.1.0" [dependencies] dashmap = { workspace = true } derivative = { workspace = true } -once_cell = { workspace = true } rayon = { workspace = true } regex = { workspace = true } rspack_collections = { path = "../rspack_collections" } diff --git a/crates/rspack_plugin_split_chunks/src/plugin/max_size.rs b/crates/rspack_plugin_split_chunks/src/plugin/max_size.rs index 1f32597afbe..41c85d15ea9 100644 --- a/crates/rspack_plugin_split_chunks/src/plugin/max_size.rs +++ b/crates/rspack_plugin_split_chunks/src/plugin/max_size.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; +use std::sync::LazyLock; -use once_cell::sync::Lazy; use rayon::prelude::*; use regex::Regex; use rspack_collections::UkeyMap; @@ -58,12 +58,12 @@ fn hash_filename(filename: &str, options: &CompilerOptions) -> String { hash_digest.rendered(8).to_string() } -static REPLACE_MODULE_IDENTIFIER_REG: Lazy = - Lazy::new(|| Regex::new(r"^.*!|\?[^?!]*$").expect("regexp init failed")); -static REPLACE_RELATIVE_PREFIX_REG: Lazy = - Lazy::new(|| Regex::new(r"^(\.\.?\/)+").expect("regexp init failed")); -static REPLACE_ILLEGEL_LETTER_REG: Lazy = - Lazy::new(|| Regex::new(r"(^[.-]|[^a-zA-Z0-9_-])+").expect("regexp init failed")); +static REPLACE_MODULE_IDENTIFIER_REG: LazyLock = + LazyLock::new(|| Regex::new(r"^.*!|\?[^?!]*$").expect("regexp init failed")); +static REPLACE_RELATIVE_PREFIX_REG: LazyLock = + LazyLock::new(|| Regex::new(r"^(\.\.?\/)+").expect("regexp init failed")); +static REPLACE_ILLEGEL_LETTER_REG: LazyLock = + LazyLock::new(|| Regex::new(r"(^[.-]|[^a-zA-Z0-9_-])+").expect("regexp init failed")); fn request_to_id(req: &str) -> String { let mut res = REPLACE_RELATIVE_PREFIX_REG.replace_all(req, "").to_string(); diff --git a/crates/rspack_plugin_swc_css_minimizer/Cargo.toml b/crates/rspack_plugin_swc_css_minimizer/Cargo.toml index 6ea82c41daf..8b705f159c2 100644 --- a/crates/rspack_plugin_swc_css_minimizer/Cargo.toml +++ b/crates/rspack_plugin_swc_css_minimizer/Cargo.toml @@ -8,16 +8,15 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -once_cell = { workspace = true } rayon = { workspace = true } regex = { workspace = true } rspack_core = { path = "../rspack_core" } rspack_error = { path = "../rspack_error" } rspack_hook = { path = "../rspack_hook" } -swc_core = { workspace = true, features = ["css_codegen", "css_parser", "css_minifier"] } -tracing = { workspace = true } rspack_regex = { path = "../rspack_regex" } rspack_util = { path = "../rspack_util" } +swc_core = { workspace = true, features = ["css_codegen", "css_parser", "css_minifier"] } +tracing = { workspace = true } [package.metadata.cargo-shear] ignored = ["tracing"] diff --git a/crates/rspack_plugin_swc_css_minimizer/src/lib.rs b/crates/rspack_plugin_swc_css_minimizer/src/lib.rs index ae331bb1326..7d6a1659a0a 100644 --- a/crates/rspack_plugin_swc_css_minimizer/src/lib.rs +++ b/crates/rspack_plugin_swc_css_minimizer/src/lib.rs @@ -1,6 +1,7 @@ mod swc_css_compiler; -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use rayon::prelude::*; use regex::Regex; use rspack_core::{rspack_sources::MapOptions, Compilation, CompilationProcessAssets, Plugin}; @@ -10,8 +11,8 @@ use rspack_regex::RspackRegex; use rspack_util::try_any_sync; use swc_css_compiler::{SwcCssCompiler, SwcCssSourceMapGenConfig}; -static CSS_ASSET_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"\.css(\?.*)?$").expect("Invalid RegExp")); +static CSS_ASSET_REGEXP: LazyLock = + LazyLock::new(|| Regex::new(r"\.css(\?.*)?$").expect("Invalid RegExp")); #[derive(Debug, Default)] pub struct SwcCssMinimizerRspackPluginOptions { diff --git a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs index ecc4c9135d1..5283b6a539f 100644 --- a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs +++ b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs @@ -5,9 +5,9 @@ mod minify; use std::collections::HashMap; use std::hash::Hash; use std::path::Path; -use std::sync::{mpsc, Mutex}; +use std::sync::{mpsc, LazyLock, Mutex}; -use once_cell::sync::{Lazy, OnceCell}; +use once_cell::sync::OnceCell; use rayon::prelude::*; use regex::Regex; use rspack_core::rspack_sources::{ConcatSource, MapOptions, RawSource, SourceExt, SourceMap}; @@ -32,8 +32,8 @@ use self::minify::{match_object, minify}; const PLUGIN_NAME: &str = "rspack.SwcJsMinimizerRspackPlugin"; -static JAVASCRIPT_ASSET_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"\.[cm]?js(\?.*)?$").expect("Invalid RegExp")); +static JAVASCRIPT_ASSET_REGEXP: LazyLock = + LazyLock::new(|| Regex::new(r"\.[cm]?js(\?.*)?$").expect("Invalid RegExp")); #[derive(Debug, Default)] pub struct SwcJsMinimizerRspackPluginOptions { diff --git a/crates/rspack_util/Cargo.toml b/crates/rspack_util/Cargo.toml index 949db49ab82..0107b720f3d 100644 --- a/crates/rspack_util/Cargo.toml +++ b/crates/rspack_util/Cargo.toml @@ -12,7 +12,6 @@ bitflags = { workspace = true } concat-string = { workspace = true } dashmap = { workspace = true } indexmap = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } rustc-hash = { workspace = true } serde = { workspace = true } diff --git a/crates/rspack_util/src/diff_mode.rs b/crates/rspack_util/src/diff_mode.rs index 7915f6ec106..07f889a416b 100644 --- a/crates/rspack_util/src/diff_mode.rs +++ b/crates/rspack_util/src/diff_mode.rs @@ -1,7 +1,7 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; -static IS_DIFF_MODE: Lazy = - Lazy::new(|| std::env::var("RSPACK_DIFF").ok().unwrap_or_default()); +static IS_DIFF_MODE: LazyLock = + LazyLock::new(|| std::env::var("RSPACK_DIFF").ok().unwrap_or_default()); pub fn is_diff_mode() -> bool { *IS_DIFF_MODE == "true" diff --git a/crates/rspack_util/src/identifier.rs b/crates/rspack_util/src/identifier.rs index 39812f5e2bc..7de539341c9 100644 --- a/crates/rspack_util/src/identifier.rs +++ b/crates/rspack_util/src/identifier.rs @@ -1,18 +1,19 @@ +use std::sync::LazyLock; use std::{ borrow::Cow, path::{Path, PathBuf}, }; use concat_string::concat_string; -use once_cell::sync::Lazy; use regex::Regex; use sugar_path::SugarPath; -static SEGMENTS_SPLIT_REGEXP: Lazy = Lazy::new(|| Regex::new(r"([|!])").expect("TODO:")); -static WINDOWS_ABS_PATH_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"^[a-zA-Z]:[/\\]").expect("TODO:")); -static WINDOWS_PATH_SEPARATOR_REGEXP: Lazy = - Lazy::new(|| Regex::new(r"[/\\]").expect("TODO:")); +static SEGMENTS_SPLIT_REGEXP: LazyLock = + LazyLock::new(|| Regex::new(r"([|!])").expect("TODO:")); +static WINDOWS_ABS_PATH_REGEXP: LazyLock = + LazyLock::new(|| Regex::new(r"^[a-zA-Z]:[/\\]").expect("TODO:")); +static WINDOWS_PATH_SEPARATOR_REGEXP: LazyLock = + LazyLock::new(|| Regex::new(r"[/\\]").expect("TODO:")); pub fn make_paths_relative(context: &str, identifier: &str) -> String { SEGMENTS_SPLIT_REGEXP .split(identifier) @@ -114,10 +115,10 @@ pub fn make_paths_absolute(context: &str, identifier: &str) -> String { .join("") } -static ZERO_WIDTH_SPACE: Lazy = - Lazy::new(|| Regex::new("\u{200b}(.)").expect("invalid regex")); +static ZERO_WIDTH_SPACE: LazyLock = + LazyLock::new(|| Regex::new("\u{200b}(.)").expect("invalid regex")); -static FRAGMENT: Lazy = Lazy::new(|| Regex::new("#").expect("invalid regex")); +static FRAGMENT: LazyLock = LazyLock::new(|| Regex::new("#").expect("invalid regex")); pub fn strip_zero_width_space_for_fragment(s: &str) -> Cow { ZERO_WIDTH_SPACE.replace_all(s, "$1") diff --git a/crates/rspack_util/src/test.rs b/crates/rspack_util/src/test.rs index 7e36d2b578e..821ed8a4812 100644 --- a/crates/rspack_util/src/test.rs +++ b/crates/rspack_util/src/test.rs @@ -1,13 +1,13 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; -static RSPACK_HOT_TEST: Lazy = - Lazy::new(|| std::env::var("RSPACK_HOT_TEST").ok().unwrap_or_default()); +static RSPACK_HOT_TEST: LazyLock = + LazyLock::new(|| std::env::var("RSPACK_HOT_TEST").ok().unwrap_or_default()); pub fn is_hot_test() -> bool { *RSPACK_HOT_TEST == "true" } -pub static HOT_TEST_DEFINE_GLOBAL: Lazy = Lazy::new(|| { +pub static HOT_TEST_DEFINE_GLOBAL: LazyLock = LazyLock::new(|| { is_hot_test() .then(|| { r#" @@ -29,7 +29,7 @@ pub static HOT_TEST_DEFINE_GLOBAL: Lazy = Lazy::new(|| { .unwrap_or_default() }); -pub static HOT_TEST_STATUS_CHANGE: Lazy = Lazy::new(|| { +pub static HOT_TEST_STATUS_CHANGE: LazyLock = LazyLock::new(|| { is_hot_test() .then(|| { r#" @@ -40,7 +40,7 @@ pub static HOT_TEST_STATUS_CHANGE: Lazy = Lazy::new(|| { .unwrap_or_default() }); -pub static HOT_TEST_OUTDATED: Lazy = Lazy::new(|| { +pub static HOT_TEST_OUTDATED: LazyLock = LazyLock::new(|| { is_hot_test() .then(|| { r#" @@ -52,7 +52,7 @@ pub static HOT_TEST_OUTDATED: Lazy = Lazy::new(|| { .unwrap_or_default() }); -pub static HOT_TEST_DISPOSE: Lazy = Lazy::new(|| { +pub static HOT_TEST_DISPOSE: LazyLock = LazyLock::new(|| { is_hot_test() .then(|| { r#" @@ -65,7 +65,7 @@ pub static HOT_TEST_DISPOSE: Lazy = Lazy::new(|| { .unwrap_or_default() }); -pub static HOT_TEST_UPDATED: Lazy = Lazy::new(|| { +pub static HOT_TEST_UPDATED: LazyLock = LazyLock::new(|| { is_hot_test() .then(|| { r#" @@ -76,7 +76,7 @@ pub static HOT_TEST_UPDATED: Lazy = Lazy::new(|| { .unwrap_or_default() }); -pub static HOT_TEST_RUNTIME: Lazy = Lazy::new(|| { +pub static HOT_TEST_RUNTIME: LazyLock = LazyLock::new(|| { is_hot_test() .then(|| { r#" @@ -92,7 +92,7 @@ pub static HOT_TEST_RUNTIME: Lazy = Lazy::new(|| { .unwrap_or_else(|| "currentUpdateRuntime[i](__webpack_require__);".to_string()) }); -pub static HOT_TEST_ACCEPT: Lazy = Lazy::new(|| { +pub static HOT_TEST_ACCEPT: LazyLock = LazyLock::new(|| { is_hot_test() .then(|| { r#"