-
Notifications
You must be signed in to change notification settings - Fork 1
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
86byre0hx: Implemented sentry generator #23
Open
rbd2q
wants to merge
5
commits into
main
Choose a base branch
from
86byre0hx-implement-sentry-generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
110600c
feat: implemented sentry generator
rbd2q 99b7a5f
feat: updated expo-app and next-app generators
rbd2q d7b7633
refactor: refactored sentry generator
rbd2q 743e548
feat: updated next and expo generators
rbd2q 81221c4
Merge branch 'main' into 86byre0hx-implement-sentry-generator
rbd2q File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
plugin/src/generators/sentry/files/app/[locale]/global-error.tsx.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use client'; | ||
|
||
import * as Sentry from '@sentry/nextjs'; | ||
import NextError from 'next/error'; | ||
import { ReactElement, useEffect } from 'react'; | ||
|
||
interface GlobalErrorProps { | ||
error: Error & { digest?: string }; | ||
} | ||
|
||
export default function GlobalError({ error }: GlobalErrorProps): ReactElement { | ||
useEffect(() => { | ||
Sentry.captureException(error); | ||
}, [error]); | ||
|
||
return ( | ||
<html> | ||
<body> | ||
<NextError statusCode={undefined as any} /> | ||
</body> | ||
</html> | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
plugin/src/generators/sentry/files/sentry.client.config.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as Sentry from '@sentry/nextjs'; | ||
|
||
Sentry.init({ | ||
dsn: '<%= DSN %>', | ||
|
||
// Adjust this value in production, or use tracesSampler for greater control | ||
tracesSampleRate: 1, | ||
|
||
debug: false, | ||
replaysOnErrorSampleRate: 1.0, | ||
|
||
// This sets the sample rate to be 10%. You may want this to be 100% while | ||
// in development and sample at a lower rate in production | ||
replaysSessionSampleRate: 0.1, | ||
|
||
// You can remove this option if you're not planning to use the Sentry Session Replay feature: | ||
integrations: [ | ||
Sentry.replayIntegration(), | ||
Sentry.inboundFiltersIntegration(), | ||
], | ||
denyUrls: ['localhost'], | ||
}); |
12 changes: 12 additions & 0 deletions
12
plugin/src/generators/sentry/files/sentry.edge.config.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as Sentry from '@sentry/nextjs'; | ||
|
||
Sentry.init({ | ||
dsn: '<%= DSN %>', | ||
|
||
// Adjust this value in production, or use tracesSampler for greater control | ||
tracesSampleRate: 1, | ||
|
||
debug: false, | ||
integrations: [Sentry.inboundFiltersIntegration()], | ||
denyUrls: ['localhost'] | ||
}); |
12 changes: 12 additions & 0 deletions
12
plugin/src/generators/sentry/files/sentry.server.config.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as Sentry from '@sentry/nextjs'; | ||
|
||
Sentry.init({ | ||
dsn: '<%= DSN %>', | ||
|
||
// Adjust this value in production, or use tracesSampler for greater control | ||
tracesSampleRate: 1, | ||
|
||
debug: false, | ||
integrations: [Sentry.inboundFiltersIntegration()], | ||
denyUrls: ['localhost'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
formatFiles, | ||
generateFiles, | ||
Tree, | ||
} from '@nx/devkit'; | ||
import * as path from 'path'; | ||
import { SentryGeneratorSchema } from './schema'; | ||
import { isExpoApp, isNextApp } from '../../shared/utils'; | ||
|
||
const nextAppDependencies = { | ||
'@sentry/nextjs': '^8.21.0', | ||
}; | ||
|
||
const expoAppDependencies = { | ||
'@sentry/react-native': '~5.22.0', | ||
}; | ||
|
||
export async function sentryGenerator( | ||
tree: Tree, | ||
options: SentryGeneratorSchema, | ||
) { | ||
const projectRoot = `apps/${options.directory}`; | ||
|
||
if (isNextApp(tree, projectRoot)) { | ||
addDependenciesToPackageJson(tree, nextAppDependencies, {}); | ||
|
||
const nextConfigContent = tree | ||
.read(`${projectRoot}/next.config.js`) | ||
.toString() | ||
.replace( | ||
/^const { withNx } = require\('@nrwl\/next\/plugins\/with-nx'\);$/gm, | ||
`const { withSentryConfig } = require("@sentry/nextjs"); | ||
const { withNx } = require('@nrwl/next/plugins/with-nx');`, | ||
) | ||
.replace( | ||
/^const nextConfig = {/gm, | ||
`const nextConfig = { | ||
sentry: { | ||
// Upload a larger set of source maps for prettier stack traces (increases build time) | ||
widenClientFileUpload: true, | ||
|
||
// Transpiles SDK to be compatible with IE11 (increases bundle size) | ||
transpileClientSDK: true, | ||
|
||
// Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers (increases server load) | ||
tunnelRoute: '/monitoring', | ||
|
||
// Hides source maps from generated client bundles | ||
hideSourceMaps: true, | ||
|
||
// Automatically tree-shake Sentry logger statements to reduce bundle size | ||
disableLogger: true | ||
}, | ||
`, | ||
) | ||
.replace( | ||
/\(nextConfig\)/gm, | ||
`(withSentryConfig(nextConfig, sentryWebpackPluginOptions))`, | ||
); | ||
|
||
tree.write( | ||
`${projectRoot}/next.config.js`, | ||
nextConfigContent + | ||
` | ||
/** | ||
* @type {import('@sentry/nextjs').SentryWebpackPluginOptions} | ||
**/ | ||
|
||
const sentryWebpackPluginOptions = { | ||
silent: true, | ||
org: '', | ||
project: 'web-next-js-client', | ||
authToken: process.env.SENTRY_AUTH_TOKEN, | ||
}; | ||
|
||
`, | ||
); | ||
|
||
const envFiles = ['.env', '.env.development', '.env.production']; | ||
envFiles.forEach((file) => { | ||
const envContent = tree.read(`${projectRoot}/${file}`).toString(); | ||
tree.write(`${projectRoot}/${file}`, envContent + 'SENTRY_AUTH_TOKEN='); | ||
}); | ||
|
||
generateFiles(tree, path.join(__dirname, 'files'), projectRoot, options); | ||
} else if (isExpoApp(tree, projectRoot)) { | ||
addDependenciesToPackageJson(tree, expoAppDependencies, {}); | ||
|
||
const layoutContent = tree | ||
.read(`${projectRoot}/app/_layout.tsx`) | ||
.toString() | ||
.replace( | ||
/^import { Stack } from 'expo-router';$/gm, | ||
`import { Stack } from 'expo-router';import * as Sentry from "@sentry/react-native";import Constants from "expo-constants";`, | ||
) | ||
.replace(/^export default function RootLayout/gm, `function RootLayout`); | ||
|
||
tree.write( | ||
`${projectRoot}/app/_layout.tsx`, | ||
layoutContent + | ||
` | ||
const routingInstrumentation = new Sentry.ReactNavigationInstrumentation(); | ||
|
||
Sentry.init({ | ||
dsn: Constants.expoConfig?.extra?.sentry?.dsn, | ||
environment: Constants.expoConfig?.extra?.env, | ||
debug: false, | ||
integrations: [new Sentry.ReactNativeTracing({ routingInstrumentation })], | ||
enabled: !__DEV__ | ||
}); | ||
|
||
export default Sentry.wrap(RootLayout);`, | ||
); | ||
|
||
const appConfigContent = tree | ||
.read(`${projectRoot}/app.config.ts`) | ||
.toString() | ||
.replace( | ||
/plugins: \[/g, | ||
`plugins: [ [ | ||
'@sentry/react-native/expo', | ||
{ | ||
// TODO Update organization and project name | ||
organization: '', | ||
project: '' | ||
} | ||
],`, | ||
); | ||
|
||
tree.write(`${projectRoot}/app.config.ts`, appConfigContent); | ||
} | ||
|
||
await formatFiles(tree); | ||
} | ||
|
||
export default sentryGenerator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface SentryGeneratorSchema { | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "https://json-schema.org/schema", | ||
"$id": "Sentry", | ||
"title": "", | ||
"type": "object", | ||
"properties": { | ||
"directory": { | ||
"type": "string", | ||
"description": "", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
}, | ||
"x-prompt": "Enter the name of the directory in the 'apps/' folder (e.g: web)" | ||
}, | ||
"DSN": { | ||
"type": "string", | ||
"description": "", | ||
"x-prompt": "What is your Sentry DSN?" | ||
} | ||
}, | ||
"required": ["directory", "DSN"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './format-utils'; | ||
export * from './cli-utils'; | ||
export * from './is-next-app'; | ||
export * from './is-expo-app' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Tree } from '@nx/devkit'; | ||
|
||
export const isExpoApp = (tree: Tree, projectRoot: string): boolean => { | ||
return tree.exists(`${projectRoot}/metro.config.js`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Tree } from '@nx/devkit'; | ||
|
||
export const isNextApp = (tree: Tree, projectRoot: string): boolean => { | ||
return tree.exists(`${projectRoot}/next.config.js`); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rbd2q Unfortunately, the method of replacing anchor strings is unreliable and will eventually fail without warning. This can happen if someone changes the anchors, e.g. by adding or rearranging imports.
Can we instead work directly with the Abstract Syntax Tree (AST)? This would make the generator more stable and less reliant on specific anchors.
For more details and an example, see: https://nx.dev/extending-nx/recipes/modifying-files#ast-manipulation.