Skip to content

Commit

Permalink
Merge pull request #135 from blitz-js/fix-134
Browse files Browse the repository at this point in the history
fix: export list for getServerSideProps
  • Loading branch information
Skn0tt authored Nov 22, 2022
2 parents 81d6fd4 + f92dc32 commit 25dcdaf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addNamed as addNamedImport } from '@babel/helper-module-imports';
import {
arrayExpression,
callExpression,
exportDefaultSpecifier,
exportNamedDeclaration,
ClassDeclaration,
classExpression,
ExportNamedDeclaration,
Expand Down Expand Up @@ -192,6 +192,52 @@ function transformImportExportDefault(paths: NodePath<any>[]) {
}
}

/**
* transforms `export { getServerSideProps }` import & export line
*/
function transformDatafetcherNamedImportExports(
paths: NodePath<any>[],
dataFetcherName: 'getServerSideProps' | 'getStaticProps'
) {
for (const path of paths) {
if (isExportNamedDeclaration(path)) {
for (const specifier of path.node.specifiers) {
if (specifier.local.name === dataFetcherName) {
paths.forEach((path) =>
path.traverse({
ImportSpecifier(specifier) {
if (specifier.node.imported.name === dataFetcherName) {
specifier.node.local.name = `_${dataFetcherName}`;
}
},
})
);

path.insertAfter(
exportNamedDeclaration(
variableDeclaration('const', [
variableDeclarator(
identifier(dataFetcherName),
identifier(`_${dataFetcherName}`)
),
])
) as any
);

path.node.specifiers.splice(
path.node.specifiers.indexOf(specifier),
1
);

if (path.node.specifiers.length === 0) {
path.remove();
}
}
}
}
}
}

function shouldBeSkipped(filePath: string) {
if (!filePath.includes('pages' + nodePath.sep)) {
return true;
Expand Down Expand Up @@ -219,6 +265,8 @@ function superJsonWithNext(): PluginObj {
}

transformImportExportDefault(path.get('body'));
transformDatafetcherNamedImportExports(path.get('body'), 'getServerSideProps');
transformDatafetcherNamedImportExports(path.get('body'), 'getStaticProps');

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {
getServerSideProps,
PreviewPage,
} from '../../../common-components/contentful-elements/pages/preview';

export { getServerSideProps };

export default PreviewPage;
10 changes: 10 additions & 0 deletions test/pages/export gssP and page from other (repro #134)/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { withSuperJSONPage as _withSuperJSONPage } from 'babel-plugin-superjson-next/tools';
import { withSuperJSONProps as _withSuperJSONProps } from 'babel-plugin-superjson-next/tools';
import {
getServerSideProps as _getServerSideProps,
PreviewPage,
} from '../../../common-components/contentful-elements/pages/preview';
export const getServerSideProps = _withSuperJSONProps(_getServerSideProps, [
'smth',
]);
export default _withSuperJSONPage(PreviewPage);

0 comments on commit 25dcdaf

Please sign in to comment.