Skip to content

Commit c6ff13d

Browse files
committed
STASH
1 parent 9d14781 commit c6ff13d

File tree

5 files changed

+78
-48
lines changed

5 files changed

+78
-48
lines changed

packages/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
"devDependencies": {
3131
"@sentry/types": "6.13.3",
32-
"@types/webpack": ">= 4.41.31",
32+
"@types/webpack": "^4.41.31",
3333
"eslint": "7.20.0",
3434
"next": "10.1.3",
3535
"rimraf": "3.0.2"

packages/nextjs/src/config/webpack.ts

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { getSentryRelease } from '@sentry/node';
22
import { dropUndefinedKeys, logger } from '@sentry/utils';
33
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
44
import * as fs from 'fs';
5+
import * as os from 'os';
56
import * as path from 'path';
6-
import { DefinePlugin, WebpackPluginInstance } from 'webpack';
7-
import { default as InjectPlugin, ENTRY_ORDER } from 'webpack-inject-plugin';
7+
// import * as rimraf from 'rimraf';
8+
import { WebpackPluginInstance } from 'webpack';
89

10+
// import { DefinePlugin, WebpackPluginInstance } from 'webpack';
11+
// import { default as InjectPlugin, ENTRY_ORDER } from 'webpack-inject-plugin';
912
import {
1013
BuildContext,
11-
EntryPointValue,
1214
EntryPropertyObject,
1315
NextConfigObject,
1416
SentryWebpackPluginOptions,
@@ -19,6 +21,13 @@ import {
1921

2022
export { SentryWebpackPlugin };
2123

24+
// const REWRITE_FRAMES_HELPER = path.resolve(
25+
// fs.mkdtempSync(path.resolve(os.tmpdir(), 'sentry-')),
26+
// 'rewriteFramesHelper.js',
27+
// );
28+
// console.log(REWRITE_FRAMES_HELPER);
29+
// debugger;
30+
2231
// TODO: merge default SentryWebpackPlugin ignore with their SentryWebpackPlugin ignore or ignoreFile
2332
// TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include
2433
// TODO: drop merged keys from override check? `includeDefaults` option?
@@ -80,25 +89,27 @@ export function constructWebpackConfigFunction(
8089
};
8190
}
8291

83-
// Support non-default output directories by making the output path (easy to get here at build-time) available to
92+
// Support non-default output directories by making the output path (easy to get here at build-time) available to
8493
// the SDK's default `RewriteFrames` instance (which needs it at runtime).
85-
const distDir = buildContext.config.distDir;
94+
// const distDir = buildContext.config.distDir;
8695
newConfig.plugins = newConfig.plugins || [];
8796

88-
const definePluginInstance = findWebpackPlugin(newConfig, 'DefinePlugin') as DefinePlugin;
89-
90-
if (definePluginInstance) {
91-
definePluginInstance.definitions['__rewriteFramesDistDir__'] = distDir;
92-
} else {
93-
newConfig.plugins.push(new DefinePlugin({ __rewriteFramesDistDir__: distDir }));
94-
}
95-
96-
newConfig.plugins.push(
97-
(new InjectPlugin(() => `global.__rewriteFramesDistDir__ = ${distDir};`, {
98-
entryName: shouldAddSentryToEntryPoint, // Limit the injected code to only the entry w/ this name
99-
entryOrder: ENTRY_ORDER.First, // Make the injected code be the first entry point
100-
}) as unknown) as WebpackPluginInstance, // necessary because of a mismatch in @types/webpack versions between this plugin
101-
);
97+
// const definePluginInstance = findWebpackPlugin(newConfig, 'DefinePlugin') as DefinePlugin;
98+
//
99+
// if (definePluginInstance) {
100+
// definePluginInstance.definitions['__rewriteFramesDistDir__'] = distDir;
101+
// } else {
102+
// newConfig.plugins.push(new DefinePlugin({ __rewriteFramesDistDir__: distDir }));
103+
// }
104+
105+
// if (buildContext.isServer) {
106+
// newConfig.plugins.push(
107+
// (new InjectPlugin(() => `global.__rewriteFramesDistDir__ = ${distDir};`, {
108+
// entryName: shouldAddSentryToEntryPoint, // Limit the injected code to only the entry w/ this name
109+
// entryOrder: ENTRY_ORDER.First, // Make the injected code be the first entry point
110+
// }) as unknown) as WebpackPluginInstance, // necessary because of a mismatch in @types/webpack versions between this plugin
111+
// );
112+
// }
102113

103114
// Enable the Sentry plugin (which uploads source maps to Sentry when not in dev) by default
104115
const enableWebpackPlugin = buildContext.isServer
@@ -149,16 +160,38 @@ async function addSentryToEntryProperty(
149160
const newEntryProperty =
150161
typeof currentEntryProperty === 'function' ? await currentEntryProperty() : { ...currentEntryProperty };
151162

163+
// `sentry.server.config.js` or `sentry.client.config.js` (or their TS equivalents)
152164
const userConfigFile = buildContext.isServer
153165
? getUserConfigFile(buildContext.dir, 'server')
154166
: getUserConfigFile(buildContext.dir, 'client');
155167

168+
// we need to turn the filename into a path so webpack can find it
169+
const filesToInject = [`./${userConfigFile}`];
170+
171+
if (buildContext.isServer) {
172+
const rewriteFramesHelper = path.resolve(
173+
fs.mkdtempSync(path.resolve(os.tmpdir(), 'sentry-')),
174+
'rewriteFramesHelper.js',
175+
);
176+
debugger;
177+
// Support non-default output directories by making the output path (easy to get here at build-time) available to
178+
// the server SDK's default `RewriteFrames` instance (which needs it at runtime).
179+
fs.writeFileSync(rewriteFramesHelper, `global.__rewriteFramesDistDir__ = '${buildContext.config.distDir}';\n`);
180+
filesToInject.push(rewriteFramesHelper);
181+
}
182+
156183
for (const entryPointName in newEntryProperty) {
157184
if (shouldAddSentryToEntryPoint(entryPointName)) {
158-
// we need to turn the filename into a path so webpack can find it
159-
addFileToExistingEntryPoint(newEntryProperty, entryPointName, `./${userConfigFile}`);
185+
addFilesToExistingEntryPoint(newEntryProperty, entryPointName, filesToInject);
160186
}
161187
}
188+
//
189+
// if (buildContext.isServer) {
190+
// debugger;
191+
// rimraf(path.dirname(REWRITE_FRAMES_HELPER), err =>
192+
// logger.warn(`Could not remove ${REWRITE_FRAMES_HELPER}. Received error: ${err}`),
193+
// );
194+
// }
162195

163196
return newEntryProperty;
164197
}
@@ -184,51 +217,52 @@ export function getUserConfigFile(projectDir: string, platform: 'server' | 'clie
184217
}
185218

186219
/**
187-
* Add a file to a specific element of the given `entry` webpack config property.
220+
* Add files to a specific element of the given `entry` webpack config property.
188221
*
189222
* @param entryProperty The existing `entry` config object
190223
* @param entryPointName The key where the file should be injected
191-
* @param filepath The path to the injected file
224+
* @param filepaths An array of paths to the injected files
192225
*/
193-
function addFileToExistingEntryPoint(
226+
function addFilesToExistingEntryPoint(
194227
entryProperty: EntryPropertyObject,
195228
entryPointName: string,
196-
filepath: string,
229+
filepaths: string[],
197230
): void {
198231
// can be a string, array of strings, or object whose `import` property is one of those two
199232
const currentEntryPoint = entryProperty[entryPointName];
200-
let newEntryPoint: EntryPointValue;
233+
let newEntryPoint = currentEntryPoint;
201234

202235
if (typeof currentEntryPoint === 'string') {
203-
newEntryPoint = [filepath, currentEntryPoint];
236+
newEntryPoint = [...filepaths, currentEntryPoint];
204237
} else if (Array.isArray(currentEntryPoint)) {
205-
newEntryPoint = [filepath, ...currentEntryPoint];
238+
newEntryPoint = [...filepaths, ...currentEntryPoint];
206239
}
207240
// descriptor object (webpack 5+)
208241
else if (typeof currentEntryPoint === 'object' && 'import' in currentEntryPoint) {
209242
const currentImportValue = currentEntryPoint.import;
210243
let newImportValue;
211244

212245
if (typeof currentImportValue === 'string') {
213-
newImportValue = [filepath, currentImportValue];
246+
newImportValue = [...filepaths, currentImportValue];
214247
} else {
215-
newImportValue = [filepath, ...currentImportValue];
248+
newImportValue = [...filepaths, ...currentImportValue];
216249
}
217250

218251
newEntryPoint = {
219252
...currentEntryPoint,
220253
import: newImportValue,
221254
};
222-
} else {
223-
// mimic the logger prefix in order to use `console.warn` (which will always be printed, regardless of SDK settings)
255+
}
256+
// malformed entry point (use `console.error` rather than `logger.error` because it will always be printed, regardless
257+
// of SDK settings)
258+
else {
224259
// eslint-disable-next-line no-console
225260
console.error(
226261
'Sentry Logger [Error]:',
227-
`Could not inject SDK initialization code into entry point ${entryPointName}, as it is not a recognized format.\n`,
262+
`Could not inject SDK initialization code into entry point ${entryPointName}, as its current value is not in a recognized format.\n`,
228263
`Expected: string | Array<string> | { [key:string]: any, import: string | Array<string> }\n`,
229264
`Got: ${currentEntryPoint}`,
230265
);
231-
return;
232266
}
233267

234268
entryProperty[entryPointName] = newEntryPoint;

packages/nextjs/src/index.server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,15 @@ function sdkAlreadyInitialized(): boolean {
4646
return !!hub.getClient();
4747
}
4848

49-
debugger;
49+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
50+
// console.log((global as any).__rewriteFramesDistDir__);
51+
// debugger;
5052

5153
// webpack will replace this placeholder at build time
52-
const SOURCEMAP_FILENAME_REGEX = new RegExp(escapeStringForRegex('__rewriteFramesDistDir__'));
54+
55+
// FIXME
56+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
57+
const SOURCEMAP_FILENAME_REGEX = new RegExp(escapeStringForRegex((global as any).__rewriteFramesDistDir__));
5358

5459
const defaultRewriteFramesIntegration = new RewriteFrames({
5560
iteratee: frame => {

packages/nextjs/vercel/install-sentry-from-branch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ PACKAGES_DIR="$REPO_DIR/packages"
4343
# Escape all of the slashes in the path for use in sed
4444
ESCAPED_PACKAGES_DIR=$(echo $PACKAGES_DIR | sed s/'\/'/'\\\/'/g)
4545

46-
PACKAGE_NAMES=$(ls PACKAGES_DIR)
46+
PACKAGE_NAMES=$(ls $PACKAGES_DIR)
4747

4848
# Modify each package's package.json file by searching in it for sentry dependencies from the monorepo and, for each
4949
# sibling dependency found, replacing the version number with a file dependency pointing to the sibling itself (so

yarn.lock

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3799,7 +3799,7 @@
37993799
"@types/source-list-map" "*"
38003800
source-map "^0.7.3"
38013801

3802-
"@types/webpack@^4", "@types/webpack@^4.4.17":
3802+
"@types/webpack@^4", "@types/webpack@^4.4.17", "@types/webpack@^4.41.31":
38033803
version "4.41.31"
38043804
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.41.31.tgz#c35f252a3559ddf9c85c0d8b0b42019025e581aa"
38053805
integrity sha512-/i0J7sepXFIp1ZT7FjUGi1eXMCg8HCCzLJEQkKsOtbJFontsJLolBcDC+3qxn5pPwiCt1G0ZdRmYRzNBtvpuGQ==
@@ -3811,15 +3811,6 @@
38113811
anymatch "^3.0.0"
38123812
source-map "^0.6.0"
38133813

3814-
"@types/webpack@^5.28.0":
3815-
version "5.28.0"
3816-
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-5.28.0.tgz#78dde06212f038d77e54116cfe69e88ae9ed2c03"
3817-
integrity sha512-8cP0CzcxUiFuA9xGJkfeVpqmWTk9nx6CWwamRGCj95ph1SmlRRk9KlCZ6avhCbZd4L68LvYT6l1kpdEnQXrF8w==
3818-
dependencies:
3819-
"@types/node" "*"
3820-
tapable "^2.2.0"
3821-
webpack "^5"
3822-
38233814
"@types/yargs-parser@*":
38243815
version "20.2.0"
38253816
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.0.tgz#dd3e6699ba3237f0348cd085e4698780204842f9"

0 commit comments

Comments
 (0)