@@ -2,13 +2,15 @@ import { getSentryRelease } from '@sentry/node';
2
2
import { dropUndefinedKeys , logger } from '@sentry/utils' ;
3
3
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin' ;
4
4
import * as fs from 'fs' ;
5
+ import * as os from 'os' ;
5
6
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' ;
8
9
10
+ // import { DefinePlugin, WebpackPluginInstance } from 'webpack';
11
+ // import { default as InjectPlugin, ENTRY_ORDER } from 'webpack-inject-plugin';
9
12
import {
10
13
BuildContext ,
11
- EntryPointValue ,
12
14
EntryPropertyObject ,
13
15
NextConfigObject ,
14
16
SentryWebpackPluginOptions ,
@@ -19,6 +21,13 @@ import {
19
21
20
22
export { SentryWebpackPlugin } ;
21
23
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
+
22
31
// TODO: merge default SentryWebpackPlugin ignore with their SentryWebpackPlugin ignore or ignoreFile
23
32
// TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include
24
33
// TODO: drop merged keys from override check? `includeDefaults` option?
@@ -80,25 +89,27 @@ export function constructWebpackConfigFunction(
80
89
} ;
81
90
}
82
91
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
84
93
// the SDK's default `RewriteFrames` instance (which needs it at runtime).
85
- const distDir = buildContext . config . distDir ;
94
+ // const distDir = buildContext.config.distDir;
86
95
newConfig . plugins = newConfig . plugins || [ ] ;
87
96
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
+ // }
102
113
103
114
// Enable the Sentry plugin (which uploads source maps to Sentry when not in dev) by default
104
115
const enableWebpackPlugin = buildContext . isServer
@@ -149,16 +160,38 @@ async function addSentryToEntryProperty(
149
160
const newEntryProperty =
150
161
typeof currentEntryProperty === 'function' ? await currentEntryProperty ( ) : { ...currentEntryProperty } ;
151
162
163
+ // `sentry.server.config.js` or `sentry.client.config.js` (or their TS equivalents)
152
164
const userConfigFile = buildContext . isServer
153
165
? getUserConfigFile ( buildContext . dir , 'server' )
154
166
: getUserConfigFile ( buildContext . dir , 'client' ) ;
155
167
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
+
156
183
for ( const entryPointName in newEntryProperty ) {
157
184
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 ) ;
160
186
}
161
187
}
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
+ // }
162
195
163
196
return newEntryProperty ;
164
197
}
@@ -184,51 +217,52 @@ export function getUserConfigFile(projectDir: string, platform: 'server' | 'clie
184
217
}
185
218
186
219
/**
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.
188
221
*
189
222
* @param entryProperty The existing `entry` config object
190
223
* @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
192
225
*/
193
- function addFileToExistingEntryPoint (
226
+ function addFilesToExistingEntryPoint (
194
227
entryProperty : EntryPropertyObject ,
195
228
entryPointName : string ,
196
- filepath : string ,
229
+ filepaths : string [ ] ,
197
230
) : void {
198
231
// can be a string, array of strings, or object whose `import` property is one of those two
199
232
const currentEntryPoint = entryProperty [ entryPointName ] ;
200
- let newEntryPoint : EntryPointValue ;
233
+ let newEntryPoint = currentEntryPoint ;
201
234
202
235
if ( typeof currentEntryPoint === 'string' ) {
203
- newEntryPoint = [ filepath , currentEntryPoint ] ;
236
+ newEntryPoint = [ ... filepaths , currentEntryPoint ] ;
204
237
} else if ( Array . isArray ( currentEntryPoint ) ) {
205
- newEntryPoint = [ filepath , ...currentEntryPoint ] ;
238
+ newEntryPoint = [ ... filepaths , ...currentEntryPoint ] ;
206
239
}
207
240
// descriptor object (webpack 5+)
208
241
else if ( typeof currentEntryPoint === 'object' && 'import' in currentEntryPoint ) {
209
242
const currentImportValue = currentEntryPoint . import ;
210
243
let newImportValue ;
211
244
212
245
if ( typeof currentImportValue === 'string' ) {
213
- newImportValue = [ filepath , currentImportValue ] ;
246
+ newImportValue = [ ... filepaths , currentImportValue ] ;
214
247
} else {
215
- newImportValue = [ filepath , ...currentImportValue ] ;
248
+ newImportValue = [ ... filepaths , ...currentImportValue ] ;
216
249
}
217
250
218
251
newEntryPoint = {
219
252
...currentEntryPoint ,
220
253
import : newImportValue ,
221
254
} ;
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 {
224
259
// eslint-disable-next-line no-console
225
260
console . error (
226
261
'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` ,
228
263
`Expected: string | Array<string> | { [key:string]: any, import: string | Array<string> }\n` ,
229
264
`Got: ${ currentEntryPoint } ` ,
230
265
) ;
231
- return ;
232
266
}
233
267
234
268
entryProperty [ entryPointName ] = newEntryPoint ;
0 commit comments