Skip to content

Commit 73dced1

Browse files
committed
modules change
1 parent 58880df commit 73dced1

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,14 @@ export function constructWebpackConfigFunction(
410410
);
411411
}
412412

413+
// We inject a map of dependencies that the nextjs app has, as we cannot reliably extract them at runtime, sadly
414+
newConfig.plugins = newConfig.plugins || [];
415+
newConfig.plugins.push(
416+
new buildContext.webpack.DefinePlugin({
417+
__SENTRY_SERVER_MODULES__: JSON.stringify(_getModules(projectDir)),
418+
}),
419+
);
420+
413421
return newConfig;
414422
};
415423
}
@@ -825,3 +833,21 @@ function addOtelWarningIgnoreRule(newConfig: WebpackConfigObjectWithModuleRules)
825833
newConfig.ignoreWarnings.push(...ignoreRules);
826834
}
827835
}
836+
837+
function _getModules(projectDir: string): Record<string, string> {
838+
try {
839+
const packageJson = path.join(projectDir, 'package.json');
840+
const packageJsonContent = fs.readFileSync(packageJson, 'utf8');
841+
const packageJsonObject = JSON.parse(packageJsonContent) as {
842+
dependencies?: Record<string, string>;
843+
devDependencies?: Record<string, string>;
844+
};
845+
846+
return {
847+
...packageJsonObject.dependencies,
848+
...packageJsonObject.devDependencies,
849+
};
850+
} catch {
851+
return {};
852+
}
853+
}

packages/node/src/integrations/modules.ts

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ import { logger } from '@sentry/core';
55
import { DEBUG_BUILD } from '../debug-build';
66
import { isCjs } from '../utils/commonjs';
77

8-
let moduleCache: { [key: string]: string };
8+
type ModuleInfo = Record<string, string>;
9+
10+
let moduleCache: ModuleInfo | undefined;
911

1012
const INTEGRATION_NAME = 'Modules';
1113

14+
declare const __SENTRY_SERVER_MODULES__: Record<string, string>;
15+
16+
/**
17+
* This is replaced at build time with the modules loaded by the server.
18+
*/
19+
const SERVER_MODULES = typeof __SENTRY_SERVER_MODULES__ === 'undefined' ? {} : __SENTRY_SERVER_MODULES__;
20+
1221
const _modulesIntegration = (() => {
1322
// This integration only works in CJS contexts
1423
if (!isCjs()) {
@@ -52,17 +61,23 @@ function getPaths(): string[] {
5261
}
5362

5463
/** Extract information about package.json modules */
55-
function collectModules(): {
56-
[name: string]: string;
57-
} {
64+
function collectModules(): ModuleInfo {
65+
return {
66+
...SERVER_MODULES,
67+
...getModulesFromPackageJson(),
68+
...collectRequireModules(),
69+
};
70+
}
71+
72+
/** Extract information about package.json modules from require.cache */
73+
function collectRequireModules(): ModuleInfo {
5874
const mainPaths = require.main?.paths || [];
5975
const paths = getPaths();
60-
const infos: {
61-
[name: string]: string;
62-
} = {};
63-
const seen: {
64-
[path: string]: boolean;
65-
} = {};
76+
77+
// We start with the modules from package.json (if possible)
78+
// These may be overwritten by more specific versions from the require.cache
79+
const infos: ModuleInfo = {};
80+
const seen = new Set<string>();
6681

6782
paths.forEach(path => {
6883
let dir = path;
@@ -72,15 +87,15 @@ function collectModules(): {
7287
const orig = dir;
7388
dir = dirname(orig);
7489

75-
if (!dir || orig === dir || seen[orig]) {
90+
if (!dir || orig === dir || seen.has(orig)) {
7691
return undefined;
7792
}
7893
if (mainPaths.indexOf(dir) < 0) {
7994
return updir();
8095
}
8196

8297
const pkgfile = join(orig, 'package.json');
83-
seen[orig] = true;
98+
seen.add(orig);
8499

85100
if (!existsSync(pkgfile)) {
86101
return updir();
@@ -104,9 +119,35 @@ function collectModules(): {
104119
}
105120

106121
/** Fetches the list of modules and the versions loaded by the entry file for your node.js app. */
107-
function _getModules(): { [key: string]: string } {
122+
function _getModules(): ModuleInfo {
108123
if (!moduleCache) {
109124
moduleCache = collectModules();
110125
}
111126
return moduleCache;
112127
}
128+
129+
interface PackageJson {
130+
dependencies?: Record<string, string>;
131+
devDependencies?: Record<string, string>;
132+
}
133+
134+
function getPackageJson(): PackageJson {
135+
try {
136+
// @ts-expect-error This actually works, we transpile this in CJS
137+
const filePath = join(dirname(import.meta.url), 'package.json');
138+
const packageJson = JSON.parse(readFileSync(filePath, 'utf8')) as PackageJson;
139+
140+
return packageJson;
141+
} catch (e) {
142+
return {};
143+
}
144+
}
145+
146+
function getModulesFromPackageJson(): ModuleInfo {
147+
const packageJson = getPackageJson();
148+
149+
return {
150+
...packageJson.dependencies,
151+
...packageJson.devDependencies,
152+
};
153+
}

0 commit comments

Comments
 (0)