@@ -2,13 +2,15 @@ import { getSentryRelease } from '@sentry/node';
22import { dropUndefinedKeys , logger } from '@sentry/utils' ;
33import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin' ;
44import * as fs from 'fs' ;
5+ import * as os from 'os' ;
56import * 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';
912import {
1013 BuildContext ,
11- EntryPointValue ,
1214 EntryPropertyObject ,
1315 NextConfigObject ,
1416 SentryWebpackPluginOptions ,
@@ -19,6 +21,13 @@ import {
1921
2022export { 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 ;
0 commit comments