Skip to content

Commit f1531a2

Browse files
authored
Bypass multiple exports (#41)
* Add test case * Bypass multiple exports
1 parent c5aefa4 commit f1531a2

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/lib.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ struct NextSuperJsonTransformer {
6868

6969
has_init_props: bool,
7070
use_init_props: bool,
71+
72+
has_multiple_props: bool,
7173
}
7274

7375
#[derive(Debug, Default, Clone, Deserialize)]
@@ -90,6 +92,8 @@ pub fn transform(config: Config) -> impl VisitMut {
9092

9193
has_init_props: false,
9294
use_init_props: false,
95+
96+
has_multiple_props: false,
9397
}
9498
}
9599

@@ -106,7 +110,7 @@ impl VisitMut for NextSuperJsonTransformer {
106110
self.find_ssg_prop(items);
107111

108112
if self.props.export.orig.is_none() {
109-
if !self.use_init_props {
113+
if !self.use_init_props || self.has_multiple_props {
110114
return;
111115
}
112116

@@ -603,11 +607,13 @@ impl NextSuperJsonTransformer {
603607
pub fn find_ssg_prop(&mut self, items: &mut Vec<ModuleItem>) {
604608
let mut ssg_prop_ident = None;
605609

606-
self.props.export.orig = items.iter_mut().position(|item| {
610+
let mut first = None;
611+
612+
items.iter_mut().enumerate().any(|(pos, item)| {
607613
// check initial props
608614
item.visit_mut_children_with(self);
609615

610-
match item {
616+
let found = match item {
611617
// check has ssg props
612618
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) => {
613619
match decl {
@@ -657,9 +663,25 @@ impl NextSuperJsonTransformer {
657663
self.props.export.spec.is_some()
658664
}
659665
_ => false,
666+
};
667+
668+
if found {
669+
if first.is_some() || self.use_init_props {
670+
self.has_multiple_props = true;
671+
return true;
672+
}
673+
first = Some(pos);
660674
}
675+
676+
false
661677
});
662678

679+
if self.has_multiple_props {
680+
return;
681+
}
682+
683+
self.props.export.orig = first;
684+
663685
if ssg_prop_ident.is_some() && !self.props.skip {
664686
let mut n = items.len();
665687

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function Page() {
2+
return <div>Page</div>;
3+
}
4+
5+
export const getStaticProps = () => {
6+
return {
7+
props: {},
8+
};
9+
}
10+
11+
export const getServerSideProps = () => {
12+
return {
13+
props: {},
14+
};
15+
}
16+
17+
Page.getInitialProps = () => {
18+
return {};
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function Page() {
2+
return <div>Page</div>;
3+
}
4+
5+
export const getStaticProps = () => {
6+
return {
7+
props: {},
8+
};
9+
}
10+
11+
export const getServerSideProps = () => {
12+
return {
13+
props: {},
14+
};
15+
}
16+
17+
Page.getInitialProps = () => {
18+
return {};
19+
}

0 commit comments

Comments
 (0)