Skip to content

Commit 157f887

Browse files
authored
perf: optimize require regex compilation using static LazyLock (#12944)
1 parent 7473a29 commit 157f887

File tree

1 file changed

+10
-4
lines changed
  • crates/rspack_plugin_javascript/src/parser_and_generator

1 file changed

+10
-4
lines changed

crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{borrow::Cow, sync::Arc};
1+
use std::{
2+
borrow::Cow,
3+
sync::{Arc, LazyLock},
4+
};
25

36
use regex::Regex;
47
use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip};
@@ -45,9 +48,13 @@ pub struct ParserRuntimeRequirementsData {
4548
pub module: String,
4649
pub exports: String,
4750
pub require: String,
48-
pub require_regex: Regex,
51+
pub require_regex: &'static LazyLock<Regex>,
4952
}
5053

54+
static LEGACY_REQUIRE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
55+
Regex::new("__webpack_require__\\s*(!?\\.)").expect("should init `REQUIRE_FUNCTION_REGEX`")
56+
});
57+
5158
impl ParserRuntimeRequirementsData {
5259
pub fn new(runtime_template: &ModuleCodegenRuntimeTemplate) -> Self {
5360
let require_name =
@@ -57,8 +64,7 @@ impl ParserRuntimeRequirementsData {
5764
let exports_name =
5865
runtime_template.render_runtime_globals_without_adding(&RuntimeGlobals::EXPORTS);
5966
Self {
60-
require_regex: Regex::new(&format!("{}\\s*(!?\\.)", &require_name))
61-
.expect("should init `REQUIRE_FUNCTION_REGEX`"),
67+
require_regex: &LEGACY_REQUIRE_REGEX,
6268
module: module_name,
6369
exports: exports_name,
6470
require: require_name,

0 commit comments

Comments
 (0)