Skip to main content

next_core/
mode.rs

1use turbo_rcstr::{RcStr, rcstr};
2use turbopack_ecmascript_runtime::RuntimeType;
3
4use crate::next_shared::webpack_rules::WebpackLoaderBuiltinCondition;
5
6/// The mode in which Next.js is running.
7#[turbo_tasks::value(shared, task_input)]
8#[derive(Debug, Copy, Clone, Ord, PartialOrd, Hash)]
9pub enum NextMode {
10    /// `next dev --turbopack`
11    Development,
12    /// `next build`
13    Build,
14}
15
16impl NextMode {
17    /// Returns the NODE_ENV value for the current mode.
18    pub fn node_env(&self) -> &'static str {
19        match self {
20            NextMode::Development => "development",
21            NextMode::Build => "production",
22        }
23    }
24
25    /// Returns conditions that can be used in the Next.js config's turbopack "rules" section for
26    /// defining webpack loader configuration.
27    pub fn webpack_loader_conditions(&self) -> impl Iterator<Item = WebpackLoaderBuiltinCondition> {
28        match self {
29            NextMode::Development => [WebpackLoaderBuiltinCondition::Development],
30            NextMode::Build => [WebpackLoaderBuiltinCondition::Production],
31        }
32        .into_iter()
33    }
34
35    /// Returns conditions used by `ResolveOptionsContext`.
36    pub fn custom_resolve_conditions(&self) -> impl Iterator<Item = RcStr> {
37        match self {
38            NextMode::Development => [rcstr!("development")],
39            NextMode::Build => [rcstr!("production")],
40        }
41        .into_iter()
42    }
43
44    /// Returns true if the development React runtime should be used.
45    pub fn is_react_development(&self) -> bool {
46        match self {
47            NextMode::Development => true,
48            NextMode::Build => false,
49        }
50    }
51
52    pub fn is_development(&self) -> bool {
53        match self {
54            NextMode::Development => true,
55            NextMode::Build => false,
56        }
57    }
58
59    pub fn is_production(&self) -> bool {
60        !self.is_development()
61    }
62
63    pub fn runtime_type(&self) -> RuntimeType {
64        match self {
65            NextMode::Development => RuntimeType::Development,
66            NextMode::Build => RuntimeType::Production,
67        }
68    }
69}