Skip to content
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

feat(nuxt): Inject sentry client config with Nuxt addPluginTemplate #12783

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions packages/nuxt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,18 @@ export default defineNuxtConfig({
});
```

2. Add a `sentry.client.config.(js|ts)` file to the root of your project:
### 3. Client-side setup

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,
});
}
Sentry.init({
dsn: env.DSN,
});
```

### 3. Server-side Setup

todo: add server-side setup

### 4. Vite Setup

todo: add vite setup
Expand Down
7 changes: 3 additions & 4 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,18 @@
"nuxt": "3.x"
},
"dependencies": {
"@nuxt/kit": "^3.12.2",
"@nuxt/kit": "^3.12.3",
"@sentry/browser": "8.15.0",
"@sentry/core": "8.15.0",
"@sentry/node": "8.15.0",
"@sentry/opentelemetry": "8.15.0",
"@sentry/types": "8.15.0",
"@sentry/utils": "8.15.0",
"@sentry/vite-plugin": "2.20.1",
"@sentry/vue": "8.15.0"
},
"devDependencies": {
"@nuxt/module-builder": "0.8.0",
"nuxt": "^3.12.2"
"@nuxt/module-builder": "0.8.1",
"nuxt": "^3.12.3"
},
"scripts": {
"build": "run-p build:transpile build:types build:nuxt-module",
Expand Down
47 changes: 0 additions & 47 deletions packages/nuxt/src/common/snippets.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/nuxt/src/index.server.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/nuxt/src/index.types.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './client';
export * from './index.client';
40 changes: 19 additions & 21 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import { type Resolver, addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
import { addImportStatement, buildSdkInitFileImportSnippet } from './common/snippets';
import { addPlugin, addPluginTemplate, createResolver, defineNuxtModule } from '@nuxt/kit';
import type { SentryNuxtOptions } from './common/types';

export type ModuleOptions = SentryNuxtOptions;
Expand All @@ -16,34 +15,33 @@ export default defineNuxtModule<ModuleOptions>({
},
defaults: {},
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}`);
}
}
const moduleDirResolver = createResolver(import.meta.url);
const buildDirResolver = createResolver(nuxt.options.buildDir);

const clientConfigFile = findDefaultSdkInitFile('client');

if (clientConfigFile) {
// Inject the client-side Sentry config file with a side effect import
addPluginTemplate({
mode: 'client',
filename: 'sentry-client-config.mjs',
getContents: () =>
`import "${buildDirResolver.resolve(`/${clientConfigFile}`)}"\n` +
'export default defineNuxtPlugin(() => {})',
});
}

if (resolver) {
addPlugin(resolver.resolve('./runtime/plugins/sentry.client'));
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/sentry.client'), mode: 'client' });
}
},
});

function findDefaultSdkInitFile(type: /* 'server' | */ 'client'): string | undefined {
function findDefaultSdkInitFile(type: /* 'server' */ 'client'): string | undefined {
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'];

const cwd = process.cwd();
return possibleFileExtensions
const filePath = possibleFileExtensions
.map(e => path.resolve(path.join(cwd, `sentry.${type}.config.${e}`)))
.find(filename => fs.existsSync(filename));

return filePath ? path.basename(filePath) : undefined;
}
98 changes: 0 additions & 98 deletions packages/nuxt/test/common/snippets.test.ts

This file was deleted.

Loading
Loading