turbopack/module_options/
module_options_context.rs

1use std::fmt::Debug;
2
3use serde::{Deserialize, Serialize};
4use turbo_esregex::EsRegex;
5use turbo_rcstr::RcStr;
6use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, ValueDefault, Vc, trace::TraceRawVcs};
7use turbo_tasks_fs::FileSystemPath;
8use turbopack_core::{
9    chunk::{MinifyType, SourceMapsType},
10    condition::ContextCondition,
11    environment::Environment,
12    resolve::options::ImportMapping,
13};
14use turbopack_ecmascript::{TreeShakingMode, references::esm::UrlRewriteBehavior};
15pub use turbopack_mdx::MdxTransformOptions;
16use turbopack_node::{
17    execution_context::ExecutionContext,
18    transforms::{postcss::PostCssTransformOptions, webpack::WebpackLoaderItems},
19};
20
21use super::ModuleRule;
22
23#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize, NonLocalValue)]
24pub struct LoaderRuleItem {
25    pub loaders: ResolvedVc<WebpackLoaderItems>,
26    pub rename_as: Option<RcStr>,
27}
28
29#[derive(Default)]
30#[turbo_tasks::value(transparent)]
31pub struct WebpackRules(FxIndexMap<RcStr, LoaderRuleItem>);
32
33#[derive(Default)]
34#[turbo_tasks::value(transparent)]
35pub struct OptionWebpackRules(Option<ResolvedVc<WebpackRules>>);
36
37#[derive(Default)]
38#[turbo_tasks::value(transparent)]
39pub struct WebpackConditions(pub FxIndexMap<RcStr, ConditionItem>);
40
41#[derive(Default)]
42#[turbo_tasks::value(transparent)]
43pub struct OptionWebpackConditions(Option<ResolvedVc<WebpackConditions>>);
44
45#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize, NonLocalValue)]
46pub enum ConditionPath {
47    Glob(RcStr),
48    Regex(ResolvedVc<EsRegex>),
49}
50
51#[turbo_tasks::value(shared)]
52#[derive(Clone, Debug)]
53pub struct ConditionItem {
54    pub path: ConditionPath,
55}
56
57#[turbo_tasks::value(shared)]
58#[derive(Clone, Debug)]
59pub struct WebpackLoadersOptions {
60    pub rules: ResolvedVc<WebpackRules>,
61    pub conditions: ResolvedVc<OptionWebpackConditions>,
62    pub loader_runner_package: Option<ResolvedVc<ImportMapping>>,
63}
64
65/// The kind of decorators transform to use.
66/// [TODO]: might need bikeshed for the name (Ecma)
67#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize, NonLocalValue)]
68pub enum DecoratorsKind {
69    Legacy,
70    Ecma,
71}
72
73/// The types when replacing `typeof window` with a constant.
74#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize, NonLocalValue)]
75pub enum TypeofWindow {
76    Object,
77    Undefined,
78}
79
80/// Configuration options for the decorators transform.
81///
82/// This is not part of Typescript transform: while there are typescript
83/// specific transforms (legay decorators), there is an ecma decorator transform
84/// as well for the JS.
85#[turbo_tasks::value(shared)]
86#[derive(Default, Clone, Debug)]
87pub struct DecoratorsOptions {
88    pub decorators_kind: Option<DecoratorsKind>,
89    /// Option to control whether to emit decorator metadata.
90    /// (https://www.typescriptlang.org/tsconfig#emitDecoratorMetadata)
91    /// This'll be applied only if `decorators_type` and
92    /// `enable_typescript_transform` is enabled.
93    pub emit_decorators_metadata: bool,
94    /// Mimic babel's `decorators.decoratorsBeforeExport` option.
95    /// This'll be applied only if `decorators_type` is enabled.
96    /// ref: https://github.com/swc-project/swc/blob/d4ebb5e6efbed0758f25e46e8f74d7c47ec6cb8f/crates/swc_ecma_parser/src/lib.rs#L327
97    /// [TODO]: this option is not actively being used currently.
98    pub decorators_before_export: bool,
99    pub use_define_for_class_fields: bool,
100}
101
102#[turbo_tasks::value_impl]
103impl ValueDefault for DecoratorsOptions {
104    #[turbo_tasks::function]
105    fn value_default() -> Vc<Self> {
106        Self::default().cell()
107    }
108}
109
110/// Subset of Typescript options configured via tsconfig.json or jsconfig.json,
111/// which affects the runtime transform output.
112#[turbo_tasks::value(shared)]
113#[derive(Default, Clone, Debug)]
114pub struct TypescriptTransformOptions {
115    pub use_define_for_class_fields: bool,
116}
117
118#[turbo_tasks::value_impl]
119impl ValueDefault for TypescriptTransformOptions {
120    #[turbo_tasks::function]
121    fn value_default() -> Vc<Self> {
122        Self::default().cell()
123    }
124}
125
126// [TODO]: should enabled_react_refresh belong to this options?
127#[turbo_tasks::value(shared)]
128#[derive(Default, Clone, Debug)]
129pub struct JsxTransformOptions {
130    pub development: bool,
131    pub react_refresh: bool,
132    pub import_source: Option<RcStr>,
133    pub runtime: Option<RcStr>,
134}
135
136#[turbo_tasks::value(shared)]
137#[derive(Clone, Default)]
138#[serde(default)]
139pub struct ModuleOptionsContext {
140    pub ecmascript: EcmascriptOptionsContext,
141    pub css: CssOptionsContext,
142
143    pub enable_postcss_transform: Option<ResolvedVc<PostCssTransformOptions>>,
144    pub enable_webpack_loaders: Option<ResolvedVc<WebpackLoadersOptions>>,
145    // [Note]: currently mdx, and mdx_rs have different configuration entrypoint from next.config.js,
146    // however we might want to unify them in the future.
147    pub enable_mdx: bool,
148    pub enable_mdx_rs: Option<ResolvedVc<MdxTransformOptions>>,
149
150    pub preset_env_versions: Option<ResolvedVc<Environment>>,
151    pub execution_context: Option<ResolvedVc<ExecutionContext>>,
152    pub side_effect_free_packages: Vec<RcStr>,
153    pub tree_shaking_mode: Option<TreeShakingMode>,
154
155    /// Generate (non-emitted) output assets for static assets and externals, to facilitate
156    /// generating a list of all non-bundled files that will be required at runtime.
157    ///
158    /// The filepath is the directory from which the bundled files will require the externals at
159    /// runtime.
160    pub enable_externals_tracing: Option<ResolvedVc<FileSystemPath>>,
161
162    /// If true, it stores the last successful parse result in state and keeps using it when
163    /// parsing fails. This is useful to keep the module graph structure intact when syntax errors
164    /// are temporarily introduced.
165    pub keep_last_successful_parse: bool,
166
167    /// Custom rules to be applied after all default rules.
168    pub module_rules: Vec<ModuleRule>,
169    /// A list of rules to use a different module option context for certain
170    /// context paths. The first matching is used.
171    pub rules: Vec<(ContextCondition, ResolvedVc<ModuleOptionsContext>)>,
172    pub placeholder_for_future_extensions: (),
173}
174
175#[turbo_tasks::value(shared)]
176#[derive(Clone, Default)]
177#[serde(default)]
178pub struct EcmascriptOptionsContext {
179    pub enable_typeof_window_inlining: Option<TypeofWindow>,
180    pub enable_jsx: Option<ResolvedVc<JsxTransformOptions>>,
181    /// Follow type references and resolve declaration files in additional to
182    /// normal resolution.
183    pub enable_types: bool,
184    pub enable_typescript_transform: Option<ResolvedVc<TypescriptTransformOptions>>,
185    pub enable_decorators: Option<ResolvedVc<DecoratorsOptions>>,
186    pub esm_url_rewrite_behavior: Option<UrlRewriteBehavior>,
187    /// References to externals from ESM imports should use `import()` and make
188    /// async modules.
189    pub import_externals: bool,
190    /// Ignore very dynamic requests which doesn't have any static known part.
191    /// If false, they will reference the whole directory. If true, they won't
192    /// reference anything and lead to an runtime error instead.
193    pub ignore_dynamic_requests: bool,
194    /// Specifies how Source Maps are handled.
195    pub source_maps: SourceMapsType,
196
197    pub placeholder_for_future_extensions: (),
198}
199
200#[turbo_tasks::value(shared)]
201#[derive(Clone, Default)]
202#[serde(default)]
203pub struct CssOptionsContext {
204    /// This skips `GlobalCss` and `ModuleCss` module assets from being
205    /// generated in the module graph, generating only `Css` module assets.
206    ///
207    /// This is useful for node-file-trace, which tries to emit all assets in
208    /// the module graph, but neither asset types can be emitted directly.
209    pub enable_raw_css: bool,
210
211    pub minify_type: MinifyType,
212
213    /// Specifies how Source Maps are handled.
214    pub source_maps: SourceMapsType,
215
216    pub placeholder_for_future_extensions: (),
217}
218
219#[turbo_tasks::value_impl]
220impl ValueDefault for ModuleOptionsContext {
221    #[turbo_tasks::function]
222    fn value_default() -> Vc<Self> {
223        Self::cell(Default::default())
224    }
225}