next_core/
mode.rs

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