Skip to content

Commit

Permalink
Merge pull request #88 from blitz-js/fix-82
Browse files Browse the repository at this point in the history
Only transform files with both gSP & Default Export
  • Loading branch information
Skn0tt authored Aug 17, 2021
2 parents 2c9101c + 4c3d211 commit dd7769a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const functionsToReplace = ['getServerSideProps', 'getStaticProps'];
function transformPropGetters(
path: NodePath<ExportNamedDeclaration>,
transform: (v: Expression) => Expression
) {
): 'found' | undefined {
const { node } = path;

if (isFunctionDeclaration(node.declaration)) {
Expand All @@ -80,19 +80,20 @@ function transformPropGetters(
),
]);

return;
return 'found';
}

if (isVariableDeclaration(node.declaration)) {
node.declaration.declarations.forEach((declaration) => {
for (const declaration of node.declaration.declarations) {
if (
isIdentifier(declaration.id) &&
functionsToReplace.includes(declaration.id.name) &&
declaration.init
) {
declaration.init = transform(declaration.init);
return 'found';
}
});
}
}
}

Expand Down Expand Up @@ -185,29 +186,46 @@ function superJsonWithNext(): PluginObj {

const body = path.get('body');

body
.filter((path) => isExportNamedDeclaration(path))
.forEach((path) => {
transformPropGetters(
path as NodePath<ExportNamedDeclaration>,
(decl) => {
return callExpression(addWithSuperJSONPropsImport(path), [
decl,
arrayExpression(
propsToBeExcluded?.map((prop) => stringLiteral(prop))
),
]);
}
);
});

const exportDefaultDeclaration = body.find((path) =>
isExportDefaultDeclaration(path)
);
if (!exportDefaultDeclaration) {
return;
}

const namedExportDeclarations = body
.filter((path) => path.isExportNamedDeclaration())
.map((path) => path as NodePath<ExportNamedDeclaration>);

const containsNextTag = namedExportDeclarations.some((path) => {
return (
isVariableDeclaration(path.node.declaration) &&
path.node.declaration.declarations.some(
(decl) => isIdentifier(decl.id) && decl.id.name.startsWith('__N_')
)
);
});

let transformedOne = false;
namedExportDeclarations.forEach((path) => {
const found = transformPropGetters(path, (decl) => {
return callExpression(addWithSuperJSONPropsImport(path), [
decl,
arrayExpression(
propsToBeExcluded?.map((prop) => stringLiteral(prop))
),
]);
});

if (found === 'found') {
transformedOne = true;
}
});

if (!transformedOne && !containsNextTag) {
return;
}

wrapExportDefaultDeclaration(exportDefaultDeclaration);
},
},
Expand Down
3 changes: 3 additions & 0 deletions test/pages/default export without gsp/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Home() {
return 'Home, Sweet Home';
}
3 changes: 3 additions & 0 deletions test/pages/default export without gsp/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Home() {
return 'Home, Sweet Home';
}
13 changes: 13 additions & 0 deletions test/pages/gSP without default export/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export async function getStaticProps() {
const products = [
{
name: 'Hat',
publishedAt: new Date(0),
},
];
return {
props: {
products,
},
};
}
13 changes: 13 additions & 0 deletions test/pages/gSP without default export/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export async function getStaticProps() {
const products = [
{
name: 'Hat',
publishedAt: new Date(0),
},
];
return {
props: {
products,
},
};
}

0 comments on commit dd7769a

Please sign in to comment.