Skip to content

Commit

Permalink
fix(nuxt): Inline nitro-utils function (#14680)
Browse files Browse the repository at this point in the history
  • Loading branch information
s1gr1d authored Dec 12, 2024
1 parent c49b247 commit 027518d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
5 changes: 1 addition & 4 deletions packages/nitro-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -35,9 +36,6 @@
]
}
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sentry/core": "8.44.0"
},
Expand All @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions packages/nuxt/build.config.ts
Original file line number Diff line number Diff line change
@@ -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({});
19 changes: 19 additions & 0 deletions packages/nuxt/generate-build-stubs.bash
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 1 addition & 2 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 0 additions & 16 deletions packages/nuxt/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
}),
),
];
38 changes: 35 additions & 3 deletions packages/nuxt/src/runtime/plugins/sentry.server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -66,3 +74,27 @@ async function flushWithTimeout(): Promise<void> {
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<EventHandler>) {
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();
}
});
},
});
}

0 comments on commit 027518d

Please sign in to comment.