Skip to content

Commit 027518d

Browse files
authored
fix(nuxt): Inline nitro-utils function (#14680)
1 parent c49b247 commit 027518d

File tree

6 files changed

+57
-29
lines changed

6 files changed

+57
-29
lines changed

packages/nitro-utils/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/nitro-utils",
77
"author": "Sentry",
88
"license": "MIT",
9+
"private": true,
910
"engines": {
1011
"node": ">=16.20"
1112
},
@@ -35,9 +36,6 @@
3536
]
3637
}
3738
},
38-
"publishConfig": {
39-
"access": "public"
40-
},
4139
"dependencies": {
4240
"@sentry/core": "8.44.0"
4341
},
@@ -55,7 +53,6 @@
5553
"build:dev:watch": "run-p build:transpile:watch build:types:watch",
5654
"build:transpile:watch": "rollup -c rollup.npm.config.mjs --watch",
5755
"build:types:watch": "tsc -p tsconfig.types.json --watch",
58-
"build:tarball": "npm pack",
5956
"clean": "rimraf build coverage sentry-internal-nitro-utils-*.tgz",
6057
"fix": "eslint . --format stylish --fix",
6158
"lint": "eslint . --format stylish",

packages/nuxt/build.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { defineBuildConfig } from 'unbuild';
22

33
// Build Config for the Nuxt Module Builder: https://github.com/nuxt/module-builder
4-
export default defineBuildConfig({
5-
// The devDependency "@sentry-internal/nitro-utils" triggers "Inlined implicit external", but it's not external
6-
failOnWarn: false,
7-
});
4+
export default defineBuildConfig({});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 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.
2+
# unbuild checks for this: https://github.com/unjs/unbuild/blob/8c647ec005a02f852e56aeef6076a35eede17df1/src/validate.ts#L81
3+
4+
# 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
5+
# as those files are generated in the other build step.
6+
7+
# Create the directories if they do not exist
8+
mkdir -p build/cjs
9+
mkdir -p build/esm
10+
mkdir -p build/types
11+
12+
# Write files if they do not exist
13+
[ ! -f build/cjs/index.server.js ] && echo "module.exports = {}" > build/cjs/index.server.js
14+
[ ! -f build/cjs/index.client.js ] && echo "module.exports = {}" > build/cjs/index.client.js
15+
[ ! -f build/esm/index.server.js ] && echo "export {}" > build/esm/index.server.js
16+
[ ! -f build/esm/index.client.js ] && echo "export {}" > build/esm/index.client.js
17+
[ ! -f build/types/index.types.d.ts ] && echo "export {}" > build/types/index.types.d.ts
18+
19+
echo "Created build stubs for missing files"

packages/nuxt/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@
5353
},
5454
"devDependencies": {
5555
"@nuxt/module-builder": "^0.8.4",
56-
"@sentry-internal/nitro-utils": "8.44.0",
5756
"nuxt": "^3.13.2"
5857
},
5958
"scripts": {
6059
"build": "run-s build:types build:transpile",
6160
"build:dev": "yarn build",
62-
"build:nuxt-module": "nuxt-module-build build --outDir build/module",
61+
"build:nuxt-module": "bash ./generate-build-stubs.bash && nuxt-module-build build --outDir build/module",
6362
"build:transpile": "rollup -c rollup.npm.config.mjs && yarn build:nuxt-module",
6463
"build:types": "tsc -p tsconfig.types.json",
6564
"build:watch": "run-p build:transpile:watch build:types:watch",

packages/nuxt/rollup.npm.config.mjs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,4 @@ export default [
1515
},
1616
}),
1717
),
18-
/* The Nuxt module plugins are also built with the @nuxt/module-builder.
19-
This rollup setup is still left here for an easier switch between the setups while
20-
manually testing different built outputs (module-builder vs. rollup only) */
21-
...makeNPMConfigVariants(
22-
makeBaseNPMConfig({
23-
entrypoints: ['src/runtime/plugins/sentry.client.ts', 'src/runtime/plugins/sentry.server.ts'],
24-
25-
packageSpecificConfig: {
26-
external: ['nuxt/app', 'nitropack/runtime', 'h3'],
27-
output: {
28-
// Preserve the original file structure (i.e., so that everything is still relative to `src`)
29-
entryFileNames: 'runtime/[name].js',
30-
},
31-
},
32-
}),
33-
),
3418
];

packages/nuxt/src/runtime/plugins/sentry.server.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { patchEventHandler } from '@sentry-internal/nitro-utils';
2-
import { GLOBAL_OBJ, flush, getClient, logger, vercelWaitUntil } from '@sentry/core';
1+
import {
2+
GLOBAL_OBJ,
3+
flush,
4+
getClient,
5+
getDefaultIsolationScope,
6+
getIsolationScope,
7+
logger,
8+
vercelWaitUntil,
9+
withIsolationScope,
10+
} from '@sentry/core';
311
import * as Sentry from '@sentry/node';
4-
import { H3Error } from 'h3';
12+
import { type EventHandler, H3Error } from 'h3';
513
import { defineNitroPlugin } from 'nitropack/runtime';
614
import type { NuxtRenderHTMLContext } from 'nuxt/app';
715
import { addSentryTracingMetaTags, extractErrorContext } from '../utils';
@@ -66,3 +74,27 @@ async function flushWithTimeout(): Promise<void> {
6674
isDebug && logger.log('Error while flushing events:\n', e);
6775
}
6876
}
77+
78+
// copied from '@sentry-internal/nitro-utils' - the nuxt-module-builder does not inline devDependencies
79+
function patchEventHandler(handler: EventHandler): EventHandler {
80+
return new Proxy(handler, {
81+
async apply(handlerTarget, handlerThisArg, handlerArgs: Parameters<EventHandler>) {
82+
const isolationScope = getIsolationScope();
83+
const newIsolationScope = isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope;
84+
85+
logger.log(
86+
`Patched h3 event handler. ${
87+
isolationScope === newIsolationScope ? 'Using existing' : 'Created new'
88+
} isolation scope.`,
89+
);
90+
91+
return withIsolationScope(newIsolationScope, async () => {
92+
try {
93+
return await handlerTarget.apply(handlerThisArg, handlerArgs);
94+
} finally {
95+
await flushIfServerless();
96+
}
97+
});
98+
},
99+
});
100+
}

0 commit comments

Comments
 (0)