From 027518d88d6cfa96e2a56d21c10dd0d4f8251f92 Mon Sep 17 00:00:00 2001 From: Sigrid Huemer <32902192+s1gr1d@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:32:22 +0100 Subject: [PATCH] fix(nuxt): Inline nitro-utils function (#14680) --- packages/nitro-utils/package.json | 5 +-- packages/nuxt/build.config.ts | 5 +-- packages/nuxt/generate-build-stubs.bash | 19 ++++++++++ packages/nuxt/package.json | 3 +- packages/nuxt/rollup.npm.config.mjs | 16 -------- .../nuxt/src/runtime/plugins/sentry.server.ts | 38 +++++++++++++++++-- 6 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 packages/nuxt/generate-build-stubs.bash diff --git a/packages/nitro-utils/package.json b/packages/nitro-utils/package.json index a81d03a3ea4c..c893d643579b 100644 --- a/packages/nitro-utils/package.json +++ b/packages/nitro-utils/package.json @@ -6,6 +6,7 @@ "homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/nitro-utils", "author": "Sentry", "license": "MIT", + "private": true, "engines": { "node": ">=16.20" }, @@ -35,9 +36,6 @@ ] } }, - "publishConfig": { - "access": "public" - }, "dependencies": { "@sentry/core": "8.44.0" }, @@ -55,7 +53,6 @@ "build:dev:watch": "run-p build:transpile:watch build:types:watch", "build:transpile:watch": "rollup -c rollup.npm.config.mjs --watch", "build:types:watch": "tsc -p tsconfig.types.json --watch", - "build:tarball": "npm pack", "clean": "rimraf build coverage sentry-internal-nitro-utils-*.tgz", "fix": "eslint . --format stylish --fix", "lint": "eslint . --format stylish", diff --git a/packages/nuxt/build.config.ts b/packages/nuxt/build.config.ts index 4cb00345dc43..5e69aa734a40 100644 --- a/packages/nuxt/build.config.ts +++ b/packages/nuxt/build.config.ts @@ -1,7 +1,4 @@ import { defineBuildConfig } from 'unbuild'; // Build Config for the Nuxt Module Builder: https://github.com/nuxt/module-builder -export default defineBuildConfig({ - // The devDependency "@sentry-internal/nitro-utils" triggers "Inlined implicit external", but it's not external - failOnWarn: false, -}); +export default defineBuildConfig({}); diff --git a/packages/nuxt/generate-build-stubs.bash b/packages/nuxt/generate-build-stubs.bash new file mode 100644 index 000000000000..914468f0c4a9 --- /dev/null +++ b/packages/nuxt/generate-build-stubs.bash @@ -0,0 +1,19 @@ +# The Nuxt package is built in 2 steps and the nuxt-module-builder shows a warning if one of the files specified in the package.json is missing. +# unbuild checks for this: https://github.com/unjs/unbuild/blob/8c647ec005a02f852e56aeef6076a35eede17df1/src/validate.ts#L81 + +# The runtime folder (which is built with the nuxt-module-builder) is separate from the rest of the package and therefore we can ignore those warnings +# as those files are generated in the other build step. + +# Create the directories if they do not exist +mkdir -p build/cjs +mkdir -p build/esm +mkdir -p build/types + +# Write files if they do not exist +[ ! -f build/cjs/index.server.js ] && echo "module.exports = {}" > build/cjs/index.server.js +[ ! -f build/cjs/index.client.js ] && echo "module.exports = {}" > build/cjs/index.client.js +[ ! -f build/esm/index.server.js ] && echo "export {}" > build/esm/index.server.js +[ ! -f build/esm/index.client.js ] && echo "export {}" > build/esm/index.client.js +[ ! -f build/types/index.types.d.ts ] && echo "export {}" > build/types/index.types.d.ts + +echo "Created build stubs for missing files" diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index e2293f05ccd4..837c50740c40 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -53,13 +53,12 @@ }, "devDependencies": { "@nuxt/module-builder": "^0.8.4", - "@sentry-internal/nitro-utils": "8.44.0", "nuxt": "^3.13.2" }, "scripts": { "build": "run-s build:types build:transpile", "build:dev": "yarn build", - "build:nuxt-module": "nuxt-module-build build --outDir build/module", + "build:nuxt-module": "bash ./generate-build-stubs.bash && nuxt-module-build build --outDir build/module", "build:transpile": "rollup -c rollup.npm.config.mjs && yarn build:nuxt-module", "build:types": "tsc -p tsconfig.types.json", "build:watch": "run-p build:transpile:watch build:types:watch", diff --git a/packages/nuxt/rollup.npm.config.mjs b/packages/nuxt/rollup.npm.config.mjs index d124ba8a7844..a94a4b5af253 100644 --- a/packages/nuxt/rollup.npm.config.mjs +++ b/packages/nuxt/rollup.npm.config.mjs @@ -15,20 +15,4 @@ export default [ }, }), ), - /* The Nuxt module plugins are also built with the @nuxt/module-builder. - This rollup setup is still left here for an easier switch between the setups while - manually testing different built outputs (module-builder vs. rollup only) */ - ...makeNPMConfigVariants( - makeBaseNPMConfig({ - entrypoints: ['src/runtime/plugins/sentry.client.ts', 'src/runtime/plugins/sentry.server.ts'], - - packageSpecificConfig: { - external: ['nuxt/app', 'nitropack/runtime', 'h3'], - output: { - // Preserve the original file structure (i.e., so that everything is still relative to `src`) - entryFileNames: 'runtime/[name].js', - }, - }, - }), - ), ]; diff --git a/packages/nuxt/src/runtime/plugins/sentry.server.ts b/packages/nuxt/src/runtime/plugins/sentry.server.ts index f85e69883bb8..b748115f5c81 100644 --- a/packages/nuxt/src/runtime/plugins/sentry.server.ts +++ b/packages/nuxt/src/runtime/plugins/sentry.server.ts @@ -1,7 +1,15 @@ -import { patchEventHandler } from '@sentry-internal/nitro-utils'; -import { GLOBAL_OBJ, flush, getClient, logger, vercelWaitUntil } from '@sentry/core'; +import { + GLOBAL_OBJ, + flush, + getClient, + getDefaultIsolationScope, + getIsolationScope, + logger, + vercelWaitUntil, + withIsolationScope, +} from '@sentry/core'; import * as Sentry from '@sentry/node'; -import { H3Error } from 'h3'; +import { type EventHandler, H3Error } from 'h3'; import { defineNitroPlugin } from 'nitropack/runtime'; import type { NuxtRenderHTMLContext } from 'nuxt/app'; import { addSentryTracingMetaTags, extractErrorContext } from '../utils'; @@ -66,3 +74,27 @@ async function flushWithTimeout(): Promise { isDebug && logger.log('Error while flushing events:\n', e); } } + +// copied from '@sentry-internal/nitro-utils' - the nuxt-module-builder does not inline devDependencies +function patchEventHandler(handler: EventHandler): EventHandler { + return new Proxy(handler, { + async apply(handlerTarget, handlerThisArg, handlerArgs: Parameters) { + const isolationScope = getIsolationScope(); + const newIsolationScope = isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope; + + logger.log( + `Patched h3 event handler. ${ + isolationScope === newIsolationScope ? 'Using existing' : 'Created new' + } isolation scope.`, + ); + + return withIsolationScope(newIsolationScope, async () => { + try { + return await handlerTarget.apply(handlerThisArg, handlerArgs); + } finally { + await flushIfServerless(); + } + }); + }, + }); +}