Skip to content

Commit 545eade

Browse files
author
Luca Forstner
committed
fix tests and rename
1 parent 7c37103 commit 545eade

File tree

5 files changed

+59
-71
lines changed

5 files changed

+59
-71
lines changed

packages/nextjs/src/config/webpackPluginOptions.ts renamed to packages/nextjs/src/config/buildPluginOptions.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,45 @@ export function getBuildPluginOptions(
2323
const sourcemapUploadIgnore: string[] = [];
2424
const filesToDeleteAfterUpload: string[] = [];
2525

26+
// We need to convert paths to posix because Glob patterns use `\` to escape
27+
// glob characters. This clashes with Windows path separators.
28+
// See: https://www.npmjs.com/package/glob
29+
const normalizedDistDirAbsPath = distDirAbsPath.replace(/\\/g, '/');
30+
2631
if (mode === 'after-production-build') {
2732
sourcemapUploadAssets.push(
28-
path.posix.join(distDirAbsPath, '**'), // This is normally where Next.js outputs things
33+
path.posix.join(normalizedDistDirAbsPath, '**'), // This is normally where Next.js outputs things
2934
);
3035
if (sentryBuildOptions.sourcemaps?.deleteSourcemapsAfterUpload) {
3136
filesToDeleteAfterUpload.push(
32-
path.posix.join(distDirAbsPath, '**', '*.js.map'),
33-
path.posix.join(distDirAbsPath, '**', '*.mjs.map'),
34-
path.posix.join(distDirAbsPath, '**', '*.cjs.map'),
37+
path.posix.join(normalizedDistDirAbsPath, '**', '*.js.map'),
38+
path.posix.join(normalizedDistDirAbsPath, '**', '*.mjs.map'),
39+
path.posix.join(normalizedDistDirAbsPath, '**', '*.cjs.map'),
3540
);
3641
}
3742
} else {
3843
if (mode === 'webpack-nodejs' || mode === 'webpack-edge') {
3944
sourcemapUploadAssets.push(
40-
path.posix.join(distDirAbsPath, 'server', '**'), // This is normally where Next.js outputs things
41-
path.posix.join(distDirAbsPath, 'serverless', '**'), // This was the output location for serverless Next.js
45+
path.posix.join(normalizedDistDirAbsPath, 'server', '**'), // This is normally where Next.js outputs things
46+
path.posix.join(normalizedDistDirAbsPath, 'serverless', '**'), // This was the output location for serverless Next.js
4247
);
4348
} else {
4449
if (sentryBuildOptions.widenClientFileUpload) {
45-
sourcemapUploadAssets.push(path.posix.join(distDirAbsPath, 'static', 'chunks', '**'));
50+
sourcemapUploadAssets.push(path.posix.join(normalizedDistDirAbsPath, 'static', 'chunks', '**'));
4651
} else {
4752
sourcemapUploadAssets.push(
48-
path.posix.join(distDirAbsPath, 'static', 'chunks', 'pages', '**'),
49-
path.posix.join(distDirAbsPath, 'static', 'chunks', 'app', '**'),
53+
path.posix.join(normalizedDistDirAbsPath, 'static', 'chunks', 'pages', '**'),
54+
path.posix.join(normalizedDistDirAbsPath, 'static', 'chunks', 'app', '**'),
5055
);
5156
}
5257

5358
// TODO: We should think about uploading these when `widenClientFileUpload` is `true`. They may be useful in some situations.
5459
sourcemapUploadIgnore.push(
55-
path.posix.join(distDirAbsPath, 'static', 'chunks', 'framework-*'),
56-
path.posix.join(distDirAbsPath, 'static', 'chunks', 'framework.*'),
57-
path.posix.join(distDirAbsPath, 'static', 'chunks', 'main-*'),
58-
path.posix.join(distDirAbsPath, 'static', 'chunks', 'polyfills-*'),
59-
path.posix.join(distDirAbsPath, 'static', 'chunks', 'webpack-*'),
60+
path.posix.join(normalizedDistDirAbsPath, 'static', 'chunks', 'framework-*'),
61+
path.posix.join(normalizedDistDirAbsPath, 'static', 'chunks', 'framework.*'),
62+
path.posix.join(normalizedDistDirAbsPath, 'static', 'chunks', 'main-*'),
63+
path.posix.join(normalizedDistDirAbsPath, 'static', 'chunks', 'polyfills-*'),
64+
path.posix.join(normalizedDistDirAbsPath, 'static', 'chunks', 'webpack-*'),
6065
);
6166
}
6267

@@ -65,9 +70,9 @@ export function getBuildPluginOptions(
6570
// We only care to delete client bundle source maps because they would be the ones being served.
6671
// Removing the server source maps crashes Vercel builds for (thus far) unknown reasons:
6772
// https://github.com/getsentry/sentry-javascript/issues/13099
68-
path.posix.join(distDirAbsPath, 'static', '**', '*.js.map'),
69-
path.posix.join(distDirAbsPath, 'static', '**', '*.mjs.map'),
70-
path.posix.join(distDirAbsPath, 'static', '**', '*.cjs.map'),
73+
path.posix.join(normalizedDistDirAbsPath, 'static', '**', '*.js.map'),
74+
path.posix.join(normalizedDistDirAbsPath, 'static', '**', '*.mjs.map'),
75+
path.posix.join(normalizedDistDirAbsPath, 'static', '**', '*.cjs.map'),
7176
);
7277
}
7378
}

packages/nextjs/src/config/runAfterProductionCompile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SentryBuildOptions } from './types';
22
import { getWebpackBuildFunctionCalled } from './util';
3-
import { getBuildPluginOptions } from './webpackPluginOptions';
3+
import { getBuildPluginOptions } from './buildPluginOptions';
44
import { glob } from 'glob';
55
import { loadModule } from '@sentry/core';
66
import type { createSentryBuildPluginManager as createSentryBuildPluginManagerType } from '@sentry/bundler-plugin-core';

packages/nextjs/src/config/webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
WebpackConfigObjectWithModuleRules,
2222
WebpackEntryProperty,
2323
} from './types';
24-
import { getBuildPluginOptions } from './webpackPluginOptions';
24+
import { getBuildPluginOptions } from './buildPluginOptions';
2525
import { getNextjsVersion, setWebpackBuildFunctionCalled } from './util';
2626

2727
// Next.js runs webpack 3 times, once for the client, the server, and for edge. Because we don't want to print certain

packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts renamed to packages/nextjs/test/config/webpack/buildPluginOptions.test.ts

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
11
import { describe, it, expect } from 'vitest';
22

3-
import type { BuildContext, NextConfigObject } from '../../../src/config/types';
4-
import { getWebpackPluginOptions } from '../../../src/config/webpackPluginOptions';
3+
import { getBuildPluginOptions } from '../../../src/config/buildPluginOptions';
54

6-
function generateBuildContext(overrides: {
7-
dir?: string;
8-
isServer: boolean;
9-
nextjsConfig?: NextConfigObject;
10-
}): BuildContext {
11-
return {
12-
dev: false, // The plugin is not included in dev mode
13-
isServer: overrides.isServer,
14-
buildId: 'test-build-id',
15-
dir: overrides.dir ?? '/my/project/dir',
16-
config: overrides.nextjsConfig ?? {},
17-
totalPages: 2,
18-
defaultLoaders: true,
19-
webpack: {
20-
version: '4.0.0',
21-
DefinePlugin: {} as any,
22-
},
23-
};
24-
}
25-
26-
describe('getWebpackPluginOptions()', () => {
5+
describe('getBuildPluginOptions()', () => {
276
it('forwards relevant options', () => {
28-
const buildContext = generateBuildContext({ isServer: false });
29-
const generatedPluginOptions = getWebpackPluginOptions(
30-
buildContext,
7+
const generatedPluginOptions = getBuildPluginOptions(
318
{
329
authToken: 'my-auth-token',
3310
headers: { 'my-test-header': 'test' },
@@ -60,6 +37,8 @@ describe('getWebpackPluginOptions()', () => {
6037
},
6138
},
6239
'my-release',
40+
'webpack-client',
41+
'/my/project/dir/.next',
6342
);
6443

6544
expect(generatedPluginOptions.authToken).toBe('my-auth-token');
@@ -119,16 +98,16 @@ describe('getWebpackPluginOptions()', () => {
11998
});
12099

121100
it('forwards bundleSizeOptimization options', () => {
122-
const buildContext = generateBuildContext({ isServer: false });
123-
const generatedPluginOptions = getWebpackPluginOptions(
124-
buildContext,
101+
const generatedPluginOptions = getBuildPluginOptions(
125102
{
126103
bundleSizeOptimizations: {
127104
excludeTracing: true,
128105
excludeReplayShadowDom: false,
129106
},
130107
},
131108
undefined,
109+
'webpack-client',
110+
'/my/project/dir/.next',
132111
);
133112

134113
expect(generatedPluginOptions).toMatchObject({
@@ -140,17 +119,15 @@ describe('getWebpackPluginOptions()', () => {
140119
});
141120

142121
it('returns the right `assets` and `ignore` values during the server build', () => {
143-
const buildContext = generateBuildContext({ isServer: true });
144-
const generatedPluginOptions = getWebpackPluginOptions(buildContext, {}, undefined);
122+
const generatedPluginOptions = getBuildPluginOptions({}, undefined, 'webpack-nodejs', '/my/project/dir/.next');
145123
expect(generatedPluginOptions.sourcemaps).toMatchObject({
146124
assets: ['/my/project/dir/.next/server/**', '/my/project/dir/.next/serverless/**'],
147125
ignore: [],
148126
});
149127
});
150128

151129
it('returns the right `assets` and `ignore` values during the client build', () => {
152-
const buildContext = generateBuildContext({ isServer: false });
153-
const generatedPluginOptions = getWebpackPluginOptions(buildContext, {}, undefined);
130+
const generatedPluginOptions = getBuildPluginOptions({}, undefined, 'webpack-client', '/my/project/dir/.next');
154131
expect(generatedPluginOptions.sourcemaps).toMatchObject({
155132
assets: ['/my/project/dir/.next/static/chunks/pages/**', '/my/project/dir/.next/static/chunks/app/**'],
156133
ignore: [
@@ -164,8 +141,12 @@ describe('getWebpackPluginOptions()', () => {
164141
});
165142

166143
it('returns the right `assets` and `ignore` values during the client build with `widenClientFileUpload`', () => {
167-
const buildContext = generateBuildContext({ isServer: false });
168-
const generatedPluginOptions = getWebpackPluginOptions(buildContext, { widenClientFileUpload: true }, undefined);
144+
const generatedPluginOptions = getBuildPluginOptions(
145+
{ widenClientFileUpload: true },
146+
undefined,
147+
'webpack-client',
148+
'/my/project/dir/.next',
149+
);
169150
expect(generatedPluginOptions.sourcemaps).toMatchObject({
170151
assets: ['/my/project/dir/.next/static/chunks/**'],
171152
ignore: [
@@ -179,20 +160,24 @@ describe('getWebpackPluginOptions()', () => {
179160
});
180161

181162
it('sets `sourcemaps.disable` plugin options to true when `sourcemaps.disable` is true', () => {
182-
const buildContext = generateBuildContext({ isServer: false });
183-
const generatedPluginOptions = getWebpackPluginOptions(buildContext, { sourcemaps: { disable: true } }, undefined);
163+
const generatedPluginOptions = getBuildPluginOptions(
164+
{ sourcemaps: { disable: true } },
165+
undefined,
166+
'webpack-client',
167+
'/my/project/dir/.next',
168+
);
184169
expect(generatedPluginOptions.sourcemaps).toMatchObject({
185170
disable: true,
186171
});
187172
});
188173

189174
it('passes posix paths to the plugin', () => {
190-
const buildContext = generateBuildContext({
191-
dir: 'C:\\my\\windows\\project\\dir',
192-
nextjsConfig: { distDir: '.dist\\v1' },
193-
isServer: false,
194-
});
195-
const generatedPluginOptions = getWebpackPluginOptions(buildContext, { widenClientFileUpload: true }, undefined);
175+
const generatedPluginOptions = getBuildPluginOptions(
176+
{ widenClientFileUpload: true },
177+
undefined,
178+
'webpack-client',
179+
'C:\\my\\windows\\project\\dir\\.dist\\v1',
180+
);
196181
expect(generatedPluginOptions.sourcemaps).toMatchObject({
197182
assets: ['C:/my/windows/project/dir/.dist/v1/static/chunks/**'],
198183
ignore: [
@@ -206,8 +191,7 @@ describe('getWebpackPluginOptions()', () => {
206191
});
207192

208193
it('sets options to not create a release or do any release operations when releaseName is undefined', () => {
209-
const buildContext = generateBuildContext({ isServer: false });
210-
const generatedPluginOptions = getWebpackPluginOptions(buildContext, {}, undefined);
194+
const generatedPluginOptions = getBuildPluginOptions({}, undefined, 'webpack-client', '/my/project/dir/.next');
211195

212196
expect(generatedPluginOptions).toMatchObject({
213197
release: {

packages/nextjs/test/config/webpack/constructWebpackConfig.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe, expect, it, vi } from 'vitest';
33
// mock helper functions not tested directly in this file
44
import '../mocks';
55

6-
import * as getWebpackPluginOptionsModule from '../../../src/config/webpackPluginOptions';
6+
import * as getBuildPluginOptionsModule from '../../../src/config/buildPluginOptions';
77
import {
88
CLIENT_SDK_CONFIG_FILE,
99
clientBuildContext,
@@ -55,7 +55,7 @@ describe('constructWebpackConfigFunction()', () => {
5555
});
5656

5757
it('automatically enables deleteSourcemapsAfterUpload for client builds when not explicitly set', async () => {
58-
const getWebpackPluginOptionsSpy = vi.spyOn(getWebpackPluginOptionsModule, 'getWebpackPluginOptions');
58+
const getBuildPluginOptionsSpy = vi.spyOn(getBuildPluginOptionsModule, 'getBuildPluginOptions');
5959
vi.spyOn(core, 'loadModule').mockImplementation(() => ({
6060
sentryWebpackPlugin: () => ({
6161
_name: 'sentry-webpack-plugin',
@@ -71,19 +71,18 @@ describe('constructWebpackConfigFunction()', () => {
7171
},
7272
});
7373

74-
expect(getWebpackPluginOptionsSpy).toHaveBeenCalledWith(
75-
expect.objectContaining({
76-
isServer: false,
77-
}),
74+
expect(getBuildPluginOptionsSpy).toHaveBeenCalledWith(
7875
expect.objectContaining({
7976
sourcemaps: {
8077
deleteSourcemapsAfterUpload: true,
8178
},
8279
}),
8380
undefined,
81+
expect.any(String),
82+
expect.any(String),
8483
);
8584

86-
getWebpackPluginOptionsSpy.mockRestore();
85+
getBuildPluginOptionsSpy.mockRestore();
8786
});
8887

8988
it('preserves unrelated webpack config options', async () => {

0 commit comments

Comments
 (0)