Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass multiple exports #41

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {};
}