Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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,5 @@
'use client';

import * as Sentry from '@sentry/nextjs';

Sentry.init({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import * as Sentry from '@sentry/nextjs';

Sentry.init({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import * as Sentry from '@sentry/nextjs';

Sentry.init({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import * as Sentry from '@sentry/nextjs';

Sentry.init({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import * as Sentry from '@sentry/nextjs';

Sentry.init({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import * as Sentry from '@sentry/nextjs';

Sentry.init({
Expand Down
15 changes: 14 additions & 1 deletion packages/nextjs/src/config/loaders/valueInjectionLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
// Rollup doesn't like if we put the directive regex as a literal (?). No idea why.
/* eslint-disable @sentry-internal/sdk/no-regexp-constructor */

import type { LoaderThis } from './types';

type LoaderOptions = {
values: Record<string, unknown>;
};

// We need to be careful not to inject anything before any `"use strict";`s or "use client"s or really any other directive.
// As an additional complication directives may come after any number of comments.
// This regex is shamelessly stolen from: https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/7f984482c73e4284e8b12a08dfedf23b5a82f0af/packages/bundler-plugin-core/src/index.ts#L535-L539
const SKIP_COMMENT_AND_DIRECTIVE_REGEX =
// Note: CodeQL complains that this regex potentially has n^2 runtime. This likely won't affect realistic files.
// biome-ignore lint/nursery/useRegexLiterals: No user input
new RegExp('^(?:\\s*|/\\*(?:.|\\r|\\n)*?\\*/|//.*[\\n\\r])*(?:"[^"]*";|\'[^\']*\';)?');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is wild


/**
* Set values on the global/window object at the start of a module.
*
Expand All @@ -22,5 +33,7 @@ export default function valueInjectionLoader(this: LoaderThis<LoaderOptions>, us
.map(([key, value]) => `globalThis["${key}"] = ${JSON.stringify(value)};`)
.join('\n');

return `${injectedCode}\n${userCode}`;
return userCode.replace(SKIP_COMMENT_AND_DIRECTIVE_REGEX, match => {
return match + injectedCode;
});
}
Loading