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 config with Nuxt addPluginTemplate #12760

Merged
merged 8 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
28 changes: 17 additions & 11 deletions packages/nuxt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,31 @@ 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
### 4. Server-side setup

Add a `sentry.server.config.(js|ts)` file to the root of your project:

```javascript
import * as Sentry from '@sentry/nuxt';

todo: add server-side setup
Sentry.init({
dsn: env.DSN,
});
```

### 4. Vite Setup
### 5. Vite Setup

todo: add vite setup

Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"nuxt": "3.x"
},
"dependencies": {
"@nuxt/kit": "^3.12.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know, is it normal for plugins like sentry to directly depend on nuxt kit or is it more like a peer dep thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw it as a dependency in other Nuxt modules and therefore added it there too

"@nuxt/kit": "^3.12.3",
"@sentry/browser": "8.14.0",
"@sentry/core": "8.14.0",
"@sentry/node": "8.14.0",
Expand All @@ -53,8 +53,8 @@
"@sentry/vue": "8.14.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
2 changes: 1 addition & 1 deletion packages/nuxt/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollu

export default makeNPMConfigVariants(
makeBaseNPMConfig({
entrypoints: ['src/index.client.ts', 'src/client/index.ts'],
entrypoints: ['src/index.server.ts', 'src/index.client.ts', 'src/client/index.ts', 'src/server/index.ts'],
}),
);
47 changes: 0 additions & 47 deletions packages/nuxt/src/common/snippets.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/nuxt/src/index.server.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export * from './server';
18 changes: 17 additions & 1 deletion packages/nuxt/src/index.types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export * from './client';
import type { Integration, Options, StackParser } from '@sentry/types';
import type * as clientSdk from './index.client';
import type * as serverSdk from './index.server';

// We export everything from both the client part of the SDK and from the server part. Some of the exports collide,
// which is not allowed, unless we re-export the colliding exports in this file - which we do below.
export * from './index.client';
export * from './index.server';

// re-export colliding types
export declare function init(options: Options | clientSdk.BrowserOptions | serverSdk.NodeOptions): void;
export declare const linkedErrorsIntegration: typeof clientSdk.linkedErrorsIntegration;
export declare const contextLinesIntegration: typeof clientSdk.contextLinesIntegration;
export declare const getDefaultIntegrations: (options: Options) => Integration[];
export declare const defaultStackParser: StackParser;
export declare const continueTrace: typeof clientSdk.continueTrace;
export declare const metrics: typeof clientSdk.metrics & typeof serverSdk.metrics;
51 changes: 31 additions & 20 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,46 @@ 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(() => {})',
});

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

if (resolver) {
addPlugin(resolver.resolve('./runtime/plugins/sentry.client'));
const serverConfigFile = findDefaultSdkInitFile('server');

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

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;
}
3 changes: 3 additions & 0 deletions packages/nuxt/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from '@sentry/node';

export { init } from './sdk';
19 changes: 19 additions & 0 deletions packages/nuxt/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { applySdkMetadata } from '@sentry/core';
import { init as initNode } from '@sentry/node';
import type { Client } from '@sentry/types';
import type { SentryNuxtOptions } from '../common/types';

/**
* Initializes the server-side of the Nuxt SDK
*
* @param options Configuration options for the SDK.
*/
export function init(options: SentryNuxtOptions): Client | undefined {
const sentryOptions = {
...options,
};

applySdkMetadata(sentryOptions, 'nuxt', ['nuxt', 'node']);

return initNode(sentryOptions);
}
98 changes: 0 additions & 98 deletions packages/nuxt/test/common/snippets.test.ts

This file was deleted.

Loading
Loading