Skip to content

Commit f064406

Browse files
committed
add proxy module template
1 parent 74929ee commit f064406

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

packages/nextjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
},
3535
"devDependencies": {
3636
"@babel/types": "7.18.10",
37+
"@sentry/nextjs": "7.11.1",
3738
"@types/jscodeshift": "^0.11.5",
3839
"@types/webpack": "^4.41.31",
3940
"next": "10.1.3"

packages/nextjs/rollup.npm.config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
1+
import { makeBaseNPMConfig, makeNPMConfigVariants, plugins } from '../../rollup/index.js';
22

33
export default [
44
...makeNPMConfigVariants(
@@ -16,10 +16,12 @@ export default [
1616
makeBaseNPMConfig({
1717
entrypoints: [
1818
'src/config/templates/prefixLoaderTemplate.ts',
19+
'src/config/templates/proxyLoaderTemplate.ts',
1920
'src/config/templates/dataFetchersLoaderTemplate.ts',
2021
],
2122

2223
packageSpecificConfig: {
24+
plugins: [plugins.makeRemoveMultiLineCommentsPlugin()],
2325
output: {
2426
// Preserve the original file structure (i.e., so that everything is still relative to `src`). (Not entirely
2527
// clear why this is necessary here and not for other entrypoints in this file.)
@@ -29,8 +31,11 @@ export default [
2931
// shouldn't have them, lest they muck with the module to which we're adding it)
3032
sourcemap: false,
3133
esModule: false,
34+
35+
// make it so Rollup calms down about the fact that we're combining default and named exports
36+
exports: 'named',
3237
},
33-
external: ['@sentry/nextjs'],
38+
external: ['@sentry/nextjs', '__RESOURCE_PATH__'],
3439
},
3540
}),
3641
),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* This file is a template for the code which will be substituted when our webpack loader handles non-API files in the
3+
* `pages/` directory.
4+
*
5+
* We use `__RESOURCE_PATH__` as a placeholder for the path to the file being wrapped. Because it's not a real package,
6+
* this causes both TS and ESLint to complain, hence the pragma comments below.
7+
*/
8+
9+
// @ts-ignore See above
10+
// eslint-disable-next-line import/no-unresolved
11+
import * as wrapee from '__RESOURCE_PATH__';
12+
import * as Sentry from '@sentry/nextjs';
13+
import type { GetServerSideProps, GetStaticProps, NextPage as NextPageComponent } from 'next';
14+
15+
type NextPageModule = {
16+
default: { getInitialProps?: NextPageComponent['getInitialProps'] };
17+
getStaticProps?: GetStaticProps;
18+
getServerSideProps?: GetServerSideProps;
19+
};
20+
21+
const userPageModule = wrapee as NextPageModule;
22+
23+
const pageComponent = userPageModule.default;
24+
25+
const origGetInitialProps = pageComponent.getInitialProps;
26+
const origGetStaticProps = userPageModule.getStaticProps;
27+
const origGetServerSideProps = userPageModule.getServerSideProps;
28+
29+
if (typeof origGetInitialProps === 'function') {
30+
pageComponent.getInitialProps = Sentry.withSentryGetInitialProps(
31+
origGetInitialProps,
32+
'__ROUTE__',
33+
) as NextPageComponent['getInitialProps'];
34+
}
35+
36+
export const getStaticProps =
37+
typeof origGetStaticProps === 'function'
38+
? Sentry.withSentryGetStaticProps(origGetStaticProps, '__ROUTE__')
39+
: undefined;
40+
export const getServerSideProps =
41+
typeof origGetServerSideProps === 'function'
42+
? Sentry.withSentryGetServerSideProps(origGetServerSideProps, '__ROUTE__')
43+
: undefined;
44+
45+
export default pageComponent;
46+
47+
// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
48+
// not include anything whose name matchs something we've explicitly exported above.
49+
// @ts-ignore See above
50+
// eslint-disable-next-line import/no-unresolved
51+
export * from '__RESOURCE_PATH__';

packages/nextjs/tsconfig.types.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"extends": "./tsconfig.json",
33

4+
// Some of the templates for code we inject into a user's app include an import from `@sentry/nextjs`. This makes
5+
// creating types for these template files a circular exercise, which causes `tsc` to crash. Fortunately, since the
6+
// templates aren't consumed as modules (they're essentially just text files which happen to contain code), we don't
7+
// actually need to create types for them.
48
"exclude": ["src/config/templates/*"],
59

610
"compilerOptions": {

0 commit comments

Comments
 (0)