Skip to content

feat(nuxt): Add base boilerplate for nuxt #12573

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

Merged
merged 22 commits into from
Jun 21, 2024
Merged
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
6 changes: 6 additions & 0 deletions dev-packages/e2e-tests/verdaccio-config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ packages:
unpublish: $all
# proxy: npmjs # Don't proxy for E2E tests!

'@sentry/nuxt':
access: $all
publish: $all
unpublish: $all
# proxy: npmjs # Don't proxy for E2E tests!

'@sentry/wasm':
access: $all
publish: $all
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"packages/integration-shims",
"packages/nextjs",
"packages/node",
"packages/nuxt",
"packages/opentelemetry",
"packages/profiling-node",
"packages/react",
Expand Down
21 changes: 21 additions & 0 deletions packages/nuxt/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
browser: true,
node: true,
},
overrides: [
{
files: ['vite.config.ts'],
parserOptions: {
project: ['tsconfig.test.json'],
},
},
{
files: ['src/vite/**', 'src/server/**'],
rules: {
'@sentry-internal/sdk/no-optional-chaining': 'off',
},
},
],
extends: ['../../.eslintrc.js'],
};
21 changes: 21 additions & 0 deletions packages/nuxt/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-2024 Functional Software, Inc. dba Sentry

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
85 changes: 85 additions & 0 deletions packages/nuxt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<p align="center">
<a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank">
<img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84">
</a>
</p>

# Official Sentry SDK for Nuxt (EXPERIMENTAL)

[![npm version](https://img.shields.io/npm/v/@sentry/nuxt.svg)](https://www.npmjs.com/package/@sentry/nuxt)
[![npm dm](https://img.shields.io/npm/dm/@sentry/nuxt.svg)](https://www.npmjs.com/package/@sentry/nuxt)
[![npm dt](https://img.shields.io/npm/dt/@sentry/nuxt.svg)](https://www.npmjs.com/package/@sentry/nuxt)

**This SDK is under active development and not yet published!**

## Links

todo: link official SDK docs

- [Official SDK Docs](https://docs.sentry.io/platforms/javascript/)

## Compatibility

The minimum supported version of Nuxt is `3.0.0`.

## General

This package is a wrapper around `@sentry/node` for the server and `@sentry/vue` for the client side, with added
functionality related to Nuxt.

## Automatic Setup

todo: add wizard instructions

Take a look at the sections below if you want to customize your SDK configuration.

## Manual Setup

If the setup through the wizard doesn't work for you, you can also set up the SDK manually.

### 1. Prerequesits & Installation

1. Install the Sentry Nuxt SDK:

```bash
# Using npm
npm install @sentry/nuxt

# Using yarn
yarn add @sentry/nuxt
```

### 2. Client-side 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`:

```javascript
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['@sentry/nuxt'],
runtimeConfig: {
public: {
sentry: {
dsn: env.DSN,
// Additional config
},
},
},
});
```

### 3. Server-side Setup

todo: add server-side setup

### 4. Vite Setup

todo: add vite setup

---

## Uploading Source Maps

todo: add source maps instructions
80 changes: 80 additions & 0 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"name": "@sentry/nuxt",
"version": "8.10.0",
"description": "Official Sentry SDK for Nuxt (EXPERIMENTAL)",
"repository": "git://github.com/getsentry/sentry-javascript.git",
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/nuxt",
"author": "Sentry",
"license": "MIT",
"engines": {
"node": ">=16"
},
"files": [
"build"
],
"main": "build/module.cjs",
"module": "build/module.mjs",
"types": "build/types.d.ts",
"exports": {
".": {
"types": "./build/types.d.ts",
"import": "./build/module.mjs",
"require": "./build/module.cjs"
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
},
"peerDependencies": {
"nuxt": "3.x"
},
"dependencies": {
"@sentry/core": "8.10.0",
"@sentry/node": "8.10.0",
"@sentry/opentelemetry": "8.10.0",
"@sentry/types": "8.10.0",
"@sentry/utils": "8.10.0",
"@sentry/vite-plugin": "2.18.0",
"@sentry/vue": "8.10.0",
"@nuxt/kit": "^3.12.2"
},
"devDependencies": {
"@nuxt/module-builder": "0.8.0",
"nuxt": "^3.12.2"
},
"scripts": {
"build": "run-p build:transpile",
"build:dev": "yarn build",
"build:transpile": "nuxt-module-build build --outDir build",
"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:types:watch": "tsc -p tsconfig.types.json --watch",
"build:tarball": "ts-node ../../scripts/prepack.ts && npm pack ./build",
"circularDepCheck": "madge --circular src/index.client.ts && madge --circular src/index.server.ts && madge --circular src/index.types.ts",
"clean": "rimraf build coverage sentry-nuxt-*.tgz",
"fix": "eslint . --format stylish --fix",
"lint": "eslint . --format stylish",
"test": "yarn test:unit",
"test:unit": "vitest run",
"test:watch": "vitest --watch",
"yalc:publish": "ts-node ../../scripts/prepack.ts && yalc publish build --push --sig"
},
"volta": {
"extends": "../../package.json"
},
"nx": {
"targets": {
"build:transpile": {
"dependsOn": [
"^build:transpile",
"^build:types"
],
"outputs": [
"{projectRoot}/build"
]
}
}
}
}
3 changes: 3 additions & 0 deletions packages/nuxt/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from '@sentry/vue';

export { init } from './sdk';
19 changes: 19 additions & 0 deletions packages/nuxt/src/client/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { applySdkMetadata } from '@sentry/core';
import type { Client } from '@sentry/types';
import { init as initVue } from '@sentry/vue';
import type { SentryVueOptions } 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 {
const sentryOptions = {
...options,
};

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

return initVue(sentryOptions);
}
3 changes: 3 additions & 0 deletions packages/nuxt/src/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { init } from '@sentry/vue';

export type SentryVueOptions = Parameters<typeof init>[0] & object;
1 change: 1 addition & 0 deletions packages/nuxt/src/index.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './client';
1 change: 1 addition & 0 deletions packages/nuxt/src/index.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions packages/nuxt/src/index.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
23 changes: 23 additions & 0 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { type Resolver, addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
import type { SentryVueOptions } from './common/types';

export type ModuleOptions = SentryVueOptions;

export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@sentry/nuxt',
configKey: 'sentry',
compatibility: {
nuxt: '^3.0.0',
},
},
// Default configuration options of the Nuxt module
defaults: {},
setup(_moduleOptions, _nuxt) {
const resolver: Resolver = createResolver(import.meta.url);

if (resolver) {
addPlugin(resolver.resolve('runtime/plugins/sentry.client.js'));
}
},
});
12 changes: 12 additions & 0 deletions packages/nuxt/src/runtime/plugins/sentry.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineNuxtPlugin, useRuntimeConfig } from 'nuxt/app';
import { init } from '../../client';

export default defineNuxtPlugin(nuxtApp => {
const config = useRuntimeConfig();
const sentryConfig = config.public.sentry || {};

init({
...sentryConfig,
app: nuxtApp.vueApp,
});
});
42 changes: 42 additions & 0 deletions packages/nuxt/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { SDK_VERSION } from '@sentry/vue';
import * as SentryVue from '@sentry/vue';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { init } from '../../src/client';

const vueInit = vi.spyOn(SentryVue, 'init');

describe('Nuxt Client SDK', () => {
describe('init', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('Adds Nuxt metadata to the SDK options', () => {
expect(vueInit).not.toHaveBeenCalled();

init({
dsn: 'https://[email protected]/1337',
});

const expectedMetadata = {
_metadata: {
sdk: {
name: 'sentry.javascript.nuxt',
version: SDK_VERSION,
packages: [
{ name: 'npm:@sentry/nuxt', version: SDK_VERSION },
{ name: 'npm:@sentry/vue', version: SDK_VERSION },
],
},
},
};

expect(vueInit).toHaveBeenCalledTimes(1);
expect(vueInit).toHaveBeenLastCalledWith(expect.objectContaining(expectedMetadata));
});

it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
});
});
3 changes: 3 additions & 0 deletions packages/nuxt/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.test.json"
}
8 changes: 8 additions & 0 deletions packages/nuxt/test/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function setup() {}

if (!globalThis.fetch) {
// @ts-expect-error - Needed for vitest to work with our fetch instrumentation
globalThis.Request = class Request {};
// @ts-expect-error - Needed for vitest to work with our fetch instrumentation
globalThis.Response = class Response {};
}
10 changes: 10 additions & 0 deletions packages/nuxt/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",

"include": ["src/**/*"],

"compilerOptions": {
// package-specific options
"module": "esnext"
}
}
10 changes: 10 additions & 0 deletions packages/nuxt/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",

"include": ["test/**/*", "vite.config.ts"],

"compilerOptions": {
// should include all types from `./tsconfig.json` plus types for all test frameworks used
"types": ["node", "vitest/globals"]
}
}
10 changes: 10 additions & 0 deletions packages/nuxt/tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",

"compilerOptions": {
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"outDir": "build/types"
}
}
Loading
Loading