Skip to content

Commit e740e6e

Browse files
committed
feat: parser support jsx
1 parent e6e2ef0 commit e740e6e

File tree

21 files changed

+253
-16
lines changed

21 files changed

+253
-16
lines changed

crates/node_binding/napi-binding.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,6 +2337,11 @@ export interface RawJavascriptParserOptions {
23372337
* @experimental
23382338
*/
23392339
typeReexportsPresence?: string
2340+
/**
2341+
* This option is experimental in Rspack only and subject to change or be removed anytime.
2342+
* @experimental
2343+
*/
2344+
jsx?: boolean
23402345
}
23412346

23422347
export interface RawJsonGeneratorOptions {

crates/rspack/src/builder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ impl ModuleOptionsBuilder {
17151715
require_resolve: Some(true),
17161716
import_dynamic: Some(true),
17171717
inline_const: Some(false),
1718+
jsx: Some(false),
17181719
..Default::default()
17191720
}),
17201721
);

crates/rspack/tests/snapshots/defaults__default_options.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,9 @@ CompilerOptions {
14411441
inline_const: Some(
14421442
false,
14431443
),
1444+
jsx: Some(
1445+
false,
1446+
),
14441447
},
14451448
),
14461449
},

crates/rspack_binding_api/src/raw_options/raw_module/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ pub struct RawJavascriptParserOptions {
297297
/// This option is experimental in Rspack only and subject to change or be removed anytime.
298298
/// @experimental
299299
pub type_reexports_presence: Option<String>,
300+
/// This option is experimental in Rspack only and subject to change or be removed anytime.
301+
/// @experimental
302+
pub jsx: Option<bool>,
300303
}
301304

302305
impl From<RawJavascriptParserOptions> for JavascriptParserOptions {
@@ -342,6 +345,7 @@ impl From<RawJavascriptParserOptions> for JavascriptParserOptions {
342345
require_resolve: value.require_resolve,
343346
import_dynamic: value.import_dynamic,
344347
inline_const: value.inline_const,
348+
jsx: value.jsx,
345349
}
346350
}
347351
}

crates/rspack_core/src/concatenated_module.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use swc_core::{
3030
ecma::{
3131
ast::{EsVersion, Program},
3232
atoms::Atom,
33-
parser::{Syntax, parse_file_as_module},
33+
parser::{EsSyntax, Syntax, parse_file_as_module},
3434
transforms::base::resolver,
3535
},
3636
};
@@ -47,13 +47,13 @@ use crate::{
4747
IdentCollector, InitFragment, InitFragmentStage, LibIdentOptions,
4848
MaybeDynamicTargetExportInfoHashKey, Module, ModuleArgument, ModuleGraph,
4949
ModuleGraphCacheArtifact, ModuleGraphConnection, ModuleIdentifier, ModuleLayer,
50-
ModuleStaticCacheArtifact, ModuleType, NAMESPACE_OBJECT_EXPORT, PrefetchExportsInfoMode, Resolve,
51-
RuntimeCondition, RuntimeGlobals, RuntimeSpec, SourceType, UsageState, UsedName, UsedNameItem,
52-
define_es_module_flag_statement, escape_identifier, filter_runtime, get_runtime_key,
53-
impl_source_map_config, merge_runtime_condition, merge_runtime_condition_non_false,
54-
module_update_hash, property_access, property_name, reserved_names::RESERVED_NAMES,
55-
returning_function, runtime_condition_expression, subtract_runtime_condition,
56-
to_identifier_with_escaped, to_normal_comment,
50+
ModuleStaticCacheArtifact, ModuleType, NAMESPACE_OBJECT_EXPORT, ParserOptions,
51+
PrefetchExportsInfoMode, Resolve, RuntimeCondition, RuntimeGlobals, RuntimeSpec, SourceType,
52+
UsageState, UsedName, UsedNameItem, define_es_module_flag_statement, escape_identifier,
53+
filter_runtime, get_runtime_key, impl_source_map_config, merge_runtime_condition,
54+
merge_runtime_condition_non_false, module_update_hash, property_access, property_name,
55+
reserved_names::RESERVED_NAMES, returning_function, runtime_condition_expression,
56+
subtract_runtime_condition, to_identifier_with_escaped, to_normal_comment,
5757
};
5858

5959
type ExportsDefinitionArgs = Vec<(String, String)>;
@@ -1989,10 +1989,27 @@ impl ConcatenatedModule {
19891989
let comments = SwcComments::default();
19901990
let mut module_info = concatenation_scope.current_module;
19911991

1992+
let jsx = compilation
1993+
.options
1994+
.module
1995+
.parser
1996+
.as_ref()
1997+
.and_then(|p| {
1998+
let options = p.get(ModuleType::JsEsm.as_str());
1999+
match options {
2000+
Some(ParserOptions::JavascriptEsm(js_options)) => js_options.jsx,
2001+
_ => None,
2002+
}
2003+
})
2004+
.unwrap_or(false);
2005+
19922006
let mut errors = vec![];
19932007
let program = match parse_file_as_module(
19942008
&fm,
1995-
Syntax::default(),
2009+
Syntax::Es(EsSyntax {
2010+
jsx,
2011+
..Default::default()
2012+
}),
19962013
EsVersion::EsNext,
19972014
Some(&comments),
19982015
&mut errors,

crates/rspack_core/src/options/module.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ pub struct JavascriptParserOptions {
283283
pub require_resolve: Option<bool>,
284284
pub import_dynamic: Option<bool>,
285285
pub inline_const: Option<bool>,
286+
pub jsx: Option<bool>,
286287
}
287288

288289
#[cacheable]

crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use rspack_core::{
55
AsyncDependenciesBlockIdentifier, BuildMetaExportsType, COLLECTED_TYPESCRIPT_INFO_PARSE_META_KEY,
66
ChunkGraph, CollectedTypeScriptInfo, Compilation, DependenciesBlock, DependencyId,
77
DependencyRange, GenerateContext, Module, ModuleGraph, ModuleType, ParseContext, ParseResult,
8-
ParserAndGenerator, SideEffectsBailoutItem, SourceType, TemplateContext, TemplateReplaceSource,
8+
ParserAndGenerator, ParserOptions, SideEffectsBailoutItem, SourceType, TemplateContext,
9+
TemplateReplaceSource,
910
diagnostics::map_box_diagnostics_to_module_parse_diagnostics,
1011
remove_bom, render_init_fragments,
1112
rspack_sources::{BoxSource, ReplaceSource, Source, SourceExt},
@@ -179,8 +180,21 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
179180
);
180181
let comments = SwcComments::default();
181182
let target = ast::EsVersion::EsNext;
183+
184+
let jsx = if let Some(js_parser_options) = module_parser_options
185+
&& let ParserOptions::Javascript(js_options)
186+
| ParserOptions::JavascriptAuto(js_options)
187+
| ParserOptions::JavascriptDynamic(js_options)
188+
| ParserOptions::JavascriptEsm(js_options) = js_parser_options
189+
{
190+
js_options.jsx.unwrap_or(false)
191+
} else {
192+
false
193+
};
194+
182195
let parser_lexer = Lexer::new(
183196
Syntax::Es(EsSyntax {
197+
jsx,
184198
allow_return_outside_function: matches!(
185199
module_type,
186200
ModuleType::JsDynamic | ModuleType::JsAuto

crates/rspack_plugin_javascript/src/visitors/dependency/parser/walk.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,16 @@ impl JavascriptParser<'_> {
431431
Expr::Update(expr) => self.walk_update_expression(expr),
432432
Expr::Yield(expr) => self.walk_yield_expression(expr),
433433
Expr::SuperProp(_) | Expr::Lit(_) | Expr::PrivateName(_) | Expr::Invalid(_) => (),
434-
Expr::Paren(_)
435-
| Expr::JSXMember(_)
434+
Expr::JSXMember(_)
436435
| Expr::JSXNamespacedName(_)
437436
| Expr::JSXEmpty(_)
438437
| Expr::JSXElement(_)
439-
| Expr::JSXFragment(_)
438+
| Expr::JSXFragment(_) => {
439+
if !self.javascript_options.jsx.unwrap_or_default() {
440+
unreachable!();
441+
}
442+
}
443+
Expr::Paren(_)
440444
| Expr::TsTypeAssertion(_)
441445
| Expr::TsConstAssertion(_)
442446
| Expr::TsNonNull(_)

packages/rspack/etc/core.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,6 +3379,7 @@ export type JavascriptParserOptions = {
33793379
importDynamic?: boolean;
33803380
inlineConst?: boolean;
33813381
typeReexportsPresence?: "no-tolerant" | "tolerant" | "tolerant-no-check";
3382+
jsx?: boolean;
33823383
};
33833384

33843385
// @public (undocumented)

packages/rspack/src/config/adapter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,8 @@ function getRawJavascriptParserOptions(
585585
requireResolve: parser.requireResolve,
586586
importDynamic: parser.importDynamic,
587587
inlineConst: parser.inlineConst,
588-
typeReexportsPresence: parser.typeReexportsPresence
588+
typeReexportsPresence: parser.typeReexportsPresence,
589+
jsx: parser.jsx
589590
};
590591
}
591592

0 commit comments

Comments
 (0)