Skip to content

Commit ec646eb

Browse files
authored
perf(rstest): minor performance optimization (#12469)
* perf(rstest): minor performance optimization * revert regex
1 parent 03e45d3 commit ec646eb

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

crates/rspack_plugin_rstest/src/parser_plugin.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,9 @@ impl RstestParserPlugin {
183183

184184
fn calc_mocked_target(&self, value: &str) -> Utf8PathBuf {
185185
// node:foo will be mocked to `__mocks__/foo`.
186-
let path_buf = Utf8PathBuf::from(
187-
value
188-
.to_string()
189-
.strip_prefix("node:")
190-
.unwrap_or(value.as_ref())
191-
.to_string(),
192-
);
193-
let is_relative_request = path_buf.to_string().starts_with("."); // TODO: consider alias?
186+
let stripped = value.strip_prefix("node:").unwrap_or(value);
187+
let path_buf = Utf8PathBuf::from(stripped);
188+
let is_relative_request = stripped.starts_with('.'); // TODO: consider alias?
194189

195190
if is_relative_request {
196191
// Mock relative request to alongside `__mocks__` directory.

crates/rspack_plugin_rstest/src/plugin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,14 @@ struct MockFlagPos {
163163

164164
#[plugin_hook(CompilationProcessAssets for RstestPlugin, stage = Compilation::PROCESS_ASSETS_STAGE_ADDITIONAL)]
165165
async fn mock_hoist_process_assets(&self, compilation: &mut Compilation) -> Result<()> {
166-
let mut files = vec![];
166+
let mut files = Vec::with_capacity(compilation.chunk_by_ukey.len());
167167

168168
for chunk in compilation.chunk_by_ukey.values() {
169169
for file in chunk.files() {
170170
files.push(file.clone());
171171
}
172172
}
173+
173174
let regex = regex::Regex::new(r"\/\* RSTEST:(MOCK|UNMOCK|MOCKREQUIRE|HOISTED)_(.*?):(.*?) \*\/")
174175
.expect("should initialize `Regex`");
175176

0 commit comments

Comments
 (0)