From 4b99aaf10afab42a7869614c8c1d3505c7461f00 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Tue, 27 Feb 2024 16:25:27 -0500 Subject: [PATCH 01/12] build: Make all packages emit mjs --- dev-packages/rollup-utils/npmHelpers.mjs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dev-packages/rollup-utils/npmHelpers.mjs b/dev-packages/rollup-utils/npmHelpers.mjs index 6085a502200f..9b309fb2f59c 100644 --- a/dev-packages/rollup-utils/npmHelpers.mjs +++ b/dev-packages/rollup-utils/npmHelpers.mjs @@ -117,11 +117,16 @@ export function makeBaseNPMConfig(options = {}) { }); } -export function makeNPMConfigVariants(baseConfig) { +export function makeNPMConfigVariants(baseConfig, options = {}) { + const { emitMjs = true } = options; const variantSpecificConfigs = [ { output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs') } }, { output: { format: 'esm', dir: path.join(baseConfig.output.dir, 'esm') } }, ]; + if (emitMjs) { + variantSpecificConfigs[1].output.entryFileNames = '[name].mjs'; + } + return variantSpecificConfigs.map(variant => deepMerge(baseConfig, variant)); } From ee1ec1975a5c376092f2b5a657721c88ef3b6924 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Tue, 27 Feb 2024 18:00:09 -0500 Subject: [PATCH 02/12] feat: Emit *.mjs files across all packages --- dev-packages/rollup-utils/npmHelpers.mjs | 8 ++++++-- packages/astro/package.json | 14 +++++++------- packages/browser/package.json | 15 ++++++++++++++- packages/bun/package.json | 15 ++++++++++++++- packages/core/package.json | 15 ++++++++++++++- packages/deno/package.json | 9 +++++++++ packages/feedback/package.json | 15 ++++++++++++++- packages/gatsby/package.json | 15 ++++++++++++++- packages/integration-shims/package.json | 15 ++++++++++++++- packages/nextjs/package.json | 17 ++++++++++++++--- packages/nextjs/rollup.npm.config.mjs | 10 ++++------ packages/nextjs/scripts/buildRollup.ts | 24 ------------------------ packages/node-experimental/package.json | 15 ++++++++++++++- packages/node/package.json | 15 ++++++++++++++- packages/opentelemetry-node/package.json | 15 ++++++++++++++- packages/opentelemetry/package.json | 15 ++++++++++++++- packages/profiling-node/package.json | 9 +++++++++ packages/react/package.json | 15 ++++++++++++++- packages/remix/package.json | 15 +++++++++++++-- packages/replay-canvas/package.json | 15 ++++++++++++++- packages/replay/package.json | 15 ++++++++++++++- packages/serverless/package.json | 15 ++++++++++++++- packages/svelte/package.json | 15 ++++++++++++++- packages/sveltekit/package.json | 2 +- packages/tracing-internal/package.json | 15 ++++++++++++++- packages/types/package.json | 15 ++++++++++++++- packages/utils/package.json | 15 ++++++++++++++- packages/vercel-edge/package.json | 15 ++++++++++++++- packages/vue/package.json | 15 ++++++++++++++- packages/wasm/package.json | 15 ++++++++++++++- 30 files changed, 357 insertions(+), 66 deletions(-) delete mode 100644 packages/nextjs/scripts/buildRollup.ts diff --git a/dev-packages/rollup-utils/npmHelpers.mjs b/dev-packages/rollup-utils/npmHelpers.mjs index 9b309fb2f59c..c42b4db2c763 100644 --- a/dev-packages/rollup-utils/npmHelpers.mjs +++ b/dev-packages/rollup-utils/npmHelpers.mjs @@ -118,14 +118,18 @@ export function makeBaseNPMConfig(options = {}) { } export function makeNPMConfigVariants(baseConfig, options = {}) { - const { emitMjs = true } = options; + const { emitMjs = true, entryFileNameWithoutExtension = '[name]'} = options; const variantSpecificConfigs = [ { output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs') } }, { output: { format: 'esm', dir: path.join(baseConfig.output.dir, 'esm') } }, ]; if (emitMjs) { - variantSpecificConfigs[1].output.entryFileNames = '[name].mjs'; + variantSpecificConfigs[1].output.entryFileNames = `${entryFileNameWithoutExtension}.mjs`; + } + + if (entryFileNameWithoutExtension !== '[name]') { + variantSpecificConfigs[0].output.entryFileNames = `${entryFileNameWithoutExtension}.js`; } return variantSpecificConfigs.map(variant => deepMerge(baseConfig, variant)); diff --git a/packages/astro/package.json b/packages/astro/package.json index 57db17ae274e..4a09165b527d 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -24,20 +24,20 @@ "types-ts3.8" ], "main": "build/cjs/index.client.js", - "module": "build/esm/index.server.js", - "browser": "build/esm/index.client.js", + "module": "build/esm/index.server.mjs", + "browser": "build/esm/index.client.mjs", "types": "build/types/index.types.d.ts", "exports": { ".": { - "node": "./build/esm/index.server.js", - "browser": "./build/esm/index.client.js", - "import": "./build/esm/index.client.js", + "node": "./build/esm/index.server.mjs", + "browser": "./build/esm/index.client.mjs", + "import": "./build/esm/index.client.mjs", "require": "./build/cjs/index.server.js", "types": "./build/types/index.types.d.ts" }, "./middleware": { - "node": "./build/esm/integration/middleware/index.js", - "import": "./build/esm/integration/middleware/index.js", + "node": "./build/esm/integration/middleware/index.mjs", + "import": "./build/esm/integration/middleware/index.mjs", "require": "./build/cjs/integration/middleware/index.js", "types": "./build/types/integration/middleware/index.types.d.ts" } diff --git a/packages/browser/package.json b/packages/browser/package.json index f7097fe6a000..95fb514193be 100644 --- a/packages/browser/package.json +++ b/packages/browser/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/npm/cjs/index.js", - "module": "build/npm/esm/index.js", + "module": "build/npm/esm/index.mjs", "types": "build/npm/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/esm/index.mjs" + }, + "require": { + "types": "./build/npm.types/index.d.ts", + "default": "./build/npm/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/npm/types/index.d.ts": [ diff --git a/packages/bun/package.json b/packages/bun/package.json index 4d5f650e8a8d..5ec3ed5233f2 100644 --- a/packages/bun/package.json +++ b/packages/bun/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/esm/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/npm/types/index.d.ts": [ diff --git a/packages/core/package.json b/packages/core/package.json index d4f2e8dfef9d..687dbd26c2ae 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/deno/package.json b/packages/deno/package.json index e7cf68aabb32..062b7f205c22 100644 --- a/packages/deno/package.json +++ b/packages/deno/package.json @@ -8,6 +8,15 @@ "license": "MIT", "module": "build/index.mjs", "types": "build/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/index.d.ts", + "default": "./build/index.mjs" + } + } + }, "publishConfig": { "access": "public" }, diff --git a/packages/feedback/package.json b/packages/feedback/package.json index 098a399d7ddd..4e285562975f 100644 --- a/packages/feedback/package.json +++ b/packages/feedback/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/npm/cjs/index.js", - "module": "build/npm/esm/index.js", + "module": "build/npm/esm/index.mjs", "types": "build/npm/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/esm/index.mjs" + }, + "require": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/npm/types/index.d.ts": [ diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index cd3e198cf20e..1d5e10de9576 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -24,8 +24,21 @@ "gatsby-node.d.ts" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/integration-shims/package.json b/packages/integration-shims/package.json index 63b9f8e73f55..35fae9e46716 100644 --- a/packages/integration-shims/package.json +++ b/packages/integration-shims/package.json @@ -3,8 +3,21 @@ "version": "7.100.0", "description": "Shims for integrations in Sentry SDK.", "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 2e7fc83b46b7..46c7b0e61167 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -10,9 +10,20 @@ "node": ">=14.8" }, "main": "build/cjs/index.server.js", - "module": "build/esm/index.server.js", - "browser": "build/esm/index.client.js", + "module": "build/esm/index.server.mjs", + "browser": "build/esm/index.client.mjs", "types": "build/types/index.types.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "browser": { + "import": "./build/esm/index.client.mjs", + "require": "./build/cjs/index.client.js" + }, + "node": "./build/cjs/index.server.js", + "types": "./build/types/index.types.d.ts" + } + }, "typesVersions": { "<4.9": { "build/npm/types/index.d.ts": [ @@ -56,7 +67,7 @@ "scripts": { "build": "run-p build:transpile build:types", "build:dev": "yarn build", - "build:transpile": "ts-node scripts/buildRollup.ts", + "build:transpile": "rollup -c rollup.npm.config.mjs", "build:types": "run-s build:types:core build:types:downlevel", "build:types:core": "tsc -p tsconfig.types.json", "build:types:downlevel": "yarn downlevel-dts build/types build/types-ts3.8 --to ts3.8", diff --git a/packages/nextjs/rollup.npm.config.mjs b/packages/nextjs/rollup.npm.config.mjs index 39b79c9593b2..231d8c97dfc0 100644 --- a/packages/nextjs/rollup.npm.config.mjs +++ b/packages/nextjs/rollup.npm.config.mjs @@ -35,9 +35,6 @@ export default [ packageSpecificConfig: { output: { - // Preserve the original file structure (i.e., so that everything is still relative to `src`) - entryFileNames: 'config/templates/[name].js', - // this is going to be add-on code, so it doesn't need the trappings of a full module (and in fact actively // shouldn't have them, lest they muck with the module to which we're adding it) sourcemap: false, @@ -55,6 +52,8 @@ export default [ ], }, }), + // Preserve the original file structure (i.e., so that everything is still relative to `src`) + { entryFileNameWithoutExtension: 'config/templates/[name]' } ), ...makeNPMConfigVariants( makeBaseNPMConfig({ @@ -62,14 +61,13 @@ export default [ packageSpecificConfig: { output: { - // Preserve the original file structure (i.e., so that everything is still relative to `src`) - entryFileNames: 'config/loaders/[name].js', - // make it so Rollup calms down about the fact that we're combining default and named exports exports: 'named', }, external: ['@rollup/plugin-commonjs', 'rollup'], }, }), + // Preserve the original file structure (i.e., so that everything is still relative to `src`) + { entryFileNameWithoutExtension: 'config/loaders/[name]' } ), ]; diff --git a/packages/nextjs/scripts/buildRollup.ts b/packages/nextjs/scripts/buildRollup.ts deleted file mode 100644 index d273146b872d..000000000000 --- a/packages/nextjs/scripts/buildRollup.ts +++ /dev/null @@ -1,24 +0,0 @@ -import * as childProcess from 'child_process'; -import * as fs from 'fs'; -import * as path from 'path'; - -/** - * Run the given shell command, piping the shell process's `stdin`, `stdout`, and `stderr` to that of the current - * process. Returns contents of `stdout`. - */ -function run(cmd: string, options?: childProcess.ExecSyncOptions): string | Buffer { - return childProcess.execSync(cmd, { stdio: 'inherit', ...options }); -} - -run('yarn rollup -c rollup.npm.config.mjs'); - -// Regardless of whether nextjs is using the CJS or ESM version of our SDK, we want the code from our templates to be in -// ESM (since we'll be adding it onto page files which are themselves written in ESM), so copy the ESM versions of the -// templates over into the CJS build directory. (Building only the ESM version and sticking it in both locations is -// something which in theory Rollup could do, but it would mean refactoring our Rollup helper functions, which isn't -// worth it just for this.) -const cjsTemplateDir = 'build/cjs/config/templates/'; -const esmTemplateDir = 'build/esm/config/templates/'; -fs.readdirSync(esmTemplateDir).forEach(templateFile => - fs.copyFileSync(path.join(esmTemplateDir, templateFile), path.join(cjsTemplateDir, templateFile)), -); diff --git a/packages/node-experimental/package.json b/packages/node-experimental/package.json index 4f4a6c7e006d..7850a1706fb5 100644 --- a/packages/node-experimental/package.json +++ b/packages/node-experimental/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/node/package.json b/packages/node/package.json index 6d85b240143b..162b474f2b93 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/opentelemetry-node/package.json b/packages/opentelemetry-node/package.json index 304f0a08d1db..e5c70d8d177b 100644 --- a/packages/opentelemetry-node/package.json +++ b/packages/opentelemetry-node/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/opentelemetry/package.json b/packages/opentelemetry/package.json index 6d16536dba84..10e45a548b79 100644 --- a/packages/opentelemetry/package.json +++ b/packages/opentelemetry/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/profiling-node/package.json b/packages/profiling-node/package.json index 659f926b4738..3c09fbeadaa2 100644 --- a/packages/profiling-node/package.json +++ b/packages/profiling-node/package.json @@ -8,6 +8,15 @@ "license": "MIT", "main": "lib/index.js", "types": "lib/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "require": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + } + } + }, "typesVersions": { "<4.9": { "lib/types/index.d.ts": [ diff --git a/packages/react/package.json b/packages/react/package.json index ec881e24d60c..697f76bac40f 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/remix/package.json b/packages/remix/package.json index c8a17f26cd6f..3cdf64fafa55 100644 --- a/packages/remix/package.json +++ b/packages/remix/package.json @@ -20,9 +20,20 @@ "scripts" ], "main": "build/cjs/index.server.js", - "module": "build/esm/index.server.js", - "browser": "build/esm/index.client.js", + "module": "build/esm/index.server.mjs", + "browser": "build/esm/index.client.mjs", "types": "build/types/index.types.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "browser": { + "import": "./build/esm/index.client.mjs", + "require": "./build/cjs/index.client.js" + }, + "node": "./build/cjs/index.server.js", + "types": "./build/types/index.types.d.ts" + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/replay-canvas/package.json b/packages/replay-canvas/package.json index c2e4b24cd5d1..e690ab7fe8e3 100644 --- a/packages/replay-canvas/package.json +++ b/packages/replay-canvas/package.json @@ -3,8 +3,21 @@ "version": "7.100.0", "description": "Replay canvas integration", "main": "build/npm/cjs/index.js", - "module": "build/npm/esm/index.js", + "module": "build/npm/esm/index.mjs", "types": "build/npm/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/esm/index.mjs" + }, + "require": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/npm/types/index.d.ts": [ diff --git a/packages/replay/package.json b/packages/replay/package.json index a362a4c59ea5..49b23cacca44 100644 --- a/packages/replay/package.json +++ b/packages/replay/package.json @@ -3,8 +3,21 @@ "version": "7.100.0", "description": "User replays for Sentry", "main": "build/npm/cjs/index.js", - "module": "build/npm/esm/index.js", + "module": "build/npm/esm/index.mjs", "types": "build/npm/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/esm/index.mjs" + }, + "require": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/npm/types/index.d.ts": [ diff --git a/packages/serverless/package.json b/packages/serverless/package.json index f1ec56e22eae..51ac34009220 100644 --- a/packages/serverless/package.json +++ b/packages/serverless/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/npm/cjs/index.js", - "module": "build/npm/esm/index.js", + "module": "build/npm/esm/index.mjs", "types": "build/npm/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/esm/index.mjs" + }, + "require": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/npm/types/index.d.ts": [ diff --git a/packages/svelte/package.json b/packages/svelte/package.json index c76ab617d367..ba3fa54db853 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/sveltekit/package.json b/packages/sveltekit/package.json index ec48fab5eb13..71b0c694319a 100644 --- a/packages/sveltekit/package.json +++ b/packages/sveltekit/package.json @@ -23,7 +23,7 @@ "./package.json": "./package.json", ".": { "browser": { - "import": "./build/esm/index.client.js", + "import": "./build/esm/index.client.mjs", "require": "./build/cjs/index.client.js" }, "node": "./build/cjs/index.server.js", diff --git a/packages/tracing-internal/package.json b/packages/tracing-internal/package.json index c87e139794ee..08db268ff0f4 100644 --- a/packages/tracing-internal/package.json +++ b/packages/tracing-internal/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/types/package.json b/packages/types/package.json index e53e1e44ad1b..09db14d6e7e7 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/utils/package.json b/packages/utils/package.json index 03b75c319953..9a0c9f90dca4 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/vercel-edge/package.json b/packages/vercel-edge/package.json index 19e817c039bd..3607628567dc 100644 --- a/packages/vercel-edge/package.json +++ b/packages/vercel-edge/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/vue/package.json b/packages/vue/package.json index 96ab7df7a099..2681399a4dda 100644 --- a/packages/vue/package.json +++ b/packages/vue/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/cjs/index.js", - "module": "build/esm/index.js", + "module": "build/esm/index.mjs", "types": "build/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/types/index.d.ts", + "default": "./build/esm/index.mjs" + }, + "require": { + "types": "./build/types/index.d.ts", + "default": "./build/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/types/index.d.ts": [ diff --git a/packages/wasm/package.json b/packages/wasm/package.json index 7942e4a2a5e4..76ff345d5a1e 100644 --- a/packages/wasm/package.json +++ b/packages/wasm/package.json @@ -16,8 +16,21 @@ "types-ts3.8" ], "main": "build/npm/cjs/index.js", - "module": "build/npm/esm/index.js", + "module": "build/npm/esm/index.mjs", "types": "build/npm/types/index.d.ts", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/esm/index.mjs" + }, + "require": { + "types": "./build/npm/types/index.d.ts", + "default": "./build/npm/cjs/index.js" + } + } + }, "typesVersions": { "<4.9": { "build/npm/types/index.d.ts": [ From 921c2836845846a096cbecc67570057ec6a1ca74 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 28 Feb 2024 11:56:52 -0500 Subject: [PATCH 03/12] fix size limit --- .size-limit.js | 34 ++++++++++++++---------- dev-packages/rollup-utils/npmHelpers.mjs | 2 +- packages/nextjs/rollup.npm.config.mjs | 4 +-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/.size-limit.js b/.size-limit.js index 739c3f62aae1..b01242b3ffb2 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -2,28 +2,28 @@ module.exports = [ // Main browser webpack builds { name: '@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Replay, browserTracingIntegration, Feedback }', gzip: true, limit: '90 KB', }, { name: '@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Replay, browserTracingIntegration }', gzip: true, limit: '75 KB', }, { name: '@sentry/browser (incl. Tracing, Replay with Canvas) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Replay, browserTracingIntegration, ReplayCanvas }', gzip: true, limit: '90 KB', }, { name: '@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Replay, browserTracingIntegration }', gzip: true, limit: '75 KB', @@ -42,35 +42,35 @@ module.exports = [ }, { name: '@sentry/browser (incl. Tracing) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, browserTracingIntegration }', gzip: true, limit: '35 KB', }, { name: '@sentry/browser (incl. browserTracingIntegration) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, browserTracingIntegration }', gzip: true, limit: '35 KB', }, { name: '@sentry/browser (incl. Feedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Feedback }', gzip: true, limit: '50 KB', }, { name: '@sentry/browser (incl. sendFeedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, sendFeedback }', gzip: true, limit: '50 KB', }, { name: '@sentry/browser - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init }', gzip: true, limit: '28 KB', @@ -137,37 +137,43 @@ module.exports = [ // React { name: '@sentry/react (incl. Tracing, Replay) - Webpack (gzipped)', - path: 'packages/react/build/esm/index.js', + path: 'packages/react/build/esm/index.mjs', import: '{ init, browserTracingIntegration, Replay }', gzip: true, limit: '75 KB', }, { name: '@sentry/react - Webpack (gzipped)', - path: 'packages/react/build/esm/index.js', + path: 'packages/react/build/esm/index.mjs', import: '{ init }', gzip: true, limit: '30 KB', }, // Next.js + // We ignore next/router and next/constants because they break size-limit calculation + // with new *.mjs bundles in v8 { name: '@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped)', - path: 'packages/nextjs/build/esm/client/index.js', + path: 'packages/nextjs/build/esm/client/index.mjs', import: '{ init, browserTracingIntegration, Replay }', + ignore: ['next/router', 'next/constants'], gzip: true, limit: '110 KB', }, { name: '@sentry/nextjs Client - Webpack (gzipped)', - path: 'packages/nextjs/build/esm/client/index.js', + path: 'packages/nextjs/build/esm/client/index.mjs', import: '{ init }', + ignore: ['next/router', 'next/constants'], gzip: true, limit: '57 KB', }, + + // Feedback { name: '@sentry-internal/feedback - Webpack (gzipped)', - path: 'packages/feedback/build/npm/esm/index.js', + path: 'packages/feedback/build/npm/esm/index.mjs', import: '{ Feedback }', gzip: true, limit: '25 KB', diff --git a/dev-packages/rollup-utils/npmHelpers.mjs b/dev-packages/rollup-utils/npmHelpers.mjs index c42b4db2c763..3e2c98401a6f 100644 --- a/dev-packages/rollup-utils/npmHelpers.mjs +++ b/dev-packages/rollup-utils/npmHelpers.mjs @@ -118,7 +118,7 @@ export function makeBaseNPMConfig(options = {}) { } export function makeNPMConfigVariants(baseConfig, options = {}) { - const { emitMjs = true, entryFileNameWithoutExtension = '[name]'} = options; + const { emitMjs = true, entryFileNameWithoutExtension = '[name]' } = options; const variantSpecificConfigs = [ { output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs') } }, { output: { format: 'esm', dir: path.join(baseConfig.output.dir, 'esm') } }, diff --git a/packages/nextjs/rollup.npm.config.mjs b/packages/nextjs/rollup.npm.config.mjs index 231d8c97dfc0..82d76504ccf1 100644 --- a/packages/nextjs/rollup.npm.config.mjs +++ b/packages/nextjs/rollup.npm.config.mjs @@ -53,7 +53,7 @@ export default [ }, }), // Preserve the original file structure (i.e., so that everything is still relative to `src`) - { entryFileNameWithoutExtension: 'config/templates/[name]' } + { entryFileNameWithoutExtension: 'config/templates/[name]' }, ), ...makeNPMConfigVariants( makeBaseNPMConfig({ @@ -68,6 +68,6 @@ export default [ }, }), // Preserve the original file structure (i.e., so that everything is still relative to `src`) - { entryFileNameWithoutExtension: 'config/loaders/[name]' } + { entryFileNameWithoutExtension: 'config/loaders/[name]' }, ), ]; From 52e88c25e1964edf723336eab17b8529de1102bf Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 29 Feb 2024 18:18:38 +0000 Subject: [PATCH 04/12] Fix a load of ESM compatibility issues --- .../node-experimental/rollup.anr-worker.config.mjs | 2 +- .../node-experimental/src/integrations/anr/index.ts | 8 +------- .../node-experimental/src/integrations/modules.ts | 11 +++++++---- .../src/integrations/tracing/prisma.ts | 5 +++-- packages/node-experimental/tsconfig.json | 3 ++- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/node-experimental/rollup.anr-worker.config.mjs b/packages/node-experimental/rollup.anr-worker.config.mjs index 9887342c63fd..d7499b194efa 100644 --- a/packages/node-experimental/rollup.anr-worker.config.mjs +++ b/packages/node-experimental/rollup.anr-worker.config.mjs @@ -6,7 +6,7 @@ function createAnrWorkerConfig(destDir, esm) { entrypoints: ['src/integrations/anr/worker.ts'], jsVersion: 'es6', licenseTitle: '@sentry/node', - outputFileBase: () => 'worker-script.js', + outputFileBase: () => (esm ? 'worker-script.mjs' : 'worker-script.js'), packageSpecificConfig: { output: { dir: destDir, diff --git a/packages/node-experimental/src/integrations/anr/index.ts b/packages/node-experimental/src/integrations/anr/index.ts index 2670f30db558..682a1a66d002 100644 --- a/packages/node-experimental/src/integrations/anr/index.ts +++ b/packages/node-experimental/src/integrations/anr/index.ts @@ -30,11 +30,6 @@ async function getContexts(client: NodeClient): Promise { return event?.contexts || {}; } -interface InspectorApi { - open: (port: number) => void; - url: () => string | undefined; -} - const INTEGRATION_NAME = 'Anr'; const _anrIntegration = ((options: Partial = {}) => { @@ -91,8 +86,7 @@ async function _startWorker(client: NodeClient, _options: Partial { return { name: INTEGRATION_NAME, processEvent(event) { - event.modules = { - ...event.modules, - ..._getModules(), - }; + // If we're running in ESM mode, we don't have access to require + if (typeof require !== 'undefined') { + event.modules = { + ...event.modules, + ..._getModules(), + }; + } return event; }, diff --git a/packages/node-experimental/src/integrations/tracing/prisma.ts b/packages/node-experimental/src/integrations/tracing/prisma.ts index 1f78262b9a3c..b104329aabc6 100644 --- a/packages/node-experimental/src/integrations/tracing/prisma.ts +++ b/packages/node-experimental/src/integrations/tracing/prisma.ts @@ -1,5 +1,6 @@ import { registerInstrumentations } from '@opentelemetry/instrumentation'; -import { PrismaInstrumentation } from '@prisma/instrumentation'; +// This is a cjs module, so have to import like this +import * as prismaInstrumentation from '@prisma/instrumentation'; import { defineIntegration } from '@sentry/core'; import type { IntegrationFn } from '@sentry/types'; @@ -10,7 +11,7 @@ const _prismaIntegration = (() => { registerInstrumentations({ instrumentations: [ // does not have a hook to adjust spans & add origin - new PrismaInstrumentation({}), + new prismaInstrumentation.PrismaInstrumentation({}), ], }); }, diff --git a/packages/node-experimental/tsconfig.json b/packages/node-experimental/tsconfig.json index 5fc0658105eb..737a316bff40 100644 --- a/packages/node-experimental/tsconfig.json +++ b/packages/node-experimental/tsconfig.json @@ -4,6 +4,7 @@ "include": ["src/**/*"], "compilerOptions": { - "lib": ["es6"] + "lib": ["es6"], + "module": "Node16" } } From 442b935277fcc5d0b0ac700a1828556d3bc67ba2 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 29 Feb 2024 22:11:19 +0000 Subject: [PATCH 05/12] Fix more ESM incompatibilities --- .../local-variables/local-variables-async.ts | 277 ------------------ .../local-variables/local-variables-sync.ts | 19 +- .../src/integrations/node-fetch.ts | 26 +- 3 files changed, 15 insertions(+), 307 deletions(-) delete mode 100644 packages/node-experimental/src/integrations/local-variables/local-variables-async.ts diff --git a/packages/node-experimental/src/integrations/local-variables/local-variables-async.ts b/packages/node-experimental/src/integrations/local-variables/local-variables-async.ts deleted file mode 100644 index baeeb4866be4..000000000000 --- a/packages/node-experimental/src/integrations/local-variables/local-variables-async.ts +++ /dev/null @@ -1,277 +0,0 @@ -import type { Session } from 'node:inspector/promises'; -import { convertIntegrationFnToClass, defineIntegration } from '@sentry/core'; -import type { Event, Exception, Integration, IntegrationClass, IntegrationFn, StackParser } from '@sentry/types'; -import { LRUMap, dynamicRequire, logger } from '@sentry/utils'; -import type { Debugger, InspectorNotification, Runtime } from 'inspector'; - -import type { NodeClient } from '../../sdk/client'; -import type { NodeClientOptions } from '../../types'; -import type { - FrameVariables, - LocalVariablesIntegrationOptions, - PausedExceptionEvent, - RateLimitIncrement, - Variables, -} from './common'; -import { createRateLimiter, functionNamesMatch, hashFrames, hashFromStack } from './common'; - -async function unrollArray(session: Session, objectId: string, name: string, vars: Variables): Promise { - const properties: Runtime.GetPropertiesReturnType = await session.post('Runtime.getProperties', { - objectId, - ownProperties: true, - }); - - vars[name] = properties.result - .filter(v => v.name !== 'length' && !isNaN(parseInt(v.name, 10))) - .sort((a, b) => parseInt(a.name, 10) - parseInt(b.name, 10)) - .map(v => v.value?.value); -} - -async function unrollObject(session: Session, objectId: string, name: string, vars: Variables): Promise { - const properties: Runtime.GetPropertiesReturnType = await session.post('Runtime.getProperties', { - objectId, - ownProperties: true, - }); - - vars[name] = properties.result - .map<[string, unknown]>(v => [v.name, v.value?.value]) - .reduce((obj, [key, val]) => { - obj[key] = val; - return obj; - }, {} as Variables); -} - -function unrollOther(prop: Runtime.PropertyDescriptor, vars: Variables): void { - if (!prop.value) { - return; - } - - if ('value' in prop.value) { - if (prop.value.value === undefined || prop.value.value === null) { - vars[prop.name] = `<${prop.value.value}>`; - } else { - vars[prop.name] = prop.value.value; - } - } else if ('description' in prop.value && prop.value.type !== 'function') { - vars[prop.name] = `<${prop.value.description}>`; - } else if (prop.value.type === 'undefined') { - vars[prop.name] = ''; - } -} - -async function getLocalVariables(session: Session, objectId: string): Promise { - const properties: Runtime.GetPropertiesReturnType = await session.post('Runtime.getProperties', { - objectId, - ownProperties: true, - }); - const variables = {}; - - for (const prop of properties.result) { - if (prop?.value?.objectId && prop?.value.className === 'Array') { - const id = prop.value.objectId; - await unrollArray(session, id, prop.name, variables); - } else if (prop?.value?.objectId && prop?.value?.className === 'Object') { - const id = prop.value.objectId; - await unrollObject(session, id, prop.name, variables); - } else if (prop?.value) { - unrollOther(prop, variables); - } - } - - return variables; -} - -const INTEGRATION_NAME = 'LocalVariablesAsync'; - -/** - * Adds local variables to exception frames - */ -const _localVariablesAsyncIntegration = ((options: LocalVariablesIntegrationOptions = {}) => { - const cachedFrames: LRUMap = new LRUMap(20); - let rateLimiter: RateLimitIncrement | undefined; - let shouldProcessEvent = false; - - async function handlePaused( - session: Session, - stackParser: StackParser, - { reason, data, callFrames }: PausedExceptionEvent, - ): Promise { - if (reason !== 'exception' && reason !== 'promiseRejection') { - return; - } - - rateLimiter?.(); - - // data.description contains the original error.stack - const exceptionHash = hashFromStack(stackParser, data?.description); - - if (exceptionHash == undefined) { - return; - } - - const frames = []; - - for (let i = 0; i < callFrames.length; i++) { - const { scopeChain, functionName, this: obj } = callFrames[i]; - - const localScope = scopeChain.find(scope => scope.type === 'local'); - - // obj.className is undefined in ESM modules - const fn = obj.className === 'global' || !obj.className ? functionName : `${obj.className}.${functionName}`; - - if (localScope?.object.objectId === undefined) { - frames[i] = { function: fn }; - } else { - const vars = await getLocalVariables(session, localScope.object.objectId); - frames[i] = { function: fn, vars }; - } - } - - cachedFrames.set(exceptionHash, frames); - } - - async function startDebugger(session: Session, clientOptions: NodeClientOptions): Promise { - session.connect(); - - let isPaused = false; - - session.on('Debugger.resumed', () => { - isPaused = false; - }); - - session.on('Debugger.paused', (event: InspectorNotification) => { - isPaused = true; - - handlePaused(session, clientOptions.stackParser, event.params as PausedExceptionEvent).then( - () => { - // After the pause work is complete, resume execution! - return isPaused ? session.post('Debugger.resume') : Promise.resolve(); - }, - _ => { - // ignore - }, - ); - }); - - await session.post('Debugger.enable'); - - const captureAll = options.captureAllExceptions !== false; - await session.post('Debugger.setPauseOnExceptions', { state: captureAll ? 'all' : 'uncaught' }); - - if (captureAll) { - const max = options.maxExceptionsPerSecond || 50; - - rateLimiter = createRateLimiter( - max, - () => { - logger.log('Local variables rate-limit lifted.'); - return session.post('Debugger.setPauseOnExceptions', { state: 'all' }); - }, - seconds => { - logger.log( - `Local variables rate-limit exceeded. Disabling capturing of caught exceptions for ${seconds} seconds.`, - ); - return session.post('Debugger.setPauseOnExceptions', { state: 'uncaught' }); - }, - ); - } - - shouldProcessEvent = true; - } - - function addLocalVariablesToException(exception: Exception): void { - const hash = hashFrames(exception.stacktrace?.frames); - - if (hash === undefined) { - return; - } - - // Check if we have local variables for an exception that matches the hash - // remove is identical to get but also removes the entry from the cache - const cachedFrame = cachedFrames.remove(hash); - - if (cachedFrame === undefined) { - return; - } - - const frameCount = exception.stacktrace?.frames?.length || 0; - - for (let i = 0; i < frameCount; i++) { - // Sentry frames are in reverse order - const frameIndex = frameCount - i - 1; - - // Drop out if we run out of frames to match up - if (!exception.stacktrace?.frames?.[frameIndex] || !cachedFrame[i]) { - break; - } - - if ( - // We need to have vars to add - cachedFrame[i].vars === undefined || - // We're not interested in frames that are not in_app because the vars are not relevant - exception.stacktrace.frames[frameIndex].in_app === false || - // The function names need to match - !functionNamesMatch(exception.stacktrace.frames[frameIndex].function, cachedFrame[i].function) - ) { - continue; - } - - exception.stacktrace.frames[frameIndex].vars = cachedFrame[i].vars; - } - } - - function addLocalVariablesToEvent(event: Event): Event { - for (const exception of event.exception?.values || []) { - addLocalVariablesToException(exception); - } - - return event; - } - - return { - name: INTEGRATION_NAME, - setup(client: NodeClient) { - const clientOptions = client.getOptions(); - - if (!clientOptions.includeLocalVariables) { - return; - } - - try { - // TODO: Use import()... - // It would be nice to use import() here, but this built-in library is not in Node <19 so webpack will pick it - // up and report it as a missing dependency - const { Session } = dynamicRequire(module, 'node:inspector/promises'); - - startDebugger(new Session(), clientOptions).catch(e => { - logger.error('Failed to start inspector session', e); - }); - } catch (e) { - logger.error('Failed to load inspector API', e); - return; - } - }, - processEvent(event: Event): Event { - if (shouldProcessEvent) { - return addLocalVariablesToEvent(event); - } - - return event; - }, - }; -}) satisfies IntegrationFn; - -export const localVariablesAsyncIntegration = defineIntegration(_localVariablesAsyncIntegration); - -/** - * Adds local variables to exception frames. - * @deprecated Use `localVariablesAsyncIntegration()` instead. - */ -// eslint-disable-next-line deprecation/deprecation -export const LocalVariablesAsync = convertIntegrationFnToClass( - INTEGRATION_NAME, - localVariablesAsyncIntegration, -) as IntegrationClass Event; setup: (client: NodeClient) => void }>; - -// eslint-disable-next-line deprecation/deprecation -export type LocalVariablesAsync = typeof LocalVariablesAsync; diff --git a/packages/node-experimental/src/integrations/local-variables/local-variables-sync.ts b/packages/node-experimental/src/integrations/local-variables/local-variables-sync.ts index 1b8d3c356187..d17cb3282b81 100644 --- a/packages/node-experimental/src/integrations/local-variables/local-variables-sync.ts +++ b/packages/node-experimental/src/integrations/local-variables/local-variables-sync.ts @@ -1,7 +1,8 @@ import { convertIntegrationFnToClass, defineIntegration, getClient } from '@sentry/core'; import type { Event, Exception, Integration, IntegrationClass, IntegrationFn, StackParser } from '@sentry/types'; import { LRUMap, logger } from '@sentry/utils'; -import type { Debugger, InspectorNotification, Runtime, Session } from 'inspector'; +import type { Debugger, InspectorNotification, Runtime } from 'inspector'; +import { Session } from 'inspector'; import { NODE_MAJOR } from '../../nodeVersion'; import type { NodeClient } from '../../sdk/client'; @@ -78,22 +79,6 @@ class AsyncSession implements DebugSession { /** Throws if inspector API is not available */ public constructor() { - /* - TODO: We really should get rid of this require statement below for a couple of reasons: - 1. It makes the integration unusable in the SvelteKit SDK, as it's not possible to use `require` - in SvelteKit server code (at least not by default). - 2. Throwing in a constructor is bad practice - - More context for a future attempt to fix this: - We already tried replacing it with import but didn't get it to work because of async problems. - We still called import in the constructor but assigned to a promise which we "awaited" in - `configureAndConnect`. However, this broke the Node integration tests as no local variables - were reported any more. We probably missed a place where we need to await the promise, too. - */ - - // Node can be built without inspector support so this can throw - // eslint-disable-next-line @typescript-eslint/no-var-requires - const { Session } = require('inspector'); this._session = new Session(); } diff --git a/packages/node-experimental/src/integrations/node-fetch.ts b/packages/node-experimental/src/integrations/node-fetch.ts index 254b3f93e930..a80d015515bb 100644 --- a/packages/node-experimental/src/integrations/node-fetch.ts +++ b/packages/node-experimental/src/integrations/node-fetch.ts @@ -27,22 +27,20 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => { const _breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs; const _ignoreOutgoingRequests = options.ignoreOutgoingRequests; - function getInstrumentation(): [Instrumentation] | void { + async function getInstrumentation(): Promise<[Instrumentation] | void> { // Only add NodeFetch if Node >= 16, as previous versions do not support it if (NODE_MAJOR < 16) { return; } try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const { FetchInstrumentation } = require('opentelemetry-instrumentation-fetch-node'); + const pkg = await import('opentelemetry-instrumentation-fetch-node'); return [ - new FetchInstrumentation({ + new pkg.FetchInstrumentation({ ignoreRequestHook: (request: { origin?: string }) => { const url = request.origin; return _ignoreOutgoingRequests && url && _ignoreOutgoingRequests(url); }, - onRequest: ({ span }: { span: Span }) => { _updateSpan(span); @@ -50,7 +48,8 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => { _addRequestBreadcrumb(span); } }, - }), + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } as any), ]; } catch (error) { // Could not load instrumentation @@ -60,13 +59,14 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => { return { name: 'NodeFetch', setupOnce() { - const instrumentations = getInstrumentation(); - - if (instrumentations) { - registerInstrumentations({ - instrumentations, - }); - } + // eslint-disable-next-line @typescript-eslint/no-floating-promises + getInstrumentation().then(instrumentations => { + if (instrumentations) { + registerInstrumentations({ + instrumentations, + }); + } + }); }, }; }) satisfies IntegrationFn; From 0c57b1155873f03ab55dddba5a0442bf01f490d9 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 1 Mar 2024 12:34:18 +0000 Subject: [PATCH 06/12] can't import opentelemetry-instrumentation-fetch-node because it breaks rollup --- packages/node-experimental/src/integrations/node-fetch.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/node-experimental/src/integrations/node-fetch.ts b/packages/node-experimental/src/integrations/node-fetch.ts index a80d015515bb..4c41a7f87057 100644 --- a/packages/node-experimental/src/integrations/node-fetch.ts +++ b/packages/node-experimental/src/integrations/node-fetch.ts @@ -34,9 +34,10 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => { } try { - const pkg = await import('opentelemetry-instrumentation-fetch-node'); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { FetchInstrumentation } = require('opentelemetry-instrumentation-fetch-node'); return [ - new pkg.FetchInstrumentation({ + new FetchInstrumentation({ ignoreRequestHook: (request: { origin?: string }) => { const url = request.origin; return _ignoreOutgoingRequests && url && _ignoreOutgoingRequests(url); From 136f396e3fd6713fc8a4d98d25ec56219043d151 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 1 Mar 2024 12:48:42 +0000 Subject: [PATCH 07/12] Remove `nativeNodeFetchIntegration` from default integrations --- packages/node-experimental/src/sdk/init.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/node-experimental/src/sdk/init.ts b/packages/node-experimental/src/sdk/init.ts index 80b69112f261..ba6185e228e6 100644 --- a/packages/node-experimental/src/sdk/init.ts +++ b/packages/node-experimental/src/sdk/init.ts @@ -28,7 +28,6 @@ import { contextLinesIntegration } from '../integrations/contextlines'; import { httpIntegration } from '../integrations/http'; import { localVariablesIntegration } from '../integrations/local-variables'; import { modulesIntegration } from '../integrations/modules'; -import { nativeNodeFetchIntegration } from '../integrations/node-fetch'; import { onUncaughtExceptionIntegration } from '../integrations/onuncaughtexception'; import { onUnhandledRejectionIntegration } from '../integrations/onunhandledrejection'; import { spotlightIntegration } from '../integrations/spotlight'; @@ -51,7 +50,6 @@ export function getDefaultIntegrations(options: Options): Integration[] { // Native Wrappers consoleIntegration(), httpIntegration(), - nativeNodeFetchIntegration(), // Global Handlers onUncaughtExceptionIntegration(), onUnhandledRejectionIntegration(), @@ -61,7 +59,6 @@ export function getDefaultIntegrations(options: Options): Integration[] { nodeContextIntegration(), modulesIntegration(), httpIntegration(), - nativeNodeFetchIntegration(), ...(hasTracingEnabled(options) ? getAutoPerformanceIntegrations() : []), ]; } From f8743f533f73e1a568674f64dd20a751896b578e Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 1 Mar 2024 14:17:23 +0000 Subject: [PATCH 08/12] Don't use mjs for browser --- packages/browser/package.json | 4 ++-- packages/browser/rollup.npm.config.mjs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/browser/package.json b/packages/browser/package.json index 9337bd4d5dd1..b8ff0444e55d 100644 --- a/packages/browser/package.json +++ b/packages/browser/package.json @@ -16,14 +16,14 @@ "types-ts3.8" ], "main": "build/npm/cjs/index.js", - "module": "build/npm/esm/index.mjs", + "module": "build/npm/esm/index.js", "types": "build/npm/types/index.d.ts", "exports": { "./package.json": "./package.json", ".": { "import": { "types": "./build/npm/types/index.d.ts", - "default": "./build/npm/esm/index.mjs" + "default": "./build/npm/esm/index.js" }, "require": { "types": "./build/npm.types/index.d.ts", diff --git a/packages/browser/rollup.npm.config.mjs b/packages/browser/rollup.npm.config.mjs index 6d09adefc859..5d4ac13d8688 100644 --- a/packages/browser/rollup.npm.config.mjs +++ b/packages/browser/rollup.npm.config.mjs @@ -5,4 +5,5 @@ export default makeNPMConfigVariants( // packages with bundles have a different build directory structure hasBundles: true, }), + { emitMjs: false }, ); From abaf959400c2d2178886eb39d569a9795425702f Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 1 Mar 2024 15:43:08 +0000 Subject: [PATCH 09/12] Don't use mjs for browser --- .size-limit.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.size-limit.js b/.size-limit.js index b01242b3ffb2..07a81a517f0d 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -2,28 +2,28 @@ module.exports = [ // Main browser webpack builds { name: '@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init, Replay, browserTracingIntegration, Feedback }', gzip: true, limit: '90 KB', }, { name: '@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init, Replay, browserTracingIntegration }', gzip: true, limit: '75 KB', }, { name: '@sentry/browser (incl. Tracing, Replay with Canvas) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init, Replay, browserTracingIntegration, ReplayCanvas }', gzip: true, limit: '90 KB', }, { name: '@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init, Replay, browserTracingIntegration }', gzip: true, limit: '75 KB', @@ -42,35 +42,35 @@ module.exports = [ }, { name: '@sentry/browser (incl. Tracing) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init, browserTracingIntegration }', gzip: true, limit: '35 KB', }, { name: '@sentry/browser (incl. browserTracingIntegration) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init, browserTracingIntegration }', gzip: true, limit: '35 KB', }, { name: '@sentry/browser (incl. Feedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init, Feedback }', gzip: true, limit: '50 KB', }, { name: '@sentry/browser (incl. sendFeedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init, sendFeedback }', gzip: true, limit: '50 KB', }, { name: '@sentry/browser - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.mjs', + path: 'packages/browser/build/npm/esm/index.js', import: '{ init }', gzip: true, limit: '28 KB', From e8ed2de712afa038691576f45380a93d37b96843 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 4 Mar 2024 14:58:21 +0000 Subject: [PATCH 10/12] Revert "Don't use mjs for browser" --- .size-limit.js | 18 +++++++++--------- packages/browser/package.json | 4 ++-- packages/browser/rollup.npm.config.mjs | 1 - 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/.size-limit.js b/.size-limit.js index 07a81a517f0d..b01242b3ffb2 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -2,28 +2,28 @@ module.exports = [ // Main browser webpack builds { name: '@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Replay, browserTracingIntegration, Feedback }', gzip: true, limit: '90 KB', }, { name: '@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Replay, browserTracingIntegration }', gzip: true, limit: '75 KB', }, { name: '@sentry/browser (incl. Tracing, Replay with Canvas) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Replay, browserTracingIntegration, ReplayCanvas }', gzip: true, limit: '90 KB', }, { name: '@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Replay, browserTracingIntegration }', gzip: true, limit: '75 KB', @@ -42,35 +42,35 @@ module.exports = [ }, { name: '@sentry/browser (incl. Tracing) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, browserTracingIntegration }', gzip: true, limit: '35 KB', }, { name: '@sentry/browser (incl. browserTracingIntegration) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, browserTracingIntegration }', gzip: true, limit: '35 KB', }, { name: '@sentry/browser (incl. Feedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, Feedback }', gzip: true, limit: '50 KB', }, { name: '@sentry/browser (incl. sendFeedback) - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init, sendFeedback }', gzip: true, limit: '50 KB', }, { name: '@sentry/browser - Webpack (gzipped)', - path: 'packages/browser/build/npm/esm/index.js', + path: 'packages/browser/build/npm/esm/index.mjs', import: '{ init }', gzip: true, limit: '28 KB', diff --git a/packages/browser/package.json b/packages/browser/package.json index b8ff0444e55d..9337bd4d5dd1 100644 --- a/packages/browser/package.json +++ b/packages/browser/package.json @@ -16,14 +16,14 @@ "types-ts3.8" ], "main": "build/npm/cjs/index.js", - "module": "build/npm/esm/index.js", + "module": "build/npm/esm/index.mjs", "types": "build/npm/types/index.d.ts", "exports": { "./package.json": "./package.json", ".": { "import": { "types": "./build/npm/types/index.d.ts", - "default": "./build/npm/esm/index.js" + "default": "./build/npm/esm/index.mjs" }, "require": { "types": "./build/npm.types/index.d.ts", diff --git a/packages/browser/rollup.npm.config.mjs b/packages/browser/rollup.npm.config.mjs index 5d4ac13d8688..6d09adefc859 100644 --- a/packages/browser/rollup.npm.config.mjs +++ b/packages/browser/rollup.npm.config.mjs @@ -5,5 +5,4 @@ export default makeNPMConfigVariants( // packages with bundles have a different build directory structure hasBundles: true, }), - { emitMjs: false }, ); From ba04d761f4585c4857ade1a4d8839c9f973c8f99 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 5 Mar 2024 11:02:11 +0000 Subject: [PATCH 11/12] Revert some changes and try nextjs paths --- packages/nextjs/package.json | 2 +- packages/nextjs/scripts/buildRollup.ts | 24 +++++++++++++++++++ .../src/config/loaders/wrappingLoader.ts | 12 +++++----- packages/nextjs/src/config/webpack.ts | 24 ++++++++++++------- packages/nextjs/test/config/loaders.test.ts | 4 ++-- .../nextjs/test/config/wrappingLoader.test.ts | 24 +++++++++---------- .../src/integrations/node-fetch.ts | 21 ++++++++-------- 7 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 packages/nextjs/scripts/buildRollup.ts diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 7c6cfc2b0b4e..741e47974572 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -67,7 +67,7 @@ "scripts": { "build": "run-p build:transpile build:types", "build:dev": "yarn build", - "build:transpile": "rollup -c rollup.npm.config.mjs", + "build:transpile": "ts-node scripts/buildRollup.ts", "build:types": "run-s build:types:core build:types:downlevel", "build:types:core": "tsc -p tsconfig.types.json", "build:types:downlevel": "yarn downlevel-dts build/types build/types-ts3.8 --to ts3.8", diff --git a/packages/nextjs/scripts/buildRollup.ts b/packages/nextjs/scripts/buildRollup.ts new file mode 100644 index 000000000000..d273146b872d --- /dev/null +++ b/packages/nextjs/scripts/buildRollup.ts @@ -0,0 +1,24 @@ +import * as childProcess from 'child_process'; +import * as fs from 'fs'; +import * as path from 'path'; + +/** + * Run the given shell command, piping the shell process's `stdin`, `stdout`, and `stderr` to that of the current + * process. Returns contents of `stdout`. + */ +function run(cmd: string, options?: childProcess.ExecSyncOptions): string | Buffer { + return childProcess.execSync(cmd, { stdio: 'inherit', ...options }); +} + +run('yarn rollup -c rollup.npm.config.mjs'); + +// Regardless of whether nextjs is using the CJS or ESM version of our SDK, we want the code from our templates to be in +// ESM (since we'll be adding it onto page files which are themselves written in ESM), so copy the ESM versions of the +// templates over into the CJS build directory. (Building only the ESM version and sticking it in both locations is +// something which in theory Rollup could do, but it would mean refactoring our Rollup helper functions, which isn't +// worth it just for this.) +const cjsTemplateDir = 'build/cjs/config/templates/'; +const esmTemplateDir = 'build/esm/config/templates/'; +fs.readdirSync(esmTemplateDir).forEach(templateFile => + fs.copyFileSync(path.join(esmTemplateDir, templateFile), path.join(cjsTemplateDir, templateFile)), +); diff --git a/packages/nextjs/src/config/loaders/wrappingLoader.ts b/packages/nextjs/src/config/loaders/wrappingLoader.ts index dab8c767c54f..e0af166c17ed 100644 --- a/packages/nextjs/src/config/loaders/wrappingLoader.ts +++ b/packages/nextjs/src/config/loaders/wrappingLoader.ts @@ -15,29 +15,29 @@ const SENTRY_WRAPPER_MODULE_NAME = 'sentry-wrapper-module'; // Needs to end in .cjs in order for the `commonjs` plugin to pick it up const WRAPPING_TARGET_MODULE_NAME = '__SENTRY_WRAPPING_TARGET_FILE__.cjs'; -const apiWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'apiWrapperTemplate.js'); +const apiWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'apiWrapperTemplate.mjs'); const apiWrapperTemplateCode = fs.readFileSync(apiWrapperTemplatePath, { encoding: 'utf8' }); -const pageWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'pageWrapperTemplate.js'); +const pageWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'pageWrapperTemplate.mjs'); const pageWrapperTemplateCode = fs.readFileSync(pageWrapperTemplatePath, { encoding: 'utf8' }); -const middlewareWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'middlewareWrapperTemplate.js'); +const middlewareWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'middlewareWrapperTemplate.mjs'); const middlewareWrapperTemplateCode = fs.readFileSync(middlewareWrapperTemplatePath, { encoding: 'utf8' }); let showedMissingAsyncStorageModuleWarning = false; -const sentryInitWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'sentryInitWrapperTemplate.js'); +const sentryInitWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'sentryInitWrapperTemplate.mjs'); const sentryInitWrapperTemplateCode = fs.readFileSync(sentryInitWrapperTemplatePath, { encoding: 'utf8' }); const serverComponentWrapperTemplatePath = path.resolve( __dirname, '..', 'templates', - 'serverComponentWrapperTemplate.js', + 'serverComponentWrapperTemplate.mjs', ); const serverComponentWrapperTemplateCode = fs.readFileSync(serverComponentWrapperTemplatePath, { encoding: 'utf8' }); -const routeHandlerWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'routeHandlerWrapperTemplate.js'); +const routeHandlerWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'routeHandlerWrapperTemplate.mjs'); const routeHandlerWrapperTemplateCode = fs.readFileSync(routeHandlerWrapperTemplatePath, { encoding: 'utf8' }); export type WrappingLoaderOptions = { diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index a6555c3b3343..1c15c84e4ee4 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -42,6 +42,12 @@ let showedHiddenSourceMapsWarningMsg = false; let showedMissingCliBinaryWarningMsg = false; let showedMissingGlobalErrorWarningMsg = false; +const ext = typeof require === 'undefined' ? 'mjs' : 'js'; + +const sdkMultiplexerLoader = path.resolve(__dirname, 'loaders', `sdkMultiplexerLoader.${ext}`); +const wrappingLoader = path.resolve(__dirname, 'loaders', `wrappingLoader.${ext}`); +const valueInjectionLoader = path.resolve(__dirname, 'loaders', `valueInjectionLoader.${ext}`); + // TODO: merge default SentryWebpackPlugin ignore with their SentryWebpackPlugin ignore or ignoreFile // TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include // TODO: drop merged keys from override check? `includeDefaults` option? @@ -92,7 +98,7 @@ export function constructWebpackConfigFunction( test: /node_modules[/\\]@sentry[/\\]nextjs/, use: [ { - loader: path.resolve(__dirname, 'loaders', 'sdkMultiplexerLoader.js'), + loader: sdkMultiplexerLoader, options: { importTarget: RUNTIME_TO_SDK_ENTRYPOINT_MAP[runtime], }, @@ -210,7 +216,7 @@ export function constructWebpackConfigFunction( test: isPageResource, use: [ { - loader: path.resolve(__dirname, 'loaders', 'wrappingLoader.js'), + loader: wrappingLoader, options: { ...staticWrappingLoaderOptions, wrappingTargetKind: 'page', @@ -248,7 +254,7 @@ export function constructWebpackConfigFunction( test: isApiRouteResource, use: [ { - loader: path.resolve(__dirname, 'loaders', 'wrappingLoader.js'), + loader: wrappingLoader, options: { ...staticWrappingLoaderOptions, vercelCronsConfig, @@ -264,7 +270,7 @@ export function constructWebpackConfigFunction( test: isMiddlewareResource, use: [ { - loader: path.resolve(__dirname, 'loaders', 'wrappingLoader.js'), + loader: wrappingLoader, options: { ...staticWrappingLoaderOptions, wrappingTargetKind: 'middleware', @@ -281,7 +287,7 @@ export function constructWebpackConfigFunction( test: isServerComponentResource, use: [ { - loader: path.resolve(__dirname, 'loaders', 'wrappingLoader.js'), + loader: wrappingLoader, options: { ...staticWrappingLoaderOptions, wrappingTargetKind: 'server-component', @@ -295,7 +301,7 @@ export function constructWebpackConfigFunction( test: isRouteHandlerResource, use: [ { - loader: path.resolve(__dirname, 'loaders', 'wrappingLoader.js'), + loader: wrappingLoader, options: { ...staticWrappingLoaderOptions, wrappingTargetKind: 'route-handler', @@ -319,7 +325,7 @@ export function constructWebpackConfigFunction( }, use: [ { - loader: path.resolve(__dirname, 'loaders', 'wrappingLoader.js'), + loader: wrappingLoader, options: { ...staticWrappingLoaderOptions, wrappingTargetKind: 'sentry-init', @@ -1011,7 +1017,7 @@ function addValueInjectionLoader( test: /sentry\.(server|edge)\.config\.(jsx?|tsx?)/, use: [ { - loader: path.resolve(__dirname, 'loaders/valueInjectionLoader.js'), + loader: valueInjectionLoader, options: { values: serverValues, }, @@ -1022,7 +1028,7 @@ function addValueInjectionLoader( test: /sentry\.client\.config\.(jsx?|tsx?)/, use: [ { - loader: path.resolve(__dirname, 'loaders/valueInjectionLoader.js'), + loader: valueInjectionLoader, options: { values: clientValues, }, diff --git a/packages/nextjs/test/config/loaders.test.ts b/packages/nextjs/test/config/loaders.test.ts index ed3589363539..ab4b3ae4c5e0 100644 --- a/packages/nextjs/test/config/loaders.test.ts +++ b/packages/nextjs/test/config/loaders.test.ts @@ -219,7 +219,7 @@ describe('webpack loaders', () => { if (expectedWrappingTargetKind) { expect(loaderApplications).toContainEqual( expect.objectContaining({ - loader: expect.stringMatching(/wrappingLoader\.js$/), + loader: expect.stringMatching(/wrappingLoader\.m?js$/), options: expect.objectContaining({ wrappingTargetKind: expectedWrappingTargetKind, }), @@ -228,7 +228,7 @@ describe('webpack loaders', () => { } else { expect(loaderApplications).not.toContainEqual( expect.objectContaining({ - loader: expect.stringMatching(/wrappingLoader\.js$/), + loader: expect.stringMatching(/wrappingLoader\.m?js$/), }), ); } diff --git a/packages/nextjs/test/config/wrappingLoader.test.ts b/packages/nextjs/test/config/wrappingLoader.test.ts index eec179725e74..f271477e1432 100644 --- a/packages/nextjs/test/config/wrappingLoader.test.ts +++ b/packages/nextjs/test/config/wrappingLoader.test.ts @@ -4,44 +4,44 @@ import * as path from 'path'; const originalReadfileSync = fs.readFileSync; jest.spyOn(fs, 'readFileSync').mockImplementation((filePath, options) => { - if (filePath.toString().endsWith('/config/templates/apiWrapperTemplate.js')) { + if (filePath.toString().endsWith('/config/templates/apiWrapperTemplate.mjs')) { return originalReadfileSync( - path.join(__dirname, '../../build/cjs/config/templates/apiWrapperTemplate.js'), + path.join(__dirname, '../../build/esm/config/templates/apiWrapperTemplate.mjs'), options, ); } - if (filePath.toString().endsWith('/config/templates/pageWrapperTemplate.js')) { + if (filePath.toString().endsWith('/config/templates/pageWrapperTemplate.mjs')) { return originalReadfileSync( - path.join(__dirname, '../../build/cjs/config/templates/pageWrapperTemplate.js'), + path.join(__dirname, '../../build/esm/config/templates/pageWrapperTemplate.mjs'), options, ); } - if (filePath.toString().endsWith('/config/templates/middlewareWrapperTemplate.js')) { + if (filePath.toString().endsWith('/config/templates/middlewareWrapperTemplate.mjs')) { return originalReadfileSync( - path.join(__dirname, '../../build/cjs/config/templates/middlewareWrapperTemplate.js'), + path.join(__dirname, '../../build/esm/config/templates/middlewareWrapperTemplate.mjs'), options, ); } - if (filePath.toString().endsWith('/config/templates/sentryInitWrapperTemplate.js')) { + if (filePath.toString().endsWith('/config/templates/sentryInitWrapperTemplate.mjs')) { return originalReadfileSync( - path.join(__dirname, '../../build/cjs/config/templates/sentryInitWrapperTemplate.js'), + path.join(__dirname, '../../build/esm/config/templates/sentryInitWrapperTemplate.mjs'), options, ); } - if (filePath.toString().endsWith('/config/templates/serverComponentWrapperTemplate.js')) { + if (filePath.toString().endsWith('/config/templates/serverComponentWrapperTemplate.mjs')) { return originalReadfileSync( - path.join(__dirname, '../../build/cjs/config/templates/serverComponentWrapperTemplate.js'), + path.join(__dirname, '../../build/esm/config/templates/serverComponentWrapperTemplate.mjs'), options, ); } - if (filePath.toString().endsWith('/config/templates/routeHandlerWrapperTemplate.js')) { + if (filePath.toString().endsWith('/config/templates/routeHandlerWrapperTemplate.mjs')) { return originalReadfileSync( - path.join(__dirname, '../../build/cjs/config/templates/routeHandlerWrapperTemplate.js'), + path.join(__dirname, '../../build/esm/config/templates/routeHandlerWrapperTemplate.mjs'), options, ); } diff --git a/packages/node-experimental/src/integrations/node-fetch.ts b/packages/node-experimental/src/integrations/node-fetch.ts index 4c41a7f87057..254b3f93e930 100644 --- a/packages/node-experimental/src/integrations/node-fetch.ts +++ b/packages/node-experimental/src/integrations/node-fetch.ts @@ -27,7 +27,7 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => { const _breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs; const _ignoreOutgoingRequests = options.ignoreOutgoingRequests; - async function getInstrumentation(): Promise<[Instrumentation] | void> { + function getInstrumentation(): [Instrumentation] | void { // Only add NodeFetch if Node >= 16, as previous versions do not support it if (NODE_MAJOR < 16) { return; @@ -42,6 +42,7 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => { const url = request.origin; return _ignoreOutgoingRequests && url && _ignoreOutgoingRequests(url); }, + onRequest: ({ span }: { span: Span }) => { _updateSpan(span); @@ -49,8 +50,7 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => { _addRequestBreadcrumb(span); } }, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } as any), + }), ]; } catch (error) { // Could not load instrumentation @@ -60,14 +60,13 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => { return { name: 'NodeFetch', setupOnce() { - // eslint-disable-next-line @typescript-eslint/no-floating-promises - getInstrumentation().then(instrumentations => { - if (instrumentations) { - registerInstrumentations({ - instrumentations, - }); - } - }); + const instrumentations = getInstrumentation(); + + if (instrumentations) { + registerInstrumentations({ + instrumentations, + }); + } }, }; }) satisfies IntegrationFn; From 2e64455e4bc4063c616fa01cd095703f7a6bc621 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 5 Mar 2024 12:25:48 +0000 Subject: [PATCH 12/12] Fix nextjs global issue --- packages/nextjs/src/client/index.ts | 3 ++- packages/nextjs/src/client/tunnelRoute.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/nextjs/src/client/index.ts b/packages/nextjs/src/client/index.ts index fd155705a885..157e2cf70560 100644 --- a/packages/nextjs/src/client/index.ts +++ b/packages/nextjs/src/client/index.ts @@ -2,6 +2,7 @@ import { addEventProcessor, applySdkMetadata, hasTracingEnabled, setTag } from ' import type { BrowserOptions } from '@sentry/react'; import { getDefaultIntegrations as getReactDefaultIntegrations, init as reactInit } from '@sentry/react'; import type { EventProcessor, Integration } from '@sentry/types'; +import { GLOBAL_OBJ } from '@sentry/utils'; import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor'; import { getVercelEnv } from '../common/getVercelEnv'; @@ -13,7 +14,7 @@ export * from '@sentry/react'; export { captureUnderscoreErrorException } from '../common/_error'; -const globalWithInjectedValues = global as typeof global & { +const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & { __rewriteFramesAssetPrefixPath__: string; }; diff --git a/packages/nextjs/src/client/tunnelRoute.ts b/packages/nextjs/src/client/tunnelRoute.ts index 6ac5fb50a640..236b49e0e868 100644 --- a/packages/nextjs/src/client/tunnelRoute.ts +++ b/packages/nextjs/src/client/tunnelRoute.ts @@ -1,9 +1,9 @@ import type { BrowserOptions } from '@sentry/react'; -import { dsnFromString, logger } from '@sentry/utils'; +import { GLOBAL_OBJ, dsnFromString, logger } from '@sentry/utils'; import { DEBUG_BUILD } from '../common/debug-build'; -const globalWithInjectedValues = global as typeof global & { +const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & { __sentryRewritesTunnelPath__?: string; };