-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): Configure sentry in external config (#12681)
To be able to differentiate between a browser/client execution context, sentry is initialized in an external config file. An import statement in `nuxt-root.vue` is added which loads this config file. Nuxt tracking issue: #9095 --------- Co-authored-by: Abhijeet Prasad <[email protected]>
- Loading branch information
1 parent
f4a289d
commit f60aae5
Showing
12 changed files
with
253 additions
and
43 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils'; | ||
|
||
export default makeNPMConfigVariants( | ||
makeBaseNPMConfig({ | ||
entrypoints: ['src/index.client.ts', 'src/client/index.ts'], | ||
}), | ||
); |
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,19 +1,19 @@ | ||
import { init as initBrowser } from '@sentry/browser'; | ||
import { applySdkMetadata } from '@sentry/core'; | ||
import type { Client } from '@sentry/types'; | ||
import { init as initVue } from '@sentry/vue'; | ||
import type { SentryVueOptions } from '../common/types'; | ||
import type { SentryNuxtOptions } from '../common/types'; | ||
|
||
/** | ||
* Initializes the client-side of the Nuxt SDK | ||
* | ||
* @param options Configuration options for the SDK. | ||
*/ | ||
export function init(options: SentryVueOptions): Client | undefined { | ||
export function init(options: SentryNuxtOptions): Client | undefined { | ||
const sentryOptions = { | ||
...options, | ||
}; | ||
|
||
applySdkMetadata(sentryOptions, 'nuxt', ['nuxt', 'vue']); | ||
|
||
return initVue(sentryOptions); | ||
return initBrowser(sentryOptions); | ||
} |
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,8 @@ | ||
declare const __DEBUG_BUILD__: boolean; | ||
|
||
/** | ||
* This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code. | ||
* | ||
* ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking. | ||
*/ | ||
export const DEBUG_BUILD = __DEBUG_BUILD__; |
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,47 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
/** Returns an import snippet */ | ||
export function buildSdkInitFileImportSnippet(filePath: string): string { | ||
const posixPath = filePath.split(path.sep).join(path.posix.sep); | ||
|
||
// normalize to forward slashed for Windows-based systems | ||
const normalizedPath = posixPath.replace(/\\/g, '/'); | ||
|
||
return `import '${normalizedPath}';`; | ||
} | ||
|
||
/** | ||
* Script tag inside `nuxt-root.vue` (root component we get from NuxtApp) | ||
*/ | ||
export const SCRIPT_TAG = '<script setup>'; | ||
|
||
/** | ||
* Adds a top-level import statement right after <script setup>. | ||
* This should happen as early as possible (e.g. in root component) | ||
*/ | ||
export function addImportStatement(filePath: string, importStatement: string): void { | ||
try { | ||
const data = fs.readFileSync(filePath, 'utf8'); | ||
const scriptIndex = data.indexOf(SCRIPT_TAG); | ||
|
||
if (scriptIndex === -1) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`[Sentry] Sentry not initialized. Could not find ${SCRIPT_TAG} in ${filePath}`); | ||
return; | ||
} | ||
|
||
// Insert the import statement after the script tag | ||
const output = data.replace(SCRIPT_TAG, `${SCRIPT_TAG}\n${importStatement}\n`); | ||
|
||
try { | ||
fs.writeFileSync(filePath, output, 'utf8'); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(`[Sentry] Error writing file to ${filePath}: ${err}`); | ||
} | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(`[Sentry] Error reading file at ${filePath}: ${err}`); | ||
} | ||
} |
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,3 +1,4 @@ | ||
import type { init } from '@sentry/vue'; | ||
|
||
export type SentryVueOptions = Parameters<typeof init>[0] & object; | ||
// Omitting 'app' as the Nuxt SDK will add the app instance in the client plugin (users do not have to provide this) | ||
export type SentryNuxtOptions = Omit<Parameters<typeof init>[0] & object, '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 |
---|---|---|
@@ -1 +1 @@ | ||
export {}; | ||
export * from './client'; |
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,23 +1,49 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { type Resolver, addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'; | ||
import type { SentryVueOptions } from './common/types'; | ||
import { addImportStatement, buildSdkInitFileImportSnippet } from './common/snippets'; | ||
import type { SentryNuxtOptions } from './common/types'; | ||
|
||
export type ModuleOptions = SentryVueOptions; | ||
export type ModuleOptions = SentryNuxtOptions; | ||
|
||
export default defineNuxtModule<ModuleOptions>({ | ||
meta: { | ||
name: '@sentry/nuxt', | ||
name: '@sentry/nuxt/module', | ||
configKey: 'sentry', | ||
compatibility: { | ||
nuxt: '^3.0.0', | ||
}, | ||
}, | ||
// Default configuration options of the Nuxt module | ||
defaults: {}, | ||
setup(_moduleOptions, _nuxt) { | ||
setup(_moduleOptions, nuxt) { | ||
const resolver: Resolver = createResolver(import.meta.url); | ||
|
||
const pathToClientInit = findDefaultSdkInitFile('client'); | ||
|
||
if (pathToClientInit) { | ||
nuxt.hook('app:templates', nuxtApp => { | ||
if (nuxtApp.rootComponent) { | ||
try { | ||
addImportStatement(nuxtApp.rootComponent, buildSdkInitFileImportSnippet(pathToClientInit)); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(`[Sentry] Could not add import statement to root component. ${err}`); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
if (resolver) { | ||
addPlugin(resolver.resolve('runtime/plugins/sentry.client.js')); | ||
addPlugin(resolver.resolve('./runtime/plugins/sentry.client')); | ||
} | ||
}, | ||
}); | ||
|
||
function findDefaultSdkInitFile(type: /* 'server' | */ 'client'): string | undefined { | ||
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts']; | ||
|
||
const cwd = process.cwd(); | ||
return possibleFileExtensions | ||
.map(e => path.resolve(path.join(cwd, `sentry.${type}.config.${e}`))) | ||
.find(filename => fs.existsSync(filename)); | ||
} |
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,12 +1,13 @@ | ||
import { defineNuxtPlugin, useRuntimeConfig } from 'nuxt/app'; | ||
import { init } from '../../client'; | ||
import { getClient } from '@sentry/core'; | ||
import { vueIntegration } from '@sentry/vue'; | ||
import { defineNuxtPlugin } from 'nuxt/app'; | ||
|
||
export default defineNuxtPlugin(nuxtApp => { | ||
const config = useRuntimeConfig(); | ||
const sentryConfig = config.public.sentry || {}; | ||
nuxtApp.hook('app:created', vueApp => { | ||
const sentryClient = getClient(); | ||
|
||
init({ | ||
...sentryConfig, | ||
app: nuxtApp.vueApp, | ||
if (sentryClient) { | ||
sentryClient.addIntegration(vueIntegration({ app: vueApp })); | ||
} | ||
}); | ||
}); |
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
Oops, something went wrong.