Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import * as path from 'path';
import type { VercelCronsConfig } from '../../common/types';
import type { RouteManifest } from '../manifest/types';
Expand Down Expand Up @@ -40,6 +41,9 @@ export function generateValueInjectionRules({
if (vercelCronsConfig) {
serverValues._sentryVercelCronsConfig = JSON.stringify(vercelCronsConfig);
}
// Inject server modules (matching webpack's __SENTRY_SERVER_MODULES__ behavior)
// Use process.cwd() to get the project directory at build time
serverValues.__SENTRY_SERVER_MODULES__ = getModulesFromPackageJson(process.cwd());

if (Object.keys(isomorphicValues).length > 0) {
clientValues = { ...clientValues, ...isomorphicValues };
Expand Down Expand Up @@ -82,3 +86,24 @@ export function generateValueInjectionRules({

return rules;
}

/**
* Extract modules from package.json (matching webpack's _getModules behavior)
*/
function getModulesFromPackageJson(projectDir: string): Record<string, string> {
try {
const packageJsonPath = path.join(projectDir, 'package.json');
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
const packageJsonObject = JSON.parse(packageJsonContent) as {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
};

return {
...packageJsonObject.dependencies,
...packageJsonObject.devDependencies,
};
} catch {
return {};
}
}
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('constructTurbopackConfig', () => {
{ path: '/users', regex: '/users' },
{ path: '/api/health', regex: '/api/health' },
],
isrRoutes: [],
};

const mockSentryOptions = {};
Expand All @@ -36,7 +37,22 @@ describe('constructTurbopackConfig', () => {
userSentryOptions: mockSentryOptions,
});

expect(result).toEqual({});
expect(result).toEqual({
rules: {
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});

it('should create turbopack config with instrumentation rule when manifest is provided', () => {
Expand All @@ -61,6 +77,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -135,6 +163,18 @@ describe('constructTurbopackConfig', () => {
},
rules: {
'*.test.js': ['jest-loader'],
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -174,6 +214,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -224,7 +276,7 @@ describe('constructTurbopackConfig', () => {
describe('with edge cases', () => {
it('should handle empty route manifest', () => {
const userNextConfig: NextConfigObject = {};
const emptyManifest: RouteManifest = { dynamicRoutes: [], staticRoutes: [] };
const emptyManifest: RouteManifest = { dynamicRoutes: [], staticRoutes: [], isrRoutes: [] };

const result = constructTurbopackConfig({
userNextConfig,
Expand All @@ -245,13 +297,26 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});

it('should handle complex route manifest', () => {
const userNextConfig: NextConfigObject = {};
const complexManifest: RouteManifest = {
isrRoutes: [],
dynamicRoutes: [
{ path: '/users/[id]/posts/[postId]', regex: '/users/([^/]+)/posts/([^/]+)', paramNames: ['id', 'postId'] },
{ path: '/api/[...params]', regex: '/api/(.+)', paramNames: ['params'] },
Expand All @@ -278,6 +343,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -308,6 +385,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -342,6 +431,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: '15.0.0',
},
},
Expand Down Expand Up @@ -397,6 +487,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: '14.0.0',
},
},
Expand Down Expand Up @@ -433,6 +524,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -493,6 +596,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: nextJsVersion,
},
},
Expand Down Expand Up @@ -534,6 +638,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: nextJsVersion,
},
},
Expand Down Expand Up @@ -586,6 +691,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: nextJsVersion,
},
},
Expand Down Expand Up @@ -624,7 +730,22 @@ describe('constructTurbopackConfig', () => {
nextJsVersion: undefined,
});

expect(result).toEqual({});
expect(result).toEqual({
rules: {
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});

it('should not create Next.js version rule when nextJsVersion is empty string', () => {
Expand All @@ -635,7 +756,22 @@ describe('constructTurbopackConfig', () => {
nextJsVersion: '',
});

expect(result).toEqual({});
expect(result).toEqual({
rules: {
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});

it('should not override existing instrumentation rule when nextJsVersion is provided', () => {
Expand Down Expand Up @@ -725,6 +861,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: nextJsVersion,
},
},
Expand Down
Loading
Loading