Skip to content

Commit ccb129b

Browse files
authored
fix(nextjs): Use value injection loader on instrumentation-client.ts|js (#16855)
1 parent 7bc6bd0 commit ccb129b

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ function addValueInjectionLoader(
744744
});
745745
} else {
746746
newConfig.module.rules.push({
747-
test: /sentry\.client\.config\.(jsx?|tsx?)/,
747+
test: /(?:sentry\.client\.config\.(jsx?|tsx?)|(?:src[\\/])?instrumentation-client\.(js|ts))$/,
748748
use: [
749749
{
750750
loader: path.resolve(__dirname, 'loaders/valueInjectionLoader.js'),

packages/nextjs/test/config/__snapshots__/valueInjectionLoader.test.ts.snap

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,32 @@ exports[`valueInjectionLoader > should correctly insert values for basic config
77
"
88
`;
99

10+
exports[`valueInjectionLoader > should correctly insert values for basic config 2`] = `
11+
"
12+
;globalThis["foo"] = "bar";import * as Sentry from '@sentry/nextjs';
13+
Sentry.init();
14+
"
15+
`;
16+
1017
exports[`valueInjectionLoader > should correctly insert values with a misplaced directive 1`] = `
1118
"
1219
;globalThis["foo"] = "bar";console.log('This will render the directive useless');
1320
"use client";
1421
1522
1623
24+
import * as Sentry from '@sentry/nextjs';
25+
Sentry.init();
26+
"
27+
`;
28+
29+
exports[`valueInjectionLoader > should correctly insert values with a misplaced directive 2`] = `
30+
"
31+
;globalThis["foo"] = "bar";console.log('This will render the directive useless');
32+
"use client";
33+
34+
35+
1736
import * as Sentry from '@sentry/nextjs';
1837
Sentry.init();
1938
"
@@ -27,6 +46,14 @@ exports[`valueInjectionLoader > should correctly insert values with directive 1`
2746
"
2847
`;
2948

49+
exports[`valueInjectionLoader > should correctly insert values with directive 2`] = `
50+
"
51+
"use client";globalThis["foo"] = "bar";
52+
import * as Sentry from '@sentry/nextjs';
53+
Sentry.init();
54+
"
55+
`;
56+
3057
exports[`valueInjectionLoader > should correctly insert values with directive and block comments 1`] = `
3158
"
3259
/* test */
@@ -36,6 +63,15 @@ exports[`valueInjectionLoader > should correctly insert values with directive an
3663
"
3764
`;
3865

66+
exports[`valueInjectionLoader > should correctly insert values with directive and block comments 2`] = `
67+
"
68+
/* test */
69+
"use client";;globalThis["foo"] = "bar";
70+
import * as Sentry from '@sentry/nextjs';
71+
Sentry.init();
72+
"
73+
`;
74+
3975
exports[`valueInjectionLoader > should correctly insert values with directive and inline comments 1`] = `
4076
"
4177
// test
@@ -45,6 +81,15 @@ exports[`valueInjectionLoader > should correctly insert values with directive an
4581
"
4682
`;
4783

84+
exports[`valueInjectionLoader > should correctly insert values with directive and inline comments 2`] = `
85+
"
86+
// test
87+
"use client";;globalThis["foo"] = "bar";
88+
import * as Sentry from '@sentry/nextjs';
89+
Sentry.init();
90+
"
91+
`;
92+
4893
exports[`valueInjectionLoader > should correctly insert values with directive and multiline block comments 1`] = `
4994
"
5095
/*
@@ -56,6 +101,17 @@ exports[`valueInjectionLoader > should correctly insert values with directive an
56101
"
57102
`;
58103

104+
exports[`valueInjectionLoader > should correctly insert values with directive and multiline block comments 2`] = `
105+
"
106+
/*
107+
test
108+
*/
109+
"use client";;globalThis["foo"] = "bar";
110+
import * as Sentry from '@sentry/nextjs';
111+
Sentry.init();
112+
"
113+
`;
114+
59115
exports[`valueInjectionLoader > should correctly insert values with directive and multiline block comments and a bunch of whitespace 1`] = `
60116
"
61117
/*
@@ -65,6 +121,24 @@ exports[`valueInjectionLoader > should correctly insert values with directive an
65121
66122
67123
124+
"use client";;globalThis["foo"] = "bar";
125+
126+
127+
128+
import * as Sentry from '@sentry/nextjs';
129+
Sentry.init();
130+
"
131+
`;
132+
133+
exports[`valueInjectionLoader > should correctly insert values with directive and multiline block comments and a bunch of whitespace 2`] = `
134+
"
135+
/*
136+
test
137+
*/
138+
139+
140+
141+
68142
"use client";;globalThis["foo"] = "bar";
69143
70144
@@ -81,3 +155,11 @@ exports[`valueInjectionLoader > should correctly insert values with directive an
81155
Sentry.init();
82156
"
83157
`;
158+
159+
exports[`valueInjectionLoader > should correctly insert values with directive and semicolon 2`] = `
160+
"
161+
"use client";;globalThis["foo"] = "bar";
162+
import * as Sentry from '@sentry/nextjs';
163+
Sentry.init();
164+
"
165+
`;

packages/nextjs/test/config/loaders.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ describe('webpack loaders', () => {
240240
});
241241

242242
expect(finalWebpackConfig.module.rules).toContainEqual({
243-
test: /sentry\.client\.config\.(jsx?|tsx?)/,
243+
test: /(?:sentry\.client\.config\.(jsx?|tsx?)|(?:src[\\/])?instrumentation-client\.(js|ts))$/,
244244
use: [
245245
{
246246
loader: expect.stringMatching(/valueInjectionLoader\.js$/),

packages/nextjs/test/config/valueInjectionLoader.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ const defaultLoaderThis = {
88
async: () => undefined,
99
cacheable: () => undefined,
1010
callback: () => undefined,
11-
};
12-
13-
const loaderThis = {
14-
...defaultLoaderThis,
15-
resourcePath: './client.config.ts',
1611
getOptions() {
1712
return {
1813
values: {
1914
foo: 'bar',
2015
},
2116
};
2217
},
18+
};
19+
20+
const clientConfigLoaderThis = {
21+
...defaultLoaderThis,
22+
resourcePath: './sentry.client.config.ts',
23+
} satisfies LoaderThis<ValueInjectionLoaderOptions>;
24+
25+
const instrumentationLoaderThis = {
26+
...defaultLoaderThis,
27+
resourcePath: './instrumentation-client.js',
2328
} satisfies LoaderThis<ValueInjectionLoaderOptions>;
2429

25-
describe('valueInjectionLoader', () => {
30+
describe.each([[clientConfigLoaderThis], [instrumentationLoaderThis]])('valueInjectionLoader', loaderThis => {
2631
it('should correctly insert values for basic config', () => {
2732
const userCode = `
2833
import * as Sentry from '@sentry/nextjs';

0 commit comments

Comments
 (0)