Skip to content

Commit

Permalink
Bypass multiple exports (#41)
Browse files Browse the repository at this point in the history
* Add test case

* Bypass multiple exports
  • Loading branch information
orionmiz authored Oct 14, 2022
1 parent c5aefa4 commit f1531a2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ struct NextSuperJsonTransformer {

has_init_props: bool,
use_init_props: bool,

has_multiple_props: bool,
}

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

has_init_props: false,
use_init_props: false,

has_multiple_props: false,
}
}

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

if self.props.export.orig.is_none() {
if !self.use_init_props {
if !self.use_init_props || self.has_multiple_props {
return;
}

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

self.props.export.orig = items.iter_mut().position(|item| {
let mut first = None;

items.iter_mut().enumerate().any(|(pos, item)| {
// check initial props
item.visit_mut_children_with(self);

match item {
let found = match item {
// check has ssg props
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) => {
match decl {
Expand Down Expand Up @@ -657,9 +663,25 @@ impl NextSuperJsonTransformer {
self.props.export.spec.is_some()
}
_ => false,
};

if found {
if first.is_some() || self.use_init_props {
self.has_multiple_props = true;
return true;
}
first = Some(pos);
}

false
});

if self.has_multiple_props {
return;
}

self.props.export.orig = first;

if ssg_prop_ident.is_some() && !self.props.skip {
let mut n = items.len();

Expand Down
19 changes: 19 additions & 0 deletions tests/fixture/skip/multiple-exports/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function Page() {
return <div>Page</div>;
}

export const getStaticProps = () => {
return {
props: {},
};
}

export const getServerSideProps = () => {
return {
props: {},
};
}

Page.getInitialProps = () => {
return {};
}
19 changes: 19 additions & 0 deletions tests/fixture/skip/multiple-exports/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function Page() {
return <div>Page</div>;
}

export const getStaticProps = () => {
return {
props: {},
};
}

export const getServerSideProps = () => {
return {
props: {},
};
}

Page.getInitialProps = () => {
return {};
}

0 comments on commit f1531a2

Please sign in to comment.