From 0b7a4f39fe38e9237d972d3ecced3d5af62b92ed Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 12 Dec 2024 14:13:35 +0100 Subject: [PATCH 1/5] fix(nuxt): Inline nitro-utils function --- packages/nitro-utils/package.json | 4 +- packages/nuxt/build.config.ts | 3 +- packages/nuxt/rollup.npm.config.mjs | 9 +++-- .../nuxt/src/runtime/plugins/sentry.server.ts | 38 +++++++++++++++++-- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/packages/nitro-utils/package.json b/packages/nitro-utils/package.json index a81d03a3ea4c..0d25078f19d5 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" }, diff --git a/packages/nuxt/build.config.ts b/packages/nuxt/build.config.ts index 4cb00345dc43..84e8fffd80ff 100644 --- a/packages/nuxt/build.config.ts +++ b/packages/nuxt/build.config.ts @@ -2,6 +2,5 @@ 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, + failOnWarn: false }); diff --git a/packages/nuxt/rollup.npm.config.mjs b/packages/nuxt/rollup.npm.config.mjs index d124ba8a7844..2b0ce5609be9 100644 --- a/packages/nuxt/rollup.npm.config.mjs +++ b/packages/nuxt/rollup.npm.config.mjs @@ -15,9 +15,11 @@ 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) */ + + // 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'], @@ -31,4 +33,5 @@ export default [ }, }), ), + */ ]; 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(); + } + }); + }, + }); +} From 1d80647e4ce61854078b6ac44357f66e20b0a5a9 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 12 Dec 2024 14:44:22 +0100 Subject: [PATCH 2/5] remove creating tarball --- packages/nitro-utils/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/nitro-utils/package.json b/packages/nitro-utils/package.json index 0d25078f19d5..c893d643579b 100644 --- a/packages/nitro-utils/package.json +++ b/packages/nitro-utils/package.json @@ -53,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", From b4b282ed1a19eb034f601f69cfe1656b3e984d46 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 12 Dec 2024 14:49:15 +0100 Subject: [PATCH 3/5] remove nitro-utils devDependency --- packages/nuxt/build.config.ts | 2 +- packages/nuxt/package.json | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/nuxt/build.config.ts b/packages/nuxt/build.config.ts index 84e8fffd80ff..73d0189043d3 100644 --- a/packages/nuxt/build.config.ts +++ b/packages/nuxt/build.config.ts @@ -2,5 +2,5 @@ import { defineBuildConfig } from 'unbuild'; // Build Config for the Nuxt Module Builder: https://github.com/nuxt/module-builder export default defineBuildConfig({ - failOnWarn: false + failOnWarn: false, }); diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index e2293f05ccd4..ddbddb3b7577 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -53,7 +53,6 @@ }, "devDependencies": { "@nuxt/module-builder": "^0.8.4", - "@sentry-internal/nitro-utils": "8.44.0", "nuxt": "^3.13.2" }, "scripts": { From d88c3dc9d83f29018ca22b10fa4154f4805049b6 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 12 Dec 2024 14:53:48 +0100 Subject: [PATCH 4/5] remove unused code --- packages/nuxt/rollup.npm.config.mjs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/packages/nuxt/rollup.npm.config.mjs b/packages/nuxt/rollup.npm.config.mjs index 2b0ce5609be9..a94a4b5af253 100644 --- a/packages/nuxt/rollup.npm.config.mjs +++ b/packages/nuxt/rollup.npm.config.mjs @@ -15,23 +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', - }, - }, - }), - ), - */ ]; From c7095a44269e7fb2db32b6944a49135045a45481 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 12 Dec 2024 16:01:33 +0100 Subject: [PATCH 5/5] generate files if not existent --- packages/nuxt/build.config.ts | 4 +--- packages/nuxt/generate-build-stubs.bash | 19 +++++++++++++++++++ packages/nuxt/package.json | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 packages/nuxt/generate-build-stubs.bash diff --git a/packages/nuxt/build.config.ts b/packages/nuxt/build.config.ts index 73d0189043d3..5e69aa734a40 100644 --- a/packages/nuxt/build.config.ts +++ b/packages/nuxt/build.config.ts @@ -1,6 +1,4 @@ import { defineBuildConfig } from 'unbuild'; // Build Config for the Nuxt Module Builder: https://github.com/nuxt/module-builder -export default defineBuildConfig({ - 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 ddbddb3b7577..837c50740c40 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -58,7 +58,7 @@ "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",