diff --git a/packages/nuxt/README.md b/packages/nuxt/README.md index dcfc869cb108..e33201b21518 100644 --- a/packages/nuxt/README.md +++ b/packages/nuxt/README.md @@ -49,27 +49,33 @@ If the setup through the wizard doesn't work for you, you can also set up the SD yarn add @sentry/nuxt ``` -### 2. Client-side Setup +### 2. Nuxt Module Setup The Sentry Nuxt SDK is based on [Nuxt Modules](https://nuxt.com/docs/api/kit/modules). -1. Add `@sentry/nuxt` to the modules section of `nuxt.config.ts`: +1. Add `@sentry/nuxt/module` to the modules section of `nuxt.config.ts`: ```javascript // nuxt.config.ts export default defineNuxtConfig({ - modules: ['@sentry/nuxt'], - runtimeConfig: { - public: { - sentry: { - dsn: env.DSN, - // Additional config - }, - }, - }, + modules: ['@sentry/nuxt/module'], }); ``` +2. Add a `sentry.client.config.(js|ts)` file to the root of your project: + +```javascript +import * as Sentry from '@sentry/nuxt'; + +if (!import.meta.env.SSR) { + Sentry.init({ + dsn: env.DSN, + replaysSessionSampleRate: 0.1, + replaysOnErrorSampleRate: 1.0, + }); +} +``` + ### 3. Server-side Setup todo: add server-side setup diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index 4862d08aba2d..1a9bbb4c009a 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -12,16 +12,28 @@ "files": [ "/build" ], - "main": "build/module.cjs", - "module": "build/module.mjs", - "types": "build/types.d.ts", + "main": "build/cjs/index.server.js", + "module": "build/esm/index.server.js", + "browser": "build/esm/index.client.js", + "types": "build/types/index.types.d.ts", "exports": { + "./package.json": "./package.json", ".": { - "types": "./build/types.d.ts", - "import": "./build/module.mjs", - "require": "./build/module.cjs" + "types": "./build/types/index.types.d.ts", + "browser": { + "import": "./build/esm/index.client.js", + "require": "./build/cjs/index.client.js" + }, + "node": { + "import": "./build/esm/index.server.js", + "require": "./build/cjs/index.server.js" + } }, - "./package.json": "./package.json" + "./module": { + "types": "./build/module/types.d.ts", + "import": "./build/module/module.mjs", + "require": "./build/module/module.cjs" + } }, "publishConfig": { "access": "public" @@ -31,6 +43,7 @@ }, "dependencies": { "@nuxt/kit": "^3.12.2", + "@sentry/browser": "8.13.0", "@sentry/core": "8.13.0", "@sentry/node": "8.13.0", "@sentry/opentelemetry": "8.13.0", @@ -44,12 +57,14 @@ "nuxt": "^3.12.2" }, "scripts": { - "build": "run-p build:transpile", + "build": "run-p build:transpile build:types build:nuxt-module", "build:dev": "yarn build", - "build:transpile": "nuxt-module-build build --outDir build", + "build:nuxt-module": "nuxt-module-build build --outDir build/module", + "build:transpile": "rollup -c rollup.npm.config.mjs", + "build:types": "tsc -p tsconfig.types.json", "build:watch": "run-p build:transpile:watch build:types:watch", "build:dev:watch": "yarn build:watch", - "build:transpile:watch": "nuxt-module-build build --outDir build --watch", + "build:transpile:watch": "rollup -c rollup.npm.config.mjs --watch", "build:types:watch": "tsc -p tsconfig.types.json --watch", "build:tarball": "npm pack", "circularDepCheck": "madge --circular src/index.client.ts && madge --circular src/index.server.ts && madge --circular src/index.types.ts", @@ -72,7 +87,8 @@ "^build:types" ], "outputs": [ - "{projectRoot}/build" + "{projectRoot}/build", + "{projectRoot}/build/module" ] } } diff --git a/packages/nuxt/rollup.npm.config.mjs b/packages/nuxt/rollup.npm.config.mjs new file mode 100644 index 000000000000..e800fdbba474 --- /dev/null +++ b/packages/nuxt/rollup.npm.config.mjs @@ -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'], + }), +); diff --git a/packages/nuxt/src/client/sdk.ts b/packages/nuxt/src/client/sdk.ts index 3a82dfb9f52f..e07c3267c902 100644 --- a/packages/nuxt/src/client/sdk.ts +++ b/packages/nuxt/src/client/sdk.ts @@ -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); } diff --git a/packages/nuxt/src/common/debug-build.ts b/packages/nuxt/src/common/debug-build.ts new file mode 100644 index 000000000000..60aa50940582 --- /dev/null +++ b/packages/nuxt/src/common/debug-build.ts @@ -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__; diff --git a/packages/nuxt/src/common/snippets.ts b/packages/nuxt/src/common/snippets.ts new file mode 100644 index 000000000000..5b8a3f1f3ea1 --- /dev/null +++ b/packages/nuxt/src/common/snippets.ts @@ -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 = '