Replies: 10 comments
-
Except fix the span, we need pass ast from swc_loader to JavaScriptParserAndGenerator. // swc_loader
fn run(loader_context) {
....
let mut additional_data = Default::default();
additional_data.insert(ast);
loader_context.finish((code, map, additional_data))
} then get ast from // parser_and_generator
fn parse(parse_context) {
let ParseContext { additional_data } = parse_context;
let ast = additional_data.get::<SwcAst>();
....
} |
Beta Was this translation helpful? Give feedback.
-
Voting for this solution. Given the status-quo, |
Beta Was this translation helpful? Give feedback.
-
Actually, we can't get mapping that from source span to transformed span using |
Beta Was this translation helpful? Give feedback.
-
In swc transformer, it would create some default span node in ast. const element = <div></div>; After transform // ast
Script(Script { span: 1..29, body: [Decl(Var(VarDecl { span: 0..0, ctxt: #0, kind: "var", declare: false, decls: [VarDeclarator { span: 0..0, name: Ident(BindingIdent { id: Ident { span: 0..0, ctxt: #0, sym: "_require", optional: false }, type_ann: None }), init: Some(Call(CallExpr { span: 0..0, ctxt: #0, callee: Expr(Ident(Ident { span: 0..0, ctxt: #0, sym: "require", optional: false })), args: [ExprOrSpread { spread: None, expr: Lit(Str(Str { span: 0..0, value: "react/jsx-runtime", raw: None })) }], type_args: None })), definite: false }, VarDeclarator { span: 0..0, name: Ident(BindingIdent { id: Ident { span: 0..0, ctxt: #0, sym: "_jsx", optional: false }, type_ann: None }), init: Some(Member(MemberExpr { span: 0..0, obj: Ident(Ident { span: 0..0, ctxt: #0, sym: "_require", optional: false }), prop: Ident(IdentName { span: 0..0, sym: "jsx" }) })), definite: false }] })), Decl(Var(VarDecl { span: 1..29, ctxt: #0, kind: "var", declare: false, decls: [VarDeclarator { span: 7..28, name: Ident(BindingIdent { id: Ident { span: 7..14, ctxt: #0, sym: "element", optional: false }, type_ann: None }), init: Some(Call(CallExpr { span: 17..28, ctxt: #0, callee: Expr(Ident(Ident { span: 0..0, ctxt: #0, sym: "_jsx", optional: false })), args: [ExprOrSpread { spread: None, expr: Lit(Str(Str { span: 18..21, value: "div", raw: None })) }, ExprOrSpread { spread: None, expr: Object(ObjectLit { span: 0..0, props: [] }) }], type_args: None })), definite: false }] }))], shebang: None }) var _require = require("react/jsx-runtime"), _jsx = _require.jsx;
var element = _jsx("div", {}); So we may not get map original_node_span to gen_node_span only using records way. |
Beta Was this translation helpful? Give feedback.
-
Is this a bug of swc ? |
Beta Was this translation helpful? Give feedback.
-
I don't think, swc can't get the span information util ast generate actual code. |
Beta Was this translation helpful? Give feedback.
-
Oh I see, it uses DUMMY_SP everywhere to generate extra AST during transform |
Beta Was this translation helpful? Give feedback.
-
I do some simple bench test. It's not too optimistic to reuse ast now.
I guess that fix span info need rewrite almost all ast node span status. It's will cost many time. |
Beta Was this translation helpful? Give feedback.
-
This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
Beta Was this translation helpful? Give feedback.
-
bump |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
span is not valid after transformation
currrently Rspack will do double parse for all file processed by builtin:swc-loader, the reason Rspack do double parse is caused by swc transform will make the origin span invalid which cause it's bad for following dependency generation
builtin:swc-loader
builtin:swc-loader
span can be fixed by srcmapbuf generated by codegen
so the span in ASTNode is not valid after transformation, but we can fix the span use src_map generated by emitter, the src_map contains the right mapping from span in ast to span after codegen
rspack/crates/rspack_plugin_javascript/src/ast/stringify.rs
Lines 79 to 84 in 9112e35
we fix ast span using
visit_mut_span
in SpanFixerVisitorast invalidation
we should mark the ast dirty | invalid, if the code is modified by following loader
reuse span in module.build
if the AST contains the valid span, then parse can reuse ast passing from builtin:swc-loader, no need to do double parse in module.build
rspack/crates/rspack_core/src/normal_module.rs
Line 542 in 9112e35
Beta Was this translation helpful? Give feedback.
All reactions