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