Unverified Commit c8ac2a52 authored by xusd320's avatar xusd320 Committed by GitHub

refactor(turbopack): remove lazy_static dependency (#80226)

## Why
lazy_static  is deprecated, see [Officially deprecate lazy_static #214
](https://github.com/rust-lang-nursery/lazy-static.rs/issues/214)
parent 737d3199
......@@ -4216,7 +4216,6 @@ dependencies = [
"indoc",
"itertools 0.10.5",
"lazy-regex",
"lazy_static",
"mime_guess",
"modularize_imports",
"next-custom-transforms",
......@@ -4268,7 +4267,6 @@ dependencies = [
"hex",
"indexmap 2.7.1",
"indoc",
"lazy_static",
"modularize_imports",
"once_cell",
"pathdiff",
......@@ -9361,7 +9359,6 @@ dependencies = [
"anyhow",
"codspeed-criterion-compat",
"difference",
"lazy_static",
"regex",
"rstest",
"rstest_reuse",
......@@ -9513,7 +9510,6 @@ dependencies = [
"either",
"futures",
"indexmap 2.7.1",
"lazy_static",
"once_cell",
"patricia_tree",
"petgraph 0.6.3",
......@@ -9627,7 +9623,6 @@ dependencies = [
"either",
"indexmap 2.7.1",
"indoc",
"lazy_static",
"num-bigint",
"num-traits",
"once_cell",
......@@ -9831,7 +9826,6 @@ name = "turbopack-resolve"
version = "0.1.0"
dependencies = [
"anyhow",
"lazy_static",
"regex",
"serde",
"serde_json",
......
......@@ -14,9 +14,7 @@ members = [
"turbopack/xtask",
]
exclude = [
"crates/next-error-code-swc-plugin",
]
exclude = ["crates/next-error-code-swc-plugin"]
[workspace.lints.clippy]
too_many_arguments = "allow"
......@@ -350,15 +348,14 @@ image = { version = "0.25.0", default-features = false }
indexmap = "2.7.1"
indoc = "2.0.0"
itertools = "0.10.5"
lazy_static = "1.4.0"
lightningcss = { version = "1.0.0-alpha.66", features = [
"serde",
"visitor",
"into_owned",
] }
lightningcss-napi = { version = "0.4.4", default-features = false, features = [
"visitor"
]}
"visitor",
] }
markdown = "1.0.0-alpha.18"
mime = "0.3.16"
napi = { version = "2", default-features = false, features = [
......@@ -418,8 +415,12 @@ triomphe = { git = "https://github.com/sokra/triomphe", branch = "sokra/unstable
unsize = "1.1.0"
url = "2.2.2"
urlencoding = "2.1.2"
vergen = { version = "9.0.6", features = ["cargo"] }
vergen-gitcl = { version = "1.0.8", features = ["cargo"] }
vergen = { version = "9.0.6", features = [
"cargo",
] }
vergen-gitcl = { version = "1.0.8", features = [
"cargo",
] }
webbrowser = "0.8.7"
[patch.crates-io]
......
......@@ -28,7 +28,6 @@ mime_guess = "2.0.4"
indoc = { workspace = true }
allsorts = { workspace = true }
futures = { workspace = true }
lazy_static = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
rustc-hash = { workspace = true }
......@@ -69,7 +68,9 @@ turbopack = { workspace = true }
turbopack-browser = { workspace = true }
turbopack-core = { workspace = true }
turbopack-ecmascript = { workspace = true }
turbopack-ecmascript-plugins = { workspace = true, features = ["transform_emotion"] }
turbopack-ecmascript-plugins = { workspace = true, features = [
"transform_emotion",
] }
turbopack-ecmascript-runtime = { workspace = true }
turbopack-image = { workspace = true }
turbopack-node = { workspace = true }
......
use std::sync::LazyLock;
use anyhow::Result;
use lazy_static::lazy_static;
use rustc_hash::FxHashMap;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
......@@ -21,24 +22,26 @@ use turbopack_core::{
use crate::{next_server::ServerContextType, next_telemetry::ModuleFeatureTelemetry};
lazy_static! {
// Set of the features we want to track, following existing references in webpack/plugins/telemetry-plugin.
static ref FEATURE_MODULES: FxHashMap<&'static str, Vec<&'static str>> = FxHashMap::from_iter([
(
"next",
vec![
"/image",
"/future/image",
"/legacy/image",
"/script",
"/dynamic",
"/font/google",
"/font/local"
]
),
("@next", vec!["/font/google", "/font/local"])
]);
}
// Set of the features we want to track, following existing references in
// webpack/plugins/telemetry-plugin.
static FEATURE_MODULES: LazyLock<FxHashMap<&'static str, Vec<&'static str>>> =
LazyLock::new(|| {
FxHashMap::from_iter([
(
"next",
vec![
"/image",
"/future/image",
"/legacy/image",
"/script",
"/dynamic",
"/font/google",
"/font/local",
],
),
("@next", vec!["/font/google", "/font/local"]),
])
});
#[turbo_tasks::value(shared)]
pub struct InvalidImportModuleIssue {
......
......@@ -38,7 +38,6 @@ serde_json = { workspace = true, features = ["preserve_order"] }
sha1 = "0.10.1"
tracing = { version = "0.1.37" }
anyhow = { workspace = true }
lazy_static = { workspace = true }
swc_core = { workspace = true, features = [
"base",
......@@ -73,5 +72,5 @@ remove_console = "0.42.0"
preset_env_base = "3.0.1"
[dev-dependencies]
swc_core = { workspace = true, features = ["testing_transform"]}
swc_core = { workspace = true, features = ["testing_transform"] }
testing = { workspace = true }
use std::iter::FromIterator;
use std::{iter::FromIterator, sync::LazyLock};
use lazy_static::lazy_static;
use rustc_hash::FxHashSet;
use swc_core::{
atoms::atom,
......@@ -15,15 +14,15 @@ use swc_core::{
use super::{ExportInfo, ExportInfoWarning};
lazy_static! {
static ref EXPORTS_SET: FxHashSet<&'static str> = FxHashSet::from_iter([
static EXPORTS_SET: LazyLock<FxHashSet<&'static str>> = LazyLock::new(|| {
FxHashSet::from_iter([
"getStaticProps",
"getServerSideProps",
"generateImageMetadata",
"generateSitemaps",
"generateStaticParams",
]);
}
])
});
pub(crate) struct CollectExportsVisitor {
pub export_info: Option<ExportInfo>,
......
......@@ -22,13 +22,12 @@ data-encoding = { workspace = true }
either = { workspace = true }
futures = { workspace = true }
indexmap = { workspace = true }
lazy_static = { workspace = true }
once_cell = { workspace = true }
patricia_tree = "0.5.5"
petgraph = { workspace = true, features = ["serde-1"] }
roaring = { version = "0.10.10", features = ["serde"] }
ref-cast = "1.0.20"
rustc-hash ={ workspace = true }
rustc-hash = { workspace = true }
regex = { workspace = true }
serde = { workspace = true, features = ["rc"] }
serde_bytes = { workspace = true }
......
use std::sync::LazyLock;
use anyhow::{Ok, Result};
use lazy_static::lazy_static;
use regex::Regex;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc};
......@@ -89,12 +90,12 @@ fn split_off_query_fragment(mut raw: &str) -> (Pattern, RcStr, RcStr) {
(Pattern::Constant(RcStr::from(raw)), query, hash)
}
lazy_static! {
static ref WINDOWS_PATH: Regex = Regex::new(r"^[A-Za-z]:\\|\\\\").unwrap();
static ref URI_PATH: Regex = Regex::new(r"^([^/\\:]+:)(.+)$").unwrap();
static ref DATA_URI_REMAINDER: Regex = Regex::new(r"^([^;,]*)(?:;([^,]+))?,(.*)$").unwrap();
static ref MODULE_PATH: Regex = Regex::new(r"^((?:@[^/]+/)?[^/]+)(.*)$").unwrap();
}
static WINDOWS_PATH: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[A-Za-z]:\\|\\\\").unwrap());
static URI_PATH: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^([^/\\:]+:)(.+)$").unwrap());
static DATA_URI_REMAINDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^([^;,]*)(?:;([^,]+))?,(.*)$").unwrap());
static MODULE_PATH: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^((?:@[^/]+/)?[^/]+)(.*)$").unwrap());
impl Request {
/// Turns the request into a string.
......
......@@ -2,10 +2,10 @@ use std::{
collections::{VecDeque, hash_map::Entry},
fmt::Display,
mem::take,
sync::LazyLock,
};
use anyhow::Result;
use lazy_static::lazy_static;
use regex::Regex;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};
......@@ -846,11 +846,11 @@ impl Pattern {
}
}
Pattern::Dynamic => {
lazy_static! {
static ref FORBIDDEN: Regex =
Regex::new(r"(/|^)(ROOT|\.|/|(node_modules|__tests?__)(/|$))").unwrap();
static ref FORBIDDEN_MATCH: Regex = Regex::new(r"\.d\.ts$|\.map$").unwrap();
}
static FORBIDDEN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(/|^)(ROOT|\.|/|(node_modules|__tests?__)(/|$))").unwrap()
});
static FORBIDDEN_MATCH: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\.d\.ts$|\.map$").unwrap());
if let Some(m) = FORBIDDEN.find(value) {
MatchResult::Consumed {
remaining: value,
......@@ -932,11 +932,11 @@ impl Pattern {
}
}
Pattern::Dynamic => {
lazy_static! {
static ref FORBIDDEN: Regex =
Regex::new(r"(/|^)(ROOT|\.|/|(node_modules|__tests?__)(/|$))").unwrap();
static ref FORBIDDEN_MATCH: Regex = Regex::new(r"\.d\.ts$|\.map$").unwrap();
}
static FORBIDDEN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(/|^)(ROOT|\.|/|(node_modules|__tests?__)(/|$))").unwrap()
});
static FORBIDDEN_MATCH: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\.d\.ts$|\.map$").unwrap());
if let Some(m) = FORBIDDEN.find(value) {
MatchResult::Consumed {
remaining: value,
......@@ -1046,11 +1046,11 @@ impl Pattern {
}
}
Pattern::Dynamic => {
lazy_static! {
static ref FORBIDDEN: Regex =
Regex::new(r"(/|^)(\.|(node_modules|__tests?__)(/|$))").unwrap();
static ref FORBIDDEN_MATCH: Regex = Regex::new(r"\.d\.ts$|\.map$").unwrap();
}
static FORBIDDEN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(/|^)(\.|(node_modules|__tests?__)(/|$))").unwrap()
});
static FORBIDDEN_MATCH: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\.d\.ts$|\.map$").unwrap());
if let Some(m) = FORBIDDEN.find(value) {
NextConstantUntilResult::Consumed(value, Some(m.start()))
} else if FORBIDDEN_MATCH.find(value).is_some() {
......
......@@ -21,7 +21,6 @@ data-encoding = { workspace = true }
either = { workspace = true }
indexmap = { workspace = true }
indoc = { workspace = true }
lazy_static = { workspace = true }
num-bigint = "0.4"
num-traits = "0.2.15"
once_cell = { workspace = true }
......
......@@ -18,14 +18,20 @@ pub mod unreachable;
pub mod util;
pub mod worker;
use std::{borrow::Cow, collections::BTreeMap, future::Future, mem::take, ops::Deref, sync::Arc};
use std::{
borrow::Cow,
collections::BTreeMap,
future::Future,
mem::take,
ops::Deref,
sync::{Arc, LazyLock},
};
use anyhow::{Result, bail};
use constant_condition::{ConstantConditionCodeGen, ConstantConditionValue};
use constant_value::ConstantValueCodeGen;
use either::Either;
use indexmap::map::Entry;
use lazy_static::lazy_static;
use num_traits::Zero;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
......@@ -632,15 +638,14 @@ pub(crate) async fn analyse_ecmascript_module_internal(
if let Some(comments) = comments.get_leading(pos) {
for comment in comments.iter() {
if let CommentKind::Line = comment.kind {
lazy_static! {
static ref REFERENCE_PATH: Regex =
Regex::new(r#"^/\s*<reference\s*path\s*=\s*["'](.+)["']\s*/>\s*$"#)
.unwrap();
static ref REFERENCE_TYPES: Regex = Regex::new(
r#"^/\s*<reference\s*types\s*=\s*["'](.+)["']\s*/>\s*$"#
)
.unwrap();
}
static REFERENCE_PATH: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"^/\s*<reference\s*path\s*=\s*["'](.+)["']\s*/>\s*$"#)
.unwrap()
});
static REFERENCE_TYPES: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"^/\s*<reference\s*types\s*=\s*["'](.+)["']\s*/>\s*$"#)
.unwrap()
});
let text = &comment.text;
if let Some(m) = REFERENCE_PATH.captures(text) {
let path = &m[1];
......@@ -673,10 +678,8 @@ pub(crate) async fn analyse_ecmascript_module_internal(
let mut paths_by_pos = Vec::new();
for (pos, comments) in comments.trailing.iter() {
for comment in comments.iter().rev() {
lazy_static! {
static ref SOURCE_MAP_FILE_REFERENCE: Regex =
Regex::new(r"# sourceMappingURL=(.*)$").unwrap();
}
static SOURCE_MAP_FILE_REFERENCE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"# sourceMappingURL=(.*)$").unwrap());
if let Some(m) = SOURCE_MAP_FILE_REFERENCE.captures(&comment.text) {
let path = m.get(1).unwrap().as_str();
paths_by_pos.push((pos, path));
......@@ -687,10 +690,9 @@ pub(crate) async fn analyse_ecmascript_module_internal(
let mut source_map_from_comment = false;
if let Some((_, path)) = paths_by_pos.into_iter().max_by_key(|&(pos, _)| pos) {
lazy_static! {
static ref JSON_DATA_URL_BASE64: Regex =
Regex::new(r"^data:application\/json;(?:charset=utf-8;)?base64").unwrap();
}
static JSON_DATA_URL_BASE64: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^data:application\/json;(?:charset=utf-8;)?base64").unwrap()
});
let origin_path = origin.origin_path();
if path.ends_with(".map") {
let source_map_origin = origin_path.parent().join(path.into());
......
......@@ -14,7 +14,6 @@ workspace = true
[dependencies]
anyhow = { workspace = true }
lazy_static = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
......
use std::sync::LazyLock;
use anyhow::Result;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use turbo_rcstr::{RcStr, rcstr};
......@@ -86,17 +87,16 @@ pub async fn resolve_node_pre_gyp_files(
config_file_pattern: Vc<Pattern>,
compile_target: Vc<CompileTarget>,
) -> Result<Vc<ModuleResolveResult>> {
lazy_static! {
static ref NAPI_VERSION_TEMPLATE: Regex =
Regex::new(r"\{(napi_build_version|node_napi_label)\}")
.expect("create napi_build_version regex failed");
static ref PLATFORM_TEMPLATE: Regex =
Regex::new(r"\{platform\}").expect("create node_platform regex failed");
static ref ARCH_TEMPLATE: Regex =
Regex::new(r"\{arch\}").expect("create node_arch regex failed");
static ref LIBC_TEMPLATE: Regex =
Regex::new(r"\{libc\}").expect("create node_libc regex failed");
}
static NAPI_VERSION_TEMPLATE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"\{(napi_build_version|node_napi_label)\}")
.expect("create napi_build_version regex failed")
});
static PLATFORM_TEMPLATE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\{platform\}").expect("create node_platform regex failed"));
static ARCH_TEMPLATE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\{arch\}").expect("create node_arch regex failed"));
static LIBC_TEMPLATE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\{libc\}").expect("create node_libc regex failed"));
let config = resolve_raw(context_dir, config_file_pattern, true)
.first_source()
.await?;
......@@ -261,12 +261,11 @@ pub async fn resolve_node_gyp_build_files(
context_dir: Vc<FileSystemPath>,
compile_target: Vc<CompileTarget>,
) -> Result<Vc<ModuleResolveResult>> {
lazy_static! {
// TODO Proper parser
static ref GYP_BUILD_TARGET_NAME: Regex =
Regex::new(r#"['"]target_name['"]\s*:\s*(?:"(.*?)"|'(.*?)')"#)
.expect("create napi_build_version regex failed");
}
// TODO Proper parser
static GYP_BUILD_TARGET_NAME: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"['"]target_name['"]\s*:\s*(?:"(.*?)"|'(.*?)')"#)
.expect("create napi_build_version regex failed")
});
let binding_gyp_pat = Pattern::new(Pattern::Constant(rcstr!("binding.gyp")));
let gyp_file = resolve_raw(context_dir, binding_gyp_pat, true);
if let [binding_gyp] = &gyp_file.primary_sources().await?[..] {
......@@ -370,15 +369,15 @@ pub async fn resolve_node_bindings_files(
context_dir: Vc<FileSystemPath>,
file_name: RcStr,
) -> Result<Vc<ModuleResolveResult>> {
lazy_static! {
static ref BINDINGS_TRY: [&'static str; 5] = [
static BINDINGS_TRY: LazyLock<[&'static str; 5]> = LazyLock::new(|| {
[
"build/bindings",
"build/Release",
"build/Release/bindings",
"out/Release/bindings",
"Release/bindings",
];
}
]
});
let mut root_context_dir = context_dir;
loop {
let resolved = resolve_raw(
......
......@@ -18,7 +18,6 @@ workspace = true
[dependencies]
anyhow = { workspace = true }
lazy_static = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
smallvec = { workspace = true }
......
......@@ -12,14 +12,13 @@ use std::{
},
io::{ErrorKind, Write as _},
path::{Path, PathBuf},
sync::{Arc, Mutex},
sync::{Arc, LazyLock, Mutex},
time::{Duration, Instant},
};
use anyhow::{Context, Result, anyhow};
use difference::Changeset;
use helpers::print_changeset;
use lazy_static::lazy_static;
use regex::Regex;
use rstest::*;
use rstest_reuse::{
......@@ -393,9 +392,8 @@ fn node_file_trace<B: Backend + 'static>(
timeout_len: u64,
create_turbo_tasks: impl Fn(&Path) -> Arc<TurboTasks<B>>,
) {
lazy_static! {
static ref BENCH_SUITES: Arc<Mutex<Vec<BenchSuite>>> = Arc::new(Mutex::new(Vec::new()));
};
static BENCH_SUITES: LazyLock<Arc<Mutex<Vec<BenchSuite>>>> =
LazyLock::new(|| Arc::new(Mutex::new(Vec::new())));
let r = &mut {
let mut builder = if multi_threaded {
......@@ -633,24 +631,20 @@ async fn exec_node(directory: &str, path: &str) -> Result<CommandOutput> {
}
fn clean_stderr(str: &str) -> String {
lazy_static! {
static ref EXPERIMENTAL_WARNING: Regex =
Regex::new(r"\(node:\d+\) ExperimentalWarning:").unwrap();
static ref DEPRECATION_WARNING: Regex =
Regex::new(r"\(node:\d+\) \[DEP\d+] DeprecationWarning:").unwrap();
}
static EXPERIMENTAL_WARNING: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\(node:\d+\) ExperimentalWarning:").unwrap());
static DEPRECATION_WARNING: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\(node:\d+\) \[DEP\d+] DeprecationWarning:").unwrap());
let str = EXPERIMENTAL_WARNING.replace_all(str, "(node:XXXX) ExperimentalWarning:");
let str = DEPRECATION_WARNING.replace_all(&str, "(node:XXXX) [DEPXXXX] DeprecationWarning:");
str.to_string()
}
fn diff(expected: &str, actual: &str) -> String {
lazy_static! {
static ref JAVASCRIPT_TIMESTAMP: Regex =
Regex::new(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z").unwrap();
static ref JAVASCRIPT_DATE_TIME: Regex =
Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{6}").unwrap();
}
static JAVASCRIPT_TIMESTAMP: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z").unwrap());
static JAVASCRIPT_DATE_TIME: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{6}").unwrap());
// Remove timestamps from the output.
if JAVASCRIPT_DATE_TIME.replace_all(JAVASCRIPT_TIMESTAMP.replace_all(actual, "").as_ref(), "")
== JAVASCRIPT_DATE_TIME
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment