Skip to content

Commit edb1f57

Browse files
committed
don't attach comments to module
1 parent b4205c6 commit edb1f57

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

crates/next-custom-transforms/src/transforms/react_server_components.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,12 @@ impl<C: Comments> ReactServerComponents<C> {
234234
self.prepend_comment_node(module, is_cjs, export_names);
235235
}
236236

237-
fn prepend_comment_node(&self, module: &Module, is_cjs: bool, export_names: &[Atom]) {
237+
fn prepend_comment_node(&self, module: &mut Module, is_cjs: bool, export_names: &[Atom]) {
238238
// Prepend a special comment to the top of the file that contains
239239
// module export names and the detected module type.
240+
let comment_span = Span::dummy_with_cmt();
240241
self.comments.add_leading(
241-
module.span.lo,
242+
comment_span.lo,
242243
Comment {
243244
span: DUMMY_SP,
244245
kind: CommentKind::Block,
@@ -250,6 +251,10 @@ impl<C: Comments> ReactServerComponents<C> {
250251
.into(),
251252
},
252253
);
254+
module.body.insert(
255+
0,
256+
ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: comment_span })),
257+
);
253258
}
254259
}
255260

crates/next-custom-transforms/src/transforms/server_actions.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use swc_core::{
2222
errors::HANDLER,
2323
source_map::{SourceMapGenConfig, PURE_SP},
2424
util::take::Take,
25-
BytePos, FileName, Mark, SourceMap, Span, SyntaxContext, DUMMY_SP,
25+
FileName, Mark, SourceMap, Span, SyntaxContext, DUMMY_SP,
2626
},
2727
ecma::{
2828
ast::*,
@@ -151,7 +151,6 @@ pub fn server_actions<C: Comments>(
151151
cm,
152152
file_name: file_name.to_string(),
153153
file_query,
154-
start_pos: BytePos(0),
155154
file_directive: None,
156155
in_exported_expr: false,
157156
in_default_export_decl: false,
@@ -216,7 +215,6 @@ struct ServerActions<C: Comments> {
216215
cm: Arc<SourceMap>,
217216
mode: ServerActionsMode,
218217

219-
start_pos: BytePos,
220218
file_directive: Option<Directive>,
221219
in_exported_expr: bool,
222220
in_default_export_decl: bool,
@@ -604,10 +602,7 @@ impl<C: Comments> ServerActions<C> {
604602
new_params.append(&mut function.params);
605603

606604
let action_name: Atom = self.gen_action_ident();
607-
let mut action_ident = Ident::new(action_name.clone(), function.span, self.private_ctxt);
608-
if action_ident.span.lo == self.start_pos {
609-
action_ident.span = Span::dummy_with_cmt();
610-
}
605+
let action_ident = Ident::new(action_name.clone(), function.span, self.private_ctxt);
611606

612607
let action_id = self.generate_server_reference_id(&action_name, false, Some(&new_params));
613608

@@ -1158,11 +1153,6 @@ impl<C: Comments> VisitMut for ServerActions<C> {
11581153
}
11591154
}
11601155

1161-
fn visit_mut_module(&mut self, m: &mut Module) {
1162-
self.start_pos = m.span.lo;
1163-
m.visit_mut_children_with(self);
1164-
}
1165-
11661156
fn visit_mut_stmt(&mut self, n: &mut Stmt) {
11671157
n.visit_mut_children_with(self);
11681158

@@ -2071,12 +2061,18 @@ impl<C: Comments> VisitMut for ServerActions<C> {
20712061
new.rotate_right(import_count);
20722062
}
20732063

2064+
let create_empty_stmt_with_cmt = |comment: Comment| {
2065+
let comment_span = Span::dummy_with_cmt();
2066+
self.comments.add_leading(comment_span.lo, comment);
2067+
ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: comment_span }))
2068+
};
2069+
20742070
if self.has_action || self.has_cache {
20752071
if self.config.is_react_server_layer {
20762072
// Prepend a special comment to the top of the file.
2077-
self.comments.add_leading(
2078-
self.start_pos,
2079-
Comment {
2073+
new.insert(
2074+
0,
2075+
create_empty_stmt_with_cmt(Comment {
20802076
span: DUMMY_SP,
20812077
kind: CommentKind::Block,
20822078
text: generate_server_actions_comment(
@@ -2087,21 +2083,18 @@ impl<C: Comments> VisitMut for ServerActions<C> {
20872083
},
20882084
)
20892085
.into(),
2090-
},
2086+
}),
20912087
);
20922088
} else {
20932089
match self.mode {
20942090
ServerActionsMode::Webpack => {
2095-
self.comments.add_leading(
2096-
self.start_pos,
2097-
Comment {
2098-
span: DUMMY_SP,
2099-
kind: CommentKind::Block,
2100-
text: generate_server_actions_comment(&actions, None).into(),
2101-
},
2102-
);
2091+
new.push(create_empty_stmt_with_cmt(Comment {
2092+
span: DUMMY_SP,
2093+
kind: CommentKind::Block,
2094+
text: generate_server_actions_comment(&actions, None).into(),
2095+
}));
21032096
new.push(client_layer_import.unwrap());
2104-
new.rotate_right(1);
2097+
new.rotate_right(2);
21052098
new.extend(client_layer_exports.into_iter().map(|(_, (v, _))| v));
21062099
}
21072100
ServerActionsMode::Turbopack => {
@@ -3289,15 +3282,19 @@ fn emit_error(error_kind: ServerActionsErrorKind) {
32893282
fn program_to_data_url(
32903283
file_name: &str,
32913284
cm: &Arc<SourceMap>,
3292-
body: Vec<ModuleItem>,
3285+
mut body: Vec<ModuleItem>,
32933286
prepend_comment: Comment,
32943287
) -> String {
3295-
let module_span = Span::dummy_with_cmt();
3288+
let comment_span = Span::dummy_with_cmt();
32963289
let comments = SingleThreadedComments::default();
3297-
comments.add_leading(module_span.lo, prepend_comment);
3290+
comments.add_leading(comment_span.lo, prepend_comment);
3291+
body.insert(
3292+
0,
3293+
ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: comment_span })),
3294+
);
32983295

32993296
let program = &Program::Module(Module {
3300-
span: module_span,
3297+
span: DUMMY_SP,
33013298
body,
33023299
shebang: None,
33033300
});

0 commit comments

Comments
 (0)